@angular/cdk
Version:
Angular Material Component Development Kit
309 lines (299 loc) • 12.5 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('tslib'), require('rxjs'), require('@angular/core')) :
typeof define === 'function' && define.amd ? define('@angular/cdk/collections', ['exports', 'tslib', 'rxjs', '@angular/core'], factory) :
(global = global || self, factory((global.ng = global.ng || {}, global.ng.cdk = global.ng.cdk || {}, global.ng.cdk.collections = {}), global.tslib, global.rxjs, global.ng.core));
}(this, (function (exports, tslib, rxjs, i0) { 'use strict';
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
var DataSource = /** @class */ (function () {
function DataSource() {
}
return DataSource;
}());
/** Checks whether an object is a data source. */
function isDataSource(value) {
// Check if the value is a DataSource by observing if it has a connect function. Cannot
// be checked as an `instanceof DataSource` since people could create their own sources
// that match the interface, but don't extend DataSource.
return value && typeof value.connect === 'function';
}
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/** DataSource wrapper for a native array. */
var ArrayDataSource = /** @class */ (function (_super) {
tslib.__extends(ArrayDataSource, _super);
function ArrayDataSource(_data) {
var _this = _super.call(this) || this;
_this._data = _data;
return _this;
}
ArrayDataSource.prototype.connect = function () {
return this._data instanceof rxjs.Observable ? this._data : rxjs.of(this._data);
};
ArrayDataSource.prototype.disconnect = function () { };
return ArrayDataSource;
}(DataSource));
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Class to be used to power selecting one or more options from a list.
*/
var SelectionModel = /** @class */ (function () {
function SelectionModel(_multiple, initiallySelectedValues, _emitChanges) {
var _this = this;
if (_multiple === void 0) { _multiple = false; }
if (_emitChanges === void 0) { _emitChanges = true; }
this._multiple = _multiple;
this._emitChanges = _emitChanges;
/** Currently-selected values. */
this._selection = new Set();
/** Keeps track of the deselected options that haven't been emitted by the change event. */
this._deselectedToEmit = [];
/** Keeps track of the selected options that haven't been emitted by the change event. */
this._selectedToEmit = [];
/** Event emitted when the value has changed. */
this.changed = new rxjs.Subject();
if (initiallySelectedValues && initiallySelectedValues.length) {
if (_multiple) {
initiallySelectedValues.forEach(function (value) { return _this._markSelected(value); });
}
else {
this._markSelected(initiallySelectedValues[0]);
}
// Clear the array in order to avoid firing the change event for preselected values.
this._selectedToEmit.length = 0;
}
}
Object.defineProperty(SelectionModel.prototype, "selected", {
/** Selected values. */
get: function () {
if (!this._selected) {
this._selected = Array.from(this._selection.values());
}
return this._selected;
},
enumerable: true,
configurable: true
});
/**
* Selects a value or an array of values.
*/
SelectionModel.prototype.select = function () {
var _this = this;
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this._verifyValueAssignment(values);
values.forEach(function (value) { return _this._markSelected(value); });
this._emitChangeEvent();
};
/**
* Deselects a value or an array of values.
*/
SelectionModel.prototype.deselect = function () {
var _this = this;
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this._verifyValueAssignment(values);
values.forEach(function (value) { return _this._unmarkSelected(value); });
this._emitChangeEvent();
};
/**
* Toggles a value between selected and deselected.
*/
SelectionModel.prototype.toggle = function (value) {
this.isSelected(value) ? this.deselect(value) : this.select(value);
};
/**
* Clears all of the selected values.
*/
SelectionModel.prototype.clear = function () {
this._unmarkAll();
this._emitChangeEvent();
};
/**
* Determines whether a value is selected.
*/
SelectionModel.prototype.isSelected = function (value) {
return this._selection.has(value);
};
/**
* Determines whether the model does not have a value.
*/
SelectionModel.prototype.isEmpty = function () {
return this._selection.size === 0;
};
/**
* Determines whether the model has a value.
*/
SelectionModel.prototype.hasValue = function () {
return !this.isEmpty();
};
/**
* Sorts the selected values based on a predicate function.
*/
SelectionModel.prototype.sort = function (predicate) {
if (this._multiple && this.selected) {
this._selected.sort(predicate);
}
};
/**
* Gets whether multiple values can be selected.
*/
SelectionModel.prototype.isMultipleSelection = function () {
return this._multiple;
};
/** Emits a change event and clears the records of selected and deselected values. */
SelectionModel.prototype._emitChangeEvent = function () {
// Clear the selected values so they can be re-cached.
this._selected = null;
if (this._selectedToEmit.length || this._deselectedToEmit.length) {
this.changed.next({
source: this,
added: this._selectedToEmit,
removed: this._deselectedToEmit
});
this._deselectedToEmit = [];
this._selectedToEmit = [];
}
};
/** Selects a value. */
SelectionModel.prototype._markSelected = function (value) {
if (!this.isSelected(value)) {
if (!this._multiple) {
this._unmarkAll();
}
this._selection.add(value);
if (this._emitChanges) {
this._selectedToEmit.push(value);
}
}
};
/** Deselects a value. */
SelectionModel.prototype._unmarkSelected = function (value) {
if (this.isSelected(value)) {
this._selection.delete(value);
if (this._emitChanges) {
this._deselectedToEmit.push(value);
}
}
};
/** Clears out the selected values. */
SelectionModel.prototype._unmarkAll = function () {
var _this = this;
if (!this.isEmpty()) {
this._selection.forEach(function (value) { return _this._unmarkSelected(value); });
}
};
/**
* Verifies the value assignment and throws an error if the specified value array is
* including multiple values while the selection model is not supporting multiple values.
*/
SelectionModel.prototype._verifyValueAssignment = function (values) {
if (values.length > 1 && !this._multiple) {
throw getMultipleValuesInSingleSelectionError();
}
};
return SelectionModel;
}());
/**
* Returns an error that reports that multiple values are passed into a selection model
* with a single value.
* @docs-private
*/
function getMultipleValuesInSingleSelectionError() {
return Error('Cannot pass multiple values into SelectionModel with single-value mode.');
}
/**
* Class to coordinate unique selection based on name.
* Intended to be consumed as an Angular service.
* This service is needed because native radio change events are only fired on the item currently
* being selected, and we still need to uncheck the previous selection.
*
* This service does not *store* any IDs and names because they may change at any time, so it is
* less error-prone if they are simply passed through when the events occur.
*/
var UniqueSelectionDispatcher = /** @class */ (function () {
function UniqueSelectionDispatcher() {
this._listeners = [];
}
/**
* Notify other items that selection for the given name has been set.
* @param id ID of the item.
* @param name Name of the item.
*/
UniqueSelectionDispatcher.prototype.notify = function (id, name) {
var e_1, _a;
try {
for (var _b = tslib.__values(this._listeners), _c = _b.next(); !_c.done; _c = _b.next()) {
var listener = _c.value;
listener(id, name);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* Listen for future changes to item selection.
* @return Function used to deregister listener
*/
UniqueSelectionDispatcher.prototype.listen = function (listener) {
var _this = this;
this._listeners.push(listener);
return function () {
_this._listeners = _this._listeners.filter(function (registered) {
return listener !== registered;
});
};
};
UniqueSelectionDispatcher.prototype.ngOnDestroy = function () {
this._listeners = [];
};
UniqueSelectionDispatcher.decorators = [
{ type: i0.Injectable, args: [{ providedIn: 'root' },] }
];
UniqueSelectionDispatcher.ɵprov = i0.ɵɵdefineInjectable({ factory: function UniqueSelectionDispatcher_Factory() { return new UniqueSelectionDispatcher(); }, token: UniqueSelectionDispatcher, providedIn: "root" });
return UniqueSelectionDispatcher;
}());
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Generated bundle index. Do not edit.
*/
exports.ArrayDataSource = ArrayDataSource;
exports.DataSource = DataSource;
exports.SelectionModel = SelectionModel;
exports.UniqueSelectionDispatcher = UniqueSelectionDispatcher;
exports.getMultipleValuesInSingleSelectionError = getMultipleValuesInSingleSelectionError;
exports.isDataSource = isDataSource;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=cdk-collections.umd.js.map