-
Notifications
You must be signed in to change notification settings - Fork 2k
/
recline_view.js
200 lines (176 loc) · 5.92 KB
/
recline_view.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
this.ckan.module('recline_view', 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: "",
controlsClassName: "controls"
},
initialize: function () {
jQuery.proxyAll(this, /_on/);
this.options.resource = JSON.parse(this.options.resource);
this.options.resourceView = JSON.parse(this.options.resourceView);
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() {
var resourceData = this.options.resource,
resourceView = this.options.resourceView;
this.loadView(resourceData, resourceView);
},
loadView: function (resourceData, reclineView) {
var self = this;
function showError(msg){
msg = msg || _('error loading view');
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);
var query = new recline.Model.Query();
query.set({ size: reclineView.limit || 100 });
query.set({ from: reclineView.offset || 0 });
if (window.parent.ckan.views && window.parent.ckan.views.viewhelpers) {
$.each(window.parent.ckan.views.viewhelpers.filters.get(), function (field,values) {
query.addFilter({type: 'term', field: field, term: values});
});
}
dataset.queryState.set(query.toJSON(), {silent: true});
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,
state,
controls = [];
if(reclineView.view_type === "recline_graph_view") {
state = {
"graphType": reclineView.graph_type,
"group": reclineView.group,
"series": [reclineView.series]
};
view = new recline.View.Graph({model: dataset, state: state});
} else if(reclineView.view_type === "recline_map_view") {
state = {
geomField: null,
latField: null,
lonField: null,
autoZoom: Boolean(reclineView.auto_zoom),
cluster: Boolean(reclineView.cluster_markers)
};
if(reclineView.map_field_type === "geojson") {
state.geomField = reclineView.geojson_field;
} else {
state.latField = reclineView.latitude_field;
state.lonField = reclineView.longitude_field;
}
view = new recline.View.Map({model: dataset, state: state});
} else if(reclineView.view_type === "recline_view") {
view = this._newDataExplorer(dataset);
} else {
// default to Grid
view = new recline.View.SlickGrid({model: dataset});
controls = [
new recline.View.Pager({model: view.model}),
new recline.View.RecordCount({model: dataset}),
new recline.View.QueryEditor({model: view.model.queryState})
];
}
// recline_view automatically adds itself to the DOM, so we don't
// need to bother with it.
if(reclineView.view_type !== 'recline_view') {
var newElements = $('<div />');
this._renderControls(newElements, controls, this.options.controlsClassName);
newElements.append(view.el);
$(this.el).html(newElements);
view.visible = true;
view.render();
}
if(reclineView.view_type === "recline_graph_view") {
view.redraw();
}
},
_newDataExplorer: function (dataset) {
var views = [
{
id: 'grid',
label: 'Grid',
view: new recline.View.SlickGrid({
model: dataset
})
},
{
id: 'graph',
label: 'Graph',
view: new recline.View.Graph({
model: dataset
})
},
{
id: 'map',
label: 'Map',
view: new recline.View.Map({
model: dataset
})
}
];
var sidebarViews = [
{
id: 'valueFilter',
label: 'Filters',
view: new recline.View.ValueFilter({
model: dataset
})
}
];
var dataExplorer = new recline.View.MultiView({
el: this.el,
model: dataset,
views: views,
sidebarViews: sidebarViews,
config: {
readOnly: true
}
});
return dataExplorer;
},
normalizeUrl: function (url) {
if (url.indexOf('https') === 0) {
return 'http' + url.slice(5);
} else {
return url;
}
},
_renderControls: function (el, controls, className) {
var controlsEl = $("<div class=\"clearfix " + className + "\" />");
for (var i = 0; i < controls.length; i++) {
controlsEl.append(controls[i].el);
}
$(el).append(controlsEl);
}
};
});