Merge branch 'MDL-68977-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Jake Dallimore
2020-06-10 15:36:44 +08:00
12 changed files with 118 additions and 36 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
function _typeof(a){"@babel/helpers - typeof";if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){_typeof=function(a){return typeof a}}else{_typeof=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return _typeof(a)}define ("enrol_manual/quickenrolment",["exports","core_table/dynamic","core/str","core/toast","core/config","core/fragment","core/modal_events","core/modal_factory","core/notification","jquery","core/prefetch"],function(a,b,c,d,e,f,g,h,i,j,k){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;b=n(b);c=n(c);d=n(d);e=l(e);f=l(f);g=l(g);h=l(h);i=l(i);j=l(j);k=l(k);function l(a){return a&&a.__esModule?a:{default:a}}function m(){if("function"!=typeof WeakMap)return null;var a=new WeakMap;m=function(){return a};return a}function n(a){if(a&&a.__esModule){return a}if(null===a||"object"!==_typeof(a)&&"function"!=typeof a){return{default:a}}var b=m();if(b&&b.has(a)){return b.get(a)}var c={},d=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var e in a){if(Object.prototype.hasOwnProperty.call(a,e)){var f=d?Object.getOwnPropertyDescriptor(a,e):null;if(f&&(f.get||f.set)){Object.defineProperty(c,e,f)}else{c[e]=a[e]}}}c.default=a;if(b){b.set(a,c)}return c}function o(a,b){return t(a)||s(a,b)||q(a,b)||p()}function p(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function q(a,b){if(!a)return;if("string"==typeof a)return r(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);if("Object"===c&&a.constructor)c=a.constructor.name;if("Map"===c||"Set"===c)return Array.from(c);if("Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c))return r(a,b)}function r(a,b){if(null==b||b>a.length)b=a.length;for(var c=0,d=Array(b);c<b;c++){d[c]=a[c]}return d}function s(a,b){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(a)))return;var c=[],d=!0,e=!1,f=void 0;try{for(var g=a[Symbol.iterator](),h;!(d=(h=g.next()).done);d=!0){c.push(h.value);if(b&&c.length===b)break}}catch(a){e=!0;f=a}finally{try{if(!d&&null!=g["return"])g["return"]()}finally{if(e)throw f}}return c}function t(a){if(Array.isArray(a))return a}var u={cohortSelector:"#id_cohortlist",triggerButtons:".enrolusersbutton.enrol_manual_plugin [type='submit']",unwantedHiddenFields:"input[value='_qf__force_multiselect_submission']",buttonWrapper:"[data-region=\"wrapper\"]"},v=function(a){return f.default.loadFragment("enrol_manual","enrol_users_form",a,{})},w=function(a){var c=a.closest(u.buttonWrapper);return b.getTableFromId(c.dataset.tableUniqueid)},x=function(a){document.addEventListener("click",function(b){if(b.target.closest(u.triggerButtons)){b.preventDefault();y(w(b.target),a)}})},y=function(a,b){return h.default.create({type:h.default.types.SAVE_CANCEL,large:!0,title:c.get_string("enrolusers","enrol_manual"),body:v(b)}).then(function(b){b.getRoot().on(g.default.save,function(a){a.preventDefault();b.getRoot().find("form").submit()});b.getRoot().on("submit","form",function(c){c.preventDefault();z(a,b)});b.getRoot().on(g.default.hidden,function(){b.destroy()});return b}).then(function(a){a.show();return a}).then(function(a){a.setSaveButtonText(c.get_string("enrolusers","enrol_manual"));a.getBodyPromise().then(function(b){if(b.get(0).querySelector(u.cohortSelector)){a.setSaveButtonText(c.get_string("enroluserscohorts","enrol_manual"))}return b}).catch();return a}).catch(i.default.exception)},z=function(a,f){var g=f.getRoot().find("form");g.get(0).querySelectorAll(u.unwantedHiddenFields).forEach(function(a){return a.remove()});f.hide();f.destroy();j.default.ajax("".concat(e.default.wwwroot,"/enrol/manual/ajax.php?").concat(g.serialize()),{type:"GET",processData:!1,contentType:"application/json"}).then(function(a){if(a.error){throw new Error(a.error)}return a.count}).then(function(d){return Promise.all([c.get_string("totalenrolledusers","enrol",d),b.refreshTableContent(a)])}).then(function(a){var b=o(a,1),c=b[0];return c}).then(function(a){return d.add(a)}).catch(function(a){i.default.addNotification({message:a.message,type:"error"})})},A=function(a){var b=a.contextid;x(b);k.default.prefetchStrings("enrol_manual",["enrolusers","enroluserscohorts"]);k.default.prefetchString("enrol","totalenrolledusers")};a.init=A});
function _typeof(a){"@babel/helpers - typeof";if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){_typeof=function(a){return typeof a}}else{_typeof=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return _typeof(a)}define ("enrol_manual/quickenrolment",["exports","core_table/dynamic","core/str","core/toast","core/config","core/fragment","core/modal_events","core/modal_factory","core/notification","jquery","core/pending","core/prefetch"],function(a,b,c,d,e,f,g,h,i,j,k,l){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;b=o(b);c=o(c);d=o(d);e=m(e);f=m(f);g=m(g);h=m(h);i=m(i);j=m(j);k=m(k);l=m(l);function m(a){return a&&a.__esModule?a:{default:a}}function n(){if("function"!=typeof WeakMap)return null;var a=new WeakMap;n=function(){return a};return a}function o(a){if(a&&a.__esModule){return a}if(null===a||"object"!==_typeof(a)&&"function"!=typeof a){return{default:a}}var b=n();if(b&&b.has(a)){return b.get(a)}var c={},d=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var e in a){if(Object.prototype.hasOwnProperty.call(a,e)){var f=d?Object.getOwnPropertyDescriptor(a,e):null;if(f&&(f.get||f.set)){Object.defineProperty(c,e,f)}else{c[e]=a[e]}}}c.default=a;if(b){b.set(a,c)}return c}function p(a,b){return u(a)||t(a,b)||r(a,b)||q()}function q(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(a,b){if(!a)return;if("string"==typeof a)return s(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);if("Object"===c&&a.constructor)c=a.constructor.name;if("Map"===c||"Set"===c)return Array.from(c);if("Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c))return s(a,b)}function s(a,b){if(null==b||b>a.length)b=a.length;for(var c=0,d=Array(b);c<b;c++){d[c]=a[c]}return d}function t(a,b){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(a)))return;var c=[],d=!0,e=!1,f=void 0;try{for(var g=a[Symbol.iterator](),h;!(d=(h=g.next()).done);d=!0){c.push(h.value);if(b&&c.length===b)break}}catch(a){e=!0;f=a}finally{try{if(!d&&null!=g["return"])g["return"]()}finally{if(e)throw f}}return c}function u(a){if(Array.isArray(a))return a}var v={cohortSelector:"#id_cohortlist",triggerButtons:".enrolusersbutton.enrol_manual_plugin [type='submit']",unwantedHiddenFields:"input[value='_qf__force_multiselect_submission']",buttonWrapper:"[data-region=\"wrapper\"]"},w=function(a){return f.default.loadFragment("enrol_manual","enrol_users_form",a,{})},x=function(a){var c=a.closest(v.buttonWrapper);return b.getTableFromId(c.dataset.tableUniqueid)},y=function(a){document.addEventListener("click",function(b){if(b.target.closest(v.triggerButtons)){b.preventDefault();z(x(b.target),a)}})},z=function(a,b){var d=new k.default("enrol_manual/quickenrolment:showModal");return h.default.create({type:h.default.types.SAVE_CANCEL,large:!0,title:c.get_string("enrolusers","enrol_manual"),body:w(b)}).then(function(b){b.getRoot().on(g.default.save,function(a){a.preventDefault();b.getRoot().find("form").submit()});b.getRoot().on("submit","form",function(c){c.preventDefault();A(a,b)});b.getRoot().on(g.default.hidden,function(){b.destroy()});return b}).then(function(a){a.show();return a}).then(function(a){a.setSaveButtonText(c.get_string("enrolusers","enrol_manual"));a.getBodyPromise().then(function(b){if(b.get(0).querySelector(v.cohortSelector)){a.setSaveButtonText(c.get_string("enroluserscohorts","enrol_manual"))}return b}).catch();d.resolve();return a}).catch(i.default.exception)},A=function(a,f){var g=f.getRoot().find("form");g.get(0).querySelectorAll(v.unwantedHiddenFields).forEach(function(a){return a.remove()});f.hide();f.destroy();j.default.ajax("".concat(e.default.wwwroot,"/enrol/manual/ajax.php?").concat(g.serialize()),{type:"GET",processData:!1,contentType:"application/json"}).then(function(a){if(a.error){throw new Error(a.error)}return a.count}).then(function(d){return Promise.all([c.get_string("totalenrolledusers","enrol",d),b.refreshTableContent(a)])}).then(function(a){var b=p(a,1),c=b[0];return c}).then(function(a){return d.add(a)}).catch(function(a){i.default.addNotification({message:a.message,type:"error"})})},B=function(a){var b=a.contextid;y(b);l.default.prefetchStrings("enrol_manual",["enrolusers","enroluserscohorts"]);l.default.prefetchString("enrol","totalenrolledusers")};a.init=B});
//# sourceMappingURL=quickenrolment.min.js.map
File diff suppressed because one or more lines are too long
+5
View File
@@ -29,6 +29,7 @@ import ModalEvents from 'core/modal_events';
import ModalFactory from 'core/modal_factory';
import Notification from 'core/notification';
import jQuery from 'jquery';
import Pending from 'core/pending';
import Prefetch from 'core/prefetch';
const Selectors = {
@@ -85,6 +86,8 @@ const registerEventListeners = contextId => {
* @returns {Promise}
*/
const showModal = (dynamicTable, contextId) => {
const pendingPromise = new Pending('enrol_manual/quickenrolment:showModal');
return ModalFactory.create({
type: ModalFactory.types.SAVE_CANCEL,
large: true,
@@ -130,6 +133,8 @@ const showModal = (dynamicTable, contextId) => {
})
.catch();
pendingPromise.resolve();
return modal;
})
.catch(Notification.exception);
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+36 -3
View File
@@ -123,9 +123,14 @@ export const updateTable = (tableRoot, {
checkTableIsDynamic(tableRoot);
const pendingPromise = new Pending('core_table/dynamic:updateTable');
let tableConfigChanged = false;
// Update sort fields.
if (sortBy && sortOrder) {
// Always update the table if requested and there were sort fields.
// These fields are only ever normalised in the backend.
tableConfigChanged = true;
const sortData = JSON.parse(tableRoot.dataset.tableSortData);
sortData.unshift({
sortby: sortBy,
@@ -136,33 +141,61 @@ export const updateTable = (tableRoot, {
// Update initials.
if (firstInitial !== null) {
if (tableRoot.dataset.tableFirstInitial !== firstInitial) {
tableConfigChanged = true;
}
tableRoot.dataset.tableFirstInitial = firstInitial;
}
if (lastInitial !== null) {
if (tableRoot.dataset.tableLastInitial !== lastInitial) {
tableConfigChanged = true;
}
tableRoot.dataset.tableLastInitial = lastInitial;
}
if (pageNumber !== null) {
if (tableRoot.dataset.tablePageNumber != pageNumber) {
tableConfigChanged = true;
}
tableRoot.dataset.tablePageNumber = pageNumber;
}
if (pageSize !== null) {
if (tableRoot.dataset.tablePageSize != pageSize) {
tableConfigChanged = true;
}
tableRoot.dataset.tablePageSize = pageSize;
}
// Update filters.
if (filters) {
tableRoot.dataset.tableFilters = JSON.stringify(filters);
const filterJson = JSON.stringify(filters);
if (tableRoot.dataset.tableFilters !== filterJson) {
tableConfigChanged = true;
}
tableRoot.dataset.tableFilters = filterJson;
}
// Update hidden columns.
if (hiddenColumns) {
tableRoot.dataset.tableHiddenColumns = JSON.stringify(hiddenColumns);
const columnJson = JSON.stringify(hiddenColumns);
if (tableRoot.dataset.tableHiddenColumns !== columnJson) {
tableConfigChanged = true;
}
tableRoot.dataset.tableHiddenColumns = columnJson;
}
// Refresh.
if (refreshContent) {
if (refreshContent && tableConfigChanged) {
return refreshTableContent(tableRoot)
.then(tableRoot => {
pendingPromise.resolve();
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core_user/local/participantsfilter/filter",["exports","core/form-autocomplete","./selectors","core/str"],function(a,b,c,d){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;b=e(b);c=e(c);function e(a){return a&&a.__esModule?a:{default:a}}function f(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function g(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var i=a.apply(b,c);function g(a){f(i,d,e,g,h,"next",a)}function h(a){f(i,d,e,g,h,"throw",a)}g(void 0)})}}function h(a,b){if(!(a instanceof b)){throw new TypeError("Cannot call a class as a function")}}function i(a,b){for(var c=0,d;c<b.length;c++){d=b[c];d.enumerable=d.enumerable||!1;d.configurable=!0;if("value"in d)d.writable=!0;Object.defineProperty(a,d.key,d)}}function j(a,b,c){if(b)i(a.prototype,b);if(c)i(a,c);return a}var k=function(a){return a.querySelectorAll(":checked")},l=function(){function a(b,c,d){h(this,a);this.filterType=b;this.rootNode=c;this.addValueSelector(d)}j(a,[{key:"tearDown",value:function tearDown(){}},{key:"addValueSelector",value:function(){var a=g(regeneratorRuntime.mark(function a(){var c=this,d,e,f,g=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:d=0<g.length&&g[0]!==void 0?g[0]:[];e=this.getFilterValueNode();e.innerHTML=this.getSourceDataForFilter().outerHTML;f=e.querySelector("select");d.forEach(function(a){var b=f.querySelector("option[value=\"".concat(a,"\"]"));if(b){b.selected=!0}else if(!c.showSuggestions){b=document.createElement("option");b.value=a;b.innerHTML=a;b.selected=!0;f.append(b)}});a.t0=b.default;a.t1=f;a.t2="1"==f.dataset.allowCustom;a.next=10;return this.placeholder;case 10:a.t3=a.sent;a.t4=this.showSuggestions;a.t5=!f.multiple;a.t6={items:"core_user/local/participantsfilter/autocomplete_selection_items",layout:"core_user/local/participantsfilter/autocomplete_layout",selection:"core_user/local/participantsfilter/autocomplete_selection"};a.t0.enhance.call(a.t0,a.t1,a.t2,null,a.t3,!1,a.t4,null,a.t5,a.t6);case 15:case"end":return a.stop();}}},a,this)}));return function addValueSelector(){return a.apply(this,arguments)}}()},{key:"getSourceDataForFilter",value:function getSourceDataForFilter(){var a=this.rootNode.querySelector(c.default.filterset.regions.datasource);return a.querySelector(c.default.data.fields.byName(this.filterType))}},{key:"getFilterValueNode",value:function getFilterValueNode(){return this.filterRoot.querySelector(c.default.filter.regions.values)}},{key:"placeholder",get:function get(){return(0,d.get_string)("placeholdertypeorselect","core_user")}},{key:"showSuggestions",get:function get(){return!0}},{key:"filterRoot",get:function get(){return this.rootNode.querySelector(c.default.filter.byName(this.filterType))}},{key:"name",get:function get(){return this.filterType}},{key:"jointype",get:function get(){return this.filterRoot.querySelector(c.default.filter.fields.join).value}},{key:"rawValues",get:function get(){var a=this.getFilterValueNode(),b=a.querySelector("select");return Object.values(k(b)).map(function(a){return a.value})}},{key:"values",get:function get(){return this.rawValues.map(function(a){return parseInt(a,10)})}},{key:"filterValue",get:function get(){return{name:this.name,jointype:this.jointype,values:this.values}}}]);return a}();a.default=l;return a.default});
define ("core_user/local/participantsfilter/filter",["exports","core/form-autocomplete","./selectors","core/str"],function(a,b,c,d){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;b=e(b);c=e(c);function e(a){return a&&a.__esModule?a:{default:a}}function f(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function g(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var i=a.apply(b,c);function g(a){f(i,d,e,g,h,"next",a)}function h(a){f(i,d,e,g,h,"throw",a)}g(void 0)})}}function h(a,b){if(!(a instanceof b)){throw new TypeError("Cannot call a class as a function")}}function i(a,b){for(var c=0,d;c<b.length;c++){d=b[c];d.enumerable=d.enumerable||!1;d.configurable=!0;if("value"in d)d.writable=!0;Object.defineProperty(a,d.key,d)}}function j(a,b,c){if(b)i(a.prototype,b);if(c)i(a,c);return a}var k=function(a){return a.querySelectorAll(":checked")},l=function(){function a(b,c,d){h(this,a);this.filterType=b;this.rootNode=c;this.addValueSelector(d)}j(a,[{key:"tearDown",value:function tearDown(){}},{key:"addValueSelector",value:function(){var a=g(regeneratorRuntime.mark(function a(){var c=this,d,e,f,g=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:d=0<g.length&&g[0]!==void 0?g[0]:[];e=this.getFilterValueNode();e.innerHTML=this.getSourceDataForFilter().outerHTML;f=e.querySelector("select");d.forEach(function(a){var b=f.querySelector("option[value=\"".concat(a,"\"]"));if(b){b.selected=!0}else if(!c.showSuggestions){b=document.createElement("option");b.value=a;b.innerHTML=a;b.selected=!0;f.append(b)}});a.t0=b.default;a.t1=f;a.t2="1"==f.dataset.allowCustom;a.next=10;return this.placeholder;case 10:a.t3=a.sent;a.t4=this.showSuggestions;a.t5=!f.multiple;a.t6={items:"core_user/local/participantsfilter/autocomplete_selection_items",layout:"core_user/local/participantsfilter/autocomplete_layout",selection:"core_user/local/participantsfilter/autocomplete_selection"};a.t0.enhance.call(a.t0,a.t1,a.t2,null,a.t3,!1,a.t4,null,a.t5,a.t6);case 15:case"end":return a.stop();}}},a,this)}));return function addValueSelector(){return a.apply(this,arguments)}}()},{key:"getSourceDataForFilter",value:function getSourceDataForFilter(){var a=this.rootNode.querySelector(c.default.filterset.regions.datasource);return a.querySelector(c.default.data.fields.byName(this.filterType))}},{key:"getFilterValueNode",value:function getFilterValueNode(){return this.filterRoot.querySelector(c.default.filter.regions.values)}},{key:"placeholder",get:function get(){return(0,d.get_string)("placeholdertypeorselect","core_user")}},{key:"showSuggestions",get:function get(){return!0}},{key:"filterRoot",get:function get(){return this.rootNode.querySelector(c.default.filter.byName(this.filterType))}},{key:"name",get:function get(){return this.filterType}},{key:"jointype",get:function get(){return parseInt(this.filterRoot.querySelector(c.default.filter.fields.join).value,10)}},{key:"rawValues",get:function get(){var a=this.getFilterValueNode(),b=a.querySelector("select");return Object.values(k(b)).map(function(a){return a.value})}},{key:"values",get:function get(){return this.rawValues.map(function(a){return parseInt(a,10)})}},{key:"filterValue",get:function get(){return{name:this.name,jointype:this.jointype,values:this.values}}}]);return a}();a.default=l;return a.default});
//# sourceMappingURL=filter.min.js.map
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -184,7 +184,7 @@ export default class {
* @returns {Number}
*/
get jointype() {
return this.filterRoot.querySelector(Selectors.filter.fields.join).value;
return parseInt(this.filterRoot.querySelector(Selectors.filter.fields.join).value, 10);
}
/**
+68 -24
View File
@@ -27,6 +27,7 @@ import * as DynamicTable from 'core_table/dynamic';
import GenericFilter from './local/participantsfilter/filter';
import {get_strings as getStrings} from 'core/str';
import Notification from 'core/notification';
import Pending from 'core/pending';
import Selectors from './local/participantsfilter/selectors';
import Templates from 'core/templates';
@@ -57,6 +58,8 @@ export const init = participantsRegionId => {
* @return {Promise}
*/
const addFilterRow = () => {
const pendingPromise = new Pending('core_user/participantsfilter:addFilterRow');
const rownum = 1 + getFilterRegion().querySelectorAll(Selectors.filter.region).length;
return Templates.renderForPromise('core_user/local/participantsfilter/filterrow', {"rownumber": rownum})
.then(({html, js}) => {
@@ -86,6 +89,11 @@ export const init = participantsRegionId => {
return filterRow;
})
.then(result => {
pendingPromise.resolve();
return result;
})
.catch(Notification.exception);
};
@@ -148,14 +156,15 @@ export const init = participantsRegionId => {
* that it is replaced instead of being removed.
*
* @param {HTMLElement} filterRow
* @param {Bool} refreshContent Whether to refresh the table content when removing
*/
const removeOrReplaceFilterRow = filterRow => {
const removeOrReplaceFilterRow = (filterRow, refreshContent) => {
const filterCount = getFilterRegion().querySelectorAll(Selectors.filter.region).length;
if (filterCount === 1) {
replaceFilterRow(filterRow);
replaceFilterRow(filterRow, refreshContent);
} else {
removeFilterRow(filterRow);
removeFilterRow(filterRow, refreshContent);
}
};
@@ -163,8 +172,12 @@ export const init = participantsRegionId => {
* Remove the specified filter row and associated class.
*
* @param {HTMLElement} filterRow
* @param {Bool} refreshContent Whether to refresh the table content when removing
*/
const removeFilterRow = async filterRow => {
const removeFilterRow = async(filterRow, refreshContent = true) => {
const filterType = filterRow.querySelector(Selectors.filter.fields.type);
const hasFilterValue = !!filterType.value;
// Remove the filter object.
removeFilterObject(filterRow.dataset.filterType);
@@ -174,8 +187,10 @@ export const init = participantsRegionId => {
// Update the list of available filter types.
updateFiltersOptions();
// Refresh the table.
updateTableFromFilter();
if (hasFilterValue && refreshContent) {
// Refresh the table if there was any content in this row.
updateTableFromFilter();
}
// Update filter fieldset legends.
const filterLegends = await getAvailableFilterLegends();
@@ -190,10 +205,11 @@ export const init = participantsRegionId => {
* Replace the specified filter row with a new one.
*
* @param {HTMLElement} filterRow
* @param {Bool} refreshContent Whether to refresh the table content when removing
* @param {Number} rowNum The number used to label the filter fieldset legend (eg Row 1). Defaults to 1 (the first filter).
* @return {Promise}
*/
const replaceFilterRow = (filterRow, rowNum = 1) => {
const replaceFilterRow = (filterRow, refreshContent = true, rowNum = 1) => {
// Remove the filter object.
removeFilterObject(filterRow.dataset.filterType);
@@ -227,9 +243,11 @@ export const init = participantsRegionId => {
})
.then(filterRow => {
// Refresh the table.
updateTableFromFilter();
return filterRow;
if (refreshContent) {
return updateTableFromFilter();
} else {
return filterRow;
}
})
.catch(Notification.exception);
};
@@ -257,11 +275,18 @@ export const init = participantsRegionId => {
* @returns {Promise}
*/
const removeAllFilters = () => {
const pendingPromise = new Pending('core_user/participantsfilter:setFilterFromConfig');
const filters = getFilterRegion().querySelectorAll(Selectors.filter.region);
filters.forEach(filterRow => removeOrReplaceFilterRow(filterRow));
filters.forEach(filterRow => removeOrReplaceFilterRow(filterRow, false));
// Refresh the table.
return updateTableFromFilter();
return updateTableFromFilter()
.then(result => {
pendingPromise.resolve();
return result;
});
};
/**
@@ -272,7 +297,7 @@ export const init = participantsRegionId => {
filters.forEach(filterRow => {
const filterType = filterRow.querySelector(Selectors.filter.fields.type);
if (!filterType.value) {
removeOrReplaceFilterRow(filterRow);
removeOrReplaceFilterRow(filterRow, false);
}
});
};
@@ -322,13 +347,14 @@ export const init = participantsRegionId => {
* @param {Object} config
* @param {Number} config.jointype
* @param {Object} config.filters
* @returns {Promise}
*/
const setFilterFromConfig = config => {
const filterConfig = Object.entries(config.filters);
if (!filterConfig.length) {
// There are no filters to set from.
return;
return Promise.resolve();
}
// Set the main join type.
@@ -337,7 +363,7 @@ export const init = participantsRegionId => {
const filterPromises = filterConfig.map(([filterType, filterData]) => {
if (filterType === 'courseid') {
// The courseid is a special case.
return Promise.resolve();
return false;
}
const filterValues = filterData.values;
@@ -345,18 +371,21 @@ export const init = participantsRegionId => {
if (!filterValues.length) {
// There are no values for this filter.
// Skip it.
return Promise.resolve();
return false;
}
return addFilterRow().then(([filterRow]) => addFilter(filterRow, filterType, filterValues));
});
}).filter(promise => promise);
Promise.all(filterPromises).then(() => {
if (!filterPromises.length) {
return Promise.resolve();
}
return Promise.all(filterPromises).then(() => {
return removeEmptyFilters();
})
.then(updateFiltersOptions)
.then(updateTableFromFilter)
.catch();
.then(updateTableFromFilter);
};
/**
@@ -365,13 +394,25 @@ export const init = participantsRegionId => {
* @return {Promise}
*/
const updateTableFromFilter = () => {
const pendingPromise = new Pending('core_user/participantsfilter:updateTableFromFilter');
const filters = {};
Object.values(activeFilters).forEach(filter => {
filters[filter.filterValue.name] = filter.filterValue;
});
return DynamicTable.setFilters(
DynamicTable.getTableFromId(filterSet.dataset.tableRegion),
{
filters: Object.values(activeFilters).map(filter => filter.filterValue),
jointype: filterSet.querySelector(Selectors.filterset.fields.join).value,
jointype: parseInt(filterSet.querySelector(Selectors.filterset.fields.join).value, 10),
filters,
}
)
.then(result => {
pendingPromise.resolve();
return result;
})
.catch(Notification.exception);
};
@@ -428,7 +469,7 @@ export const init = participantsRegionId => {
if (e.target.closest(Selectors.filter.actions.remove)) {
e.preventDefault();
removeOrReplaceFilterRow(e.target.closest(Selectors.filter.region));
removeOrReplaceFilterRow(e.target.closest(Selectors.filter.region), true);
}
});
@@ -449,7 +490,10 @@ export const init = participantsRegionId => {
const tableRoot = DynamicTable.getTableFromId(filterSet.dataset.tableRegion);
const initialFilters = DynamicTable.getFilters(tableRoot);
if (initialFilters) {
const initialFilterPromise = new Pending('core_user/participantsfilter:setFilterFromConfig');
// Apply the initial filter configuration.
setFilterFromConfig(initialFilters);
setFilterFromConfig(initialFilters)
.then(() => initialFilterPromise.resolve())
.catch();
}
};