Blame view

bower_components/iron-selector/iron-selectable.html 8.21 KB
73bcce88   luigser   COMPONENTS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <!--
  Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
  This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  Code distributed by Google as part of the polymer project is also
  subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  -->
  
  <link rel="import" href="../polymer/polymer.html">
  <link rel="import" href="iron-selection.html">
  
  <script>
  
    /** @polymerBehavior */
    Polymer.IronSelectableBehavior = {
  
        /**
e619a3b0   Luigi Serra   Controllet cross ...
19
20
21
         * Fired when iron-selector is activated (selected or deselected).
         * It is fired before the selected items are changed.
         * Cancel the event to abort selection.
73bcce88   luigser   COMPONENTS
22
23
         *
         * @event iron-activate
e619a3b0   Luigi Serra   Controllet cross ...
24
25
         */
  
73bcce88   luigser   COMPONENTS
26
        /**
e619a3b0   Luigi Serra   Controllet cross ...
27
         * Fired when an item is selected
73bcce88   luigser   COMPONENTS
28
29
         *
         * @event iron-select
e619a3b0   Luigi Serra   Controllet cross ...
30
31
         */
  
73bcce88   luigser   COMPONENTS
32
        /**
e619a3b0   Luigi Serra   Controllet cross ...
33
         * Fired when an item is deselected
73bcce88   luigser   COMPONENTS
34
35
         *
         * @event iron-deselect
e619a3b0   Luigi Serra   Controllet cross ...
36
37
38
39
40
41
         */
  
        /**
         * Fired when the list of selectable items changes (e.g., items are
         * added or removed). The detail of the event is a list of mutation
         * records that describe what changed.
73bcce88   luigser   COMPONENTS
42
         *
e619a3b0   Luigi Serra   Controllet cross ...
43
44
         * @event iron-items-changed
         */
73bcce88   luigser   COMPONENTS
45
46
47
48
49
50
  
      properties: {
  
        /**
         * If you want to use the attribute value of an element for `selected` instead of the index,
         * set this to the name of the attribute.
73bcce88   luigser   COMPONENTS
51
52
53
54
55
56
57
58
         */
        attrForSelected: {
          type: String,
          value: null
        },
  
        /**
         * Gets or sets the selected element. The default is to use the index of the item.
73bcce88   luigser   COMPONENTS
59
60
61
62
63
64
65
66
         */
        selected: {
          type: String,
          notify: true
        },
  
        /**
         * Returns the currently selected item.
a1a3bc73   Luigi Serra   graphs updates
67
68
         *
         * @type {?Object}
73bcce88   luigser   COMPONENTS
69
70
71
72
73
74
75
76
77
78
79
         */
        selectedItem: {
          type: Object,
          readOnly: true,
          notify: true
        },
  
        /**
         * The event that fires from items when they are selected. Selectable
         * will listen for this event from items and update the selection state.
         * Set to empty string to listen to no events.
73bcce88   luigser   COMPONENTS
80
81
82
83
84
85
86
87
88
89
         */
        activateEvent: {
          type: String,
          value: 'tap',
          observer: '_activateEventChanged'
        },
  
        /**
         * This is a CSS selector string.  If this is set, only items that match the CSS selector
         * are selectable.
73bcce88   luigser   COMPONENTS
90
91
92
93
94
         */
        selectable: String,
  
        /**
         * The class to set on elements when selected.
73bcce88   luigser   COMPONENTS
95
96
97
98
99
100
101
102
         */
        selectedClass: {
          type: String,
          value: 'iron-selected'
        },
  
        /**
         * The attribute to set on elements when selected.
73bcce88   luigser   COMPONENTS
103
104
105
106
107
108
109
         */
        selectedAttribute: {
          type: String,
          value: null
        },
  
        /**
a1a3bc73   Luigi Serra   graphs updates
110
111
112
113
114
115
116
117
118
119
120
         * The list of items from which a selection can be made.
         */
        items: {
          type: Array,
          readOnly: true,
          value: function() {
            return [];
          }
        },
  
        /**
e619a3b0   Luigi Serra   Controllet cross ...
121
         * The set of excluded elements where the key is the `localName`
73bcce88   luigser   COMPONENTS
122
123
         * of the element that will be ignored from the item list.
         *
73bcce88   luigser   COMPONENTS
124
125
         * @default {template: 1}
         */
eb240478   Luigi Serra   public room cards...
126
        _excludedLocalNames: {
73bcce88   luigser   COMPONENTS
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
          type: Object,
          value: function() {
            return {
              'template': 1
            };
          }
        }
      },
  
      observers: [
        '_updateSelected(attrForSelected, selected)'
      ],
  
      created: function() {
        this._bindFilterItem = this._filterItem.bind(this);
        this._selection = new Polymer.IronSelection(this._applySelection.bind(this));
73bcce88   luigser   COMPONENTS
143
144
145
146
      },
  
      attached: function() {
        this._observer = this._observeItems(this);
a1a3bc73   Luigi Serra   graphs updates
147
148
        this._updateItems();
        if (!this._shouldUpdateSelection) {
e619a3b0   Luigi Serra   Controllet cross ...
149
150
          this._updateSelected(this.attrForSelected,this.selected)
        }
eb240478   Luigi Serra   public room cards...
151
        this._addListener(this.activateEvent);
73bcce88   luigser   COMPONENTS
152
153
154
155
      },
  
      detached: function() {
        if (this._observer) {
a1a3bc73   Luigi Serra   graphs updates
156
          Polymer.dom(this).unobserveNodes(this._observer);
73bcce88   luigser   COMPONENTS
157
158
159
160
161
        }
        this._removeListener(this.activateEvent);
      },
  
      /**
73bcce88   luigser   COMPONENTS
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
       * Returns the index of the given item.
       *
       * @method indexOf
       * @param {Object} item
       * @returns Returns the index of the item
       */
      indexOf: function(item) {
        return this.items.indexOf(item);
      },
  
      /**
       * Selects the given value.
       *
       * @method select
       * @param {string} value the value to select.
       */
      select: function(value) {
        this.selected = value;
      },
  
      /**
       * Selects the previous item.
       *
       * @method selectPrevious
       */
      selectPrevious: function() {
        var length = this.items.length;
        var index = (Number(this._valueToIndex(this.selected)) - 1 + length) % length;
        this.selected = this._indexToValue(index);
      },
  
      /**
       * Selects the next item.
       *
       * @method selectNext
       */
      selectNext: function() {
        var index = (Number(this._valueToIndex(this.selected)) + 1) % this.items.length;
        this.selected = this._indexToValue(index);
      },
  
a1a3bc73   Luigi Serra   graphs updates
203
204
205
      get _shouldUpdateSelection() {
        return this.selected != null;
      },
eb240478   Luigi Serra   public room cards...
206
  
a1a3bc73   Luigi Serra   graphs updates
207
      _addListener: function(eventName) {
73bcce88   luigser   COMPONENTS
208
209
210
211
212
        this.listen(this, eventName, '_activateHandler');
      },
  
      _removeListener: function(eventName) {
        this.unlisten(this, eventName, '_activateHandler');
73bcce88   luigser   COMPONENTS
213
214
215
216
217
218
219
      },
  
      _activateEventChanged: function(eventName, old) {
        this._removeListener(old);
        this._addListener(eventName);
      },
  
a1a3bc73   Luigi Serra   graphs updates
220
221
222
223
224
225
      _updateItems: function() {
        var nodes = Polymer.dom(this).queryDistributedElements(this.selectable || '*');
        nodes = Array.prototype.filter.call(nodes, this._bindFilterItem);
        this._setItems(nodes);
      },
  
73bcce88   luigser   COMPONENTS
226
227
228
229
230
231
232
233
234
      _updateSelected: function() {
        this._selectSelected(this.selected);
      },
  
      _selectSelected: function(selected) {
        this._selection.select(this._valueToItem(this.selected));
      },
  
      _filterItem: function(node) {
eb240478   Luigi Serra   public room cards...
235
        return !this._excludedLocalNames[node.localName];
73bcce88   luigser   COMPONENTS
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
      },
  
      _valueToItem: function(value) {
        return (value == null) ? null : this.items[this._valueToIndex(value)];
      },
  
      _valueToIndex: function(value) {
        if (this.attrForSelected) {
          for (var i = 0, item; item = this.items[i]; i++) {
            if (this._valueForItem(item) == value) {
              return i;
            }
          }
        } else {
          return Number(value);
        }
      },
  
      _indexToValue: function(index) {
        if (this.attrForSelected) {
          var item = this.items[index];
          if (item) {
            return this._valueForItem(item);
          }
        } else {
          return index;
        }
      },
  
      _valueForItem: function(item) {
        return item[this.attrForSelected] || item.getAttribute(this.attrForSelected);
      },
  
      _applySelection: function(item, isSelected) {
        if (this.selectedClass) {
          this.toggleClass(this.selectedClass, isSelected, item);
        }
        if (this.selectedAttribute) {
          this.toggleAttribute(this.selectedAttribute, isSelected, item);
        }
        this._selectionChange();
        this.fire('iron-' + (isSelected ? 'select' : 'deselect'), {item: item});
      },
  
      _selectionChange: function() {
        this._setSelectedItem(this._selection.get());
      },
  
73bcce88   luigser   COMPONENTS
284
285
      // observe items change under the given node.
      _observeItems: function(node) {
a1a3bc73   Luigi Serra   graphs updates
286
        return Polymer.dom(node).observeNodes(function(mutations) {
e619a3b0   Luigi Serra   Controllet cross ...
287
288
289
290
291
292
293
          // Let other interested parties know about the change so that
          // we don't have to recreate mutation observers everywher.
          this.fire('iron-items-changed', mutations, {
            bubbles: false,
            cancelable: false
          });
  
a1a3bc73   Luigi Serra   graphs updates
294
295
296
          this._updateItems();
  
          if (this._shouldUpdateSelection) {
73bcce88   luigser   COMPONENTS
297
298
            this._updateSelected();
          }
73bcce88   luigser   COMPONENTS
299
        });
73bcce88   luigser   COMPONENTS
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
      },
  
      _activateHandler: function(e) {
        var t = e.target;
        var items = this.items;
        while (t && t != this) {
          var i = items.indexOf(t);
          if (i >= 0) {
            var value = this._indexToValue(i);
            this._itemActivate(value, t);
            return;
          }
          t = t.parentNode;
        }
      },
  
      _itemActivate: function(value, item) {
        if (!this.fire('iron-activate',
            {selected: value, item: item}, {cancelable: true}).defaultPrevented) {
          this.select(value);
        }
      }
  
    };
  
  </script>