-
Notifications
You must be signed in to change notification settings - Fork 2k
/
preview_recline.js
105 lines (91 loc) · 2.99 KB
/
preview_recline.js
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
// recline preview module
this.ckan.module('reclinepreview', function (jQuery, _) {
return {
options: {
i18n: {
errorLoadingPreview: "Could not load view",
errorDataProxy: "DataProxy returned an error",
errorDataStore: "DataStore returned an error",
previewNotAvailableForDataType: "View not available for data type: "
},
site_url: ""
},
initialize: function () {
jQuery.proxyAll(this, /_on/);
this.el.ready(this._onReady);
// hack to make leaflet use a particular location to look for images
L.Icon.Default.imagePath = this.options.site_url + 'vendor/leaflet/0.4.4/images';
},
_onReady: function() {
this.loadView(preload_resource, preload_resource_view);
},
loadView: function (resourceData, reclineView) {
var self = this;
function showError(msg){
msg = msg || _('error loading preview');
window.parent.ckan.pubsub.publish('data-viewer-error', msg);
}
resourceData.url = this.normalizeUrl(resourceData.url);
if (resourceData.formatNormalized === '') {
var tmp = resourceData.url.split('/');
tmp = tmp[tmp.length - 1];
tmp = tmp.split('?'); // query strings
tmp = tmp[0];
var ext = tmp.split('.');
if (ext.length > 1) {
resourceData.formatNormalized = ext[ext.length-1];
}
}
var errorMsg, dataset;
resourceData.backend = 'ckan';
resourceData.endpoint = jQuery('body').data('site-root') + 'api';
dataset = new recline.Model.Dataset(resourceData);
dataset.query({
"from": reclineView.offset || 0,
"size": reclineView.limit || 100
});
errorMsg = this.options.i18n.errorLoadingPreview + ': ' + this.options.i18n.errorDataStore;
dataset.fetch()
.done(function(dataset){
self.initializeView(dataset, reclineView);
})
.fail(function(error){
if (error.message) errorMsg += ' (' + error.message + ')';
showError(errorMsg);
});
},
initializeView: function (dataset, reclineView) {
var view;
if(reclineView.view_type === "recline_graph") {
view = new recline.View.Graph({
model: dataset,
state: {"graphType": reclineView.graph_type,
"group": reclineView.group,
"series": [reclineView.series]}
});
} else if(reclineView.view_type == "recline_map") {
view = new recline.View.Map({
model: dataset,
el: this.el
});
} else {
view = new recline.View.SlickGrid({
model: dataset
});
}
this.el.replaceWith(view.el);
view.visible = true;
view.render();
if(reclineView.view_type === "recline_graph") {
view.redraw();
}
},
normalizeUrl: function (url) {
if (url.indexOf('https') === 0) {
return 'http' + url.slice(5);
} else {
return url;
}
}
};
});