Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[widgets/datapreview][s]: (refs #32) fix to correctly parse data from…

… webstore - preview is working again!.

* index.html: tested on jsonpdataproxy as well
* data-preview.js: new method convertData to convert data coming in. Was set up to parse data coming from jsonpdataproxy not from webstore but now support both.
  * Also a fix so we use right callback parameter as jsonpdataproxy is different from webstore
  • Loading branch information...
commit a0c281453896ac3395a351936a542363f110668f 1 parent 83537f8
Rufus Pollock authored
3  widgets/datapreview/index.html
@@ -33,7 +33,8 @@
33 33
       dp.initialize(dataproxyUrl, 'ckanext-datapreview-dialog');
34 34
 
35 35
       // Load a datasource without seting up the download links.
36  
-      var dataSource = 'http://webstore.thedatahub.org/rufuspollock/gold_prices/data.jsonp?_limit=30';
  36
+      var dataSource = 'http://webstore.thedatahub.org/rufuspollock/gold_prices/data.jsonp?_limit=500';
  37
+      // var dataSource = 'http://jsonpdataproxy.appspot.com/?url=http://old.openeconomics.net/store/thatcher_wages_middle_ages/data&type=csv'
37 38
       var resource = {
38 39
         'url': dataSource,
39 40
         'hash': 'b21ae9c691445b73773156380336e2fab821cb64',
68  widgets/datapreview/lib/data-preview.js
@@ -149,6 +149,12 @@
149 149
       });
150 150
     }, dp.timeout);
151 151
 
  152
+    // have to set jsonp because webstore requires _callback but that breaks jsonpdataproxy
  153
+    var jsonp = '_callback';
  154
+    if (preview.url.indexOf('jsonpdataproxy') != -1) {
  155
+      jsonp = 'callback';
  156
+    }
  157
+
152 158
     // We need to provide the `cache: true` parameter to prevent jQuery appending
153 159
     // a cache busting `={timestamp}` parameter to the query as the webstore
154 160
     // currently cannot handle custom parameters.
@@ -156,7 +162,7 @@
156 162
       url: preview.url,
157 163
       cache: true,
158 164
       dataType: 'jsonp',
159  
-      jsonp: '_callback',
  165
+      jsonp: jsonp,
160 166
       success: function(data) {
161 167
         clearTimeout(timer);
162 168
         callback(preview, data);
@@ -378,21 +384,57 @@
378 384
     if(data.error) {
379 385
       return dp.showError(data.error);
380 386
     }
  387
+    var tabular = dp.convertData(data);
381 388
 
382  
-    var columns = $.map(data.fields || [], function (column, i) {
383  
-      return {id: 'header-' + i, name: column, field: 'column-' + i, sortable: true};
384  
-    });
  389
+    dp.loadDataPreview(preview, tabular.columns, tabular.data);
  390
+  };
385 391
 
386  
-    var data = $.map(data.data || [], function (row, id) {
387  
-      var cells = {id: id};
388  
-      for (var i = 0, c = row.length; i < c; i++) {
389  
-        var isNumeric = (/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/).test(row[i]);
390  
-        cells['column-' + i] = isNumeric ? parseFloat(row[i]) : row[i];
391  
-      }
392  
-      return cells;
393  
-    });
  392
+  // **Public: parse data from webstore or other source into form for data
  393
+  // preview UI**
  394
+  //
  395
+  // :param data: An object of parsed CSV data returned by the webstore.
  396
+  //
  397
+  // :return: parsed data.
  398
+  //
  399
+  dp.convertData = function(data) {
  400
+    var tabular = {
  401
+      columns: [],
  402
+      data: []
  403
+    };
  404
+    isNumericRegex = (/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/);
394 405
 
395  
-    dp.loadDataPreview(preview, columns, data);
  406
+    // two types of data: that from webstore and that from jsonpdataproxy
  407
+    // if fields then from dataproxy
  408
+    if (data.fields) {
  409
+      tabular.columns = $.map(data.fields || [], function (column, i) {
  410
+        return {id: 'header-' + i, name: column, field: 'column-' + i, sortable: true};
  411
+      });
  412
+
  413
+      tabular.data = $.map(data.data || [], function (row, id) {
  414
+        var cells = {id: id};
  415
+        for (var i = 0, c = row.length; i < c; i++) {
  416
+          var isNumeric = isNumericRegex.test(row[i]);
  417
+          cells['column-' + i] = isNumeric ? parseFloat(row[i]) : row[i];
  418
+        }
  419
+        return cells;
  420
+      });
  421
+    } else {
  422
+      if (data.length) {
  423
+        tabular.columns = $.map(data[0], function(val, key) {
  424
+          return {id: 'header-' + key, name: key, field: 'column-' + key, sortable: true};
  425
+        });
  426
+        tabular.data = $.map(data, function(row, id) {
  427
+          var cells = {id: id};
  428
+          for(i in tabular.columns) {
  429
+            var val = row[tabular.columns[i].name];
  430
+            var isNumeric = isNumericRegex.test(val);
  431
+            cells['column-' + tabular.columns[i].name] = isNumeric ? parseFloat(val) : val;
  432
+          }
  433
+          return cells;
  434
+        });
  435
+      }
  436
+    }
  437
+    return tabular;
396 438
   };
397 439
 
398 440
   // Public: Displays a String of data in a fullscreen dialog.

0 notes on commit a0c2814

Please sign in to comment.
Something went wrong with that request. Please try again.