118 lines
No EOL
4.3 KiB
JavaScript
118 lines
No EOL
4.3 KiB
JavaScript
// Put this file in path/to/plugin/amd/src
|
|
// You can call it anything you like
|
|
|
|
define(['jquery', 'core/str', 'core/ajax', 'block_gradelevel/jscolor'], function ($, str, ajax, jscolor) {
|
|
let skill_id = null;
|
|
let $skill_table = null;
|
|
|
|
let self = {
|
|
init: function init() {
|
|
|
|
// Find skill table and skill id
|
|
$skill_table = $("table#level_config");
|
|
skill_id = Number($skill_table.attr('data-skill'));
|
|
|
|
if (!skill_id && skill_id !== 0 && skill_id !== '0') {
|
|
console.error("Cannot find configured skill id");
|
|
return;
|
|
}
|
|
|
|
// load and fill the level table
|
|
self.refresh();
|
|
|
|
// make sure all uint type numbers are not able to go below 0
|
|
$(document).on('change', "input[type=number].uint", function (evt) {
|
|
let $this = $(this);
|
|
if ($this.val() < 0) {
|
|
$this.val(0);
|
|
}
|
|
});
|
|
|
|
// On click handler for add level link
|
|
$("a[data-action=addlevel]").on('click', function (evt) {
|
|
self.add_levelrow(-255, '', '7F7F7F');
|
|
});
|
|
|
|
// On click handler for save changes button
|
|
$("button[data-action=savechanges]").on('click', self.submit);
|
|
|
|
|
|
},
|
|
add_levelrow: function (id, points, color) {
|
|
// create new row
|
|
let $newtr = $("<tr data-rowid='"+id+"'>"
|
|
+ "<td><input data-name='points' type='number' class='uint' value='"+points+"'></td>"
|
|
+ "<td><input type='text' data-name='color' class='jscolor' value='"+color+"'></td></tr>");
|
|
|
|
// apply jscolor to new row
|
|
$newtr.find("input.jscolor").each(function () {
|
|
let picker = new jscolor(this);
|
|
});
|
|
|
|
// and add to the body of the table
|
|
$skill_table.find("tbody").append($newtr);
|
|
},
|
|
refresh: function refresh() {
|
|
console.info("Attempting to refresh");
|
|
|
|
// perform refresh call
|
|
let promises = ajax.call([{
|
|
methodname: 'block_gradelevel_list_levels',
|
|
args: { skill_id: skill_id },
|
|
}]);
|
|
|
|
// and link promise returns to callbacks
|
|
promises[0].done(self.success_refill_table).fail(self.fail_report_exception);
|
|
},
|
|
submit: function submit(evt) {
|
|
let $table = $("table#level_config");
|
|
|
|
let level_data = [];
|
|
$table.find("tbody tr[data-rowid]").each(function () {
|
|
let $this = $(this);
|
|
let row = {
|
|
id: Number($this.attr('data-rowid')),
|
|
points: $this.find("input[data-name=points]").val(),
|
|
badgecolor: $this.find("input[data-name=color]").val(),
|
|
}
|
|
|
|
if (row.points === "") { row.points = -255; }
|
|
row.points = Number(row.points);
|
|
|
|
level_data.push(row);
|
|
});
|
|
|
|
//console.info("Attempting to submit", level_data);
|
|
|
|
// perform ajax call
|
|
let promises = ajax.call([{
|
|
methodname: 'block_gradelevel_submit_levels',
|
|
args: { skill_id: skill_id, levels: level_data },
|
|
}]);
|
|
|
|
// and link promise returns to callbacks
|
|
promises[0].done(self.success_refill_table).fail(self.fail_report_exception);
|
|
},
|
|
success_refill_table: function success_refill_table(response) {
|
|
//console.info("Response from webservice: ", response);
|
|
let $tbody = $skill_table.find('tbody');
|
|
$tbody.empty();
|
|
for (let idx in response) {
|
|
let lvl = response[idx];
|
|
//console.info("Level:", lvl);
|
|
self.add_levelrow(lvl.id, lvl.points, lvl.badgecolor);
|
|
}
|
|
},
|
|
fail_report_exception: function fail_report_exception(ex) {
|
|
if (ex.error != undefined) {
|
|
console.error("Error from webservice: ", ex.error, "\n" , ex.debuginfo, "\n---Stack trace---\n", ex.stacktrace, "\n", ex);
|
|
}
|
|
else {
|
|
console.error("Exception from webservice: ", ex.message, "\n", ex.debuginfo, "\n---Stack trace---\n", ex.backtrace, "\n", ex);
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
return self;
|
|
}); |