datalet-selection-controllet.html
18.1 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!--
@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/paper-dialog/paper-dialog.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="../../bower_components/paper-toast/paper-toast.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">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.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-dialog {
position: fixed;
top: 16px;
right: 16px;
width: 400px;
height: 400px;
}*/
html /deep/ paper-dialog { /*::shadow #datalet_dialog_content*/
/*position: fixed;
top: 25%;
left: 25%;
bottom: 25%;
right: 25%;
margin: 0px;*/
width: 90%;
height: 95%;
}
/*Toggle style*/
input.toggle {
max-height: 0;
max-width: 0;
opacity: 0;
}
input.toggle + label {
display: block;
position: relative;
box-shadow: inset 0 0 0px 1px #d5d5d5;
text-indent: -5000px;
height: 30px;
width: 50px;
border-radius: 15px;
}
input.toggle + label:before {
content: "";
position: absolute;
display: block;
height: 30px;
width: 30px;
top: 0;
left: 0;
border-radius: 15px;
background: rgba(19, 191, 17, 0);
-moz-transition: .25s ease-in-out;
-webkit-transition: .25s ease-in-out;
transition: .25s ease-in-out;
}
input.toggle + label:after {
content: "";
position: absolute;
display: block;
height: 30px;
width: 30px;
top: 0;
left: 0px;
border-radius: 15px;
background: white;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .2), 0 2px 4px rgba(0, 0, 0, .2);
-moz-transition: .25s ease-in-out;
-webkit-transition: .25s ease-in-out;
transition: .25s ease-in-out;
}
input.toggle:checked + label:before {
width: 50px;
background: rgba(19, 191, 17, 1);
}
input.toggle:checked + label:after {
left: 20px;
box-shadow: inset 0 0 0 1px rgba(19, 191, 17, 1), 0 2px 4px rgba(0, 0, 0, .2);
}
/deep/ #fields_order_placeholder { list-style-type: none; margin: 0; padding: 0; width: 60%; }
/deep/ #fields_order_placeholder li { margin: 0 5px 5px 5px; padding: 5px; font-size: 1.2em; height: 1.5em; }
/deep/ html>body #fields_order_placeholder li { height: 1.5em; line-height: 1.2em; }
/deep/ .ui-state-highlight { height: 1.5em; line-height: 1.2em; }
</style>
<iron-ajax
auto
id="data_request"
url={{dataUrl}}
handle-as="json"
verbose="true"
on-response="handleResponseData"
debounce-duration="300">
</iron-ajax>
<iron-ajax
id="datales_list_request"
auto
url={{dataletsListUrl}}
handle-as="json"
on-response="handleResponseDatalets"
debounce-duration="300">
</iron-ajax>
<div id="modals">
</div>
<paper-dialog id="datalet_dialog" modal="true" transition="core-transition-center" closeAttribute="close">
<h2>Datalets preview</h2>
<paper-dialog-scrollable id="datalet_dialog_content">
</paper-dialog-scrollable>
<div class="buttons">
<paper-button dialog-dismiss>Close</paper-button>
</div>
</paper-dialog>
<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="{{dataUrl}}"></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">
</div>
</div>
</section>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Selection fileds order</h3>
</div>
<div class="panel-body">
<section>
<div class="horizontal center-justified layout">
<ul id="fields_order_placeholder">
</ul>
</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">
<label for="datalets_selector">Available Datalets list:</label>
<select class="form-control" id="datalets_selector">
</select>
</div>
</div>
</div>
<section>
<paper-button on-click="goto" raised>Go to visualization</paper-button>
</section>
<paper-toast id="message" text=""></paper-toast>
<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 src="../shared_js/jquery-ui-1.11.4.custom/jquery-ui.min.js"></script>
<script src="../../../DEEPCLIENT/js/deepClient.js"></script>
<script>
DataletSelectionControllet = Polymer({
is: 'datalet-selection-controllet',
listeners: {
'tap' : 'clickHandler'
},
hostAttributes: {
dataUrl: "http://demo.ckan.org/api/action/datastore_search?resource_id=8324a4c9-66d6-47bf-a898-94237cc39b9f&limit=50",
deepUrl: "http://service.routetopa.eu/WebComponentsDEV/DEEP/",
dataletsListUrl: "http://service.routetopa.eu/WebComponentsDEV/DEEP/datalets-list"
},
properties: {
/**
* It represent the data url from CKAN api
*
* @attribute dataUrl
* @type string
* @default 'null'
*/
dataUrl: {
type: String,
value: "http://demo.ckan.org/api/action/datastore_search?resource_id=8324a4c9-66d6-47bf-a898-94237cc39b9f&limit=50",
observer: '_dataUrlChanged'
},
/**
* It represent the DEEP url to get information about the datalets
*
* @attribute deepUrl
* @type string
* @default 'null'
*/
deepUrl: {
type: String,
value: "http://demo.ckan.org/api/action/datastore_search?resource_id=8324a4c9-66d6-47bf-a898-94237cc39b9f&limit=50"
},
/**
* 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_order:{
type: String,
value: []
},
fieldsMap: {
type: Map,
value: null
},
datalet_query:{
type: String,
value: ""
}
},
/**
* data-url member changed watcher. It's called when dataUrl changes its value
*
* @method dataUrlChanged
*/
_dataUrlChanged: function(newValue, oldValue){
this.$.data_request.generateRequest();
},
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;
},
createCustomStyledToggle: function(id, label){
return '<div class="wrapper">'
+ '<input id="'+ id +'" type="checkbox" class="toggle">'
+ '<label for="'+ id +'">'+ label +'</label>'
+ label
+ '</div>';
},
/**
* Callback to parse the data requested when dataUrl 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.innerHTML = "";
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(new Array("root"), null, e.detail.response);
},
analyzeObject: function(parent_list, curr_field, object) {
if(curr_field != null){
if(object == null) object = "";
if(object.constructor == Array){//Deal with flat array case
if(object[0].constructor != Object){
this.fieldsMap[parent_list[parent_list.length - 2]].childNodes[1].childNodes[0].innerHTML += this.createCustomStyledToggle(parent_list.toString(), curr_field);
return;
}
}
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{
//CUSTOM STYLED TOGGLE
this.fieldsMap[parent_list[parent_list.length - 2]].childNodes[1].childNodes[0].innerHTML += this.createCustomStyledToggle(parent_list.toString(), curr_field);
}
}
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]);
}
}
},
/**
* Callback to parse the components response object
*
* @method handleResponseComponents
*/
handleResponseDatalets: function(e){
html = "";
for(var i=0;i < e.detail.response.length;i++){
html += '<option value="' + e.detail.response[i] + '"' + ((i==0) ? ' selected>' : '>') + e.detail.response[i] + '</option>';
}
$(this.$.datalets_selector).append(html);
},
/**
* Callback called when "Go to visualization" button is pressed
*
* @method handleResponseComponents
*/
goto: function(){
var fields = Polymer.dom(this.$.fields_order_placeholder).querySelectorAll('li');
this.fields_order = "";
for(var i=0;i<fields.length;i++){
this.fields_order += fields[i].id + ",";
}
this.fields_order = this.fields_order.substring(0, this.fields_order.length - 1);
var cbs = Polymer.dom(this.$.fields_placeholder).querySelectorAll('input[type=checkbox]:checked');
this.datalet_query = "";
if(cbs.length == 0){
this.$.message.text = "No fields selected.";
this.$.message.show();
return;
}
var fields = Array();
for(var i=0;i<cbs.length;i++){
fields.push(cbs[i].id.replace("root,", ""));
}
var datalet_params ={
component : this.$.datalets_selector.options[this.$.datalets_selector.selectedIndex].text,
params :{
'data-url' : this.dataUrl,
'fields-order' : this.fields_order
},
fields : fields,
placeHolder : this.$.datalet_dialog_content
};
ComponentService.deep_url = this.deepUrl;
ComponentService.getComponent(datalet_params);
document.getElementById("datalet_dialog").open();
},
clickHandler : function(e){
if(!e.target.control) {
if (e.target.className == 'toggle') {
(this.$.fields_order_placeholder).innerHTML = "";
var cbs = Polymer.dom(this.$.fields_placeholder).querySelectorAll('input[type=checkbox]:checked');
for(var i=0;i<cbs.length;i++) {
var fields = cbs[i].id.split(',');
var ele = document.createElement('li');
ele.className = "ui-state-default";
ele.id = i;
ele.innerHTML = fields[fields.length -1];
(this.$.fields_order_placeholder).appendChild(ele);
}
}
}
},
ready: function(){
$(this.$.fields_order_placeholder).sortable({
placeholder: "ui-state-highlight"
});
$(this.$.fields_order_placeholder).disableSelection();
}
});
</script>
</dom-module>