Blame view

controllets/tree-view-controllet/tree-view-controllet.html 8.04 KB
24be6abb   Luigi Serra   selection control...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  <link rel="import" href="../../bower_components/polymer/polymer.html" />
  
  <link rel="import" href="../../bower_components/iron-collapse/iron-collapse.html">
  <link rel="import" href="../../bower_components/paper-menu/paper-menu.html">
  <link rel="import" href="../../bower_components/paper-menu/paper-submenu.html">
  <link rel="import" href="../../bower_components/paper-item/paper-item.html">
  
  <dom-module id="tree-view-controllet">
  
      <style is="custom-style">
  
          --paper-item {
          }
  
          paper-item {
              cursor: pointer;
          }
  
          paper-item.menu-trigger {
              font-weight: 700;
          }
  
9d4a34db   Luigi Serra   selection control...
23
24
25
26
          paper-item.menu-trigger.iron-selected {
              background-color: #B6B6B6;
          }
  
24be6abb   Luigi Serra   selection control...
27
28
          paper-item:not(.menu-trigger).iron-selected {
              background-color: #2196F3;
9d4a34db   Luigi Serra   selection control...
29
              color: #FFFFFF;
24be6abb   Luigi Serra   selection control...
30
31
32
33
34
35
36
          }
  
          .sublist {
              padding-left: 20px;
              padding-right: 20px;
          }
  
9d4a34db   Luigi Serra   selection control...
37
38
39
40
41
          :host{
              --paper-menu-focused-item-after: {
                  opacity: 0;
              };
          }
24be6abb   Luigi Serra   selection control...
42
43
44
45
      </style>
  
      <template>
  
9d4a34db   Luigi Serra   selection control...
46
          <paper-menu id="paper_tree"></paper-menu>
24be6abb   Luigi Serra   selection control...
47
48
49
50
51
52
53
54
55
56
57
58
  
      </template>
  
      <script>
          Polymer({
  
              is : 'tree-view-controllet',
  
              properties : {
  
                  rootName : {
                      type  : String,
9d4a34db   Luigi Serra   selection control...
59
                      value : "root"
24be6abb   Luigi Serra   selection control...
60
61
62
63
                  },
  
                  jsonData : {
                      type  : Object,
9d4a34db   Luigi Serra   selection control...
64
                      value : undefined
24be6abb   Luigi Serra   selection control...
65
66
67
68
69
70
71
                  },
  
                  selectedFields : {
                      type  : Array,
                      value : []
                  },
  
9d4a34db   Luigi Serra   selection control...
72
73
74
75
76
                  preselectedFields : {
                      type : Array,
                      value : []
                  },
  
24be6abb   Luigi Serra   selection control...
77
78
                  openedPath : {
                      type : String,
9d4a34db   Luigi Serra   selection control...
79
                      value : undefined
24be6abb   Luigi Serra   selection control...
80
81
82
83
84
85
86
87
88
89
90
                  }
  
              },
  
              listeners: {
                  'iron-select': '_onSelect',
                  'iron-deselect': '_onDeselect'
              },
  
              ready : function() {
                  if(this.jsonData)
9d4a34db   Luigi Serra   selection control...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
                      this._init();
              },
  
              getFields : function() {
                  return this.selectedFields;
              },
  
              getFlatFields : function() {
                  var fields = [];
  
                  for(var A in this.selectedFields)
                      for(var e in this.selectedFields[A])
                          fields.push(this.selectedFields[A][e]);
  
                  return fields;
24be6abb   Luigi Serra   selection control...
106
107
              },
  
9d4a34db   Luigi Serra   selection control...
108
109
110
111
              _init : function() {
                  this._injectBoundHTML(this._initCreateTree(this.rootName, this.jsonData), this.$.paper_tree);
                  this._preselectFields();
                  this._openPath();
24be6abb   Luigi Serra   selection control...
112
113
              },
  
9d4a34db   Luigi Serra   selection control...
114
              _injectBoundHTML : function(html, element) {
24be6abb   Luigi Serra   selection control...
115
116
117
118
119
120
121
122
123
124
                  var template = document.createElement('template');
                  template.innerHTML = html;
                  var fragment = this.instanceTemplate(template);
                  if (element) {
                      element.textContent = '';
                      element.appendChild(fragment);
                  }
                  return fragment;
              },
  
9d4a34db   Luigi Serra   selection control...
125
126
127
128
129
130
              _initCreateTree : function(nodeName, node) {
                  var list  = new Array();
                  for(var child in node)
                      list.push(this._createTree(child, node[child]));
                  return this._paper_submenu(nodeName, list);
              },
24be6abb   Luigi Serra   selection control...
131
  
9d4a34db   Luigi Serra   selection control...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
              _createTree : function(nodeName, node) {
                  var html = "";
                  if(node.constructor == Object){
                      var list  = new Array();
                      for(var child in node)
                          list.push(this._createTree(nodeName+","+child, node[child]));
                      html = this._paper_submenu(nodeName, list);
                  }
                  else if (node.constructor == Array){
                      var list  = new Array();
                      if(node[0].constructor == Object){
                          for(var child in node[0])
                              list.push(this._createTree(nodeName+","+child, node[0][child]));
                          html = this._paper_submenu(nodeName, list);
                      }
                      else{
                          html = nodeName;
                      }
                  }
                  else{
                      html = nodeName;
                  }
                  return html;
              },
24be6abb   Luigi Serra   selection control...
156
  
9d4a34db   Luigi Serra   selection control...
157
158
              _paper_submenu : function(str, list) {
                  var submenu = "<paper-submenu>";
24be6abb   Luigi Serra   selection control...
159
  
9d4a34db   Luigi Serra   selection control...
160
                  submenu += "<paper-item class=\"menu-trigger\">"+this._getName(str)+"</paper-item>" + "<paper-menu id=\""+str+"\" class=\"menu-content sublist\" multi>";
24be6abb   Luigi Serra   selection control...
161
  
9d4a34db   Luigi Serra   selection control...
162
163
164
165
166
                  for(var i in list){
                      if(list[i].indexOf("paper-submenu") != -1)
                          submenu += list[i];
                      else
                          submenu += this._paper_item(list[i]);
24be6abb   Luigi Serra   selection control...
167
                  }
9d4a34db   Luigi Serra   selection control...
168
169
170
171
  
                  submenu += "</paper-menu>" + "</paper-submenu>";
  
                  return submenu;
24be6abb   Luigi Serra   selection control...
172
173
              },
  
9d4a34db   Luigi Serra   selection control...
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
              _paper_item : function(str){
                  return "<paper-item id=\""+str+"\">"+this._getName(str)+"</paper-item>";
              },
  
              _getName : function (str){
                  str = str.split(",");
                  return str[str.length-1];
              },
  
              _preselectFields : function() {
                  if (this.preselectedFields){
                      for(var field in this.preselectedFields){
                          var path = this.preselectedFields[field];
  
                          if (path.lastIndexOf(",") != -1)
                              path = path.substring(0, path.lastIndexOf(","));
                          else
                              path = this.rootName;
  
                          var menu = document.getElementById(path);
                          var item = document.getElementById(this.preselectedFields[field]);
                          var index = menu.items.indexOf(item);
  
                          if (menu.selectedValues == undefined){
                              menu.selectedValues = [index];
                          }
                          else{
                              var selectedValues = [];
                              for (var i in menu.selectedValues)
                                  selectedValues.push(menu.selectedValues[i]);
                              selectedValues.push(index);
                              menu.selectedValues = selectedValues;
                          }
                      }
                  }
              },
  
              _openPath : function() {
                  this.$.paper_tree.firstChild.open();
  
                  if (this.openedPath) {
                      var openedPath = this.openedPath;
                      var nodes = openedPath.split(",");
                      openedPath = "";
  
                      while (nodes.length != 0) {
                          openedPath += nodes.splice(0, 1);
  
                          var menu = document.getElementById(openedPath);
                          var submenu = menu.parentNode.parentNode;
                          submenu.open();
  
                          openedPath += ",";
                      }
                  }
              },
24be6abb   Luigi Serra   selection control...
230
  
9d4a34db   Luigi Serra   selection control...
231
232
              _onSelect : function(e) {
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
233
234
235
              },
  
              _onDeselect : function(e) {
9d4a34db   Luigi Serra   selection control...
236
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
237
238
              },
  
9d4a34db   Luigi Serra   selection control...
239
              _updateSelectedFields : function(e) {
24be6abb   Luigi Serra   selection control...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
                  var menuId = e.target.id;
                  var selectedIds = [];
  
                  var selectedItems = (e.target).selectedItems;
                  for(var item in selectedItems){
                      var id = selectedItems[item].id;
                      if(id != "")//submenu
                          selectedIds.push(id);
                  }
  
                  if(selectedIds.length)
                      this.selectedFields[menuId] = selectedIds;
                  else
                      delete this.selectedFields[menuId];
24be6abb   Luigi Serra   selection control...
254
  
9d4a34db   Luigi Serra   selection control...
255
                  this.fire('tree-view-controllet_selected-fields', {fields : this.getFlatFields()});
24be6abb   Luigi Serra   selection control...
256
257
258
259
260
261
              }
  
          });
  
      </script>
  
24be6abb   Luigi Serra   selection control...
262
  </dom-module>