1 /* global H5PAdminIntegration*/
2 var H5PUtils = H5PUtils || {};
6 * Generic function for creating a table including the headers
8 * @param {array} headers List of headers
10 H5PUtils.createTable = function (headers) {
11 var $table = $('<table class="h5p-admin-table' + (H5PAdminIntegration.extraTableClasses !== undefined ? ' ' + H5PAdminIntegration.extraTableClasses : '') + '"></table>');
14 var $thead = $('<thead></thead>');
15 var $tr = $('<tr></tr>');
17 $.each(headers, function (index, value) {
18 if (!(value instanceof Object)) {
24 $('<th/>', value).appendTo($tr);
27 $table.append($thead.append($tr));
34 * Generic function for creating a table row
36 * @param {array} rows Value list. Object name is used as class name in <TD>
38 H5PUtils.createTableRow = function (rows) {
39 var $tr = $('<tr></tr>');
41 $.each(rows, function (index, value) {
42 if (!(value instanceof Object)) {
48 $('<td/>', value).appendTo($tr);
55 * Generic function for creating a field containing label and value
57 * @param {string} label The label displayed in front of the value
58 * @param {string} value The value
60 H5PUtils.createLabeledField = function (label, value) {
61 var $field = $('<div class="h5p-labeled-field"></div>');
63 $field.append('<div class="h5p-label">' + label + '</div>');
64 $field.append('<div class="h5p-value">' + value + '</div>');
70 * Replaces placeholder fields in translation strings
72 * @param {string} template The translation template string in the following format: "$name is a $sex"
73 * @param {array} replacors An js object with key and values. Eg: {'$name': 'Frode', '$sex': 'male'}
75 H5PUtils.translateReplace = function (template, replacors) {
76 $.each(replacors, function (key, value) {
77 template = template.replace(new RegExp('\\'+key, 'g'), value);
83 * Get throbber with given text.
85 * @param {String} text
88 H5PUtils.throbber = function (text) {
90 class: 'h5p-throbber',
96 * Makes it possbile to rebuild all content caches from admin UI.
97 * @param {Object} notCached
100 H5PUtils.getRebuildCache = function (notCached) {
101 var $container = $('<div class="h5p-admin-rebuild-cache"><p class="message">' + notCached.message + '</p><p class="progress">' + notCached.progress + '</p></div>');
102 var $button = $('<button>' + notCached.button + '</button>').appendTo($container).click(function () {
103 var $spinner = $('<div/>', {class: 'h5p-spinner'}).replaceAll($button);
104 var parts = ['|', '/', '-', '\\'];
106 var spinning = setInterval(function () {
107 $spinner.text(parts[current]);
109 if (current === parts.length) current = 0;
112 var $counter = $container.find('.progress');
113 var build = function () {
114 $.post(notCached.url, function (left) {
116 clearInterval(spinning);
121 var counter = $counter.text().split(' ');
123 $counter.text(counter.join(' '));
135 * Generic table class with useful helpers.
138 * @param {Object} classes
139 * Custom html classes to use on elements.
140 * e.g. {tableClass: 'fixed'}.
142 H5PUtils.Table = function (classes) {
149 // Create basic table
150 var tableOptions = {};
151 if (classes.table !== undefined) {
152 tableOptions['class'] = classes.table;
154 var $table = $('<table/>', tableOptions);
155 var $thead = $('<thead/>').appendTo($table);
156 var $tfoot = $('<tfoot/>').appendTo($table);
157 var $tbody = $('<tbody/>').appendTo($table);
160 * Add columns to given table row.
163 * @param {jQuery} $tr Table row
164 * @param {(String|Object)} col Column properties
165 * @param {Number} id Used to seperate the columns
167 var addCol = function ($tr, col, id) {
172 if (!(col instanceof Object)) {
176 if (col.text !== undefined) {
177 options.text = col.text;
179 if (col.class !== undefined) {
180 options.class = col.class;
183 if (sortByCol !== undefined && col.sortable === true) {
185 options.role = 'button';
186 options.tabIndex = 0;
188 // This is the first sortable column, use as default sort
189 if (sortCol === undefined) {
194 // This is the sort column
195 if (sortCol === id) {
196 options['class'] = 'h5p-sort';
198 options['class'] += ' h5p-reverse';
202 options.on.click = function () {
205 options.on.keypress = function (event) {
206 if ((event.charCode || event.keyCode) === 32) { // Space
214 var $th = $('<th>', options).appendTo($tr);
215 if (sortCol === id) {
216 $sortCol = $th; // Default sort column
221 * Updates the UI when a column header has been clicked.
222 * Triggers sorting callback.
225 * @param {jQuery} $th Table header
226 * @param {Number} id Used to seperate the columns
228 var sort = function ($th, id) {
229 if (id === sortCol) {
230 // Change sorting direction
233 $th.addClass('h5p-reverse');
237 $th.removeClass('h5p-reverse');
241 // Change sorting column
242 $sortCol.removeClass('h5p-sort').removeClass('h5p-reverse');
243 $sortCol = $th.addClass('h5p-sort');
258 * @param {Array} cols
259 * Table header data. Can be strings or objects with options like
260 * "text" and "sortable". E.g.
261 * [{text: 'Col 1', sortable: true}, 'Col 2', 'Col 3']
262 * @param {Function} sort Callback which is runned when sorting changes
263 * @param {Object} [order]
265 this.setHeaders = function (cols, sort, order) {
266 numCols = cols.length;
275 var $newThead = $('<thead/>');
276 var $tr = $('<tr/>').appendTo($newThead);
277 for (var i = 0; i < cols.length; i++) {
278 addCol($tr, cols[i], i);
282 $thead.replaceWith($newThead);
290 * @param {Array} rows Table rows with cols: [[1,'hello',3],[2,'asd',6]]
292 this.setRows = function (rows) {
293 var $newTbody = $('<tbody/>');
295 for (var i = 0; i < rows.length; i++) {
296 var $tr = $('<tr/>').appendTo($newTbody);
298 for (var j = 0; j < rows[i].length; j++) {
305 $tbody.replaceWith($newTbody);
312 * Set custom table body content. This can be a message or a throbber.
313 * Will cover all table columns.
316 * @param {jQuery} $content Custom content
318 this.setBody = function ($content) {
319 var $newTbody = $('<tbody/>');
320 var $tr = $('<tr/>').appendTo($newTbody);
323 }).append($content).appendTo($tr);
324 $tbody.replaceWith($newTbody);
329 * Set custom table foot content. This can be a pagination widget.
330 * Will cover all table columns.
333 * @param {jQuery} $content Custom content
335 this.setFoot = function ($content) {
336 var $newTfoot = $('<tfoot/>');
337 var $tr = $('<tr/>').appendTo($newTfoot);
340 }).append($content).appendTo($tr);
341 $tfoot.replaceWith($newTfoot);
346 * Appends the table to the given container.
349 * @param {jQuery} $container
351 this.appendTo = function ($container) {
352 $table.appendTo($container);
357 * Generic pagination class. Creates a useful pagination widget.
360 * @param {Number} num Total number of items to pagiate.
361 * @param {Number} limit Number of items to dispaly per page.
362 * @param {Function} goneTo
363 * Callback which is fired when the user wants to go to another page.
364 * @param {Object} l10n
365 * Localization / translations. e.g.
367 * currentPage: 'Page $current of $total',
368 * nextPage: 'Next page',
369 * previousPage: 'Previous page'
372 H5PUtils.Pagination = function (num, limit, goneTo, l10n) {
374 var pages = Math.ceil(num / limit);
379 var $left = $('<button/>', {
382 title: l10n.previousPage
383 }).click(function () {
388 var $text = $('<span/>').click(function () {
389 $input.width($text.width()).show().val(current + 1).focus();
393 // Jump to page input
394 var $input = $('<input/>', {
399 'blur': function () {
402 'keyup': function (event) {
403 if (event.keyCode === 13) {
412 var $right = $('<button/>', {
416 }).click(function () {
421 * Check what page the user has typed in and jump to it.
425 var gotInput = function () {
426 var page = parseInt($input.hide().val());
434 * Update UI elements.
438 var updateUI = function () {
439 var next = current + 1;
441 // Disable or enable buttons
442 $left.attr('disabled', current === 0);
443 $right.attr('disabled', next === pages);
446 $text.html(l10n.currentPage.replace('$current', next).replace('$total', pages));
450 * Try to go to the requested page.
453 * @param {Number} page
455 var goTo = function (page) {
456 if (page === current || page < 0 || page >= pages) {
457 return; // Invalid page number
464 goneTo(page * limit);
468 * Update number of items and limit.
471 * @param {Number} newNum Total number of items to pagiate.
472 * @param {Number} newLimit Number of items to dispaly per page.
474 this.update = function (newNum, newLimit) {
475 if (newNum !== num || newLimit !== limit) {
476 // Update num and limit
479 pages = Math.ceil(num / limit);
480 $input.attr('max', pages);
482 if (current >= pages) {
483 // Content is gone, move to last page.
493 * Append the pagination widget to the given container.
496 * @param {jQuery} $container
498 this.appendTo = function ($container) {
499 $left.add($text).add($input).add($right).appendTo($container);