diff --git a/h5p-development.class.php b/h5p-development.class.php index d599ca1..75e9daf 100644 --- a/h5p-development.class.php +++ b/h5p-development.class.php @@ -30,7 +30,7 @@ class H5PDevelopment { $this->findLibraries($filesPath . '/development'); } } - + /** * Get contents of file. * @@ -41,15 +41,15 @@ class H5PDevelopment { if (file_exists($file) === FALSE) { return NULL; } - + $contents = file_get_contents($file); if ($contents === FALSE) { return NULL; } - + return $contents; } - + /** * Scans development directory and find all libraries. * @@ -57,39 +57,39 @@ class H5PDevelopment { */ private function findLibraries($path) { $this->libraries = array(); - + if (is_dir($path) === FALSE) { - return; + return; } - + $contents = scandir($path); - + for ($i = 0, $s = count($contents); $i < $s; $i++) { if ($contents[$i]{0} === '.') { continue; // Skip hidden stuff. } - + $libraryPath = $path . '/' . $contents[$i]; $libraryJSON = $this->getFileContents($libraryPath . '/library.json'); if ($libraryJSON === NULL) { continue; // No JSON file, skip. } - + $library = json_decode($libraryJSON, TRUE); if ($library === FALSE) { continue; // Invalid JSON. } - + // TODO: Validate props? Not really needed, is it? this is a dev site. - + // Save/update library. $library['libraryId'] = $this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']); $this->h5pF->saveLibraryData($library, $library['libraryId'] === FALSE); - + $library['path'] = $libraryPath; $this->libraries[H5PDevelopment::libraryToString($library['machineName'], $library['majorVersion'], $library['minorVersion'])] = $library; } - + // TODO: Should we remove libraries without files? Not really needed, but must be cleaned up some time, right? // Go trough libraries and insert dependencies. Missing deps. will just be ignored and not available. (I guess?!) @@ -106,17 +106,17 @@ class H5PDevelopment { } // TODO: Deps must be inserted into h5p_nodes_libraries as well... ? But only if they are used?! } - + /** * @return array Libraris in development folder. */ public function getLibraries() { return $this->libraries; } - + /** * Get library - * + * * @param string $name of the library. * @param int $majorVersion of the library. * @param int $minorVersion of the library. @@ -126,10 +126,10 @@ class H5PDevelopment { $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion); return isset($this->libraries[$library]) === TRUE ? $this->libraries[$library] : NULL; } - + /** * Get semantics for the given library. - * + * * @param string $name of the library. * @param int $majorVersion of the library. * @param int $minorVersion of the library. @@ -137,32 +137,32 @@ class H5PDevelopment { */ public function getSemantics($name, $majorVersion, $minorVersion) { $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion); - + if (isset($this->libraries[$library]) === FALSE) { return NULL; } - + return $this->getFileContents($this->libraries[$library]['path'] . '/semantics.json'); } - + /** * Get translations for the given library. - * + * * @param string $name of the library. * @param int $majorVersion of the library. * @param int $minorVersion of the library. * @return string Translation */ - public function getLanguage($name, $majorVersion, $minorVersion) { + public function getLanguage($name, $majorVersion, $minorVersion, $language) { $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion); - + if (isset($this->libraries[$library]) === FALSE) { return NULL; } - - return $this->getFileContents($this->libraries[$library]['path'] . '/language/' . $this->language . '.json'); + + return $this->getFileContents($this->libraries[$library]['path'] . '/language/' . $language . '.json'); } - + /** * Writes library as string on the form "name majorVersion.minorVersion" * @@ -175,4 +175,3 @@ class H5PDevelopment { return $name . ' ' . $majorVersion . '.' . $minorVersion; } } - diff --git a/h5p.classes.php b/h5p.classes.php index f6f9855..5b23280 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -394,10 +394,10 @@ interface H5PFrameworkInterface { /** * Delete a library from database and file system * - * @param int $libraryId - * Library identifier + * @param stdClass $library + * Library object with id, name, major version and minor version. */ - public function deleteLibrary($libraryId); + public function deleteLibrary($library); /** * Load content. @@ -444,57 +444,58 @@ interface H5PFrameworkInterface { public function loadContentDependencies($id, $type = NULL); /** - * Get data from cache. + * Get stored setting. * - * @param string $group - * Identifier for the cache group - * @param string $key - * Unique identifier within the group + * @param string $name + * Identifier for the setting + * @param string $default + * Optional default value if settings is not set * @return mixed - * Whatever has been stored in the cache. NULL if the entry doesn't exist + * Whatever has been stored as the setting */ - public function cacheGet($group, $key); + public function getOption($name, $default = NULL); /** - * Store data in cache. + * Stores the given setting. + * For example when did we last check h5p.org for updates to our libraries. * - * @param string $group - * The cache group where the data should be stored - * @param string $key - * A unique key identifying where the data should be stored - * @param mixed $data - * The data you want to cache + * @param string $name + * Identifier for the setting + * @param mixed $value Data + * Whatever we want to store as the setting */ - public function cacheSet($group, $key, $data); + public function setOption($name, $value); /** - * Delete data from cache. + * This will set the filtered parameters for the given content. * - * @param string $group - * Identifier for the cache group - * @param string $key - * Unique identifier within the group + * @param int $content_id + * @param string $parameters filtered */ - public function cacheDel($group, $key = NULL); + public function setFilteredParameters($content_id, $parameters = ''); /** - * Will invalidate the cache for the content that uses the specified library. - * This means that the content dependencies has to be rebuilt, and the parameters refiltered. + * Will clear filtered params for all the content that uses the specified + * library. This means that the content dependencies will have to be rebuilt, + * and the parameters refiltered. * - * @param int $libraryId + * @param int $library_id */ - public function invalidateContentCache($libraryId); + public function clearFilteredParameters($library_id); /** - * Get number of content that hasn't been cached + * Get number of contents that has to get their content dependencies rebuilt + * and parameters refiltered. + * + * @return int */ - public function getNotCached(); + public function getNumNotFiltered(); /** * Get number of contents using library as main library. * * @param int $libraryId - * Identifier for a library + * @return int */ public function getNumContent($libraryId); } @@ -1265,7 +1266,7 @@ class H5PStorage { } // Make sure libraries dependencies, parameter filtering and export files gets regenerated for all content who uses this library. - $this->h5pF->invalidateContentCache($library['libraryId']); + $this->h5pF->clearFilteredParameters($library['libraryId']); $upgradedLibsCount++; } @@ -1525,9 +1526,9 @@ class H5PCore { public static $defaultContentWhitelist = 'json png jpg jpeg gif bmp tif tiff svg eot ttf woff otf webm mp4 ogg mp3 txt pdf rtf doc docx xls xlsx ppt pptx odt ods odp xml csv diff patch swf md textile'; public static $defaultLibraryWhitelistExtras = 'js css'; + public $librariesJsonData, $contentJsonData, $mainJsonData, $h5pF, $path, $development_mode, $h5pD, $disableFileCheck; const SECONDS_IN_WEEK = 604800; - public $librariesJsonData, $contentJsonData, $mainJsonData, $h5pF, $path, $development_mode, $h5pD; private $exportEnabled; /** @@ -1567,11 +1568,6 @@ class H5PCore { $content['id'] = $this->h5pF->insertContent($content, $contentMainId); } - if (!isset($content['filtered'])) { - // TODO: Add filtered to all impl. and remove - $this->h5pF->cacheDel('parameters', $content['id']); - } - return $content['id']; } @@ -1619,16 +1615,8 @@ class H5PCore { * @return Object NULL on failure. */ public function filterParameters($content) { - if (isset($content['filtered'])) { - $params = ($content['filtered'] === '' ? NULL : $content['filtered']); - } - else { - // TODO: Add filtered to all impl. and remove - $params = $this->h5pF->cacheGet('parameters', $content['id']); - } - - if ($params !== NULL) { - return $params; + if (isset($content['filtered']) && $content['filtered'] !== '') { + return $content['filtered']; } // Validate and filter against main library semantics. @@ -1655,7 +1643,7 @@ class H5PCore { } // Cache. - $this->h5pF->cacheSet('parameters', $content['id'], $params); + $this->h5pF->setFilteredParameters($content['id'], $params); return $params; } @@ -2309,6 +2297,10 @@ class H5PContentValidator { * FALSE if one or more files fail validation. Error message should be set accordingly by validator. */ public function validateContentFiles($contentPath, $isLibrary = FALSE) { + if ($this->h5pC->disableFileCheck === TRUE) { + return TRUE; + } + // Scan content directory for files, recurse into sub directories. $files = array_diff(scandir($contentPath), array('.','..')); $valid = TRUE; @@ -2598,7 +2590,7 @@ class H5PContentValidator { if (!isset($this->libraries[$value->library])) { $libspec = H5PCore::libraryFromString($value->library); $library = $this->h5pC->loadLibrary($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']); - $library['semantics'] = json_decode($library['semantics']); + $library['semantics'] = $this->h5pC->loadLibrarySemantics($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']); $this->libraries[$value->library] = $library; // Find all dependencies for this library diff --git a/js/h5p-content-upgrade.js b/js/h5p-content-upgrade.js index bbea037..ff3756f 100644 --- a/js/h5p-content-upgrade.js +++ b/js/h5p-content-upgrade.js @@ -1,19 +1,20 @@ +/*jshint -W083 */ var H5PUpgrades = H5PUpgrades || {}; (function ($) { var info, $container, librariesCache = {}; - + // Initialize $(document).ready(function () { // Get library info info = H5PIntegration.getLibraryInfo(); - + // Get and reset container $container = $('#h5p-admin-container').html('

' + info.message + '

'); - + // Make it possible to select version var $version = $(getVersionSelect(info.versions)).appendTo($container); - + // Add "go" button $(''); H5PLibraryDetails.$next = $(''); - + H5PLibraryDetails.$previous.on('click', function () { if(H5PLibraryDetails.$previous.hasClass('disabled')) { return; } - + H5PLibraryDetails.currentPage--; H5PLibraryDetails.updatePager(); H5PLibraryDetails.createContentTable(); }); - + H5PLibraryDetails.$next.on('click', function () { if(H5PLibraryDetails.$next.hasClass('disabled')) { return; } - + H5PLibraryDetails.currentPage++; H5PLibraryDetails.updatePager(); H5PLibraryDetails.createContentTable(); }); - + // This is the Page x of y widget: H5PLibraryDetails.$pagerInfo = $(''); - + H5PLibraryDetails.$pager = $('
').append(H5PLibraryDetails.$previous, H5PLibraryDetails.$pagerInfo, H5PLibraryDetails.$next); H5PLibraryDetails.$content.append(H5PLibraryDetails.$pager); - + H5PLibraryDetails.$pagerInfo.on('click', function () { var width = H5PLibraryDetails.$pagerInfo.innerWidth(); H5PLibraryDetails.$pagerInfo.hide(); @@ -134,24 +134,24 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; var pageNumerUpdated = function() { var newPageNum = $gotoInput.val()-1; var intRegex = /^\d+$/; - + $goto.remove(); H5PLibraryDetails.$pagerInfo.css({display: 'inline-block'}); - + // Check if input value is valid, and that it has actually changed if(!(intRegex.test(newPageNum) && newPageNum >= 0 && newPageNum < H5PLibraryDetails.getNumPages() && newPageNum != H5PLibraryDetails.currentPage)) { return; } - + H5PLibraryDetails.currentPage = newPageNum; H5PLibraryDetails.updatePager(); H5PLibraryDetails.createContentTable(); }; - + // We create an input box where the user may type in the page number // he wants to be displayed. // Reson for doing this is when user has ten-thousands of elements in list, - // this is the easiest way of getting to a specified page + // this is the easiest way of getting to a specified page var $gotoInput = $('', { type: 'number', min : 1, @@ -166,30 +166,30 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; } } }).css({width: width}); - var $goto = $('', { + var $goto = $('', { 'class': 'h5p-pager-goto' }).css({width: width}).append($gotoInput).insertAfter(H5PLibraryDetails.$pagerInfo); - + $gotoInput.focus(); }); - + H5PLibraryDetails.updatePager(); } }; - + /** * Calculates number of pages */ H5PLibraryDetails.getNumPages = function () { return Math.ceil(H5PLibraryDetails.currentContent.length / H5PLibraryDetails.PAGER_SIZE); }; - + /** * Update the pager text, and enables/disables the next and previous buttons as needed */ H5PLibraryDetails.updatePager = function () { H5PLibraryDetails.$pagerInfo.css({display: 'inline-block'}); - + if(H5PLibraryDetails.getNumPages() > 0) { var message = H5PUtils.translateReplace(H5PLibraryDetails.library.translations.pageXOfY, { '$x': (H5PLibraryDetails.currentPage+1), @@ -200,26 +200,26 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; else { H5PLibraryDetails.$pagerInfo.html(''); } - + H5PLibraryDetails.$previous.toggleClass('disabled', H5PLibraryDetails.currentPage <= 0); H5PLibraryDetails.$next.toggleClass('disabled', H5PLibraryDetails.currentContent.length < (H5PLibraryDetails.currentPage+1)*H5PLibraryDetails.PAGER_SIZE); }; - + /** * Creates the search element */ H5PLibraryDetails.createSearchElement = function () { - + H5PLibraryDetails.$search = $(''); - + var performSeach = function () { var searchString = $('.h5p-content-search > input').val(); - + // If search string same as previous, just do nothing if(H5PLibraryDetails.currentFilter === searchString) { return; } - + if (searchString.trim().length === 0) { // If empty search, use the complete list H5PLibraryDetails.currentContent = H5PLibraryDetails.library.content; @@ -230,7 +230,7 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; } else { var listToFilter = H5PLibraryDetails.library.content; - + // Check if we can filter the already filtered results (for performance) if(searchString.length > 1 && H5PLibraryDetails.currentFilter === searchString.substr(0, H5PLibraryDetails.currentFilter.length)) { listToFilter = H5PLibraryDetails.currentContent; @@ -239,47 +239,47 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; return content.title && content.title.match(new RegExp(searchString, 'i')); }); } - + H5PLibraryDetails.currentFilter = searchString; // Cache the current result H5PLibraryDetails.filterCache[searchString] = H5PLibraryDetails.currentContent; H5PLibraryDetails.currentPage = 0; H5PLibraryDetails.createContentTable(); - + // Display search results: if (H5PLibraryDetails.$searchResults) { H5PLibraryDetails.$searchResults.remove(); } if (searchString.trim().length > 0) { - H5PLibraryDetails.$searchResults = $('' + H5PLibraryDetails.currentContent.length + ' hits on ' + H5PLibraryDetails.currentFilter + ''); + H5PLibraryDetails.$searchResults = $('' + H5PLibraryDetails.currentContent.length + ' hits on ' + H5PLibraryDetails.currentFilter + ''); H5PLibraryDetails.$search.append(H5PLibraryDetails.$searchResults); } H5PLibraryDetails.updatePager(); }; - - var inputTimer = undefined; + + var inputTimer; $('input', H5PLibraryDetails.$search).on('change keypress paste input', function () { // Here we start the filtering // We wait at least 500 ms after last input to perform search if(inputTimer) { clearTimeout(inputTimer); } - + inputTimer = setTimeout( function () { performSeach(); }, 500); }); - + H5PLibraryDetails.$content.append(H5PLibraryDetails.$search); }; - + /** * Creates the page size selector */ H5PLibraryDetails.createPageSizeSelector = function () { H5PLibraryDetails.$search.append('
' + H5PLibraryDetails.library.translations.pageSizeSelectorLabel + ':102050100200
'); - - // Listen to clicks on the page size selector: + + // Listen to clicks on the page size selector: $('.h5p-admin-pager-size-selector > span', H5PLibraryDetails.$search).on('click', function () { H5PLibraryDetails.PAGER_SIZE = $(this).data('page-size'); $('.h5p-admin-pager-size-selector > span', H5PLibraryDetails.$search).removeClass('selected'); @@ -289,7 +289,7 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; H5PLibraryDetails.updatePager(); }); }; - + // Initialize me: $(document).ready(function () { if (!H5PLibraryDetails.initialized) { @@ -297,5 +297,5 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; H5PLibraryDetails.init(); } }); - -})(H5P.jQuery); \ No newline at end of file + +})(H5P.jQuery); diff --git a/js/h5p-library-list.js b/js/h5p-library-list.js index ed5a035..4382b28 100644 --- a/js/h5p-library-list.js +++ b/js/h5p-library-list.js @@ -1,4 +1,5 @@ -var H5PLibraryList= H5PLibraryList || {}; +/*jshint multistr: true */ +var H5PLibraryList = H5PLibraryList || {}; (function ($) { @@ -7,19 +8,19 @@ var H5PLibraryList= H5PLibraryList || {}; */ H5PLibraryList.init = function () { var $adminContainer = H5PIntegration.getAdminContainer(); - + var libraryList = H5PIntegration.getLibraryList(); if (libraryList.notCached) { $adminContainer.append(H5PUtils.getRebuildCache(libraryList.notCached)); } - + // Create library list $adminContainer.append(H5PLibraryList.createLibraryList(H5PIntegration.getLibraryList())); }; - + /** * Create the library list - * + * * @param {object} libraries List of libraries and headers */ H5PLibraryList.createLibraryList = function (libraries) { @@ -27,11 +28,11 @@ var H5PLibraryList= H5PLibraryList || {}; if(libraries.listData === undefined || libraries.listData.length === 0) { return $('
' + t.NA + '
'); } - + // Create table var $table = H5PUtils.createTable(libraries.listHeaders); $table.addClass('libraries'); - + // Add libraries $.each (libraries.listData, function (index, library) { var $libraryRow = H5PUtils.createTableRow([ @@ -55,7 +56,7 @@ var H5PLibraryList= H5PLibraryList || {}; \ ' ]); - + H5PLibraryList.addRestricted($('.h5p-admin-restricted', $libraryRow), library.restrictedUrl, library.restricted); var hasContent = !(library.numContent === '' || library.numContent === 0); @@ -70,12 +71,12 @@ var H5PLibraryList= H5PLibraryList || {}; window.location.href = library.upgradeUrl; }); } - + // Open details view when clicked $('.h5p-admin-view-library', $libraryRow).on('click', function () { window.location.href = library.detailsUrl; }); - + var $deleteButton = $('.h5p-admin-delete-library', $libraryRow); if (libraries.notCached !== undefined || hasContent || (library.numContentDependencies !== '' && library.numContentDependencies !== 0) || (library.numLibraryDependencies !== '' && library.numLibraryDependencies !== 0)) { // Disabled delete if content. @@ -90,10 +91,10 @@ var H5PLibraryList= H5PLibraryList || {}; $table.append($libraryRow); }); - + return $table; }; - + H5PLibraryList.addRestricted = function ($checkbox, url, selected) { if (selected === null) { $checkbox.remove(); @@ -130,5 +131,5 @@ var H5PLibraryList= H5PLibraryList || {}; H5PLibraryList.init(); } }); - + })(H5P.jQuery); diff --git a/js/h5p-utils.js b/js/h5p-utils.js index 26ffa1d..00af7bc 100644 --- a/js/h5p-utils.js +++ b/js/h5p-utils.js @@ -3,7 +3,7 @@ var H5PUtils = H5PUtils || {}; (function ($) { /** * Generic function for creating a table including the headers - * + * * @param {array} headers List of headers */ H5PUtils.createTable = function (headers) { @@ -12,62 +12,62 @@ var H5PUtils = H5PUtils || {}; if(headers) { var $thead = $(''); var $tr = $(''); - + $.each(headers, function (index, value) { if (!(value instanceof Object)) { value = { html: value }; } - + $('', value).appendTo($tr); }); - + $table.append($thead.append($tr)); } - + return $table; }; - + /** * Generic function for creating a table row - * + * * @param {array} rows Value list. Object name is used as class name in */ H5PUtils.createTableRow = function (rows) { var $tr = $(''); - + $.each(rows, function (index, value) { if (!(value instanceof Object)) { value = { html: value }; } - + $('', value).appendTo($tr); }); - + return $tr; }; - + /** * Generic function for creating a field containing label and value - * - * @param {string} label The label displayed in front of the value + * + * @param {string} label The label displayed in front of the value * @param {string} value The value */ H5PUtils.createLabeledField = function (label, value) { var $field = $('
'); - + $field.append('
' + label + '
'); $field.append('
' + value + '
'); - + return $field; }; - + /** * Replaces placeholder fields in translation strings - * + * * @param {string} template The translation template string in the following format: "$name is a $sex" * @param {array} replacors An js object with key and values. Eg: {'$name': 'Frode', '$sex': 'male'} */ @@ -77,10 +77,10 @@ var H5PUtils = H5PUtils || {}; }); return template; }; - + /** * Get throbber with given text. - * + * * @param {String} text * @returns {$} */ @@ -90,7 +90,7 @@ var H5PUtils = H5PUtils || {}; text: text }); }; - + /** * Makes it possbile to rebuild all content caches from admin UI. * @param {Object} notCached @@ -107,7 +107,7 @@ var H5PUtils = H5PUtils || {}; current++; if (current === parts.length) current = 0; }, 100); - + var $counter = $container.find('.progress'); var build = function () { $.post(notCached.url, function (left) { @@ -126,8 +126,357 @@ var H5PUtils = H5PUtils || {}; }; build(); }); - + return $container; }; - -})(H5P.jQuery); \ No newline at end of file + + /** + * Generic table class with useful helpers. + * + * @class + * @param {Object} classes + * Custom html classes to use on elements. + * e.g. {tableClass: 'fixed'}. + */ + H5PUtils.Table = function (classes) { + var numCols; + var sortByCol; + var $sortCol; + var sortCol; + var sortDir; + + // Create basic table + var tableOptions = {}; + if (classes.table !== undefined) { + tableOptions['class'] = classes.table; + } + var $table = $('', tableOptions); + var $thead = $('').appendTo($table); + var $tfoot = $('').appendTo($table); + var $tbody = $('').appendTo($table); + + /** + * Add columns to given table row. + * + * @private + * @param {jQuery} $tr Table row + * @param {(String|Object)} col Column properties + * @param {Number} id Used to seperate the columns + */ + var addCol = function ($tr, col, id) { + var options = { + on: {} + }; + + if (!(col instanceof Object)) { + options.text = col; + } + else { + if (col.text !== undefined) { + options.text = col.text; + } + if (col.class !== undefined) { + options.class = col.class; + } + + if (sortByCol !== undefined && col.sortable === true) { + // Make sortable + options.role = 'button'; + options.tabIndex = 1; + + // This is the first sortable column, use as default sort + if (sortCol === undefined) { + sortCol = id; + sortDir = 0; + options['class'] = 'h5p-sort'; + } + + options.on.click = function () { + sort($th, id); + }; + } + } + + // Append + var $th = $(''); + var $tr = $('').appendTo($newThead); + for (var i = 0; i < cols.length; i++) { + addCol($tr, cols[i], i); + } + + // Update DOM + $thead.replaceWith($newThead); + $thead = $newThead; + }; + + /** + * Set table rows. + * + * @public + * @param {Array} rows Table rows with cols: [[1,'hello',3],[2,'asd',6]] + */ + this.setRows = function (rows) { + var $newTbody = $(''); + + for (var i = 0; i < rows.length; i++) { + var $tr = $('').appendTo($newTbody); + + for (var j = 0; j < rows[i].length; j++) { + $(''); + var $tr = $('').appendTo($newTbody); + $(''); + var $tr = $('').appendTo($newTfoot); + $('\s*$/g,At={option:[1,""],legend:[1,"
","
"],area:[1,"",""],param:[1,"",""],thead:[1,"
', options).appendTo($tr); + if (sortCol === id) { + $sortCol = $th; // Default sort column + } + }; + + /** + * Updates the UI when a column header has been clicked. + * Triggers sorting callback. + * + * @private + * @param {jQuery} $th Table header + * @param {Number} id Used to seperate the columns + */ + var sort = function ($th, id) { + if (id === sortCol) { + // Change sorting direction + if (sortDir === 0) { + sortDir = 1; + $th.addClass('h5p-reverse'); + } + else { + sortDir = 0; + $th.removeClass('h5p-reverse'); + } + } + else { + // Change sorting column + $sortCol.removeClass('h5p-sort').removeClass('h5p-reverse'); + $sortCol = $th.addClass('h5p-sort'); + sortCol = id; + sortDir = 0; + } + + sortByCol(sortCol, sortDir); + }; + + /** + * Set table headers. + * + * @public + * @param {Array} cols + * Table header data. Can be strings or objects with options like + * "text" and "sortable". E.g. + * [{text: 'Col 1', sortable: true}, 'Col 2', 'Col 3'] + * @param {Function} sort Callback which is runned when sorting changes + */ + this.setHeaders = function (cols, sort) { + numCols = cols.length; + sortByCol = sort; + + // Create new head + var $newThead = $('
', { + html: rows[i][j] + }).appendTo($tr); + } + } + + $tbody.replaceWith($newTbody); + $tbody = $newTbody; + }; + + /** + * Set custom table body content. This can be a message or a throbber. + * Will cover all table columns. + * + * @public + * @param {jQuery} $content Custom content + */ + this.setBody = function ($content) { + var $newTbody = $('
', { + colspan: numCols + }).append($content).appendTo($tr); + $tbody.replaceWith($newTbody); + $tbody = $newTbody; + }; + + /** + * Set custom table foot content. This can be a pagination widget. + * Will cover all table columns. + * + * @public + * @param {jQuery} $content Custom content + */ + this.setFoot = function ($content) { + var $newTfoot = $('
', { + colspan: numCols + }).append($content).appendTo($tr); + $tfoot.replaceWith($newTfoot); + }; + + + /** + * Appends the table to the given container. + * + * @public + * @param {jQuery} $container + */ + this.appendTo = function ($container) { + $table.appendTo($container); + }; + }; + + /** + * Generic pagination class. Creates a useful pagination widget. + * + * @class + * @param {Number} num Total number of items to pagiate. + * @param {Number} limit Number of items to dispaly per page. + * @param {Function} goneTo + * Callback which is fired when the user wants to go to another page. + * @param {Object} l10n + * Localization / translations. e.g. + * { + * currentPage: 'Page $current of $total', + * nextPage: 'Next page', + * previousPage: 'Previous page' + * } + */ + H5PUtils.Pagination = function (num, limit, goneTo, l10n) { + var current = 0; + var pages = Math.ceil(num / limit); + + // Create components + + // Previous button + var $left = $('
","
"],tr:[2,"","
"],col:[2,"","
"],td:[3,"","
"],_default:b.support.htmlSerialize?[0,"",""]:[1,"X
","
"]},jt=dt(o),Dt=jt.appendChild(o.createElement("div"));At.optgroup=At.option,At.tbody=At.tfoot=At.colgroup=At.caption=At.thead,At.th=At.td,b.fn.extend({text:function(e){return b.access(this,function(e){return e===t?b.text(this):this.empty().append((this[0]&&this[0].ownerDocument||o).createTextNode(e))},null,e,arguments.length)},wrapAll:function(e){if(b.isFunction(e))return this.each(function(t){b(this).wrapAll(e.call(this,t))});if(this[0]){var t=b(e,this[0].ownerDocument).eq(0).clone(!0);this[0].parentNode&&t.insertBefore(this[0]),t.map(function(){var e=this;while(e.firstChild&&1===e.firstChild.nodeType)e=e.firstChild;return e}).append(this)}return this},wrapInner:function(e){return b.isFunction(e)?this.each(function(t){b(this).wrapInner(e.call(this,t))}):this.each(function(){var t=b(this),n=t.contents();n.length?n.wrapAll(e):t.append(e)})},wrap:function(e){var t=b.isFunction(e);return this.each(function(n){b(this).wrapAll(t?e.call(this,n):e)})},unwrap:function(){return this.parent().each(function(){b.nodeName(this,"body")||b(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,!0,function(e){(1===this.nodeType||11===this.nodeType||9===this.nodeType)&&this.appendChild(e)})},prepend:function(){return this.domManip(arguments,!0,function(e){(1===this.nodeType||11===this.nodeType||9===this.nodeType)&&this.insertBefore(e,this.firstChild)})},before:function(){return this.domManip(arguments,!1,function(e){this.parentNode&&this.parentNode.insertBefore(e,this)})},after:function(){return this.domManip(arguments,!1,function(e){this.parentNode&&this.parentNode.insertBefore(e,this.nextSibling)})},remove:function(e,t){var n,r=0;for(;null!=(n=this[r]);r++)(!e||b.filter(e,[n]).length>0)&&(t||1!==n.nodeType||b.cleanData(Ot(n)),n.parentNode&&(t&&b.contains(n.ownerDocument,n)&&Mt(Ot(n,"script")),n.parentNode.removeChild(n)));return this},empty:function(){var e,t=0;for(;null!=(e=this[t]);t++){1===e.nodeType&&b.cleanData(Ot(e,!1));while(e.firstChild)e.removeChild(e.firstChild);e.options&&b.nodeName(e,"select")&&(e.options.length=0)}return this},clone:function(e,t){return e=null==e?!1:e,t=null==t?e:t,this.map(function(){return b.clone(this,e,t)})},html:function(e){return b.access(this,function(e){var n=this[0]||{},r=0,i=this.length;if(e===t)return 1===n.nodeType?n.innerHTML.replace(gt,""):t;if(!("string"!=typeof e||Tt.test(e)||!b.support.htmlSerialize&&mt.test(e)||!b.support.leadingWhitespace&&yt.test(e)||At[(bt.exec(e)||["",""])[1].toLowerCase()])){e=e.replace(vt,"<$1>");try{for(;i>r;r++)n=this[r]||{},1===n.nodeType&&(b.cleanData(Ot(n,!1)),n.innerHTML=e);n=0}catch(o){}}n&&this.empty().append(e)},null,e,arguments.length)},replaceWith:function(e){var t=b.isFunction(e);return t||"string"==typeof e||(e=b(e).not(this).detach()),this.domManip([e],!0,function(e){var t=this.nextSibling,n=this.parentNode;n&&(b(this).remove(),n.insertBefore(e,t))})},detach:function(e){return this.remove(e,!0)},domManip:function(e,n,r){e=f.apply([],e);var i,o,a,s,u,l,c=0,p=this.length,d=this,h=p-1,g=e[0],m=b.isFunction(g);if(m||!(1>=p||"string"!=typeof g||b.support.checkClone)&&Ct.test(g))return this.each(function(i){var o=d.eq(i);m&&(e[0]=g.call(this,i,n?o.html():t)),o.domManip(e,n,r)});if(p&&(l=b.buildFragment(e,this[0].ownerDocument,!1,this),i=l.firstChild,1===l.childNodes.length&&(l=i),i)){for(n=n&&b.nodeName(i,"tr"),s=b.map(Ot(l,"script"),Ht),a=s.length;p>c;c++)o=l,c!==h&&(o=b.clone(o,!0,!0),a&&b.merge(s,Ot(o,"script"))),r.call(n&&b.nodeName(this[c],"table")?Lt(this[c],"tbody"):this[c],o,c);if(a)for(u=s[s.length-1].ownerDocument,b.map(s,qt),c=0;a>c;c++)o=s[c],kt.test(o.type||"")&&!b._data(o,"globalEval")&&b.contains(u,o)&&(o.src?b.ajax({url:o.src,type:"GET",dataType:"script",async:!1,global:!1,"throws":!0}):b.globalEval((o.text||o.textContent||o.innerHTML||"").replace(St,"")));l=i=null}return this}});function Lt(e,t){return e.getElementsByTagName(t)[0]||e.appendChild(e.ownerDocument.createElement(t))}function Ht(e){var t=e.getAttributeNode("type");return e.type=(t&&t.specified)+"/"+e.type,e}function qt(e){var t=Et.exec(e.type);return t?e.type=t[1]:e.removeAttribute("type"),e}function Mt(e,t){var n,r=0;for(;null!=(n=e[r]);r++)b._data(n,"globalEval",!t||b._data(t[r],"globalEval"))}function _t(e,t){if(1===t.nodeType&&b.hasData(e)){var n,r,i,o=b._data(e),a=b._data(t,o),s=o.events;if(s){delete a.handle,a.events={};for(n in s)for(r=0,i=s[n].length;i>r;r++)b.event.add(t,n,s[n][r])}a.data&&(a.data=b.extend({},a.data))}}function Ft(e,t){var n,r,i;if(1===t.nodeType){if(n=t.nodeName.toLowerCase(),!b.support.noCloneEvent&&t[b.expando]){i=b._data(t);for(r in i.events)b.removeEvent(t,r,i.handle);t.removeAttribute(b.expando)}"script"===n&&t.text!==e.text?(Ht(t).text=e.text,qt(t)):"object"===n?(t.parentNode&&(t.outerHTML=e.outerHTML),b.support.html5Clone&&e.innerHTML&&!b.trim(t.innerHTML)&&(t.innerHTML=e.innerHTML)):"input"===n&&Nt.test(e.type)?(t.defaultChecked=t.checked=e.checked,t.value!==e.value&&(t.value=e.value)):"option"===n?t.defaultSelected=t.selected=e.defaultSelected:("input"===n||"textarea"===n)&&(t.defaultValue=e.defaultValue)}}b.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(e,t){b.fn[e]=function(e){var n,r=0,i=[],o=b(e),a=o.length-1;for(;a>=r;r++)n=r===a?this:this.clone(!0),b(o[r])[t](n),d.apply(i,n.get());return this.pushStack(i)}});function Ot(e,n){var r,o,a=0,s=typeof e.getElementsByTagName!==i?e.getElementsByTagName(n||"*"):typeof e.querySelectorAll!==i?e.querySelectorAll(n||"*"):t;if(!s)for(s=[],r=e.childNodes||e;null!=(o=r[a]);a++)!n||b.nodeName(o,n)?s.push(o):b.merge(s,Ot(o,n));return n===t||n&&b.nodeName(e,n)?b.merge([e],s):s}function Bt(e){Nt.test(e.type)&&(e.defaultChecked=e.checked)}b.extend({clone:function(e,t,n){var r,i,o,a,s,u=b.contains(e.ownerDocument,e);if(b.support.html5Clone||b.isXMLDoc(e)||!mt.test("<"+e.nodeName+">")?o=e.cloneNode(!0):(Dt.innerHTML=e.outerHTML,Dt.removeChild(o=Dt.firstChild)),!(b.support.noCloneEvent&&b.support.noCloneChecked||1!==e.nodeType&&11!==e.nodeType||b.isXMLDoc(e)))for(r=Ot(o),s=Ot(e),a=0;null!=(i=s[a]);++a)r[a]&&Ft(i,r[a]);if(t)if(n)for(s=s||Ot(e),r=r||Ot(o),a=0;null!=(i=s[a]);a++)_t(i,r[a]);else _t(e,o);return r=Ot(o,"script"),r.length>0&&Mt(r,!u&&Ot(e,"script")),r=s=i=null,o},buildFragment:function(e,t,n,r){var i,o,a,s,u,l,c,p=e.length,f=dt(t),d=[],h=0;for(;p>h;h++)if(o=e[h],o||0===o)if("object"===b.type(o))b.merge(d,o.nodeType?[o]:o);else if(wt.test(o)){s=s||f.appendChild(t.createElement("div")),u=(bt.exec(o)||["",""])[1].toLowerCase(),c=At[u]||At._default,s.innerHTML=c[1]+o.replace(vt,"<$1>")+c[2],i=c[0];while(i--)s=s.lastChild;if(!b.support.leadingWhitespace&&yt.test(o)&&d.push(t.createTextNode(yt.exec(o)[0])),!b.support.tbody){o="table"!==u||xt.test(o)?""!==c[1]||xt.test(o)?0:s:s.firstChild,i=o&&o.childNodes.length;while(i--)b.nodeName(l=o.childNodes[i],"tbody")&&!l.childNodes.length&&o.removeChild(l) }b.merge(d,s.childNodes),s.textContent="";while(s.firstChild)s.removeChild(s.firstChild);s=f.lastChild}else d.push(t.createTextNode(o));s&&f.removeChild(s),b.support.appendChecked||b.grep(Ot(d,"input"),Bt),h=0;while(o=d[h++])if((!r||-1===b.inArray(o,r))&&(a=b.contains(o.ownerDocument,o),s=Ot(f.appendChild(o),"script"),a&&Mt(s),n)){i=0;while(o=s[i++])kt.test(o.type||"")&&n.push(o)}return s=null,f},cleanData:function(e,t){var n,r,o,a,s=0,u=b.expando,l=b.cache,p=b.support.deleteExpando,f=b.event.special;for(;null!=(n=e[s]);s++)if((t||b.acceptData(n))&&(o=n[u],a=o&&l[o])){if(a.events)for(r in a.events)f[r]?b.event.remove(n,r):b.removeEvent(n,r,a.handle);l[o]&&(delete l[o],p?delete n[u]:typeof n.removeAttribute!==i?n.removeAttribute(u):n[u]=null,c.push(o))}}});var Pt,Rt,Wt,$t=/alpha\([^)]*\)/i,It=/opacity\s*=\s*([^)]*)/,zt=/^(top|right|bottom|left)$/,Xt=/^(none|table(?!-c[ea]).+)/,Ut=/^margin/,Vt=RegExp("^("+x+")(.*)$","i"),Yt=RegExp("^("+x+")(?!px)[a-z%]+$","i"),Jt=RegExp("^([+-])=("+x+")","i"),Gt={BODY:"block"},Qt={position:"absolute",visibility:"hidden",display:"block"},Kt={letterSpacing:0,fontWeight:400},Zt=["Top","Right","Bottom","Left"],en=["Webkit","O","Moz","ms"];function tn(e,t){if(t in e)return t;var n=t.charAt(0).toUpperCase()+t.slice(1),r=t,i=en.length;while(i--)if(t=en[i]+n,t in e)return t;return r}function nn(e,t){return e=t||e,"none"===b.css(e,"display")||!b.contains(e.ownerDocument,e)}function rn(e,t){var n,r,i,o=[],a=0,s=e.length;for(;s>a;a++)r=e[a],r.style&&(o[a]=b._data(r,"olddisplay"),n=r.style.display,t?(o[a]||"none"!==n||(r.style.display=""),""===r.style.display&&nn(r)&&(o[a]=b._data(r,"olddisplay",un(r.nodeName)))):o[a]||(i=nn(r),(n&&"none"!==n||!i)&&b._data(r,"olddisplay",i?n:b.css(r,"display"))));for(a=0;s>a;a++)r=e[a],r.style&&(t&&"none"!==r.style.display&&""!==r.style.display||(r.style.display=t?o[a]||"":"none"));return e}b.fn.extend({css:function(e,n){return b.access(this,function(e,n,r){var i,o,a={},s=0;if(b.isArray(n)){for(o=Rt(e),i=n.length;i>s;s++)a[n[s]]=b.css(e,n[s],!1,o);return a}return r!==t?b.style(e,n,r):b.css(e,n)},e,n,arguments.length>1)},show:function(){return rn(this,!0)},hide:function(){return rn(this)},toggle:function(e){var t="boolean"==typeof e;return this.each(function(){(t?e:nn(this))?b(this).show():b(this).hide()})}}),b.extend({cssHooks:{opacity:{get:function(e,t){if(t){var n=Wt(e,"opacity");return""===n?"1":n}}}},cssNumber:{columnCount:!0,fillOpacity:!0,fontWeight:!0,lineHeight:!0,opacity:!0,orphans:!0,widows:!0,zIndex:!0,zoom:!0},cssProps:{"float":b.support.cssFloat?"cssFloat":"styleFloat"},style:function(e,n,r,i){if(e&&3!==e.nodeType&&8!==e.nodeType&&e.style){var o,a,s,u=b.camelCase(n),l=e.style;if(n=b.cssProps[u]||(b.cssProps[u]=tn(l,u)),s=b.cssHooks[n]||b.cssHooks[u],r===t)return s&&"get"in s&&(o=s.get(e,!1,i))!==t?o:l[n];if(a=typeof r,"string"===a&&(o=Jt.exec(r))&&(r=(o[1]+1)*o[2]+parseFloat(b.css(e,n)),a="number"),!(null==r||"number"===a&&isNaN(r)||("number"!==a||b.cssNumber[u]||(r+="px"),b.support.clearCloneStyle||""!==r||0!==n.indexOf("background")||(l[n]="inherit"),s&&"set"in s&&(r=s.set(e,r,i))===t)))try{l[n]=r}catch(c){}}},css:function(e,n,r,i){var o,a,s,u=b.camelCase(n);return n=b.cssProps[u]||(b.cssProps[u]=tn(e.style,u)),s=b.cssHooks[n]||b.cssHooks[u],s&&"get"in s&&(a=s.get(e,!0,r)),a===t&&(a=Wt(e,n,i)),"normal"===a&&n in Kt&&(a=Kt[n]),""===r||r?(o=parseFloat(a),r===!0||b.isNumeric(o)?o||0:a):a},swap:function(e,t,n,r){var i,o,a={};for(o in t)a[o]=e.style[o],e.style[o]=t[o];i=n.apply(e,r||[]);for(o in t)e.style[o]=a[o];return i}}),e.getComputedStyle?(Rt=function(t){return e.getComputedStyle(t,null)},Wt=function(e,n,r){var i,o,a,s=r||Rt(e),u=s?s.getPropertyValue(n)||s[n]:t,l=e.style;return s&&(""!==u||b.contains(e.ownerDocument,e)||(u=b.style(e,n)),Yt.test(u)&&Ut.test(n)&&(i=l.width,o=l.minWidth,a=l.maxWidth,l.minWidth=l.maxWidth=l.width=u,u=s.width,l.width=i,l.minWidth=o,l.maxWidth=a)),u}):o.documentElement.currentStyle&&(Rt=function(e){return e.currentStyle},Wt=function(e,n,r){var i,o,a,s=r||Rt(e),u=s?s[n]:t,l=e.style;return null==u&&l&&l[n]&&(u=l[n]),Yt.test(u)&&!zt.test(n)&&(i=l.left,o=e.runtimeStyle,a=o&&o.left,a&&(o.left=e.currentStyle.left),l.left="fontSize"===n?"1em":u,u=l.pixelLeft+"px",l.left=i,a&&(o.left=a)),""===u?"auto":u});function on(e,t,n){var r=Vt.exec(t);return r?Math.max(0,r[1]-(n||0))+(r[2]||"px"):t}function an(e,t,n,r,i){var o=n===(r?"border":"content")?4:"width"===t?1:0,a=0;for(;4>o;o+=2)"margin"===n&&(a+=b.css(e,n+Zt[o],!0,i)),r?("content"===n&&(a-=b.css(e,"padding"+Zt[o],!0,i)),"margin"!==n&&(a-=b.css(e,"border"+Zt[o]+"Width",!0,i))):(a+=b.css(e,"padding"+Zt[o],!0,i),"padding"!==n&&(a+=b.css(e,"border"+Zt[o]+"Width",!0,i)));return a}function sn(e,t,n){var r=!0,i="width"===t?e.offsetWidth:e.offsetHeight,o=Rt(e),a=b.support.boxSizing&&"border-box"===b.css(e,"boxSizing",!1,o);if(0>=i||null==i){if(i=Wt(e,t,o),(0>i||null==i)&&(i=e.style[t]),Yt.test(i))return i;r=a&&(b.support.boxSizingReliable||i===e.style[t]),i=parseFloat(i)||0}return i+an(e,t,n||(a?"border":"content"),r,o)+"px"}function un(e){var t=o,n=Gt[e];return n||(n=ln(e,t),"none"!==n&&n||(Pt=(Pt||b("