Blame view

datalets/base-ajax-json-jsonpath-datalet/static/js/AjaxJsonJsonPathBehavior.js 3.13 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
  

5e6ba8af   isisadmin   datalet doc update
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
  

a1f0799c   isisadmin   datalet global re...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  

  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
44
45
46
47
48
      /**

       * Make an AJAX call to the dataset URL

       *

       * @method requestData

       */

a1f0799c   isisadmin   datalet global re...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      requestData: function(){

  

          var comp = this;

  

          $.ajax({

              url: this._component.dataUrl,

              dataType: "json",

              success: function(e){

                  comp.handleResponse(e);

              }

          });

      },

  

      /**

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

a1f0799c   isisadmin   datalet global re...
64
65
66
67
68
       *

       * @method handleResponse

       */

      handleResponse: function(e) {

          this.properties.json_results.value = e;

a1f0799c   isisadmin   datalet global re...
69
70
71
          this.runWorkcycle();

      },

  

5e6ba8af   isisadmin   datalet doc update
72
73
74
75
76
77
      /**

       * 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

       */

5bc002a2   isisadmin   datalet refactoring
78
      selectData : function(){

a1f0799c   isisadmin   datalet global re...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  

          this.data = [];

          this._component.fields = JSON.parse(this._component.fields);

  

          for(var i=0;i < this._component.fields.length; i++){

              var query = "$";

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

              for(var j=0; j < query_elements.length - 1;j++){

                  query += "['" + query_elements[j] + "']";

              }

              query += "[*]" + "['" + query_elements[query_elements.length - 1] + "']";

              this.data.push({name : query_elements[query_elements.length - 1], data : jsonPath(this.properties.json_results.value, query)});

          }

      }

  

  };