-
Notifications
You must be signed in to change notification settings - Fork 6
/
catalog.js
111 lines (97 loc) · 2.56 KB
/
catalog.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
var Catalog = {
Models: {},
Views: {}
};
(function(my, $) {
my.Views.Application = Backbone.View.extend({
initialize: function () {
var self = this;
this.el = $(this.el);
_.bindAll(this);
},
// Helpers
// -------
// Views
// -------
});
my.Views.DataFile = Backbone.View.extend({
initialize: function() {
this.$el = $(this.el);
_.bindAll(this);
},
render: function(resourceIndex) {
var $viewer = this.$el;
$viewer.html('Loading View <img src="http://assets.okfn.org/images/icons/ajaxload-circle.gif" />');
var table = my.dataPackageResourceToDataset(this.model.toJSON(), resourceIndex);
table.fetch().done(function() {
var gridView = {
id: 'grid',
label: 'Table',
type: 'SlickGrid',
state: {
fitColumns: true
}
};
DataViews.push(gridView);
var viewsForRecline = _.map(DataViews, function(viewInfo) {
var out = _.clone(viewInfo);
out.view = new recline.View[viewInfo.type]({
model: table,
state: viewInfo.state
});
if (!out.label) {
out.label = out.id;
}
return out;
});
var explorer = new recline.View.MultiView({
model: table,
views: viewsForRecline,
sidebarViews: []
});
$viewer.empty().append(explorer.el);
table.query({size: table.recordCount});
});
return this;
}
});
my.Views.Search = Backbone.View.extend({
events: {
'submit .dataset-search': 'onSearchSubmit'
},
initialize: function() {
this.$el = $(this.el);
},
onSearchSubmit: function(e) {
var self = this;
e.preventDefault();
var q = $(e.target).find('input').val();
this.model.search(q, function(error, result) {
self.results.reset(result);
});
}
});
my.Models.DataFile = Backbone.Model.extend({
});
// convert a Data Package structure to a recline.Model.Dataset
my.dataPackageResourceToDataset = function(dataPackage, resourceIndex) {
if (!resourceIndex) {
resourceIndex = 0;
}
var reclineInfo = dataPackage.resources[resourceIndex];
reclineInfo.backend = 'csv';
// Recline expects fields at top level
reclineInfo.fields = _.map(reclineInfo.schema.fields, function(field) {
if (field.name && !field.id) {
field.id = field.name;
}
return field;
});
if (reclineInfo.localurl) {
reclineInfo.remoteurl = reclineInfo.url;
reclineInfo.url = reclineInfo.localurl;
}
var dataset = new recline.Model.Dataset(reclineInfo);
return dataset;
}
}(Catalog, jQuery));