1 |
efrain |
1 |
define(['jquery', 'jqueryui', 'core/log', 'core/ajax', 'core/notification', 'core/modal_events',
|
|
|
2 |
'block_dash/preferences_modal', 'block_dash/datepicker', 'block_dash/select2', 'core/fragment', 'core/templates'],
|
|
|
3 |
function($, UI, Log, Ajax, Notification, ModalEvents, PreferencesModal, DatePicker, Select2, Fragment, Templates) {
|
|
|
4 |
|
|
|
5 |
var DashInstance = function(root, blockInstanceId, blockContextid, editing, istotara, pagelayout, pagecontext) {
|
|
|
6 |
this.root = $(root);
|
|
|
7 |
this.blockInstanceId = blockInstanceId;
|
|
|
8 |
this.blockContextid = blockContextid;
|
|
|
9 |
this.currentPage = 0;
|
|
|
10 |
this.blockPreferencesModal = null;
|
|
|
11 |
this.editing = editing;
|
|
|
12 |
this.sortField = null;
|
|
|
13 |
this.sortDirections = {};
|
|
|
14 |
this.isTotara = istotara;
|
|
|
15 |
this.pageLayout = pagelayout;
|
|
|
16 |
this.pageContext = pagecontext;
|
|
|
17 |
this.init();
|
|
|
18 |
};
|
|
|
19 |
|
|
|
20 |
DashInstance.prototype.BLOCK_CONTENT_SELECTOR = '.dash-block-content';
|
|
|
21 |
DashInstance.prototype.FILTER_FORM_SELECTOR = '.filter-form';
|
|
|
22 |
|
|
|
23 |
DashInstance.prototype.init = function() {
|
|
|
24 |
|
|
|
25 |
Log.debug('Initializing dash instance', this);
|
|
|
26 |
|
|
|
27 |
// Select datasource for configuration.
|
|
|
28 |
if (this.getRoot().find('.dash-configuration-form').length > 0) {
|
|
|
29 |
|
|
|
30 |
this.getRoot().find('.dash-configuration-form').removeClass('hide');
|
|
|
31 |
this.getRoot()
|
|
|
32 |
.find('[data-target="subsource-config"] [type=radio], [name="config_data_source_idnumber"]')
|
|
|
33 |
.addClass('norefresh');
|
|
|
34 |
|
|
|
35 |
// Hide the preference link for others.
|
|
|
36 |
this.getRoot().find('.dash-edit-preferences').hide();
|
|
|
37 |
|
|
|
38 |
// Select the parent datasource for the sub config.
|
|
|
39 |
this.getRoot().on('change', '[data-target="subsource-config"] [type=radio]', function(e) {
|
|
|
40 |
var subConfig;
|
|
|
41 |
if (e.target.closest('[data-target="subsource-config"]')) {
|
|
|
42 |
subConfig = e.target.closest('[data-target="subsource-config"]');
|
|
|
43 |
if (subConfig.parentNode !== null) {
|
|
|
44 |
var dataSource = subConfig.parentNode.querySelector('[name="config_data_source_idnumber"]');
|
|
|
45 |
dataSource.click(); // = true;
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
}.bind(this));
|
|
|
49 |
|
|
|
50 |
this.getRoot().find('.dash-configuration-form [name="config_data_source_idnumber"]').on('change', function() {
|
|
|
51 |
|
|
|
52 |
var dataSource = this.getRoot().find('.dash-configuration-form');
|
|
|
53 |
var formData = $(dataSource).find('form').serialize();
|
|
|
54 |
|
|
|
55 |
// Now we can continue...
|
|
|
56 |
Ajax.call([{
|
|
|
57 |
methodname: 'block_dash_submit_preferences_form',
|
|
|
58 |
args: {
|
|
|
59 |
contextid: this.blockContextid,
|
|
|
60 |
jsonformdata: JSON.stringify(formData)
|
|
|
61 |
},
|
|
|
62 |
done: function() {
|
|
|
63 |
// Hide the preference link for others.
|
|
|
64 |
this.getRoot().find('.dash-edit-preferences').show();
|
|
|
65 |
this.refresh();
|
|
|
66 |
}.bind(this),
|
|
|
67 |
}])[0].fail(Notification.exception);
|
|
|
68 |
}.bind(this));
|
|
|
69 |
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
this.initDatePickers();
|
|
|
73 |
this.initSelect2();
|
|
|
74 |
|
|
|
75 |
if (this.editing) {
|
|
|
76 |
this.blockPreferencesModal = new PreferencesModal(this.getRoot().find('.dash-edit-preferences'),
|
|
|
77 |
this.blockContextid, function() {
|
|
|
78 |
// Preferences changed, go back to first page.
|
|
|
79 |
this.currentPage = 0;
|
|
|
80 |
this.refresh();
|
|
|
81 |
}.bind(this));
|
|
|
82 |
}
|
|
|
83 |
this.getRoot().on('change', 'select:not(.norefresh), input:not(.select2-search__field, .norefresh)',
|
|
|
84 |
function(e) {
|
|
|
85 |
e.preventDefault();
|
|
|
86 |
|
|
|
87 |
Log.debug('Submitting filter form');
|
|
|
88 |
Log.debug(e);
|
|
|
89 |
Log.debug($(e.target).serializeArray());
|
|
|
90 |
|
|
|
91 |
// Filter results, go back to first page.
|
|
|
92 |
this.currentPage = 0;
|
|
|
93 |
this.refresh();
|
|
|
94 |
}.bind(this));
|
|
|
95 |
|
|
|
96 |
this.getRoot().on('submit', '.downloadreport .reportoption form', function(e) {
|
|
|
97 |
e.preventDefault();
|
|
|
98 |
let params = new URLSearchParams($(e.target).serialize());
|
|
|
99 |
let sortDirection = null;
|
|
|
100 |
if (this.sortField && this.sortDirections.hasOwnProperty(this.sortField)) {
|
|
|
101 |
sortDirection = this.sortDirections[this.sortField];
|
|
|
102 |
}
|
|
|
103 |
var args = {
|
|
|
104 |
'download' : params.get('download'),
|
|
|
105 |
"block_instance_id": this.blockInstanceId,
|
|
|
106 |
"filter_form_data": JSON.stringify(this.getFilterForm().serializeArray()),
|
|
|
107 |
"page": this.currentPage,
|
|
|
108 |
"sort_field": this.sortField,
|
|
|
109 |
"sort_direction": sortDirection,
|
|
|
110 |
};
|
|
|
111 |
let url = M.cfg.wwwroot + '/blocks/dash/download.php';
|
|
|
112 |
// Create a new form element.
|
|
|
113 |
const form = $('<form>', {
|
|
|
114 |
method: 'post',
|
|
|
115 |
action: url,
|
|
|
116 |
target: '_self',
|
|
|
117 |
});
|
|
|
118 |
|
|
|
119 |
// Add input fields with your data
|
|
|
120 |
form.append($('<input>', { type: 'hidden', name: 'download', value: args.download }));
|
|
|
121 |
form.append($('<input>', { type: 'hidden', name: 'block_instance_id', value: args.block_instance_id }));
|
|
|
122 |
form.append($('<input>', { type: 'hidden', name: 'filter_form_data', value: args.filter_form_data }));
|
|
|
123 |
form.append($('<input>', { type: 'hidden', name: 'page', value: args.page }));
|
|
|
124 |
form.append($('<input>', { type: 'hidden', name: 'sort_field', value: args.sort_field }));
|
|
|
125 |
form.append($('<input>', { type: 'hidden', name: 'sort_direction', value: args.sort_direction }));
|
|
|
126 |
|
|
|
127 |
// Append the form to the body and submit it
|
|
|
128 |
form.appendTo('body').submit();
|
|
|
129 |
|
|
|
130 |
}.bind(this));
|
|
|
131 |
|
|
|
132 |
// Adding support for tab filters.
|
|
|
133 |
this.getRoot().on('click', 'button.tab-filter', function(e) {
|
|
|
134 |
e.preventDefault();
|
|
|
135 |
var elem = $(e.currentTarget);
|
|
|
136 |
|
|
|
137 |
var value = $(e.currentTarget).attr('data-value');
|
|
|
138 |
var select = $(e.currentTarget).parents('.sort-group').find('select');
|
|
|
139 |
var prevVal = select.val();
|
|
|
140 |
// Remove checked is already checked.
|
|
|
141 |
if (elem.hasClass('is-checked')) {
|
|
|
142 |
elem.removeClass('is-checked');
|
|
|
143 |
elem.removeClass('btn-primary');
|
|
|
144 |
if (select.prop('multiple')) {
|
|
|
145 |
var index = prevVal.indexOf(value.toString());
|
|
|
146 |
if (index > -1) {
|
|
|
147 |
prevVal.splice(index, 1);
|
|
|
148 |
value = prevVal;
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
} else {
|
|
|
152 |
elem.addClass('is-checked');
|
|
|
153 |
elem.addClass('btn-primary');
|
|
|
154 |
if (select.prop('multiple')) {
|
|
|
155 |
prevVal.push(value);
|
|
|
156 |
value = prevVal;
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
// Set value for select.
|
|
|
160 |
select.val(value);
|
|
|
161 |
// Filter results, go back to first page.
|
|
|
162 |
this.currentPage = 0;
|
|
|
163 |
this.refresh();
|
|
|
164 |
}.bind(this));
|
|
|
165 |
|
|
|
166 |
this.getBlockContentArea().on('click', '.page-link', function(e) {
|
|
|
167 |
e.preventDefault();
|
|
|
168 |
this.currentPage = $(e.target).data('page');
|
|
|
169 |
this.refresh();
|
|
|
170 |
}.bind(this));
|
|
|
171 |
|
|
|
172 |
this.getBlockContentArea().on('click', '.dash-sort', function(e) {
|
|
|
173 |
const $target = $(e.target);
|
|
|
174 |
this.sortField = $target.data('sort');
|
|
|
175 |
|
|
|
176 |
// Set sorting to asc by default.
|
|
|
177 |
if (!this.sortDirections.hasOwnProperty(this.sortField)) {
|
|
|
178 |
this.sortDirections[this.sortField] = 'asc';
|
|
|
179 |
} else {
|
|
|
180 |
// Toggle sort direction on field.
|
|
|
181 |
this.sortDirections[this.sortField] = this.sortDirections[this.sortField] === 'asc' ? 'desc' : 'asc';
|
|
|
182 |
}
|
|
|
183 |
this.refresh();
|
|
|
184 |
}.bind(this));
|
|
|
185 |
|
|
|
186 |
if (this.isTotara) {
|
|
|
187 |
this.setDynamicTable();
|
|
|
188 |
}
|
|
|
189 |
};
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Get the root element of this dash instance.
|
|
|
193 |
*
|
|
|
194 |
* @method getRoot
|
|
|
195 |
* @return {object} jQuery object
|
|
|
196 |
*/
|
|
|
197 |
DashInstance.prototype.getRoot = function() {
|
|
|
198 |
return this.root;
|
|
|
199 |
};
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Get the content element of this dash instance.
|
|
|
203 |
*
|
|
|
204 |
* @method getRoot
|
|
|
205 |
* @return {object} jQuery object
|
|
|
206 |
*/
|
|
|
207 |
DashInstance.prototype.getBlockContentArea = function() {
|
|
|
208 |
return this.getRoot().find(this.BLOCK_CONTENT_SELECTOR);
|
|
|
209 |
};
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* Get filter form element.
|
|
|
213 |
*
|
|
|
214 |
* @returns {object} jQuery object
|
|
|
215 |
*/
|
|
|
216 |
DashInstance.prototype.getFilterForm = function() {
|
|
|
217 |
return this.getRoot().find(this.FILTER_FORM_SELECTOR);
|
|
|
218 |
};
|
|
|
219 |
|
|
|
220 |
DashInstance.prototype.getBlockContent = function() {
|
|
|
221 |
let sortDirection = null;
|
|
|
222 |
if (this.sortField && this.sortDirections.hasOwnProperty(this.sortField)) {
|
|
|
223 |
sortDirection = this.sortDirections[this.sortField];
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
var request = {
|
|
|
227 |
methodname: 'block_dash_get_block_content',
|
|
|
228 |
args: {
|
|
|
229 |
"block_instance_id": this.blockInstanceId,
|
|
|
230 |
"filter_form_data": JSON.stringify(this.getFilterForm().serializeArray()),
|
|
|
231 |
"page": this.currentPage,
|
|
|
232 |
"sort_field": this.sortField,
|
|
|
233 |
"sort_direction": sortDirection,
|
|
|
234 |
"pagelayout" : this.pageLayout,
|
|
|
235 |
"pagecontext" : this.pageContext,
|
|
|
236 |
}
|
|
|
237 |
};
|
|
|
238 |
return Ajax.call([request])[0];
|
|
|
239 |
};
|
|
|
240 |
|
|
|
241 |
DashInstance.prototype.refresh = function() {
|
|
|
242 |
this.getBlockContentArea().css('opacity', 0.5);
|
|
|
243 |
this.getBlockContent()
|
|
|
244 |
.then(function(response) {
|
|
|
245 |
this.getBlockContentArea().html(response.html);
|
|
|
246 |
this.getBlockContentArea().css('opacity', 1);
|
|
|
247 |
this.initDatePickers();
|
|
|
248 |
this.initSelect2();
|
|
|
249 |
if (response.scripts) {
|
|
|
250 |
Templates.runTemplateJS($(response.scripts).html() || response.scripts);
|
|
|
251 |
}
|
|
|
252 |
}.bind(this))
|
|
|
253 |
.catch(Notification.exception);
|
|
|
254 |
};
|
|
|
255 |
|
|
|
256 |
DashInstance.prototype.initDatePickers = function() {
|
|
|
257 |
this.getRoot().find('.datepicker').datepicker2({
|
|
|
258 |
autoclose: true,
|
|
|
259 |
format: "dd/mm/yyyy"
|
|
|
260 |
});
|
|
|
261 |
};
|
|
|
262 |
|
|
|
263 |
DashInstance.prototype.initSelect2 = function() {
|
|
|
264 |
this.getRoot().find('.select2').each(function(index, element) {
|
|
|
265 |
let placeholder = null;
|
|
|
266 |
if ($(element).find("option[value='-1']")) {
|
|
|
267 |
placeholder = {
|
|
|
268 |
id: '-1', // The value of the option.
|
|
|
269 |
text: $(element).find("option[value='-1']").text()
|
|
|
270 |
};
|
|
|
271 |
}
|
|
|
272 |
$(element).select2({
|
|
|
273 |
dropdownParent: this.getRoot(),
|
|
|
274 |
allowClear: true,
|
|
|
275 |
theme: 'bootstrap4',
|
|
|
276 |
placeholder: placeholder
|
|
|
277 |
}).on('select2:unselecting', function() {
|
|
|
278 |
$(this).data('unselecting', true);
|
|
|
279 |
}).on('select2:opening', function(e) {
|
|
|
280 |
if ($(this).data('unselecting')) {
|
|
|
281 |
$(this).removeData('unselecting');
|
|
|
282 |
e.preventDefault();
|
|
|
283 |
}
|
|
|
284 |
});
|
|
|
285 |
}.bind(this));
|
|
|
286 |
};
|
|
|
287 |
|
|
|
288 |
DashInstance.prototype.setDynamicTable = function() {
|
|
|
289 |
|
|
|
290 |
$('body').delegate('[data-table-dynamic="true"] thead th a', 'click', function(e) {
|
|
|
291 |
e.preventDefault();
|
|
|
292 |
updateTable($(this));
|
|
|
293 |
});
|
|
|
294 |
|
|
|
295 |
$('body').delegate('.modal-body .paging a', 'click', function(e) {
|
|
|
296 |
e.preventDefault();
|
|
|
297 |
updateTable($(this));
|
|
|
298 |
});
|
|
|
299 |
|
|
|
300 |
var updateTable = function(element) {
|
|
|
301 |
var table = element.parents('.modal-body').find('table');
|
|
|
302 |
|
|
|
303 |
var href = element.attr('href');
|
|
|
304 |
var params = new URL(href).searchParams;
|
|
|
305 |
var page = params.get('page');
|
|
|
306 |
var sortfield = params.get('tsort');
|
|
|
307 |
if (sortfield == '') {
|
|
|
308 |
sortfield = table.data('table-sort');
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
var tablehandler = table.data('table-handler');
|
|
|
312 |
var filter = table.data('table-filter');
|
|
|
313 |
var uniqueid = table.data('table-uniqueid');
|
|
|
314 |
var context = table.data('table-context');
|
|
|
315 |
|
|
|
316 |
var data = {
|
|
|
317 |
handler: tablehandler,
|
|
|
318 |
filter: filter,
|
|
|
319 |
uniqueid: uniqueid,
|
|
|
320 |
sort: sortfield,
|
|
|
321 |
page: page
|
|
|
322 |
};
|
|
|
323 |
|
|
|
324 |
Fragment.loadFragment('block_dash', 'loadtable', context, data).then((html, js) => {
|
|
|
325 |
$('.modal-body').html(html);
|
|
|
326 |
Templates.runTemplateJS(js);
|
|
|
327 |
return html;
|
|
|
328 |
}).catch(Notification.exception);
|
|
|
329 |
};
|
|
|
330 |
};
|
|
|
331 |
|
|
|
332 |
return DashInstance;
|
|
|
333 |
});
|