| 1441 |
ariadna |
1 |
/* global H5PUtils */
|
|
|
2 |
var H5PDataView = (function ($) {
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Initialize a new H5P data view.
|
|
|
6 |
*
|
|
|
7 |
* @class
|
|
|
8 |
* @param {Object} container
|
|
|
9 |
* Element to clear out and append to.
|
|
|
10 |
* @param {String} source
|
|
|
11 |
* URL to get data from. Data format: {num: 123, rows:[[1,2,3],[2,4,6]]}
|
|
|
12 |
* @param {Array} headers
|
|
|
13 |
* List with column headers. Can be strings or objects with options like
|
|
|
14 |
* "text" and "sortable". E.g.
|
|
|
15 |
* [{text: 'Col 1', sortable: true}, 'Col 2', 'Col 3']
|
|
|
16 |
* @param {Object} l10n
|
|
|
17 |
* Localization / translations. e.g.
|
|
|
18 |
* {
|
|
|
19 |
* loading: 'Loading data.',
|
|
|
20 |
* ajaxFailed: 'Failed to load data.',
|
|
|
21 |
* noData: "There's no data available that matches your criteria.",
|
|
|
22 |
* currentPage: 'Page $current of $total',
|
|
|
23 |
* nextPage: 'Next page',
|
|
|
24 |
* previousPage: 'Previous page',
|
|
|
25 |
* search: 'Search'
|
|
|
26 |
* }
|
|
|
27 |
* @param {Object} classes
|
|
|
28 |
* Custom html classes to use on elements.
|
|
|
29 |
* e.g. {tableClass: 'fixed'}.
|
|
|
30 |
* @param {Array} filters
|
|
|
31 |
* Make it possible to filter/search in the given column.
|
|
|
32 |
* e.g. [null, true, null, null] will make it possible to do a text
|
|
|
33 |
* search in column 2.
|
|
|
34 |
* @param {Function} loaded
|
|
|
35 |
* Callback for when data has been loaded.
|
|
|
36 |
* @param {Object} order
|
|
|
37 |
*/
|
|
|
38 |
function H5PDataView(container, source, headers, l10n, classes, filters, loaded, order) {
|
|
|
39 |
var self = this;
|
|
|
40 |
|
|
|
41 |
self.$container = $(container).addClass('h5p-data-view').html('');
|
|
|
42 |
|
|
|
43 |
self.source = source;
|
|
|
44 |
self.headers = headers;
|
|
|
45 |
self.l10n = l10n;
|
|
|
46 |
self.classes = (classes === undefined ? {} : classes);
|
|
|
47 |
self.filters = (filters === undefined ? [] : filters);
|
|
|
48 |
self.loaded = loaded;
|
|
|
49 |
self.order = order;
|
|
|
50 |
|
|
|
51 |
self.limit = 20;
|
|
|
52 |
self.offset = 0;
|
|
|
53 |
self.filterOn = [];
|
|
|
54 |
self.facets = {};
|
|
|
55 |
|
|
|
56 |
// Index of column with author name; could be made more general by passing database column names and checking for position
|
|
|
57 |
self.columnIdAuthor = 2;
|
|
|
58 |
|
|
|
59 |
// Future option: Create more general solution for filter presets
|
|
|
60 |
if (H5PIntegration.user && parseInt(H5PIntegration.user.canToggleViewOthersH5PContents) === 1) {
|
|
|
61 |
self.updateTable([]);
|
|
|
62 |
self.filterByFacet(self.columnIdAuthor, H5PIntegration.user.id, H5PIntegration.user.name || '');
|
|
|
63 |
}
|
|
|
64 |
else {
|
|
|
65 |
self.loadData();
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Load data from source URL.
|
|
|
71 |
*/
|
|
|
72 |
H5PDataView.prototype.loadData = function () {
|
|
|
73 |
var self = this;
|
|
|
74 |
|
|
|
75 |
// Throbb
|
|
|
76 |
self.setMessage(H5PUtils.throbber(self.l10n.loading));
|
|
|
77 |
|
|
|
78 |
// Create URL
|
|
|
79 |
var url = self.source;
|
|
|
80 |
url += (url.indexOf('?') === -1 ? '?' : '&') + 'offset=' + self.offset + '&limit=' + self.limit;
|
|
|
81 |
|
|
|
82 |
// Add sorting
|
|
|
83 |
if (self.order !== undefined) {
|
|
|
84 |
url += '&sortBy=' + self.order.by + '&sortDir=' + self.order.dir;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Add filters
|
|
|
88 |
var filtering;
|
|
|
89 |
for (var i = 0; i < self.filterOn.length; i++) {
|
|
|
90 |
if (self.filterOn[i] === undefined) {
|
|
|
91 |
continue;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
filtering = true;
|
|
|
95 |
url += '&filters[' + i + ']=' + encodeURIComponent(self.filterOn[i]);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// Add facets
|
|
|
99 |
for (var col in self.facets) {
|
|
|
100 |
if (!self.facets.hasOwnProperty(col)) {
|
|
|
101 |
continue;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
url += '&facets[' + col + ']=' + self.facets[col].id;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// Fire ajax request
|
|
|
108 |
$.ajax({
|
|
|
109 |
dataType: 'json',
|
|
|
110 |
cache: true,
|
|
|
111 |
url: url
|
|
|
112 |
}).fail(function () {
|
|
|
113 |
// Error handling
|
|
|
114 |
self.setMessage($('<p/>', {text: self.l10n.ajaxFailed}));
|
|
|
115 |
}).done(function (data) {
|
|
|
116 |
if (!data.rows.length) {
|
|
|
117 |
self.setMessage($('<p/>', {text: filtering ? self.l10n.noData : self.l10n.empty}));
|
|
|
118 |
}
|
|
|
119 |
else {
|
|
|
120 |
// Update table data
|
|
|
121 |
self.updateTable(data.rows);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// Update pagination widget
|
|
|
125 |
self.updatePagination(data.num);
|
|
|
126 |
|
|
|
127 |
if (self.loaded !== undefined) {
|
|
|
128 |
self.loaded();
|
|
|
129 |
}
|
|
|
130 |
});
|
|
|
131 |
};
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Display the given message to the user.
|
|
|
135 |
*
|
|
|
136 |
* @param {jQuery} $message wrapper with message
|
|
|
137 |
*/
|
|
|
138 |
H5PDataView.prototype.setMessage = function ($message) {
|
|
|
139 |
var self = this;
|
|
|
140 |
|
|
|
141 |
if (self.table === undefined) {
|
|
|
142 |
self.$container.html('').append($message);
|
|
|
143 |
}
|
|
|
144 |
else {
|
|
|
145 |
self.table.setBody($message);
|
|
|
146 |
}
|
|
|
147 |
};
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Update table data.
|
|
|
151 |
*
|
|
|
152 |
* @param {Array} rows
|
|
|
153 |
*/
|
|
|
154 |
H5PDataView.prototype.updateTable = function (rows) {
|
|
|
155 |
var self = this;
|
|
|
156 |
|
|
|
157 |
if (self.table === undefined) {
|
|
|
158 |
// Clear out container
|
|
|
159 |
self.$container.html('');
|
|
|
160 |
|
|
|
161 |
// Add filters
|
|
|
162 |
self.addFilters();
|
|
|
163 |
|
|
|
164 |
// Add toggler for others' content
|
|
|
165 |
if (H5PIntegration.user && parseInt(H5PIntegration.user.canToggleViewOthersH5PContents) > 0) {
|
|
|
166 |
// canToggleViewOthersH5PContents = 1 is setting for only showing current user's contents
|
|
|
167 |
self.addOthersContentToggler(parseInt(H5PIntegration.user.canToggleViewOthersH5PContents) === 1);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
// Add facets
|
|
|
171 |
self.$facets = $('<div/>', {
|
|
|
172 |
'class': 'h5p-facet-wrapper',
|
|
|
173 |
appendTo: self.$container
|
|
|
174 |
});
|
|
|
175 |
|
|
|
176 |
// Create new table
|
|
|
177 |
self.table = new H5PUtils.Table(self.classes, self.headers);
|
|
|
178 |
self.table.setHeaders(self.headers, function (order) {
|
|
|
179 |
// Sorting column or direction has changed.
|
|
|
180 |
self.order = order;
|
|
|
181 |
self.loadData();
|
|
|
182 |
}, self.order);
|
|
|
183 |
self.table.appendTo(self.$container);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
// Process cell data before updating table
|
|
|
187 |
for (var i = 0; i < self.headers.length; i++) {
|
|
|
188 |
if (self.headers[i].facet === true) {
|
|
|
189 |
// Process rows for col, expect object or array
|
|
|
190 |
for (var j = 0; j < rows.length; j++) {
|
|
|
191 |
rows[j][i] = self.createFacets(rows[j][i], i);
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
// Add/update rows
|
|
|
197 |
var $tbody = self.table.setRows(rows);
|
|
|
198 |
|
|
|
199 |
// Add event handlers for facets
|
|
|
200 |
$('.h5p-facet', $tbody).click(function () {
|
|
|
201 |
var $facet = $(this);
|
|
|
202 |
self.filterByFacet($facet.data('col'), $facet.data('id'), $facet.text());
|
|
|
203 |
}).keypress(function (event) {
|
|
|
204 |
if (event.which === 32) {
|
|
|
205 |
var $facet = $(this);
|
|
|
206 |
self.filterByFacet($facet.data('col'), $facet.data('id'), $facet.text());
|
|
|
207 |
}
|
|
|
208 |
});
|
|
|
209 |
};
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* Create button for adding facet to filter.
|
|
|
213 |
*
|
|
|
214 |
* @param (object|Array) input
|
|
|
215 |
* @param number col ID of column
|
|
|
216 |
*/
|
|
|
217 |
H5PDataView.prototype.createFacets = function (input, col) {
|
|
|
218 |
var facets = '';
|
|
|
219 |
|
|
|
220 |
if (input instanceof Array) {
|
|
|
221 |
// Facet can be filtered on multiple values at the same time
|
|
|
222 |
for (var i = 0; i < input.length; i++) {
|
|
|
223 |
if (facets !== '') {
|
|
|
224 |
facets += ', ';
|
|
|
225 |
}
|
|
|
226 |
facets += '<span class="h5p-facet" role="button" tabindex="0" data-id="' + input[i].id + '" data-col="' + col + '">' + input[i].title + '</span>';
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
else {
|
|
|
230 |
// Single value facet filtering
|
|
|
231 |
facets += '<span class="h5p-facet" role="button" tabindex="0" data-id="' + input.id + '" data-col="' + col + '">' + input.title + '</span>';
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
return facets === '' ? '—' : facets;
|
|
|
235 |
};
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Adds a filter based on the given facet.
|
|
|
239 |
*
|
|
|
240 |
* @param number col ID of column we're filtering
|
|
|
241 |
* @param number id ID to filter on
|
|
|
242 |
* @param string text Human readable label for the filter
|
|
|
243 |
*/
|
|
|
244 |
H5PDataView.prototype.filterByFacet = function (col, id, text) {
|
|
|
245 |
var self = this;
|
|
|
246 |
|
|
|
247 |
if (self.facets[col] !== undefined) {
|
|
|
248 |
if (self.facets[col].id === id) {
|
|
|
249 |
return; // Don't use the same filter again
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
// Remove current filter for this col
|
|
|
253 |
self.facets[col].$tag.remove();
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
// Add to UI
|
|
|
257 |
self.facets[col] = {
|
|
|
258 |
id: id,
|
|
|
259 |
'$tag': $('<span/>', {
|
|
|
260 |
'class': 'h5p-facet-tag',
|
|
|
261 |
text: text,
|
|
|
262 |
appendTo: self.$facets,
|
|
|
263 |
})
|
|
|
264 |
};
|
|
|
265 |
/**
|
|
|
266 |
* Callback for removing filter.
|
|
|
267 |
*
|
|
|
268 |
* @private
|
|
|
269 |
*/
|
|
|
270 |
var remove = function () {
|
|
|
271 |
// Uncheck toggler for others' H5P contents
|
|
|
272 |
if ( self.$othersContentToggler && self.facets.hasOwnProperty( self.columnIdAuthor ) ) {
|
|
|
273 |
self.$othersContentToggler.prop('checked', false );
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
self.facets[col].$tag.remove();
|
|
|
277 |
delete self.facets[col];
|
|
|
278 |
self.loadData();
|
|
|
279 |
};
|
|
|
280 |
|
|
|
281 |
// Remove button
|
|
|
282 |
$('<span/>', {
|
|
|
283 |
role: 'button',
|
|
|
284 |
tabindex: 0,
|
|
|
285 |
appendTo: self.facets[col].$tag,
|
|
|
286 |
text: self.l10n.remove,
|
|
|
287 |
title: self.l10n.remove,
|
|
|
288 |
on: {
|
|
|
289 |
click: remove,
|
|
|
290 |
keypress: function (event) {
|
|
|
291 |
if (event.which === 32) {
|
|
|
292 |
remove();
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
});
|
|
|
297 |
|
|
|
298 |
// Load data with new filter
|
|
|
299 |
self.loadData();
|
|
|
300 |
};
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Update pagination widget.
|
|
|
304 |
*
|
|
|
305 |
* @param {Number} num size of data collection
|
|
|
306 |
*/
|
|
|
307 |
H5PDataView.prototype.updatePagination = function (num) {
|
|
|
308 |
var self = this;
|
|
|
309 |
|
|
|
310 |
if (self.pagination === undefined) {
|
|
|
311 |
if (self.table === undefined) {
|
|
|
312 |
// No table, no pagination
|
|
|
313 |
return;
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
// Create new widget
|
|
|
317 |
var $pagerContainer = $('<div/>', {'class': 'h5p-pagination'});
|
|
|
318 |
self.pagination = new H5PUtils.Pagination(num, self.limit, function (offset) {
|
|
|
319 |
// Handle page changes in pagination widget
|
|
|
320 |
self.offset = offset;
|
|
|
321 |
self.loadData();
|
|
|
322 |
}, self.l10n);
|
|
|
323 |
|
|
|
324 |
self.pagination.appendTo($pagerContainer);
|
|
|
325 |
self.table.setFoot($pagerContainer);
|
|
|
326 |
}
|
|
|
327 |
else {
|
|
|
328 |
// Update existing widget
|
|
|
329 |
self.pagination.update(num, self.limit);
|
|
|
330 |
}
|
|
|
331 |
};
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Add filters.
|
|
|
335 |
*/
|
|
|
336 |
H5PDataView.prototype.addFilters = function () {
|
|
|
337 |
var self = this;
|
|
|
338 |
|
|
|
339 |
for (var i = 0; i < self.filters.length; i++) {
|
|
|
340 |
if (self.filters[i] === true) {
|
|
|
341 |
// Add text input filter for col i
|
|
|
342 |
self.addTextFilter(i);
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
};
|
|
|
346 |
|
|
|
347 |
/**
|
|
|
348 |
* Add text filter for given col num.
|
|
|
349 |
*
|
|
|
350 |
* @param {Number} col
|
|
|
351 |
*/
|
|
|
352 |
H5PDataView.prototype.addTextFilter = function (col) {
|
|
|
353 |
var self = this;
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Find input value and filter on it.
|
|
|
357 |
* @private
|
|
|
358 |
*/
|
|
|
359 |
var search = function () {
|
|
|
360 |
var filterOn = $input.val().replace(/^\s+|\s+$/g, '');
|
|
|
361 |
if (filterOn === '') {
|
|
|
362 |
filterOn = undefined;
|
|
|
363 |
}
|
|
|
364 |
if (filterOn !== self.filterOn[col]) {
|
|
|
365 |
self.filterOn[col] = filterOn;
|
|
|
366 |
self.loadData();
|
|
|
367 |
}
|
|
|
368 |
};
|
|
|
369 |
|
|
|
370 |
// Add text field for filtering
|
|
|
371 |
var typing;
|
|
|
372 |
var $input = $('<input/>', {
|
|
|
373 |
type: 'text',
|
|
|
374 |
placeholder: self.l10n.search,
|
|
|
375 |
on: {
|
|
|
376 |
'blur': function () {
|
|
|
377 |
clearTimeout(typing);
|
|
|
378 |
search();
|
|
|
379 |
},
|
|
|
380 |
'keyup': function (event) {
|
|
|
381 |
if (event.keyCode === 13) {
|
|
|
382 |
clearTimeout(typing);
|
|
|
383 |
search();
|
|
|
384 |
return false;
|
|
|
385 |
}
|
|
|
386 |
else {
|
|
|
387 |
clearTimeout(typing);
|
|
|
388 |
typing = setTimeout(function () {
|
|
|
389 |
search();
|
|
|
390 |
}, 500);
|
|
|
391 |
}
|
|
|
392 |
}
|
|
|
393 |
}
|
|
|
394 |
}).appendTo(self.$container);
|
|
|
395 |
};
|
|
|
396 |
|
|
|
397 |
/**
|
|
|
398 |
* Add toggle for others' H5P content.
|
|
|
399 |
* @param {boolean} [checked=false] Initial check setting.
|
|
|
400 |
*/
|
|
|
401 |
H5PDataView.prototype.addOthersContentToggler = function (checked) {
|
|
|
402 |
var self = this;
|
|
|
403 |
|
|
|
404 |
checked = (typeof checked === 'undefined') ? false : checked;
|
|
|
405 |
|
|
|
406 |
// Checkbox
|
|
|
407 |
this.$othersContentToggler = $('<input/>', {
|
|
|
408 |
type: 'checkbox',
|
|
|
409 |
'class': 'h5p-others-contents-toggler',
|
|
|
410 |
'id': 'h5p-others-contents-toggler',
|
|
|
411 |
'checked': checked,
|
|
|
412 |
'click': function () {
|
|
|
413 |
if ( this.checked ) {
|
|
|
414 |
// Add filter on current user
|
|
|
415 |
self.filterByFacet( self.columnIdAuthor, H5PIntegration.user.id, H5PIntegration.user.name );
|
|
|
416 |
}
|
|
|
417 |
else {
|
|
|
418 |
// Remove facet indicator and reload full data view
|
|
|
419 |
if ( self.facets.hasOwnProperty( self.columnIdAuthor ) && self.facets[self.columnIdAuthor].$tag ) {
|
|
|
420 |
self.facets[self.columnIdAuthor].$tag.remove();
|
|
|
421 |
}
|
|
|
422 |
delete self.facets[self.columnIdAuthor];
|
|
|
423 |
self.loadData();
|
|
|
424 |
}
|
|
|
425 |
}
|
|
|
426 |
});
|
|
|
427 |
|
|
|
428 |
// Label
|
|
|
429 |
var $label = $('<label>', {
|
|
|
430 |
'class': 'h5p-others-contents-toggler-label',
|
|
|
431 |
'text': this.l10n.showOwnContentOnly,
|
|
|
432 |
'for': 'h5p-others-contents-toggler'
|
|
|
433 |
}).prepend(this.$othersContentToggler);
|
|
|
434 |
|
|
|
435 |
$('<div>', {
|
|
|
436 |
'class': 'h5p-others-contents-toggler-wrapper'
|
|
|
437 |
}).append($label)
|
|
|
438 |
.appendTo(this.$container);
|
|
|
439 |
};
|
|
|
440 |
|
|
|
441 |
return H5PDataView;
|
|
|
442 |
})(H5P.jQuery);
|