Blame view

datalets/base-ajax-json-jsonpath-datalet/static/js/AjaxJsonJsonPathBehavior.js 4.29 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
59
60
61
62
63
64
65
66
67
68
      requestData: function(){

  

          var comp = this;

  

          $.ajax({

              url: this._component.dataUrl,

              dataType: "json",

              success: function(e){

                  comp.handleResponse(e);

              }

          });

      },

  

      /**

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

a1f0799c   isisadmin   datalet global re...
70
71
72
73
74
       *

       * @method handleResponse

       */

      handleResponse: function(e) {

          this.properties.json_results.value = e;

a1f0799c   isisadmin   datalet global re...
75
76
          this.runWorkcycle();

      },

35c4a6d8   Luigi Serra   Datasets provider...
77
78
79
80
81
82
83
84
      /**

       * 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...
85
  

35c4a6d8   Luigi Serra   Datasets provider...
86
87
88
89
90
         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...
91
         if(obj == null) return false;

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

      },

a1f0799c   isisadmin   datalet global re...
94
  

5e6ba8af   isisadmin   datalet doc update
95
96
97
98
99
100
      /**

       * 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 ...
101
      selectData : function() {

a1f0799c   isisadmin   datalet global re...
102
103
  

          this.data = [];

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

4f0683af   Luigi Serra   uploads
105
106
107
          //Deal the fields with "'" char

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

  

a1f0799c   isisadmin   datalet global re...
108
109
          this._component.fields = JSON.parse(this._component.fields);

  

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

a1f0799c   isisadmin   datalet global re...
111
112
              var query = "$";

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

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

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

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

35c4a6d8   Luigi Serra   Datasets provider...
116
117
                      query += "[*]";

                  }

a1f0799c   isisadmin   datalet global re...
118
              }

73cc8795   pina   dataset page and ...
119
120
121
122
              this.data.push({

                  name: query_elements[query_elements.length - 1],

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

              });

a1f0799c   isisadmin   datalet global re...
123
          }

73cc8795   pina   dataset page and ...
124
125
          this.deleteWaitImage();

      },

a1f0799c   isisadmin   datalet global re...
126
  

73cc8795   pina   dataset page and ...
127
128
129
130
131
132
      /**

       * Delete a image after loading a datalet

       */

      deleteWaitImage : function() {

          $("img[src$='spin.svg']").remove();

      }

a1f0799c   isisadmin   datalet global re...
133
  };