collections.es5.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { __extends } from 'tslib';
  9. import { Observable, of, Subject } from 'rxjs';
  10. import { Injectable, ɵɵdefineInjectable } from '@angular/core';
  11. /**
  12. * @fileoverview added by tsickle
  13. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  14. */
  15. /**
  16. * @abstract
  17. * @template T
  18. */
  19. var /**
  20. * @abstract
  21. * @template T
  22. */
  23. DataSource = /** @class */ (function () {
  24. function DataSource() {
  25. }
  26. return DataSource;
  27. }());
  28. /**
  29. * Checks whether an object is a data source.
  30. * @param {?} value
  31. * @return {?}
  32. */
  33. function isDataSource(value) {
  34. // Check if the value is a DataSource by observing if it has a connect function. Cannot
  35. // be checked as an `instanceof DataSource` since people could create their own sources
  36. // that match the interface, but don't extend DataSource.
  37. return value && typeof value.connect === 'function';
  38. }
  39. /**
  40. * @fileoverview added by tsickle
  41. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  42. */
  43. /**
  44. * DataSource wrapper for a native array.
  45. * @template T
  46. */
  47. var /**
  48. * DataSource wrapper for a native array.
  49. * @template T
  50. */
  51. ArrayDataSource = /** @class */ (function (_super) {
  52. __extends(ArrayDataSource, _super);
  53. function ArrayDataSource(_data) {
  54. var _this = _super.call(this) || this;
  55. _this._data = _data;
  56. return _this;
  57. }
  58. /**
  59. * @return {?}
  60. */
  61. ArrayDataSource.prototype.connect = /**
  62. * @return {?}
  63. */
  64. function () {
  65. return this._data instanceof Observable ? this._data : of(this._data);
  66. };
  67. /**
  68. * @return {?}
  69. */
  70. ArrayDataSource.prototype.disconnect = /**
  71. * @return {?}
  72. */
  73. function () { };
  74. return ArrayDataSource;
  75. }(DataSource));
  76. /**
  77. * @fileoverview added by tsickle
  78. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  79. */
  80. /**
  81. * @fileoverview added by tsickle
  82. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  83. */
  84. /**
  85. * Class to be used to power selecting one or more options from a list.
  86. * @template T
  87. */
  88. var /**
  89. * Class to be used to power selecting one or more options from a list.
  90. * @template T
  91. */
  92. SelectionModel = /** @class */ (function () {
  93. function SelectionModel(_multiple, initiallySelectedValues, _emitChanges) {
  94. var _this = this;
  95. if (_multiple === void 0) { _multiple = false; }
  96. if (_emitChanges === void 0) { _emitChanges = true; }
  97. this._multiple = _multiple;
  98. this._emitChanges = _emitChanges;
  99. /**
  100. * Currently-selected values.
  101. */
  102. this._selection = new Set();
  103. /**
  104. * Keeps track of the deselected options that haven't been emitted by the change event.
  105. */
  106. this._deselectedToEmit = [];
  107. /**
  108. * Keeps track of the selected options that haven't been emitted by the change event.
  109. */
  110. this._selectedToEmit = [];
  111. /**
  112. * Event emitted when the value has changed.
  113. */
  114. this.changed = new Subject();
  115. /**
  116. * Event emitted when the value has changed.
  117. * @deprecated Use `changed` instead.
  118. * \@breaking-change 8.0.0 To be changed to `changed`
  119. */
  120. this.onChange = this.changed;
  121. if (initiallySelectedValues && initiallySelectedValues.length) {
  122. if (_multiple) {
  123. initiallySelectedValues.forEach((/**
  124. * @param {?} value
  125. * @return {?}
  126. */
  127. function (value) { return _this._markSelected(value); }));
  128. }
  129. else {
  130. this._markSelected(initiallySelectedValues[0]);
  131. }
  132. // Clear the array in order to avoid firing the change event for preselected values.
  133. this._selectedToEmit.length = 0;
  134. }
  135. }
  136. Object.defineProperty(SelectionModel.prototype, "selected", {
  137. /** Selected values. */
  138. get: /**
  139. * Selected values.
  140. * @return {?}
  141. */
  142. function () {
  143. if (!this._selected) {
  144. this._selected = Array.from(this._selection.values());
  145. }
  146. return this._selected;
  147. },
  148. enumerable: true,
  149. configurable: true
  150. });
  151. /**
  152. * Selects a value or an array of values.
  153. */
  154. /**
  155. * Selects a value or an array of values.
  156. * @param {...?} values
  157. * @return {?}
  158. */
  159. SelectionModel.prototype.select = /**
  160. * Selects a value or an array of values.
  161. * @param {...?} values
  162. * @return {?}
  163. */
  164. function () {
  165. var _this = this;
  166. var values = [];
  167. for (var _i = 0; _i < arguments.length; _i++) {
  168. values[_i] = arguments[_i];
  169. }
  170. this._verifyValueAssignment(values);
  171. values.forEach((/**
  172. * @param {?} value
  173. * @return {?}
  174. */
  175. function (value) { return _this._markSelected(value); }));
  176. this._emitChangeEvent();
  177. };
  178. /**
  179. * Deselects a value or an array of values.
  180. */
  181. /**
  182. * Deselects a value or an array of values.
  183. * @param {...?} values
  184. * @return {?}
  185. */
  186. SelectionModel.prototype.deselect = /**
  187. * Deselects a value or an array of values.
  188. * @param {...?} values
  189. * @return {?}
  190. */
  191. function () {
  192. var _this = this;
  193. var values = [];
  194. for (var _i = 0; _i < arguments.length; _i++) {
  195. values[_i] = arguments[_i];
  196. }
  197. this._verifyValueAssignment(values);
  198. values.forEach((/**
  199. * @param {?} value
  200. * @return {?}
  201. */
  202. function (value) { return _this._unmarkSelected(value); }));
  203. this._emitChangeEvent();
  204. };
  205. /**
  206. * Toggles a value between selected and deselected.
  207. */
  208. /**
  209. * Toggles a value between selected and deselected.
  210. * @param {?} value
  211. * @return {?}
  212. */
  213. SelectionModel.prototype.toggle = /**
  214. * Toggles a value between selected and deselected.
  215. * @param {?} value
  216. * @return {?}
  217. */
  218. function (value) {
  219. this.isSelected(value) ? this.deselect(value) : this.select(value);
  220. };
  221. /**
  222. * Clears all of the selected values.
  223. */
  224. /**
  225. * Clears all of the selected values.
  226. * @return {?}
  227. */
  228. SelectionModel.prototype.clear = /**
  229. * Clears all of the selected values.
  230. * @return {?}
  231. */
  232. function () {
  233. this._unmarkAll();
  234. this._emitChangeEvent();
  235. };
  236. /**
  237. * Determines whether a value is selected.
  238. */
  239. /**
  240. * Determines whether a value is selected.
  241. * @param {?} value
  242. * @return {?}
  243. */
  244. SelectionModel.prototype.isSelected = /**
  245. * Determines whether a value is selected.
  246. * @param {?} value
  247. * @return {?}
  248. */
  249. function (value) {
  250. return this._selection.has(value);
  251. };
  252. /**
  253. * Determines whether the model does not have a value.
  254. */
  255. /**
  256. * Determines whether the model does not have a value.
  257. * @return {?}
  258. */
  259. SelectionModel.prototype.isEmpty = /**
  260. * Determines whether the model does not have a value.
  261. * @return {?}
  262. */
  263. function () {
  264. return this._selection.size === 0;
  265. };
  266. /**
  267. * Determines whether the model has a value.
  268. */
  269. /**
  270. * Determines whether the model has a value.
  271. * @return {?}
  272. */
  273. SelectionModel.prototype.hasValue = /**
  274. * Determines whether the model has a value.
  275. * @return {?}
  276. */
  277. function () {
  278. return !this.isEmpty();
  279. };
  280. /**
  281. * Sorts the selected values based on a predicate function.
  282. */
  283. /**
  284. * Sorts the selected values based on a predicate function.
  285. * @param {?=} predicate
  286. * @return {?}
  287. */
  288. SelectionModel.prototype.sort = /**
  289. * Sorts the selected values based on a predicate function.
  290. * @param {?=} predicate
  291. * @return {?}
  292. */
  293. function (predicate) {
  294. if (this._multiple && this.selected) {
  295. (/** @type {?} */ (this._selected)).sort(predicate);
  296. }
  297. };
  298. /**
  299. * Gets whether multiple values can be selected.
  300. */
  301. /**
  302. * Gets whether multiple values can be selected.
  303. * @return {?}
  304. */
  305. SelectionModel.prototype.isMultipleSelection = /**
  306. * Gets whether multiple values can be selected.
  307. * @return {?}
  308. */
  309. function () {
  310. return this._multiple;
  311. };
  312. /** Emits a change event and clears the records of selected and deselected values. */
  313. /**
  314. * Emits a change event and clears the records of selected and deselected values.
  315. * @private
  316. * @return {?}
  317. */
  318. SelectionModel.prototype._emitChangeEvent = /**
  319. * Emits a change event and clears the records of selected and deselected values.
  320. * @private
  321. * @return {?}
  322. */
  323. function () {
  324. // Clear the selected values so they can be re-cached.
  325. this._selected = null;
  326. if (this._selectedToEmit.length || this._deselectedToEmit.length) {
  327. this.changed.next({
  328. source: this,
  329. added: this._selectedToEmit,
  330. removed: this._deselectedToEmit
  331. });
  332. this._deselectedToEmit = [];
  333. this._selectedToEmit = [];
  334. }
  335. };
  336. /** Selects a value. */
  337. /**
  338. * Selects a value.
  339. * @private
  340. * @param {?} value
  341. * @return {?}
  342. */
  343. SelectionModel.prototype._markSelected = /**
  344. * Selects a value.
  345. * @private
  346. * @param {?} value
  347. * @return {?}
  348. */
  349. function (value) {
  350. if (!this.isSelected(value)) {
  351. if (!this._multiple) {
  352. this._unmarkAll();
  353. }
  354. this._selection.add(value);
  355. if (this._emitChanges) {
  356. this._selectedToEmit.push(value);
  357. }
  358. }
  359. };
  360. /** Deselects a value. */
  361. /**
  362. * Deselects a value.
  363. * @private
  364. * @param {?} value
  365. * @return {?}
  366. */
  367. SelectionModel.prototype._unmarkSelected = /**
  368. * Deselects a value.
  369. * @private
  370. * @param {?} value
  371. * @return {?}
  372. */
  373. function (value) {
  374. if (this.isSelected(value)) {
  375. this._selection.delete(value);
  376. if (this._emitChanges) {
  377. this._deselectedToEmit.push(value);
  378. }
  379. }
  380. };
  381. /** Clears out the selected values. */
  382. /**
  383. * Clears out the selected values.
  384. * @private
  385. * @return {?}
  386. */
  387. SelectionModel.prototype._unmarkAll = /**
  388. * Clears out the selected values.
  389. * @private
  390. * @return {?}
  391. */
  392. function () {
  393. var _this = this;
  394. if (!this.isEmpty()) {
  395. this._selection.forEach((/**
  396. * @param {?} value
  397. * @return {?}
  398. */
  399. function (value) { return _this._unmarkSelected(value); }));
  400. }
  401. };
  402. /**
  403. * Verifies the value assignment and throws an error if the specified value array is
  404. * including multiple values while the selection model is not supporting multiple values.
  405. */
  406. /**
  407. * Verifies the value assignment and throws an error if the specified value array is
  408. * including multiple values while the selection model is not supporting multiple values.
  409. * @private
  410. * @param {?} values
  411. * @return {?}
  412. */
  413. SelectionModel.prototype._verifyValueAssignment = /**
  414. * Verifies the value assignment and throws an error if the specified value array is
  415. * including multiple values while the selection model is not supporting multiple values.
  416. * @private
  417. * @param {?} values
  418. * @return {?}
  419. */
  420. function (values) {
  421. if (values.length > 1 && !this._multiple) {
  422. throw getMultipleValuesInSingleSelectionError();
  423. }
  424. };
  425. return SelectionModel;
  426. }());
  427. /**
  428. * Returns an error that reports that multiple values are passed into a selection model
  429. * with a single value.
  430. * \@docs-private
  431. * @return {?}
  432. */
  433. function getMultipleValuesInSingleSelectionError() {
  434. return Error('Cannot pass multiple values into SelectionModel with single-value mode.');
  435. }
  436. /**
  437. * @fileoverview added by tsickle
  438. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  439. */
  440. /**
  441. * Class to coordinate unique selection based on name.
  442. * Intended to be consumed as an Angular service.
  443. * This service is needed because native radio change events are only fired on the item currently
  444. * being selected, and we still need to uncheck the previous selection.
  445. *
  446. * This service does not *store* any IDs and names because they may change at any time, so it is
  447. * less error-prone if they are simply passed through when the events occur.
  448. */
  449. var UniqueSelectionDispatcher = /** @class */ (function () {
  450. function UniqueSelectionDispatcher() {
  451. this._listeners = [];
  452. }
  453. /**
  454. * Notify other items that selection for the given name has been set.
  455. * @param id ID of the item.
  456. * @param name Name of the item.
  457. */
  458. /**
  459. * Notify other items that selection for the given name has been set.
  460. * @param {?} id ID of the item.
  461. * @param {?} name Name of the item.
  462. * @return {?}
  463. */
  464. UniqueSelectionDispatcher.prototype.notify = /**
  465. * Notify other items that selection for the given name has been set.
  466. * @param {?} id ID of the item.
  467. * @param {?} name Name of the item.
  468. * @return {?}
  469. */
  470. function (id, name) {
  471. for (var _i = 0, _a = this._listeners; _i < _a.length; _i++) {
  472. var listener = _a[_i];
  473. listener(id, name);
  474. }
  475. };
  476. /**
  477. * Listen for future changes to item selection.
  478. * @return Function used to deregister listener
  479. */
  480. /**
  481. * Listen for future changes to item selection.
  482. * @param {?} listener
  483. * @return {?} Function used to deregister listener
  484. */
  485. UniqueSelectionDispatcher.prototype.listen = /**
  486. * Listen for future changes to item selection.
  487. * @param {?} listener
  488. * @return {?} Function used to deregister listener
  489. */
  490. function (listener) {
  491. var _this = this;
  492. this._listeners.push(listener);
  493. return (/**
  494. * @return {?}
  495. */
  496. function () {
  497. _this._listeners = _this._listeners.filter((/**
  498. * @param {?} registered
  499. * @return {?}
  500. */
  501. function (registered) {
  502. return listener !== registered;
  503. }));
  504. });
  505. };
  506. /**
  507. * @return {?}
  508. */
  509. UniqueSelectionDispatcher.prototype.ngOnDestroy = /**
  510. * @return {?}
  511. */
  512. function () {
  513. this._listeners = [];
  514. };
  515. UniqueSelectionDispatcher.decorators = [
  516. { type: Injectable, args: [{ providedIn: 'root' },] },
  517. ];
  518. /** @nocollapse */ UniqueSelectionDispatcher.ngInjectableDef = ɵɵdefineInjectable({ factory: function UniqueSelectionDispatcher_Factory() { return new UniqueSelectionDispatcher(); }, token: UniqueSelectionDispatcher, providedIn: "root" });
  519. return UniqueSelectionDispatcher;
  520. }());
  521. /**
  522. * @fileoverview added by tsickle
  523. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  524. */
  525. /**
  526. * @fileoverview added by tsickle
  527. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  528. */
  529. /**
  530. * @fileoverview added by tsickle
  531. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  532. */
  533. export { UniqueSelectionDispatcher, ArrayDataSource, isDataSource, DataSource, getMultipleValuesInSingleSelectionError, SelectionModel };
  534. //# sourceMappingURL=collections.es5.js.map