cdk-collections.umd.js 17 KB

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