Blame view

controllets/datalet-selection-controllet/datalet-selection-controllet_.html 14.3 KB
21e3e3f2   isisadmin   fix controllet da...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  <!--

  @group ROUTETOPA Polymer Core Elements

  

  The `select-visualization-form-controllet` is a slider container of elements.

  Pass to this component an array of components, a data url(CKAN api uri) and a string with one or multiple query in JsonPath format(separated by spaces) and it'll show a

  slider with all components initialized with data and query attributes

  

  ### Code

  

  Example

  

  @class select-visualization-form-controllet

  

  -->

  <link rel="import" href="../../bower_components/polymer/polymer.html">

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

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

  <!--<link rel="import" href="../../bower_components/iron-ajax/iron-request.html">

  <link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">-->

  

  <dom-module id="datalet-selection-controllet">

      <template>

          <link rel="stylesheet" href="../shared_js/bootstrap-3.3.4-dist/css/bootstrap.paper.style.min.css">

          <link rel="stylesheet" href="../shared_js/bootstrap-toggle-master/css/bootstrap-toggle.min.css">

          <style is="custom-style">

              body {

                  font-family: RobotoDraft, 'Helvetica Neue', Helvetica, Arial;

                  font-size: 14px;

                  margin: 0;

                  padding: 24px;

                  -webkit-tap-highlight-color: rgba(0,0,0,0);

                  -webkit-touch-callout: none;

              }

  

              paper-input {

                  width: 80%;

              }

  

              paper-checkbox {

                  display: block;

                  margin-bottom: 40px;

              }

  

          </style>

  

          <!--<iron-ajax

                  auto

                  id="data_request"

                  url={{data_url}}

                  handle-as="json"

                  verbose="true"

                  on-response="this.handleResponseData"

                  debounce-duration="300">

          </iron-ajax>

  

          <iron-ajax

                  auto

                  url="http://service.routetopa.eu/WebComponentsDEV/DEEP/datalets-list"

                  handle-as="json"

                  on-response="handleResponseDatalets"

                  debounce-duration="300">

          </iron-ajax>

  

          <iron-request id="xhr"></iron-request>-->

  

          <div class="panel panel-default">

              <div class="panel-heading">

                  <h3 class="panel-title">Data source</h3>

              </div>

              <div class="panel-body">

                  <section>

                      <paper-input id="data_url" label="CKAN api data url" floatingLabel value="{{data_url}}"></paper-input>

                  </section>

              </div>

          </div>

  

          <div class="panel panel-default">

              <div class="panel-heading">

                  <h3 class="panel-title">Selection data fileds to visualize</h3>

              </div>

              <div class="panel-body">

                  <section>

                      <div class="horizontal center-justified layout">

                          <div id="fields_placeholder" class="horizontal-section">

                             <!-- <template is="dom-repeat" items="{{fields}}">

                                  <paper-checkbox>{{item}}</paper-checkbox>

                              </template>-->

                          </div>

                      </div>

                  </section>

              </div>

          </div>

  

          <div class="panel panel-default">

              <div class="panel-heading">

                  <h3 class="panel-title">Visualization tools selection</h3>

              </div>

              <div class="panel-body">

                  <div id="datalets_menu" class="layout"></div>

              </div>

          </div>

  

          <section>

              <!--<paper-button on-click="goto" raised>Go to visualization</paper-button>-->

              <button id="goto" type="button" class="btn btn-primary">Go to visualization</button>

          </section>

          <content></content>

      </template>

  

      <script src="../shared_js/bootstrap-3.3.4-dist/js/bootstrap.min.js"></script>

      <script src="../shared_js/bootstrap-toggle-master/js/bootstrap-toggle.min.js"></script>

  

      <script>

  

          DataletSelectionControllet = Polymer({

              is: 'datalet-selection-controllet',

              properties: {

                  /**

                   * It represent the data url from CKAN api

                   *

                   * @attribute data_url

                   * @type string

                   * @default 'null'

                   */

                  data_url: {

                      type: String,

                      value: "http://demo.ckan.org/api/action/datastore_search?resource_id=8324a4c9-66d6-47bf-a898-94237cc39b9f&limit=50",

                      observer: '_data_urlChanged'

                  },

                  /**

                   * It stores all tools available in WebComponent server

                   *

                   * @attribute tools

                   * @type array

                   * @default 'null'

                   */

                  tools:{

                     type: Array,

                     value: []

                  },

                  /**

                   * It stores all fields related to data retrieved from CKAN api url

                   *

                   * @attribute fields

                   * @type array

                   * @default 'null'

                   */

                  fields:{

                     type: Array,

                     value: []

                  },

                  fieldsMap: {

                      type: Map,

                      value: null

                  }

              },

              /**

               * data-url member changed watcher. It's called when data_url changes its value

               *

               * @method data_urlChanged

               */

              _data_urlChanged: function(newValue, oldValue){

                  //this.$.xhr.send({url: newValue, body: {}}).then(function (e){this.handleResponseData(e)});

                  //get the data fields

                  var comp = this;

                  $.ajax({

                      url: newValue,

                      dataType: "json",

                      success: function(e){comp.handleResponseData(e)}

                  });

                  //get datalets list

                  $.ajax({

                      url: "http://service.routetopa.eu/WebComponentsDEV/DEEP/datalets-list",

                      dataType: "json",

                      success: function(e){comp.handleResponseDatalets(e)}

                  });

              },

              createFieldsContainer: function(id, heading){

                  var panelDefault       = document.createElement('div');

                  panelDefault.className = 'panel panel-default';

                  var panelHeading       = document. createElement('div');

                  panelHeading.className = 'panel-heading';

                  panelHeading.innerHTML = heading;

                  var panelBody          = document. createElement('div');

                  panelBody.className    = 'panel-body';

                  panelBody.id           = id;

                  var fieldPanel         = document.createElement('div');

                  fieldPanel.className   = 'panel-body';

  

                  panelBody.appendChild(fieldPanel);

                  panelDefault.appendChild(panelHeading);

                  panelDefault.appendChild(panelBody);

  

                  return panelDefault;

  

              },

              cretePaperCheckbox: function (id, label ) {

                  var html = '<paper-checkbox id="' + id + '" role="checkbox" tabindex="0" toggles="" aria-pressed="false" aria-disabled="false" aria-checked="false" aria-label="' + label + '" class="x-scope paper-checkbox-0">'

                          +    '<div id="checkboxContainer" class="style-scope paper-checkbox">'

                          +       '<paper-ripple id="ink" class="circle style-scope paper-checkbox" center="">'

                          +          '<div id="background" class="style-scope paper-ripple"></div>'

                          +          '<div id="waves" class="style-scope paper-ripple"></div>'

                          +       '</paper-ripple>'

                          +    '<div id="checkbox" class="style-scope paper-checkbox">'

                          +       '<div id="checkmark" class="hidden style-scope paper-checkbox"></div>'

                          +       '</div>'

                          +    '</div>'

                          +    '<div id="checkboxLabel" aria-hidden="true" class="style-scope paper-checkbox">' + label + '</div>'

                          +  '</paper-checkbox>';

  

                  return html;

              },

              createBootstrapCheckbox: function(id, label) {

                  return '<div class="checkbox checkbox-primary">'

                          + '<label class="checkbox-inline">'

                          +    '<input type="checkbox" data-label="'+ label +'" id="'+ id +'"/>' + label

                          + '</label>'

                          + '</div>';

  

              },

              createBootstrapToggle: function(id, label) {

                  return '<div class="checkbox">'

                          + '<label style="padding-left: 3px; text-decoration: double;">'

                          +    '<input type="checkbox" data-label="'+ label +'" id="'+ id +'" data-toggle="toggle" />   ' + label

                          + '</label>'

                          + '</div>';

  

              },

              /**

               * Callback to parse the data requested when data_url change its value

               *

               * @method handleResponseData

               */

              handleResponseData: function(e){

                  //crete root node and insert it in to shadow dom

                  var mainPanel = this.createFieldsContainer("root", "Data fields");

                  this.$.fields_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("root", null, e);

              },

              analyzeObject: function(parent, curr_field, object) {

                  if(curr_field != null){

                      var parent_list = parent.split('.');

                      var ele = null;

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

                          var panel = this.createFieldsContainer(curr_field, parent_list[parent_list.length - 1]);

                          this.fieldsMap[curr_field] = panel;

                          this.fieldsMap[parent_list[parent_list.length - 2]].childNodes[1].appendChild(this.fieldsMap[curr_field]);

  

                      }else{

                          /*var checkbox = document.createElement('paper-checkbox');

                          checkbox.innerHTML = "<b>" + curr_field + '</b><br>';

                          checkbox.id        = parent;

                          this.fieldsMap[parent_list[parent_list.length - 2]].childNodes[1].childNodes[0].appendChild(checkbox);*/

                          //Polymer.dom.flush();

                          //this.fieldsMap[parent_list[parent_list.length - 2]].childNodes[1].childNodes[0].innerHTML += this.cretePaperCheckbox(parent, curr_field);

                          this.fieldsMap[parent_list[parent_list.length - 2]].childNodes[1].childNodes[0].innerHTML += this.createBootstrapToggle(parent, curr_field);

  

                      }

                  }

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

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

                      for (var field in obj) {

                          if (obj[field] != null) {

                                  parent_list = parent.split('.');

                                  this.analyzeObject(parent + '.' + field, field, obj[field]);

                          }

                      }

                  }

              },

              /**

               * Callback to parse the components response object

               *

               * @method handleResponseComponents

               */

              handleResponseDatalets: function(e){

                  this.tools = e;

                  /*html = '<paper-menu>';

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

                      html += '<paper-item>' + e[i] + '</paper-item>';

                  }

                  html += '</paper-menu>';*/

                  html = '<label for="datalets_selector">Available Datalets list:</label>'

                       + '<select class="form-control" id="datalets_selector">';

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

                      html += '<option>' + e[i] + '</option>';

                  }

                  html += '</select>';

                  $(this.$.datalets_menu).append(html);

              },

              /**

               * Callback called when "Go to visualization" button is pressed

               *

               * @method handleResponseComponents

               */

              goto: function(){

                  var url = "http://service.routetopa.eu/oxwall/webcomp";

                  var query = "$result$records[";

                  var cbs = this.shadowRoot.querySelectorAll("paper-checkbox");

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

                      if(cbs[i].checked){

                          query += cbs[i].id + ",";

                      }

                  }

                  query = query.slice(0,-1);

                  query += "]";

  

                  var cmp = this.$.toolsmenu.selectedItemLabel;

  

                  url += "/" + cmp + "/" + btoa(this.data_url) + "/" + query + "/0";

  

                  window.location = url;

              },

              ready: function(){

  

                  /*this.fields = [

                      {parent:"root", name :"No fields available"}

                  ];*/

  

                  this.tools = [

                      'No tools available'

                  ];

  

                  //this.$.data_request.addEventListener('response', this.handleResponseData);

  

                 /* var comp = this;

                  $.ajax({

                      url: "http://service.routetopa.eu/WebComponentsDEV/DEEP/datalets-list",

                      dataType: "json",

                      success: comp.handleResponseDatalets

  

                  });*/

                  $(this.$.goto).click(this.goto());

              }

          });

      </script>

  </dom-module>