1 |
efrain |
1 |
/* global H5PAdminIntegration H5PUtils */
|
|
|
2 |
var H5PLibraryList = H5PLibraryList || {};
|
|
|
3 |
|
|
|
4 |
(function ($) {
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* Initializing
|
|
|
8 |
*/
|
|
|
9 |
H5PLibraryList.init = function () {
|
|
|
10 |
var $adminContainer = H5P.jQuery(H5PAdminIntegration.containerSelector).html('');
|
|
|
11 |
|
|
|
12 |
var libraryList = H5PAdminIntegration.libraryList;
|
|
|
13 |
if (libraryList.notCached) {
|
|
|
14 |
$adminContainer.append(H5PUtils.getRebuildCache(libraryList.notCached));
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
// Create library list
|
|
|
18 |
$adminContainer.append(H5PLibraryList.createLibraryList(H5PAdminIntegration.libraryList));
|
|
|
19 |
};
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Create the library list
|
|
|
23 |
*
|
|
|
24 |
* @param {object} libraries List of libraries and headers
|
|
|
25 |
*/
|
|
|
26 |
H5PLibraryList.createLibraryList = function (libraries) {
|
|
|
27 |
var t = H5PAdminIntegration.l10n;
|
|
|
28 |
if (libraries.listData === undefined || libraries.listData.length === 0) {
|
|
|
29 |
return $('<div>' + t.NA + '</div>');
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
// Create table
|
|
|
33 |
var $table = H5PUtils.createTable(libraries.listHeaders);
|
|
|
34 |
$table.addClass('libraries');
|
|
|
35 |
|
|
|
36 |
// Add libraries
|
|
|
37 |
$.each (libraries.listData, function (index, library) {
|
|
|
38 |
var $libraryRow = H5PUtils.createTableRow([
|
|
|
39 |
library.title,
|
|
|
40 |
'<input class="h5p-admin-restricted" type="checkbox"/>',
|
|
|
41 |
{
|
|
|
42 |
text: library.numContent,
|
|
|
43 |
class: 'h5p-admin-center'
|
|
|
44 |
},
|
|
|
45 |
{
|
|
|
46 |
text: library.numContentDependencies,
|
|
|
47 |
class: 'h5p-admin-center'
|
|
|
48 |
},
|
|
|
49 |
{
|
|
|
50 |
text: library.numLibraryDependencies,
|
|
|
51 |
class: 'h5p-admin-center'
|
|
|
52 |
},
|
|
|
53 |
'<div class="h5p-admin-buttons-wrapper">' +
|
|
|
54 |
'<button class="h5p-admin-upgrade-library"></button>' +
|
|
|
55 |
(library.detailsUrl ? '<button class="h5p-admin-view-library" title="' + t.viewLibrary + '"></button>' : '') +
|
|
|
56 |
(library.deleteUrl ? '<button class="h5p-admin-delete-library"></button>' : '') +
|
|
|
57 |
'</div>'
|
|
|
58 |
]);
|
|
|
59 |
|
|
|
60 |
H5PLibraryList.addRestricted($('.h5p-admin-restricted', $libraryRow), library.restrictedUrl, library.restricted);
|
|
|
61 |
|
|
|
62 |
var hasContent = !(library.numContent === '' || library.numContent === 0);
|
|
|
63 |
if (library.upgradeUrl === null) {
|
|
|
64 |
$('.h5p-admin-upgrade-library', $libraryRow).remove();
|
|
|
65 |
}
|
|
|
66 |
else if (library.upgradeUrl === false || !hasContent) {
|
|
|
67 |
$('.h5p-admin-upgrade-library', $libraryRow).attr('disabled', true);
|
|
|
68 |
}
|
|
|
69 |
else {
|
|
|
70 |
$('.h5p-admin-upgrade-library', $libraryRow).attr('title', t.upgradeLibrary).click(function () {
|
|
|
71 |
window.location.href = library.upgradeUrl;
|
|
|
72 |
});
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// Open details view when clicked
|
|
|
76 |
$('.h5p-admin-view-library', $libraryRow).on('click', function () {
|
|
|
77 |
window.location.href = library.detailsUrl;
|
|
|
78 |
});
|
|
|
79 |
|
|
|
80 |
var $deleteButton = $('.h5p-admin-delete-library', $libraryRow);
|
|
|
81 |
if (libraries.notCached !== undefined ||
|
|
|
82 |
hasContent ||
|
|
|
83 |
(library.numContentDependencies !== '' &&
|
|
|
84 |
library.numContentDependencies !== 0) ||
|
|
|
85 |
(library.numLibraryDependencies !== '' &&
|
|
|
86 |
library.numLibraryDependencies !== 0)) {
|
|
|
87 |
// Disabled delete if content.
|
|
|
88 |
$deleteButton.attr('disabled', true);
|
|
|
89 |
}
|
|
|
90 |
else {
|
|
|
91 |
// Go to delete page om click.
|
|
|
92 |
$deleteButton.attr('title', t.deleteLibrary).on('click', function () {
|
|
|
93 |
window.location.href = library.deleteUrl;
|
|
|
94 |
});
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
$table.append($libraryRow);
|
|
|
98 |
});
|
|
|
99 |
|
|
|
100 |
return $table;
|
|
|
101 |
};
|
|
|
102 |
|
|
|
103 |
H5PLibraryList.addRestricted = function ($checkbox, url, selected) {
|
|
|
104 |
if (selected === null) {
|
|
|
105 |
$checkbox.remove();
|
|
|
106 |
}
|
|
|
107 |
else {
|
|
|
108 |
$checkbox.change(function () {
|
|
|
109 |
$checkbox.attr('disabled', true);
|
|
|
110 |
|
|
|
111 |
$.ajax({
|
|
|
112 |
dataType: 'json',
|
|
|
113 |
url: url,
|
|
|
114 |
cache: false
|
|
|
115 |
}).fail(function () {
|
|
|
116 |
$checkbox.attr('disabled', false);
|
|
|
117 |
|
|
|
118 |
// Reset
|
|
|
119 |
$checkbox.attr('checked', !$checkbox.is(':checked'));
|
|
|
120 |
}).done(function (result) {
|
|
|
121 |
url = result.url;
|
|
|
122 |
$checkbox.attr('disabled', false);
|
|
|
123 |
});
|
|
|
124 |
});
|
|
|
125 |
|
|
|
126 |
if (selected) {
|
|
|
127 |
$checkbox.attr('checked', true);
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
};
|
|
|
131 |
|
|
|
132 |
// Initialize me:
|
|
|
133 |
$(document).ready(function () {
|
|
|
134 |
if (!H5PLibraryList.initialized) {
|
|
|
135 |
H5PLibraryList.initialized = true;
|
|
|
136 |
H5PLibraryList.init();
|
|
|
137 |
}
|
|
|
138 |
});
|
|
|
139 |
|
|
|
140 |
})(H5P.jQuery);
|