openmct/src/Selection.js
Andrew Henry 1879c122c7 Mutation API (#1074)
* [API] Allow selection

* [API] Keep in sync using model

* [API] Add selection as EventEmitter

* [API] Use selection from ToDo tutorial

* Object events prototype

* Added examples

* Transitional API

* Modified todo list code to work with new setters

* [API] Removed emitting of events on container when property changes value to remove ambiguity. Listeners must be listening to the same path used in the setter to catch changes
2016-07-07 14:30:45 -07:00

33 lines
839 B
JavaScript

define(['EventEmitter'], function (EventEmitter) {
function Selection() {
EventEmitter.call(this);
this.selectedValues = [];
}
Selection.prototype = Object.create(EventEmitter.prototype);
Selection.prototype.select = function (value) {
this.selectedValues.push(value);
this.emit('change');
return this.deselect.bind(this, value);
};
Selection.prototype.deselect = function (value) {
this.selectedValues = this.selectedValues.filter(function (v) {
return v !== value;
});
this.emit('change');
};
Selection.prototype.selected = function () {
return this.selectedValues;
};
Selection.prototype.clear = function () {
this.selectedValues = [];
this.emit('change');
};
return Selection;
});