summaryrefslogtreecommitdiffstats
path: root/third_party/webkit/PerformanceTests/Speedometer/resources/flightjs-example-app/app/component_ui/with_select.js
blob: 2a99feaadf4354bf1c67b119eaf7bb2772def009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';

define(

  function() {

    return withSelect;

    function withSelect() {

      this.defaultAttrs({
        selectedIds: []
      });

      this.initializeSelections = function() {
        this.select('selectedItemSelector').toArray().forEach(function(el) {
          this.attr.selectedIds.push(el.getAttribute('id'));
        }, this);
      };

      this.getSelectedIds = function() {
        return this.attr.selectedIds;
      };

      this.toggleItemSelect = function(ev, data) {
        var $item = $(data.el), append;

        if ($item.hasClass(this.attr.selectedClass)) {
          this.unselectItem($item);
        } else {
          append = this.attr.allowMultiSelect && (ev.metaKey || ev.ctrlKey || ev.shiftKey);
          this.selectItem($item, append);
        }
      };

      this.selectItem = function($item, append) {
        if (!append) {
          this.select('selectedItemSelector').removeClass(this.attr.selectedClass);
          this.attr.selectedIds = [];
        }
        $item.addClass(this.attr.selectedClass);

        this.attr.selectedIds.push($item.attr('id'));
        this.trigger(this.attr.selectionChangedEvent, {selectedIds: this.attr.selectedIds});
      };

      this.unselectItem = function($item) {
        $item.removeClass(this.attr.selectedClass);

        var thisIdIndex = this.attr.selectedIds.indexOf($item.attr('id'));
        this.attr.selectedIds.splice(thisIdIndex, 1);
        this.trigger(this.attr.selectionChangedEvent, {selectedIds: this.attr.selectedIds});
      };

      this.after("initialize", function() {
        this.on('click', {
          'itemSelector': this.toggleItemSelect
        });

        this.initializeSelections();
      });
    }
  }
);