1
2    Polymer('core-selection', {
3      /**
4       * If true, multiple selections are allowed.
5       *
6       * @attribute multi
7       * @type boolean
8       * @default false
9       */
10      multi: false,
11      ready: function() {
12        this.clear();
13      },
14      clear: function() {
15        this.selection = [];
16      },
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.
23      */
24      getSelection: function() {
25        return this.multi ? this.selection : this.selection[0];
26      },
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.
32      */
33      isSelected: function(item) {
34        return this.selection.indexOf(item) >= 0;
35      },
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);
44            }
45          }
46          this.fire("core-select", {isSelected: isSelected, item: item});
47        }
48      },
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.
55      */
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);
62        }
63      },
64      /**
65       * Toggles the selection state for `item`.
66       * @method toggle
67       * @param {any} item: The item to toggle.
68      */
69      toggle: function(item) {
70        this.setItemSelected(item, !this.isSelected(item));
71      }
72    });
73