Blame view

datalets/base-ajax-json-jsonpath-datalet/static/js/AjaxJsonJsonPathBehavior.js 6.59 KB
a1f0799c   isisadmin   datalet global re...
1
  /*

5e6ba8af   isisadmin   datalet doc update
2
3
  @license

      The MIT License (MIT)

a1f0799c   isisadmin   datalet global re...
4
  

73cc8795   pina   dataset page and ...
5
      Copyright (c) 2015 Dipartimento di Informatica - Universit di Salerno - Italy

a1f0799c   isisadmin   datalet global re...
6
  

5e6ba8af   isisadmin   datalet doc update
7
8
9
10
11
12
      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:

a1f0799c   isisadmin   datalet global re...
13
  

5e6ba8af   isisadmin   datalet doc update
14
15
16
17
18
19
20
21
22
23
24
      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.

  */

a1f0799c   isisadmin   datalet global re...
25
  

950d181d   Luigi Serra   license updates
26
27
28
29
  /**

   * Developed by :

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

   *

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

  

a1f0799c   isisadmin   datalet global re...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  

  var AjaxJsonJsonPathBehavior = {

  

      properties: {

  

          /**

           * It contains the json data from async xhr call returned from core-ajax core component

           *

           * @attribute json_results

           * @type object

           * @default 'null'.

           */

          json_results: {

              type: Object,

              value: {}

          }

      },

  

5e6ba8af   isisadmin   datalet doc update
50
51
52
53
54
      /**

       * Make an AJAX call to the dataset URL

       *

       * @method requestData

       */

a1f0799c   isisadmin   datalet global re...
55
56
57
58
      requestData: function(){

  

          var comp = this;

  

474d4e8f   Andrea Petta   cache dataset exp...
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
          /*if(typeof(Worker) !== "undefined") {

  

              var myWorker = function() {

  

                  self.onmessage = function(event)

                  {

                      console.log(event.data);

  

                      var xmlhttp;

  

                      if ( XMLHttpRequest) {

                          // code for IE7+, Firefox, Chrome, Opera, Safari

                          xmlhttp = new XMLHttpRequest();

                      } else {

                          // code for IE6, IE5

                          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

                      }

  

                      xmlhttp.onreadystatechange = function() {

                          if (xmlhttp.readyState == XMLHttpRequest.DONE ) {

                              if(xmlhttp.status == 200){

                                  //console.log(xmlhttp.responseText);

                                  self.postMessage(xmlhttp.responseText);

                              }

                              else if(xmlhttp.status == 400) {

                                  console.log('There was an error 400')

                              }

                              else {

                                  console.log('something else other than 200 was returned')

                              }

                          }

                      }

  

                      //xmlhttp.open("GET", event.data, true);

                      xmlhttp.open("GET", "http://172.16.15.77/openwall/api/datasetTree", true);

                      xmlhttp.send();

                  }

              };

  

              var workerData = new Blob(['(' + myWorker.toString() + ')()'], {

                  type: "text/javascript"

              });

  

              myWorker = new Worker(window.URL.createObjectURL(workerData));

              myWorker.postMessage(this._component.dataUrl);

              myWorker.onmessage = function (e) {

                  console.log(e);

                  comp.handleResponse(JSON.parse(e.data));

              };

  

          } else {

              $.ajax({

                  url: this._component.dataUrl,

                  dataType: "json",

                  async : true,

                  success: function(e){

                      comp.handleResponse(e);

                  }

              });

          }*/

  

a1f0799c   isisadmin   datalet global re...
120
121
122
          $.ajax({

              url: this._component.dataUrl,

              dataType: "json",

474d4e8f   Andrea Petta   cache dataset exp...
123
124
              async : true,

              cache : false,

a1f0799c   isisadmin   datalet global re...
125
126
127
128
129
130
131
              success: function(e){

                  comp.handleResponse(e);

              }

          });

      },

  

      /**

5e6ba8af   isisadmin   datalet doc update
132
       * Called when core-ajax component receive the json data from called url.

a1f0799c   isisadmin   datalet global re...
133
134
135
136
137
       *

       * @method handleResponse

       */

      handleResponse: function(e) {

          this.properties.json_results.value = e;

a1f0799c   isisadmin   datalet global re...
138
139
          this.runWorkcycle();

      },

35c4a6d8   Luigi Serra   Datasets provider...
140
141
142
143
144
145
146
147
      /**

       * Check if input field(passed as an array of separated value that mach with field path in received object) is an array of objet.

       * The field is checked on current json object retrieved from the async request.

       *

       * @param field

       */

      isFieldArray : function(field){

         if(field.length == 0) return false;

f0ea8ac7   Luigi Serra   controllets and b...
148
  

35c4a6d8   Luigi Serra   Datasets provider...
149
150
151
152
153
         var obj = this.properties.json_results.value[field[0]];

         for(var i=1; i < field.length; i++){

            obj = (obj.constructor == Array) ? obj[0][field[i]] : obj[field[i]];

         }

  

f0ea8ac7   Luigi Serra   controllets and b...
154
         if(obj == null) return false;

35c4a6d8   Luigi Serra   Datasets provider...
155
156
         return (obj.constructor === Array && obj[0].constructor == Object) ? true : false;

      },

a1f0799c   isisadmin   datalet global re...
157
  

5e6ba8af   isisadmin   datalet doc update
158
159
160
161
162
163
      /**

       * selectData built a JSONPATH query based on the user selected fields then extract data from the JSON response.

       * This method built an objects <name, data> for every user selected field and push it into the data array.

       *

       * @method selectData

       */

73cc8795   pina   dataset page and ...
164
      selectData : function() {

a1f0799c   isisadmin   datalet global re...
165
166
  

          this.data = [];

82ad1c41   Luigi Serra   Text-element-cont...
167
  

4f0683af   Luigi Serra   uploads
168
169
170
          //Deal the fields with "'" char

          //this._component.fields = this._component.fields.replace(/#/g,"'");

  

a1f0799c   isisadmin   datalet global re...
171
172
          this._component.fields = JSON.parse(this._component.fields);

  

73cc8795   pina   dataset page and ...
173
          for (var i = 0; i < this._component.fields.length; i++) {

a1f0799c   isisadmin   datalet global re...
174
175
              var query = "$";

              var query_elements = this._component.fields[i].split(',');

73cc8795   pina   dataset page and ...
176
              for (var j = 0; j < query_elements.length; j++) {

a1f0799c   isisadmin   datalet global re...
177
                  query += "['" + query_elements[j] + "']";

73cc8795   pina   dataset page and ...
178
                  if (this.isFieldArray(query_elements.slice(0, j + 1))) {

35c4a6d8   Luigi Serra   Datasets provider...
179
180
                      query += "[*]";

                  }

a1f0799c   isisadmin   datalet global re...
181
              }

73cc8795   pina   dataset page and ...
182
183
184
185
              this.data.push({

                  name: query_elements[query_elements.length - 1],

                  data: jsonPath(this.properties.json_results.value, query)

              });

a1f0799c   isisadmin   datalet global re...
186
          }

98d9d8a5   Renato De Donato   filters+groupby
187
          this._deleteWaitImage();

73cc8795   pina   dataset page and ...
188
      },

a1f0799c   isisadmin   datalet global re...
189
  

73cc8795   pina   dataset page and ...
190
191
192
      /**

       * Delete a image after loading a datalet

       */

98d9d8a5   Renato De Donato   filters+groupby
193
      _deleteWaitImage : function() {

73cc8795   pina   dataset page and ...
194
195
          $("img[src$='spin.svg']").remove();

      }

a1f0799c   isisadmin   datalet global re...
196
  };