MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / third_party / polymer / components-chromium / core-selection / core-selection-extracted.js
blob86994c98c2218109634a13b4eb24c0e964b6eeef
2 Polymer('core-selection', {
3 /**
4 * If true, multiple selections are allowed.
6 * @attribute multi
7 * @type boolean
8 * @default false
9 */
10 multi: false,
11 ready: function() {
12 this.clear();
14 clear: function() {
15 this.selection = [];
17 /**
18 * Retrieves the selected item(s).
19 * @method getSelection
20 * @returns Returns the selected item(s). If the multi property is true,
21 * getSelection will return an array, otherwise it will return
22 * the selected item or undefined if there is no selection.
24 getSelection: function() {
25 return this.multi ? this.selection : this.selection[0];
27 /**
28 * Indicates if a given item is selected.
29 * @method isSelected
30 * @param {any} item The item whose selection state should be checked.
31 * @returns Returns true if `item` is selected.
33 isSelected: function(item) {
34 return this.selection.indexOf(item) >= 0;
36 setItemSelected: function(item, isSelected) {
37 if (item !== undefined && item !== null) {
38 if (isSelected) {
39 this.selection.push(item);
40 } else {
41 var i = this.selection.indexOf(item);
42 if (i >= 0) {
43 this.selection.splice(i, 1);
46 this.fire("core-select", {isSelected: isSelected, item: item});
49 /**
50 * Set the selection state for a given `item`. If the multi property
51 * is true, then the selected state of `item` will be toggled; otherwise
52 * the `item` will be selected.
53 * @method select
54 * @param {any} item: The item to select.
56 select: function(item) {
57 if (this.multi) {
58 this.toggle(item);
59 } else if (this.getSelection() !== item) {
60 this.setItemSelected(this.getSelection(), false);
61 this.setItemSelected(item, true);
64 /**
65 * Toggles the selection state for `item`.
66 * @method toggle
67 * @param {any} item: The item to toggle.
69 toggle: function(item) {
70 this.setItemSelected(item, !this.isSelected(item));
72 });