Blame view

controllets/treeview-controllet/treeview-controllet.html 17.9 KB
938d1928   Luigi Serra   Update documentat...
1
  <!--

584d6ecd   Luigi Serra   Update components...
2
3
  @license

      The MIT License (MIT)

938d1928   Luigi Serra   Update documentat...
4
  

584d6ecd   Luigi Serra   Update components...
5
      Copyright (c) 2015 Dipartimento di Informatica - Università di Salerno - Italy

938d1928   Luigi Serra   Update documentat...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  

      Permission is hereby granted, free of charge, to any person obtaining a copy

      of this software and associated documentation files (the "Software"), to deal

      in the Software without restriction, including without limitation the rights

      to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

      copies of the Software, and to permit persons to whom the Software is

      furnished to do so, subject to the following conditions:

  

      The above copyright notice and this permission notice shall be included in

      all copies or substantial portions of the Software.

  

      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

      THE SOFTWARE.

  -->

  

ae17a8dc   Luigi Serra   Controllet and da...
26
  <!--

950d181d   Luigi Serra   license updates
27
28
  * Developed by :

  * ROUTE-TO-PA Project - grant No 645860. - www.routetopa.eu

ae17a8dc   Luigi Serra   Controllet and da...
29
30
31
  *

  -->

  

73bcce88   luigser   COMPONENTS
32
33
  <link rel="import" href="../../bower_components/polymer/polymer.html">

  <link rel="import" href="../../bower_components/paper-styles/color.html">

74249687   Luigi Serra   Cross browser con...
34
  <link rel="import" href="../../bower_components/paper-checkbox/paper-checkbox.html">

73bcce88   luigser   COMPONENTS
35
  

584d6ecd   Luigi Serra   Update components...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  <!--

   `treeview-controllet` build a treeview from an input object. Each leaf fields of the object has a material design checkbox. When the user select a field

    an event will broadcast in order to make the component that use the treeview to get the current selected filed.

    After insert the `treeview-controllet` in the DOM you have to initialize it with the object you want put in the tree because of to make this component able

    to work with async object request.

  

  Example:

  

      <treeview-controllet id="treeviewID"></treeview-controllet>

  

      in the javascript code:

  

         this.$.treeviewID.init(object);

  

  @element items-slider-controllet

  @status beta

  @homepage index.html

  @group controllets

  -->

  

73bcce88   luigser   COMPONENTS
56
57
  <dom-module id="treeview-controllet">

      <template>

74249687   Luigi Serra   Cross browser con...
58
  

73bcce88   luigser   COMPONENTS
59
60
          <style is="custom-style">

  

74249687   Luigi Serra   Cross browser con...
61
              ::content .list {

73bcce88   luigser   COMPONENTS
62
63
64
65
66
                  margin: .5rem;

                  display: block;

                  list-style-type: none;

              }

  

835a711f   Luigi Serra   Controllet cross ...
67
              ::content li.list__item{

74249687   Luigi Serra   Cross browser con...
68
69
70
                  position: relative;

                  float: left;

                  width: 100%;

73bcce88   luigser   COMPONENTS
71
72
              }

  

835a711f   Luigi Serra   Controllet cross ...
73
              ::content ul.list{

73bcce88   luigser   COMPONENTS
74
                  position: relative;

74249687   Luigi Serra   Cross browser con...
75
76
                  float : left   ;

                  width: 100%;

73bcce88   luigser   COMPONENTS
77
78
              }

  

74249687   Luigi Serra   Cross browser con...
79
80
81
              ::content .list__item {

                  margin: 0 0 .5rem 0;

                  padding: 0;

73bcce88   luigser   COMPONENTS
82
83
              }

  

74249687   Luigi Serra   Cross browser con...
84
85
86
87
88
89
90
              ::content main   { font-size:16px; }

              ::content .stuff { padding-top:60px; }

  

              /* basic css */

              ::content input[type=checkbox]   { display:none; position:absolute; left:0;}

              ::content label, p               { padding-left:10px; }

              ::content .check-awesome         { position:relative; float:left; }

73bcce88   luigser   COMPONENTS
91
  

74249687   Luigi Serra   Cross browser con...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
              /* checkbox css */

              ::content label                  { cursor:pointer; }

              ::content label span             { display:block; position:absolute; left:0;

                  -webkit-transition-duration:0.3s; -moz-transition-duration:0.3s; transition-duration:0.3s;

              }

              ::content label .circle          {

                  background:#FFF;

                  left:-30px;

                  top:-30px;

                  height:80px;

                  width:80px;

                  z-index:-1;

                  border-radius:50%; -moz-border-radius:50%; -webkit-border-radius:50%;

              }

              ::content label .box     {

                  border:2px solid #DDDDDD;

                  height:16px;

                  width:16px;

                  z-index:888;

                  -webkit-transition-delay:0.1s; -moz-transition-delay:0.1s; transition-delay:0.1s;

73bcce88   luigser   COMPONENTS
112
                  position: relative;

74249687   Luigi Serra   Cross browser con...
113
                  float: left;

73bcce88   luigser   COMPONENTS
114
              }

74249687   Luigi Serra   Cross browser con...
115
116
117
118
119
120
121
122
123
124
125
126
              ::content label .check         {

                  top: -7px;

                  left: 6px;

                  width: 11px;

                  height: 16px;

                  border:2px solid #2196f3;

                  border-top: none;

                  border-left: none;

                  opacity:0;

                  z-index:888;

                  -webkit-transform:rotate(180deg); -moz-transform:rotate(180deg); transform:rotate(180deg);

                  -webkit-transition-delay:0.1s; -moz-transition-delay:0.1s; transition-delay:0.1s;

73bcce88   luigser   COMPONENTS
127
128
              }

  

74249687   Luigi Serra   Cross browser con...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
              /* handling click events */

  

              /* when checkbox is checked */

              ::content label .circle {

                  -webkit-animation   :growCircle 0.3s ease;

                  -moz-animation      :growCircle 0.3s ease;

                  animation           :growCircle 0.3s ease;

              }

              ::content input[type=checkbox]:checked ~ label .box {

                  opacity:0;

                  -webkit-transform   :scale(0) rotate(-180deg);

                  -moz-transform        :scale(0) rotate(-180deg);

                  transform                 :scale(0) rotate(-180deg);

              }

              ::content input[type=checkbox]:checked ~ label .check {

                  opacity:1;

                  -webkit-transform   :scale(1) rotate(45deg);

                  -moz-transform      :scale(1) rotate(45deg);

                  transform           :scale(1) rotate(45deg);

73bcce88   luigser   COMPONENTS
148
149
              }

  

74249687   Luigi Serra   Cross browser con...
150
151
152
153
154
155
156
157
158
159
160
161
162
              /* bubble animation */

  

              ::content @-webkit-keyframes growCircle {

                  0%, 100%   { -webkit-transform:scale(0); }

                  70%        { background:#DDD; -webkit-transform:scale(1.25); }

              }

              ::content @-moz-keyframes growCircle {

                  0%, 100%   { -moz-transform:scale(0); }

                  70%        { background:#DDD; -moz-transform:scale(1.25); }

              }

              ::content @keyframes growCircle {

                  0%, 100%   { transform:scale(0); }

                  70%        { background:#DDD; transform:scale(1.25); }

73bcce88   luigser   COMPONENTS
163
164
              }

  

74249687   Luigi Serra   Cross browser con...
165
166
              ::content .select-all{

                 /* position:relative;

73bcce88   luigser   COMPONENTS
167
                  float:left;

74249687   Luigi Serra   Cross browser con...
168
                  left: 20px;*/

73bcce88   luigser   COMPONENTS
169
170
              }

  

74249687   Luigi Serra   Cross browser con...
171
172
173
174
175
176
              ::content div.hiding_panel{

                  width: 100%;

                  height: 15px;

                  margin-bottom: 18px;

                  /*padding: 3px;

                  height: 35px;

73bcce88   luigser   COMPONENTS
177
178
179
                  border: 1px;

                  background: #fff;

                  border-radius: 0.125rem;

74249687   Luigi Serra   Cross browser con...
180
                  box-shadow: 0 0.125rem 0.3125rem 0 rgba(0, 0, 0, 0.25);*/

73bcce88   luigser   COMPONENTS
181
182
              }

  

74249687   Luigi Serra   Cross browser con...
183
              ::content div.panel-title{

73bcce88   luigser   COMPONENTS
184
185
186
                  position: relative;

                  float: left;

                  padding-left: 10px;

74249687   Luigi Serra   Cross browser con...
187
                  padding-right: 10px;

73bcce88   luigser   COMPONENTS
188
189
190
191
                  font-weight: bolder;

                  color: #000000;

              }

  

74249687   Luigi Serra   Cross browser con...
192
              ::content iron-icon.panel-hide-trigger {

73bcce88   luigser   COMPONENTS
193
194
195
196
197
198
199
200
201
202
203
                  position: relative;

                  float: left;

                  height: 24px;

                  width: 24px;

                  border-radius: 50%;

                  /*background: var(--paper-pink-500);*/

                  background: #348ED5;

                  color: white;

                  line-height: 64px;

              }

  

74249687   Luigi Serra   Cross browser con...
204
              ::content iron-icon.panel-hide-trigger:hover {

73bcce88   luigser   COMPONENTS
205
206
207
208
                  /*color: var(--google-gray-700);*/

                  color : #000000;

              }

  

74249687   Luigi Serra   Cross browser con...
209
              ::content .items-list{

73bcce88   luigser   COMPONENTS
210
211
212
213
214
                  display: none;

              }

  

          </style>

  

dbc787cf   Luigi Serra   Controllet cross ...
215
216
  

  

73bcce88   luigser   COMPONENTS
217
218
219
220
221
222
223
224
225
226
227
228
          <div class="vertical-section">

              <div id="treeview_placeholder" class="horizontal-section"></div>

          </div>

  

      </template>

  

      <script>

  

          Polymer({

  

              is : 'treeview-controllet',

  

035bbee3   Luigi Serra   Datalets and cont...
229
230
231
232
233
234
              /**

               * Fired when the user check a checkbox

               *

               * @event treeview-controllet_fileds-selected

               * */

  

73bcce88   luigser   COMPONENTS
235
236
237
238
239
              listeners : {

                  'tap' : '_clickHandler'

              },

  

              properties : {

584d6ecd   Luigi Serra   Update components...
240
241
242
243
                  /**

                   * It stores the fields structure of the tree

                   *

                   */

73bcce88   luigser   COMPONENTS
244
245
246
                  fieldsMap : {

                      type : Map,

                      value : null

24be6abb   Luigi Serra   selection control...
247
248
249
250
251
252
253
                  },

                  /**

                   * Array of pre-selected fields. it's used to open the treeview with pre-selected checkbox checked

                   */

                  selectedFields:{

                     type: Array,

                     value: undefined

73bcce88   luigser   COMPONENTS
254
255
256
                  }

  

              },

584d6ecd   Luigi Serra   Update components...
257
258
259
260
              /**

               * Create the container for all leaf node fields when a field has a children set.

               *

               */

74249687   Luigi Serra   Cross browser con...
261
              createFieldsContainer : function(id, heading){

73bcce88   luigser   COMPONENTS
262
263
264
265
266
267
268
  

                  var ul = document.createElement('ul');

                  ul.className     = "list";

                  ul.id                  = id;

                  ul.innerHTML           = '<div class="hiding_panel">' +

                                              '<iron-icon icon="chevron-right" class="panel-hide-trigger"></iron-icon>' +

                                              '<div class="panel-title">' + heading + '</div>' +

74249687   Luigi Serra   Cross browser con...
269
270
271
272
273
274
275
276
277
278
                                              /*'<input id="'+ heading +'" type="checkbox" class="checkbox select-all" style="display:block;">' +*/

                                              /*'<div class="check-awesome">' +

                                                  '<input class="checkbox select-all" type="checkbox" id="'+ heading + '">' +

                                                      '<label for="'+ heading + '"class="label">' +

                                                      '<span></span>' +

                                                      '<span class="check"></span>' +

                                                      '<span class="box"></span>'+

                                                  '</label>' +

                                              '</div>' +*/

                                           '</div>';

cb1aab91   Luigi Serra   Controllets update
279
280
                                           /*'<div class="items-list"></div>';*/

                  ul.innerHTML += (id.indexOf("records") != -1)  ?  '<div class="items-list" style="display:block;"></div>' : '<div class="items-list"></div>';

73bcce88   luigser   COMPONENTS
281
282
283
284
  

                  return ul;

  

              },

584d6ecd   Luigi Serra   Update components...
285
              /**

24be6abb   Luigi Serra   selection control...
286
287
288
289
290
291
292
293
294
               * check if fields is in preselection list

               */

              checkIsFieldsIsSelected : function(field){

                  if(this.selectedFields == undefined) return false;

                  for(var i = 0; i < this.selectedFields.length; i++)

                      if(field.replace("root,","") == this.selectedFields[i]) return true;

                  return false;

              },

              /**

584d6ecd   Luigi Serra   Update components...
295
296
297
               * Create the checkbox for current leaf node field.

               *

               */

73bcce88   luigser   COMPONENTS
298
              createListItem : function(id, label) {

74249687   Luigi Serra   Cross browser con...
299
300
301
  

                 return '<li class="list__item">' +

                           '<div class="check-awesome">' +

24be6abb   Luigi Serra   selection control...
302
                              '<input class="checkbox" type="checkbox" id="'+ id + '"' + ((this.checkIsFieldsIsSelected(id)) ? 'checked' : '')  + '>' +

74249687   Luigi Serra   Cross browser con...
303
304
305
306
307
308
309
310
                              '<label for="'+ id + '" style="padding-left:10px;">' +

                                  '<span></span>' +

                                  '<span class="check"></span>' +

                                  '<span class="box"></span>' +

                                     label +

                              '</label>' +

                           '</div>' +

                        '</li>';

73bcce88   luigser   COMPONENTS
311
              },

584d6ecd   Luigi Serra   Update components...
312
313
314
315
              /**

               * Recursively analyze the input object and build the treeview

               *

               */

73bcce88   luigser   COMPONENTS
316
              analyzeObject : function(parent_list, curr_field, object) {

0b818d7e   Luigi Serra   Selection control...
317
                  if(object == null) return;

73bcce88   luigser   COMPONENTS
318
319
320
                  if(curr_field != null){

                      if(object == null) object = "";

  

4e7eacf4   Luigi Serra   Controllets and d...
321
322
                      var curr_map_parent_list = parent_list.slice();

                      curr_map_parent_list.splice(-1);

73bcce88   luigser   COMPONENTS
323
324
325
                      if(object.constructor == Array){//Deal with flat array case

                          if(object[0].constructor != Object){

                              //MATERIAL CHECKBOX UL

4e7eacf4   Luigi Serra   Controllets and d...
326
                              this.fieldsMap[curr_map_parent_list.toString()].children[1].innerHTML += this.createListItem(parent_list.toString(), curr_field);

73bcce88   luigser   COMPONENTS
327
328
329
330
                              return;

                          }

                      }

                      if(object.constructor == Array || object.constructor == Object){

74249687   Luigi Serra   Cross browser con...
331
                          var panel = this.createFieldsContainer(curr_field, parent_list[parent_list.length - 1]);

4e7eacf4   Luigi Serra   Controllets and d...
332
                          this.fieldsMap[/*curr_field*/parent_list.toString()] = panel;

73bcce88   luigser   COMPONENTS
333
                          //MATERIAL CHECKBOX UL

4e7eacf4   Luigi Serra   Controllets and d...
334
                          this.fieldsMap[curr_map_parent_list.toString()].appendChild(this.fieldsMap[parent_list.toString()]);

73bcce88   luigser   COMPONENTS
335
336
                      }else{

                          //MATERIAL CHECKBOX UL

4e7eacf4   Luigi Serra   Controllets and d...
337
                          this.fieldsMap[curr_map_parent_list.toString()].children[1].innerHTML += this.createListItem(parent_list.toString(), curr_field);

73bcce88   luigser   COMPONENTS
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
                      }

                  }

                  if(object.constructor == Array || object.constructor == Object) {

                      var obj = (object.constructor == Array) ? object[0] : object;

                      if(Object.prototype.toString.call(obj) === '[object String]') return;//Deal with flat array case

                      for (var field in obj) {

                          var parents = new Array();

                          parent_list.every(function (element, index, array) {

                              parents.push(element);

                              return true;

                          });

                          parents.push(field);

                          this.analyzeObject(parents, field, obj[field]);

                      }

                  }

              },

584d6ecd   Luigi Serra   Update components...
354
355
356
357
358
              /**

               * Initialize treeview with the object you want put in the tree because of to make this component able

               * to work with async object request.

               *

               */

24be6abb   Luigi Serra   selection control...
359
360
              init : function(data, preselection){

                  this.selectedFields = preselection;

73bcce88   luigser   COMPONENTS
361
                  //crete root node and insert it in to shadow dom

74249687   Luigi Serra   Cross browser con...
362
                  var mainPanel = this.createFieldsContainer("root", "Data fields");

73bcce88   luigser   COMPONENTS
363
                  //MATERIAL CHECKBOX UL

74249687   Luigi Serra   Cross browser con...
364
                  mainPanel.children[1].style.display = "block";

73bcce88   luigser   COMPONENTS
365
366
367
368
369
370
371
372
  

                  this.$.treeview_placeholder.innerHTML = "";

                  this.$.treeview_placeholder.appendChild(mainPanel);

                  this.fieldsMap = new Map;

                  this.fieldsMap["root"] = mainPanel;

                  //call recursive analyze function for current json to get all fields user can select

                  this.analyzeObject(new Array("root"), null, data);

  

24be6abb   Luigi Serra   selection control...
373
374
                  if(this.selectedFields != undefined) this.fireSelectedFields();

  

73bcce88   luigser   COMPONENTS
375
              },

584d6ecd   Luigi Serra   Update components...
376
377
378
379
              /**

               * When a user click on a checkbox it retrieves all checked fields and fire an event with this information.

               *

               */

73bcce88   luigser   COMPONENTS
380
381
382
383
384
385
386
387
388
              fireSelectedFields : function(){

  

                  var cbs = Polymer.dom(this.$.treeview_placeholder).querySelectorAll('input[type=checkbox]:checked');

                  var fields = Array();

                  for(var i=0;i<cbs.length;i++) {

                      if(cbs[i].className.includes("select-all")) continue;

                      fields.push(cbs[i].id.replace("root,",""));

                  }

  

035bbee3   Luigi Serra   Datalets and cont...
389
                  this.fire('treeview-controllet_fileds-selected', {fields : fields});

73bcce88   luigser   COMPONENTS
390
391
392
              },

  

              _clickHandler : function(e){

74249687   Luigi Serra   Cross browser con...
393
394
395
396
                  if(Object.getPrototypeOf(e) !== CustomEvent.prototype) {

                      e = e[Object.keys(e)[0]];

                  }

  

73bcce88   luigser   COMPONENTS
397
398
                  if(!e.target.control) {

                      switch((e.target.className.indexOf("iron-icon") != -1) ? "panel-hide-trigger" : e.target.className){

74249687   Luigi Serra   Cross browser con...
399
                          case 'box':

73bcce88   luigser   COMPONENTS
400
  

74249687   Luigi Serra   Cross browser con...
401
402
403
404
405
406
407
408
                              /*var el = e.target.previousSibling.previousSibling;

                              el.classList = 'circle';

                              var newone = el.clone(true);

                              el.before(newone);

                              $("." + el.attr("class") + ":last").remove();*/

  

                              break;

                          case 'checkbox':

73bcce88   luigser   COMPONENTS
409
410
411
412
413
                              this.fireSelectedFields();

  

                              break;

                          case 'panel-hide-trigger':

  

74249687   Luigi Serra   Cross browser con...
414
415
                              if(e.target.parentNode.parentNode.children[1].style.display == "block") {

                                  e.target.parentNode.parentNode.children[1].style.cssText = "display: none;";

73bcce88   luigser   COMPONENTS
416
417
418
                                  e.target.icon = "chevron-right";

  

                              }else{

74249687   Luigi Serra   Cross browser con...
419
                                  e.target.parentNode.parentNode.children[1].style.cssText = "display: block;";

73bcce88   luigser   COMPONENTS
420
421
422
423
                                  e.target.icon = "expand-more";

                              }

                              break;

                          case 'checkbox select-all':

74249687   Luigi Serra   Cross browser con...
424
                                  var childs = e.srcElement.parentNode.parentNode.children[1].childNodes;

73bcce88   luigser   COMPONENTS
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
                                  for(var i=0; i < childs.length; i++){

                                      childs[i].children[0].checked = e.srcElement.checked;

                                  }

  

                                  this.fireSelectedFields();

  

                              break;

                      }

                  }

              }

          });

  

      </script>

  

  </dom-module>