Blame view

controllets/tree-view-controllet/tree-view-controllet.html 8.05 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
          --paper-item {
          }
  
          paper-item {
94601c1f   Renato De Donato   trevieww multitable
15
              font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
24be6abb   Luigi Serra   selection control...
16
17
18
19
20
21
22
              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
          :host{
              --paper-menu-focused-item-after: {
5054a06e   Renato De Donato   tree-view update
39
                   opacity: 0;
9d4a34db   Luigi Serra   selection control...
40
41
              };
          }
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
              _createTree : function(nodeName, node) {
                  var html = "";
5054a06e   Renato De Donato   tree-view update
134
135
136
137
                  if(!node){
                      html = nodeName;
                  }
                  else if(node.constructor == Object){
9d4a34db   Luigi Serra   selection control...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
                      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...
159
  
9d4a34db   Luigi Serra   selection control...
160
161
              _paper_submenu : function(str, list) {
                  var submenu = "<paper-submenu>";
24be6abb   Luigi Serra   selection control...
162
  
9d4a34db   Luigi Serra   selection control...
163
                  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...
164
  
9d4a34db   Luigi Serra   selection control...
165
166
167
168
169
                  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...
170
                  }
9d4a34db   Luigi Serra   selection control...
171
172
173
174
  
                  submenu += "</paper-menu>" + "</paper-submenu>";
  
                  return submenu;
24be6abb   Luigi Serra   selection control...
175
176
              },
  
9d4a34db   Luigi Serra   selection control...
177
178
179
180
181
182
183
184
185
186
187
188
              _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){
94601c1f   Renato De Donato   trevieww multitable
189
190
191
192
193
194
195
196
197
198
199
                          var fieldId = this.preselectedFields[field];
  
                          var menuId = this.rootName;
                          if (fieldId.lastIndexOf(",") != -1)
                              menuId = fieldId.substring(0, fieldId.lastIndexOf(","));
  
                          var menu = document.getElementById(menuId);
                          var field = document.getElementById(fieldId);
                          var index = menu.items.indexOf(field);
  
                          menu.select(index);
9d4a34db   Luigi Serra   selection control...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
                      }
                  }
              },
  
              _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...
223
  
9d4a34db   Luigi Serra   selection control...
224
225
              _onSelect : function(e) {
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
226
227
228
              },
  
              _onDeselect : function(e) {
9d4a34db   Luigi Serra   selection control...
229
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
230
231
              },
  
9d4a34db   Luigi Serra   selection control...
232
              _updateSelectedFields : function(e) {
5054a06e   Renato De Donato   tree-view update
233
  
24be6abb   Luigi Serra   selection control...
234
235
236
237
238
239
240
241
242
243
                  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
244
245
246
                  var prevLength;
                  if(this.selectedFields[menuId])
                      prevLength = this.selectedFields[menuId].length;
24be6abb   Luigi Serra   selection control...
247
                  else
5054a06e   Renato De Donato   tree-view update
248
249
                      prevLength = 0;
                  var currLength = selectedIds.length
24be6abb   Luigi Serra   selection control...
250
  
5054a06e   Renato De Donato   tree-view update
251
252
253
254
255
256
257
258
259
                  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...
260
261
262
263
264
265
              }
  
          });
  
      </script>
  
24be6abb   Luigi Serra   selection control...
266
  </dom-module>