1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @author Andreas Grabs <moodle@grabs-edv.de>
|
|
|
19 |
* @copyright 2024 Andreas Grabs
|
|
|
20 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
define(['jquery', 'core/fragment', 'core/templates', 'core/notification'], function(
|
|
|
24 |
$, fragment, templates, notification) {
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Class constructor
|
|
|
28 |
* @param {string} contentcontainerselector
|
|
|
29 |
* @param {string} fragmentcall
|
|
|
30 |
* @param {object} serviceparams
|
|
|
31 |
* @param {int} contextid
|
|
|
32 |
*/
|
|
|
33 |
var ContentLoader = function(contentcontainerselector, fragmentcall, serviceparams, contextid) {
|
|
|
34 |
this.contentcontainerselector = contentcontainerselector;
|
|
|
35 |
this.fragmentcall = fragmentcall;
|
|
|
36 |
if (serviceparams === undefined) {
|
|
|
37 |
this.serviceparams = { };
|
|
|
38 |
} else {
|
|
|
39 |
this.serviceparams = serviceparams;
|
|
|
40 |
}
|
|
|
41 |
this.contextid = contextid;
|
|
|
42 |
this.isshown = false;
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Load content by the fragment api
|
|
|
47 |
* @param {string} adjacentPosition Can be on of the following values: afterbegin, afterend, beforebegin, beforeend.
|
|
|
48 |
* @returns {Promise}
|
|
|
49 |
*/
|
|
|
50 |
ContentLoader.prototype.loadContent = function(adjacentPosition) {
|
|
|
51 |
var _this = this; // We have to save this because the context in the promise is another.
|
|
|
52 |
// Show a spinner while loading the table if not disabled.
|
|
|
53 |
if (_this.disablespinner === undefined) {
|
|
|
54 |
$(_this.contentcontainerselector).html('');
|
|
|
55 |
var spinnerhtml = '<div class="text-center" id="myspinner"><i class="fa fa-spinner fa-2x fa-spin"></i></div>';
|
|
|
56 |
$(_this.contentcontainerselector).prepend(spinnerhtml);
|
|
|
57 |
}
|
|
|
58 |
var fragmentpromise = fragment.loadFragment('mod_unilabel', _this.fragmentcall, _this.contextid, _this.serviceparams);
|
|
|
59 |
return fragmentpromise.then(function(html, js) {
|
|
|
60 |
var container = document.querySelector(_this.contentcontainerselector);
|
|
|
61 |
container.insertAdjacentHTML(adjacentPosition, html);
|
|
|
62 |
if (js) {
|
|
|
63 |
templates.runTemplateJS(js);
|
|
|
64 |
}
|
|
|
65 |
_this.isshown = true;
|
|
|
66 |
$('#myspinner').remove();
|
|
|
67 |
return true;
|
|
|
68 |
}).fail(notification.exception);
|
|
|
69 |
};
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Load html content on the given event.
|
|
|
73 |
* @param {string} triggerselector
|
|
|
74 |
* @param {string} triggerevent
|
|
|
75 |
* @param {string} adjacentPosition Can be on of the following values: afterbegin, afterend, beforebegin, beforeend.
|
|
|
76 |
*/
|
|
|
77 |
ContentLoader.prototype.autoload = function(triggerselector, triggerevent, adjacentPosition) {
|
|
|
78 |
var _this = this;
|
|
|
79 |
$(triggerselector).on(triggerevent, function() {
|
|
|
80 |
if (_this.isshown == false) {
|
|
|
81 |
_this.loadContent(adjacentPosition);
|
|
|
82 |
_this.isshown = true;
|
|
|
83 |
}
|
|
|
84 |
});
|
|
|
85 |
};
|
|
|
86 |
|
|
|
87 |
return ContentLoader;
|
|
|
88 |
|
|
|
89 |
});
|