Blame view

controllets/tree-view-controllet/tree-view-controllet.html 8.46 KB
24be6abb   Luigi Serra   selection control...
1
2
3
4
5
6
7
8
9
10
  <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">
24be6abb   Luigi Serra   selection control...
11
12
13
14
15
16
17
18
19
20
21
          --paper-item {
          }
  
          paper-item {
              cursor: pointer;
          }
  
          paper-item.menu-trigger {
              font-weight: 700;
          }
  
9d4a34db   Luigi Serra   selection control...
22
23
24
25
          paper-item.menu-trigger.iron-selected {
              background-color: #B6B6B6;
          }
  
24be6abb   Luigi Serra   selection control...
26
27
          paper-item:not(.menu-trigger).iron-selected {
              background-color: #2196F3;
9d4a34db   Luigi Serra   selection control...
28
              color: #FFFFFF;
24be6abb   Luigi Serra   selection control...
29
30
31
32
33
34
35
          }
  
          .sublist {
              padding-left: 20px;
              padding-right: 20px;
          }
  
9d4a34db   Luigi Serra   selection control...
36
37
          :host{
              --paper-menu-focused-item-after: {
5054a06e   Renato De Donato   tree-view update
38
                   opacity: 0;
9d4a34db   Luigi Serra   selection control...
39
40
              };
          }
24be6abb   Luigi Serra   selection control...
41
42
43
44
      </style>
  
      <template>
  
9d4a34db   Luigi Serra   selection control...
45
          <paper-menu id="paper_tree"></paper-menu>
24be6abb   Luigi Serra   selection control...
46
47
48
49
50
51
52
53
54
55
56
57
  
      </template>
  
      <script>
          Polymer({
  
              is : 'tree-view-controllet',
  
              properties : {
  
                  rootName : {
                      type  : String,
9d4a34db   Luigi Serra   selection control...
58
                      value : "root"
24be6abb   Luigi Serra   selection control...
59
60
61
62
                  },
  
                  jsonData : {
                      type  : Object,
9d4a34db   Luigi Serra   selection control...
63
                      value : undefined
24be6abb   Luigi Serra   selection control...
64
65
66
67
68
69
70
                  },
  
                  selectedFields : {
                      type  : Array,
                      value : []
                  },
  
9d4a34db   Luigi Serra   selection control...
71
72
73
74
75
                  preselectedFields : {
                      type : Array,
                      value : []
                  },
  
24be6abb   Luigi Serra   selection control...
76
77
                  openedPath : {
                      type : String,
9d4a34db   Luigi Serra   selection control...
78
                      value : undefined
24be6abb   Luigi Serra   selection control...
79
80
81
82
83
84
85
86
87
88
89
                  }
  
              },
  
              listeners: {
                  'iron-select': '_onSelect',
                  'iron-deselect': '_onDeselect'
              },
  
              ready : function() {
                  if(this.jsonData)
9d4a34db   Luigi Serra   selection control...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                      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...
105
106
              },
  
9d4a34db   Luigi Serra   selection control...
107
108
109
110
              _init : function() {
                  this._injectBoundHTML(this._initCreateTree(this.rootName, this.jsonData), this.$.paper_tree);
                  this._preselectFields();
                  this._openPath();
24be6abb   Luigi Serra   selection control...
111
112
              },
  
9d4a34db   Luigi Serra   selection control...
113
              _injectBoundHTML : function(html, element) {
24be6abb   Luigi Serra   selection control...
114
115
116
117
118
119
120
121
122
123
                  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...
124
125
126
127
128
129
              _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...
130
  
9d4a34db   Luigi Serra   selection control...
131
132
              _createTree : function(nodeName, node) {
                  var html = "";
5054a06e   Renato De Donato   tree-view update
133
134
135
136
                  if(!node){
                      html = nodeName;
                  }
                  else if(node.constructor == Object){
9d4a34db   Luigi Serra   selection control...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
                      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...
158
  
9d4a34db   Luigi Serra   selection control...
159
160
              _paper_submenu : function(str, list) {
                  var submenu = "<paper-submenu>";
24be6abb   Luigi Serra   selection control...
161
  
9d4a34db   Luigi Serra   selection control...
162
                  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...
163
  
9d4a34db   Luigi Serra   selection control...
164
165
166
167
168
                  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...
169
                  }
9d4a34db   Luigi Serra   selection control...
170
171
172
173
  
                  submenu += "</paper-menu>" + "</paper-submenu>";
  
                  return submenu;
24be6abb   Luigi Serra   selection control...
174
175
              },
  
9d4a34db   Luigi Serra   selection control...
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
230
231
              _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...
232
  
9d4a34db   Luigi Serra   selection control...
233
234
              _onSelect : function(e) {
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
235
236
237
              },
  
              _onDeselect : function(e) {
9d4a34db   Luigi Serra   selection control...
238
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
239
240
              },
  
9d4a34db   Luigi Serra   selection control...
241
              _updateSelectedFields : function(e) {
5054a06e   Renato De Donato   tree-view update
242
  
24be6abb   Luigi Serra   selection control...
243
244
245
246
247
248
249
250
251
252
                  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);
                  }
  
5054a06e   Renato De Donato   tree-view update
253
254
255
                  var prevLength;
                  if(this.selectedFields[menuId])
                      prevLength = this.selectedFields[menuId].length;
24be6abb   Luigi Serra   selection control...
256
                  else
5054a06e   Renato De Donato   tree-view update
257
258
                      prevLength = 0;
                  var currLength = selectedIds.length
24be6abb   Luigi Serra   selection control...
259
  
5054a06e   Renato De Donato   tree-view update
260
261
262
263
264
265
266
267
268
                  if(prevLength != currLength) {
  
                      if (selectedIds.length)
                          this.selectedFields[menuId] = selectedIds;
                      else
                          delete this.selectedFields[menuId];
  
                      this.fire('tree-view-controllet_selected-fields', {fields: this.getFlatFields()});
                  }
24be6abb   Luigi Serra   selection control...
269
270
271
272
273
274
              }
  
          });
  
      </script>
  
24be6abb   Luigi Serra   selection control...
275
  </dom-module>