Blame view

controllets/tree-view-controllet/tree-view-controllet.html 8.47 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
              _initCreateTree : function(nodeName, node) {
29124159   root   ckan tree-view up...
126
127
128
129
130
  //                var list  = new Array();
  //                for(var child in node)
  //                    list.push(this._createTree(child, node[child]));
  //                return this._paper_submenu(nodeName, list);
  
9d4a34db   Luigi Serra   selection control...
131
                  var list  = new Array();
29124159   root   ckan tree-view up...
132
133
134
135
136
137
138
139
  
                  if(node.constructor == Array)
                      for(var child in node[0])
                          list.push(this._createTree(nodeName+","+child, node[0][child]));
                  else
                      for(var child in node)
                          list.push(this._createTree(child, node[child]));
  
9d4a34db   Luigi Serra   selection control...
140
141
                  return this._paper_submenu(nodeName, list);
              },
24be6abb   Luigi Serra   selection control...
142
  
9d4a34db   Luigi Serra   selection control...
143
144
              _createTree : function(nodeName, node) {
                  var html = "";
5054a06e   Renato De Donato   tree-view update
145
146
147
148
                  if(!node){
                      html = nodeName;
                  }
                  else if(node.constructor == Object){
9d4a34db   Luigi Serra   selection control...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
                      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...
170
  
9d4a34db   Luigi Serra   selection control...
171
172
              _paper_submenu : function(str, list) {
                  var submenu = "<paper-submenu>";
24be6abb   Luigi Serra   selection control...
173
  
9d4a34db   Luigi Serra   selection control...
174
                  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...
175
  
9d4a34db   Luigi Serra   selection control...
176
177
178
179
180
                  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...
181
                  }
9d4a34db   Luigi Serra   selection control...
182
183
184
185
  
                  submenu += "</paper-menu>" + "</paper-submenu>";
  
                  return submenu;
24be6abb   Luigi Serra   selection control...
186
187
              },
  
9d4a34db   Luigi Serra   selection control...
188
189
190
191
192
193
194
195
196
197
198
199
              _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
200
201
202
203
204
205
206
207
208
209
210
                          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...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
                      }
                  }
              },
  
              _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...
234
  
9d4a34db   Luigi Serra   selection control...
235
236
              _onSelect : function(e) {
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
237
238
239
              },
  
              _onDeselect : function(e) {
9d4a34db   Luigi Serra   selection control...
240
                  this._updateSelectedFields(e);
24be6abb   Luigi Serra   selection control...
241
242
              },
  
9d4a34db   Luigi Serra   selection control...
243
              _updateSelectedFields : function(e) {
5054a06e   Renato De Donato   tree-view update
244
  
24be6abb   Luigi Serra   selection control...
245
246
247
248
249
250
251
252
253
254
                  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
255
256
257
                  var prevLength;
                  if(this.selectedFields[menuId])
                      prevLength = this.selectedFields[menuId].length;
24be6abb   Luigi Serra   selection control...
258
                  else
5054a06e   Renato De Donato   tree-view update
259
260
                      prevLength = 0;
                  var currLength = selectedIds.length
24be6abb   Luigi Serra   selection control...
261
  
5054a06e   Renato De Donato   tree-view update
262
263
264
265
266
267
268
269
270
                  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...
271
272
273
274
275
276
              }
  
          });
  
      </script>
  
24be6abb   Luigi Serra   selection control...
277
  </dom-module>