md2
Version:
Angular2 based Material Design components, directives and services are Accordion, Autocomplete, Chips(Tags), Collapse, Colorpicker, Data Table, Datepicker, Dialog(Modal), Menu, Multiselect, Select, Tabs, Tags(Chips), Toast and Tooltip.
149 lines • 5.12 kB
JavaScript
import { Subject } from 'rxjs/Subject';
/**
* Class to be used to power selecting one or more options from a list.
* @docs-private
*/
var SelectionModel = (function () {
function SelectionModel(_isMulti, initiallySelectedValues, _emitChanges) {
if (_isMulti === void 0) { _isMulti = false; }
if (_emitChanges === void 0) { _emitChanges = true; }
var _this = this;
this._isMulti = _isMulti;
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 option that haven't been emitted by the change event. */
this._selectedToEmit = [];
/** Event emitted when the value has changed. */
this.onChange = this._emitChanges ? new Subject() : null;
if (initiallySelectedValues) {
if (_isMulti) {
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 value(s). */
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 (value) {
this._markSelected(value);
this._emitChangeEvent();
};
/**
* Deselects a value or an array of values.
*/
SelectionModel.prototype.deselect = function (value) {
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._isMulti && this.selected) {
this._selected.sort(predicate);
}
};
/** Emits a change event and clears the records of selected and deselected values. */
SelectionModel.prototype._emitChangeEvent = function () {
if (this._selectedToEmit.length || this._deselectedToEmit.length) {
var eventData = new SelectionChange(this._selectedToEmit, this._deselectedToEmit);
this.onChange.next(eventData);
this._deselectedToEmit = [];
this._selectedToEmit = [];
}
this._selected = null;
};
/** Selects a value. */
SelectionModel.prototype._markSelected = function (value) {
if (!this.isSelected(value)) {
if (!this._isMulti) {
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); });
}
};
return SelectionModel;
}());
export { SelectionModel };
/**
* Describes an event emitted when the value of a MdSelectionModel has changed.
* @docs-private
*/
var SelectionChange = (function () {
function SelectionChange(added, removed) {
this.added = added;
this.removed = removed;
}
return SelectionChange;
}());
export { SelectionChange };
//# sourceMappingURL=selection.js.map