This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
forked from GetDKAN/recline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecline.app.js
170 lines (162 loc) · 6.48 KB
/
recline.app.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
/**
* @file
* Provides options for recline visualization.
*/
(function ($) {
Drupal.behaviors.Recline = {
attach: function (context) {
delimiter = Drupal.settings.recline.delimiter;
file = Drupal.settings.recline.file;
grid = Drupal.settings.recline.grid;
graph = Drupal.settings.recline.graph;
map = Drupal.settings.recline.map;
uuid = Drupal.settings.recline.uuid;
dkan = Drupal.settings.recline.dkan;
fileType = Drupal.settings.recline.fileType;
window.dataExplorer = null;
window.explorerDiv = $('.data-explorer');
// This is the very basic state collection.
var state = recline.View.parseQueryString(decodeURIComponent(window.location.hash));
if ('#map' in state) {
state['currentView'] = 'map';
} else if ('#graph' in state) {
state['currentView'] = 'graph';
} else if ('#timeline' in state) {
state['currentView'] = 'timeline';
}
// Checks if dkan_datastore is installed.
if (dkan) {
var drupal_base_path = Drupal.settings.basePath;
var DKAN_API = drupal_base_path + 'api/action/datastore/search.json';
var url = window.location.origin + DKAN_API + '?resource_id=' + uuid;
var DkanDatastore = false;
var DkanApi = $.ajax({
type: 'GET',
url: url,
dataType: 'json',
success: function(data, status) {
if ('success' in data && data.success) {
var dataset = new recline.Model.Dataset({
endpoint: window.location.origin + drupal_base_path + '/api',
url: url,
id: uuid,
backend: 'ckan'
});
dataset.fetch();
return createExplorer(dataset, state);
}
else {
$('.data-explorer').append('<div class="messages status">Error returned from datastore: ' + data + '.</div>');
}
},
error: function(data, status) {
$('.data-explorer').append('<div class="messages status">Unable to connect to the datastore.</div>');
}
});
}
else if (fileType == 'text/csv') {
var options = {delimiter: delimiter};
$.ajax({
url: file,
dataType: "text",
timeout: 500,
success: function(data) {
// Converts line endings in either format to unix format.
data = data.replace(/(\r\n|\n|\r)/gm,"\n");
var dataset = new recline.Model.Dataset({
records: recline.Backend.CSV.parseCSV(data, options)
});
dataset.fetch();
var views = createExplorer(dataset, state);
// The map needs to get redrawn when we are delivering from the ajax
// call.
$.each(views, function(i, view) {
if (view.id == 'map') {
view.view.redraw('refresh');
}
});
},
error: function(x, t, m) {
if (t === "timeout") {
$('.data-explorer').append('<div class="messages status">File was too large or unavailable for preview.</div>');
} else {
$('.data-explorer').append('<div class="messages status">Data preview unavailable.</div>');
}
}
});
}
// Checks if xls.
else if (fileType == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || fileType == 'application/vnd.ms-excel') {
var dataset = new recline.Model.Dataset({
url: file,
backend: 'dataproxy'
});
dataset.fetch();
var views = createExplorer(dataset, state);
}
else {
$('.data-explorer').append('<div class="messages status">File type ' + fileType + ' not supported for preview.</div>');
}
}
}
// make Explorer creation / initialization in a function so we can call it
// again and again
var createExplorer = function(dataset, state) {
// remove existing data explorer view
var reload = false;
if (window.dataExplorer) {
window.dataExplorer.remove();
reload = true;
}
window.dataExplorer = null;
var $el = $('<div />');
$el.appendTo(window.explorerDiv);
var views = [];
if (grid) {
views.push(
{
id: 'grid',
label: 'Grid',
view: new recline.View.SlickGrid({
model: dataset
})
}
);
}
if (graph) {
views.push(
{
id: 'graph',
label: 'Graph',
view: new recline.View.Graph({
model: dataset
})
}
);
}
if (map) {
views.push(
{
id: 'map',
label: 'Map',
view: new recline.View.Map({
model: dataset
})
}
);
}
Drupal.settings.recline.args = {
model: dataset,
el: $el,
state: state,
views: views
}
window.dataExplorer = new recline.View.MultiView(Drupal.settings.recline.args);
$.event.trigger('createDataExplorer');
return views;
}
$(".recline-embed a.embed-link").live('click', function(){
$(this).parents('.recline-embed').find('.embed-code-wrapper').toggle();
return false;
});
})(jQuery);