2014-10-01 10:05:04 +02:00
|
|
|
/*jshint multistr: true */
|
2014-10-23 15:18:03 +02:00
|
|
|
var H5PLibraryList = H5PLibraryList || {};
|
2014-03-26 08:43:29 +01:00
|
|
|
|
|
|
|
(function ($) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializing
|
|
|
|
*/
|
|
|
|
H5PLibraryList.init = function () {
|
2015-03-02 13:42:15 +01:00
|
|
|
var $adminContainer = H5P.jQuery(H5PAdminIntegration.containerSelector).html('');
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2015-02-27 13:59:42 +01:00
|
|
|
var libraryList = H5PAdminIntegration.libraryList;
|
2014-05-28 13:18:24 +02:00
|
|
|
if (libraryList.notCached) {
|
|
|
|
$adminContainer.append(H5PUtils.getRebuildCache(libraryList.notCached));
|
|
|
|
}
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
// Create library list
|
2015-02-27 13:59:42 +01:00
|
|
|
$adminContainer.append(H5PLibraryList.createLibraryList(H5PAdminIntegration.libraryList));
|
2014-03-26 08:43:29 +01:00
|
|
|
};
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
|
|
|
* Create the library list
|
2014-10-01 10:05:04 +02:00
|
|
|
*
|
2014-03-26 08:43:29 +01:00
|
|
|
* @param {object} libraries List of libraries and headers
|
|
|
|
*/
|
|
|
|
H5PLibraryList.createLibraryList = function (libraries) {
|
2015-02-27 13:59:42 +01:00
|
|
|
var t = H5PAdminIntegration.l10n;
|
2014-03-26 08:43:29 +01:00
|
|
|
if(libraries.listData === undefined || libraries.listData.length === 0) {
|
2014-09-23 11:35:21 +02:00
|
|
|
return $('<div>' + t.NA + '</div>');
|
2014-03-26 08:43:29 +01:00
|
|
|
}
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
// Create table
|
|
|
|
var $table = H5PUtils.createTable(libraries.listHeaders);
|
|
|
|
$table.addClass('libraries');
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
// Add libraries
|
|
|
|
$.each (libraries.listData, function (index, library) {
|
|
|
|
var $libraryRow = H5PUtils.createTableRow([
|
2014-06-03 16:07:50 +02:00
|
|
|
library.title,
|
2014-08-29 16:30:44 +02:00
|
|
|
'<input class="h5p-admin-restricted" type="checkbox"/>',
|
2014-09-24 16:24:56 +02:00
|
|
|
{
|
|
|
|
text: library.numContent,
|
|
|
|
class: 'h5p-admin-center'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: library.numContentDependencies,
|
|
|
|
class: 'h5p-admin-center'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: library.numLibraryDependencies,
|
|
|
|
class: 'h5p-admin-center'
|
|
|
|
},
|
2014-06-03 16:07:50 +02:00
|
|
|
'<div class="h5p-admin-buttons-wrapper">\
|
|
|
|
<button class="h5p-admin-upgrade-library"></button>\
|
|
|
|
<button class="h5p-admin-view-library" title="' + t.viewLibrary + '"></button>\
|
|
|
|
<button class="h5p-admin-delete-library"></button>\
|
|
|
|
</div>'
|
2014-03-26 08:43:29 +01:00
|
|
|
]);
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-08-29 16:30:44 +02:00
|
|
|
H5PLibraryList.addRestricted($('.h5p-admin-restricted', $libraryRow), library.restrictedUrl, library.restricted);
|
|
|
|
|
2014-09-25 14:38:05 +02:00
|
|
|
var hasContent = !(library.numContent === '' || library.numContent === 0);
|
2014-08-22 12:00:06 +02:00
|
|
|
if (library.upgradeUrl === null) {
|
|
|
|
$('.h5p-admin-upgrade-library', $libraryRow).remove();
|
|
|
|
}
|
2014-09-25 14:38:05 +02:00
|
|
|
else if (library.upgradeUrl === false || !hasContent) {
|
2014-08-22 12:00:06 +02:00
|
|
|
$('.h5p-admin-upgrade-library', $libraryRow).attr('disabled', true);
|
|
|
|
}
|
|
|
|
else {
|
2014-06-03 16:07:50 +02:00
|
|
|
$('.h5p-admin-upgrade-library', $libraryRow).attr('title', t.upgradeLibrary).click(function () {
|
|
|
|
window.location.href = library.upgradeUrl;
|
|
|
|
});
|
|
|
|
}
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
// Open details view when clicked
|
|
|
|
$('.h5p-admin-view-library', $libraryRow).on('click', function () {
|
|
|
|
window.location.href = library.detailsUrl;
|
|
|
|
});
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
var $deleteButton = $('.h5p-admin-delete-library', $libraryRow);
|
2014-09-25 14:38:05 +02:00
|
|
|
if (libraries.notCached !== undefined || hasContent || (library.numContentDependencies !== '' && library.numContentDependencies !== 0) || (library.numLibraryDependencies !== '' && library.numLibraryDependencies !== 0)) {
|
2014-03-26 08:43:29 +01:00
|
|
|
// Disabled delete if content.
|
2014-08-22 12:00:06 +02:00
|
|
|
$deleteButton.attr('disabled', true);
|
2014-03-26 08:43:29 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Go to delete page om click.
|
2014-08-22 12:00:06 +02:00
|
|
|
$deleteButton.attr('title', t.deleteLibrary).on('click', function () {
|
2014-03-26 08:43:29 +01:00
|
|
|
window.location.href = library.deleteUrl;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$table.append($libraryRow);
|
|
|
|
});
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
return $table;
|
|
|
|
};
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-08-29 16:30:44 +02:00
|
|
|
H5PLibraryList.addRestricted = function ($checkbox, url, selected) {
|
|
|
|
if (selected === null) {
|
|
|
|
$checkbox.remove();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$checkbox.change(function () {
|
|
|
|
$checkbox.attr('disabled', true);
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
dataType: 'json',
|
|
|
|
url: url,
|
|
|
|
cache: false
|
|
|
|
}).fail(function () {
|
|
|
|
$checkbox.attr('disabled', false);
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
$checkbox.attr('checked', !$checkbox.is(':checked'));
|
|
|
|
}).done(function (result) {
|
|
|
|
url = result.url;
|
|
|
|
$checkbox.attr('disabled', false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (selected) {
|
|
|
|
$checkbox.attr('checked', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-02-27 13:59:42 +01:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
// Initialize me:
|
|
|
|
$(document).ready(function () {
|
|
|
|
if (!H5PLibraryList.initialized) {
|
|
|
|
H5PLibraryList.initialized = true;
|
|
|
|
H5PLibraryList.init();
|
|
|
|
}
|
|
|
|
});
|
2014-10-01 10:05:04 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
})(H5P.jQuery);
|