-
Notifications
You must be signed in to change notification settings - Fork 2k
/
recline.js
171 lines (160 loc) · 5.33 KB
/
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
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
// recline preview module
ckan.module('reclinepreview', function (jQuery, _) {
return {
options: {
i18n: {
errorLoadingPreview: "Could not load preview",
errorDataProxy: "DataProxy returned an error",
errorDataStore: "DataStore returned an error",
previewNotAvailableForDataType: "Preview not available for data type: "
}
},
initialize: function() {
this.loadPreviewDialog(preload_resource);
},
// **Public: Loads a data preview**
//
// Fetches the preview data object from the link provided and loads the
// parsed data from the webstore displaying it in the most appropriate
// manner.
//
// link - Preview button.
//
// Returns nothing.
loadPreviewDialog: function (resourceData) {
var self = this;
function showError(msg){
msg = msg || _('error loading preview');
return $('#ckanext-datapreview')
.append('<div></div>')
.addClass('alert alert-error fade in')
.html(msg);
}
// 3 situations
// a) something was posted to the datastore - need to check for this
// b) csv or xls (but not datastore)
// c) can be treated as plain text
resourceData.formatNormalized = this.normalizeFormat(resourceData.format);
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];
}
}
// Set recline CKAN backend API endpoint to right location (so it can locate
// CKAN DataStore)
recline.Backend.Ckan.API_ENDPOINT = $('body').data('site-root') + 'api';
if (resourceData.datastore_active) {
resourceData.backend = 'ckan';
var dataset = new recline.Model.Dataset(resourceData);
var errorMsg = this.options.i18n.errorLoadingPreview + ': ' + this.options.i18n.errorDataStore;
dataset.fetch()
.done(function(dataset){
self.initializeDataExplorer(dataset);
})
.fail(function(error){
if (error.message) errorMsg += ' (' + error.message + ')';
showError(errorMsg);
});
} else if (resourceData.formatNormalized in {'csv': '', 'xls': ''}) {
// set format as this is used by Recline in setting format for DataProxy
resourceData.format = resourceData.formatNormalized;
resourceData.backend = 'dataproxy';
var dataset = new recline.Model.Dataset(resourceData);
var errorMsg = this.options.i18n.errorLoadingPreview + ': ' +this.options.i18n.errorDataProxy;
dataset.fetch()
.done(function(dataset){
dataset.bind('query:fail', function(error) {
$('.data-view-container', self.el).hide();
$('.header', self.el).hide();
});
self.initializeDataExplorer(dataset);
$('.recline-query-editor .text-query').hide();
})
.fail(function(error){
if (error.message) errorMsg += ' (' + error.message + ')';
showError(errorMsg);
});
}
},
initializeDataExplorer: 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 dataExplorer = new recline.View.MultiView({
el: this.el,
model: dataset,
views: views,
config: {
readOnly: true
}
});
// Hide the fields control by default
// (This should be done in recline!)
$('.menu-right a[data-action="fields"]').click();
},
showError: function (error) {
var _html = _.template(
'<div class="alert alert-error"><strong><%= title %></strong><br /><%= message %></div>',
error
);
this.el.html(_html);
},
normalizeFormat: function(format) {
var out = format.toLowerCase();
out = out.split('/');
out = out[out.length-1];
return out;
},
normalizeUrl: function(url) {
if (url.indexOf('https') === 0) {
return 'http' + url.slice(5);
} else {
return url;
}
},
// **Public: Creates a link to the embeddable page.
//
// For a given DataExplorer state, this function constructs and returns the
// url to the embeddable view of the current dataexplorer state.
makeEmbedLink: function(explorerState) {
var state = explorerState.toJSON();
state.state_version = 1;
var queryString = '?';
var items = [];
$.each(state, function(key, value) {
if (typeof(value) === 'object') {
value = JSON.stringify(value);
}
items.push(key + '=' + escape(value));
});
queryString += items.join('&');
return embedPath + queryString;
}
};
});