mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 09:26:35 +02:00
MDL-68506 core_table: Add a tableContentRefreshed event
This commit is contained in:
parent
9df4a4de18
commit
ca69d3875e
9 changed files with 69 additions and 6 deletions
2
lib/table/amd/build/dynamic.min.js
vendored
2
lib/table/amd/build/dynamic.min.js
vendored
|
@ -1,2 +1,2 @@
|
|||
define ("core_table/dynamic",["exports","core_table/local/dynamic/repository","core_table/local/dynamic/selectors"],function(a,b,c){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=a.setLastInitial=a.setFirstInitial=a.setPageSize=a.setPageNumber=a.setSortOrder=a.setFilters=a.updateTable=a.refreshTableContent=void 0;c=function(a){if(a&&a.__esModule){return a}else{var b={};if(null!=a){for(var c in a){if(Object.prototype.hasOwnProperty.call(a,c)){var d=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(a,c):{};if(d.get||d.set){Object.defineProperty(b,c,d)}else{b[c]=a[c]}}}}b.default=a;return b}}(c);function d(a){return g(a)||f(a)||e()}function e(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function f(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function g(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++){c[b]=a[b]}return c}}var h=!1,i=function(a){if(!a){throw new Error("The table specified is not a dynamic table and cannot be updated")}if(!a.matches(c.main.region)){throw new Error("The table specified is not a dynamic table and cannot be updated")}return!0},j=function(a){return JSON.parse(a.dataset.tableFilters)},k=function(a){var c=j(a);return(0,b.fetch)(a.dataset.tableComponent,a.dataset.tableHandler,a.dataset.tableUniqueid,{sortBy:a.dataset.tableSortBy,sortOrder:a.dataset.tableSortOrder,joinType:c.jointype,filters:c.filters,firstinitial:a.dataset.tableFirstInitial,lastinitial:a.dataset.tableLastInitial,pageNumber:a.dataset.tablePageNumber,pageSize:a.dataset.tablePageSize}).then(function(b){var c=document.createElement("div");c.innerHTML=b.html;a.replaceWith.apply(a,d(c.childNodes));return b})};a.refreshTableContent=k;var l=function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},c=b.sortBy,d=void 0===c?null:c,e=b.sortOrder,f=void 0===e?null:e,g=b.filters,h=void 0===g?null:g,j=b.firstInitial,l=void 0===j?null:j,m=b.lastInitial,n=void 0===m?null:m,o=b.pageNumber,p=void 0===o?null:o,q=b.pageSize,r=void 0===q?null:q,s=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;i(a);if(d&&f){a.dataset.tableSortBy=d;a.dataset.tableSortOrder=f}if(null!==l){a.dataset.tableFirstInitial=l}if(null!==n){a.dataset.tableLastInitial=n}if(null!==p){a.dataset.tablePageNumber=p}if(null!==r){a.dataset.tablePageSize=r}if(h){a.dataset.tableFilters=JSON.stringify(h)}if(s){return k(a)}else{return Promise.resolve()}};a.updateTable=l;a.setFilters=function setFilters(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return l(a,{filters:b},c)};var m=function(a,b,c){var d=3<arguments.length&&arguments[3]!==void 0?arguments[3]:!0;return l(a,{sortBy:b,sortOrder:c},d)};a.setSortOrder=m;var n=function(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return l(a,{pageNumber:b},c)};a.setPageNumber=n;a.setPageSize=function setPageSize(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return l(a,{pageSize:b,pageNumber:0},c)};var o=function(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return l(a,{firstInitial:b},c)};a.setFirstInitial=o;var p=function(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return l(a,{lastInitial:b},c)};a.setLastInitial=p;var q=function(){if(h){return}h=!0;document.addEventListener("click",function(a){var b=a.target.closest(c.main.region);if(!b){return}var d=a.target.closest(c.table.links.sortableColumn);if(d){a.preventDefault();m(b,d.dataset.sortby,d.dataset.sortorder)}var e=a.target.closest(c.initialsBar.links.firstInitial);if(null!==e){a.preventDefault();o(b,e.dataset.initial)}var f=a.target.closest(c.initialsBar.links.lastInitial);if(null!==f){a.preventDefault();p(b,f.dataset.initial)}var g=a.target.closest(c.paginationBar.links.pageItem);if(g){a.preventDefault();n(b,g.dataset.pageNumber)}})};a.init=q});
|
||||
define ("core_table/dynamic",["exports","core_table/local/dynamic/repository","core_table/local/dynamic/selectors","./local/dynamic/events"],function(a,b,c,d){"use strict";Object.defineProperty(a,"__esModule",{value:!0});Object.defineProperty(a,"Events",{enumerable:!0,get:function get(){return d.default}});a.getTableFromId=a.init=a.setLastInitial=a.setFirstInitial=a.setPageSize=a.setPageNumber=a.setSortOrder=a.setFilters=a.updateTable=a.refreshTableContent=void 0;c=function(a){if(a&&a.__esModule){return a}else{var b={};if(null!=a){for(var c in a){if(Object.prototype.hasOwnProperty.call(a,c)){var d=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(a,c):{};if(d.get||d.set){Object.defineProperty(b,c,d)}else{b[c]=a[c]}}}}b.default=a;return b}}(c);d=function(a){return a&&a.__esModule?a:{default:a}}(d);function e(a){return h(a)||g(a)||f()}function f(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function g(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function h(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++){c[b]=a[b]}return c}}var i=!1,j=function(a){if(!a){throw new Error("The table specified is not a dynamic table and cannot be updated")}if(!a.matches(c.main.region)){throw new Error("The table specified is not a dynamic table and cannot be updated")}return!0},k=function(a){return JSON.parse(a.dataset.tableFilters)},l=function(a){var c=k(a);return(0,b.fetch)(a.dataset.tableComponent,a.dataset.tableHandler,a.dataset.tableUniqueid,{sortBy:a.dataset.tableSortBy,sortOrder:a.dataset.tableSortOrder,joinType:c.jointype,filters:c.filters,firstinitial:a.dataset.tableFirstInitial,lastinitial:a.dataset.tableLastInitial,pageNumber:a.dataset.tablePageNumber,pageSize:a.dataset.tablePageSize}).then(function(b){var c=document.createElement("div");c.innerHTML=b.html;a.replaceWith.apply(a,e(c.childNodes));return s(a.dataset.tableUniqueid)}).then(function(a){a.dispatchEvent(new CustomEvent(d.default.tableContentRefreshed,{bubbles:!0}));return a})};a.refreshTableContent=l;var m=function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},c=b.sortBy,d=void 0===c?null:c,e=b.sortOrder,f=void 0===e?null:e,g=b.filters,h=void 0===g?null:g,i=b.firstInitial,k=void 0===i?null:i,m=b.lastInitial,n=void 0===m?null:m,o=b.pageNumber,p=void 0===o?null:o,q=b.pageSize,r=void 0===q?null:q,s=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;j(a);if(d&&f){a.dataset.tableSortBy=d;a.dataset.tableSortOrder=f}if(null!==k){a.dataset.tableFirstInitial=k}if(null!==n){a.dataset.tableLastInitial=n}if(null!==p){a.dataset.tablePageNumber=p}if(null!==r){a.dataset.tablePageSize=r}if(h){a.dataset.tableFilters=JSON.stringify(h)}if(s){return l(a)}else{return Promise.resolve(a)}};a.updateTable=m;a.setFilters=function setFilters(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return m(a,{filters:b},c)};var n=function(a,b,c){var d=3<arguments.length&&arguments[3]!==void 0?arguments[3]:!0;return m(a,{sortBy:b,sortOrder:c},d)};a.setSortOrder=n;var o=function(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return m(a,{pageNumber:b},c)};a.setPageNumber=o;a.setPageSize=function setPageSize(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return m(a,{pageSize:b,pageNumber:0},c)};var p=function(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return m(a,{firstInitial:b},c)};a.setFirstInitial=p;var q=function(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0;return m(a,{lastInitial:b},c)};a.setLastInitial=q;var r=function(){if(i){return}i=!0;document.addEventListener("click",function(a){var b=a.target.closest(c.main.region);if(!b){return}var d=a.target.closest(c.table.links.sortableColumn);if(d){a.preventDefault();n(b,d.dataset.sortby,d.dataset.sortorder)}var e=a.target.closest(c.initialsBar.links.firstInitial);if(null!==e){a.preventDefault();p(b,e.dataset.initial)}var f=a.target.closest(c.initialsBar.links.lastInitial);if(null!==f){a.preventDefault();q(b,f.dataset.initial)}var g=a.target.closest(c.paginationBar.links.pageItem);if(g){a.preventDefault();o(b,g.dataset.pageNumber)}})};a.init=r;var s=function(a){var b=document.querySelector(c.main.fromRegionId(a));if(!b){throw new Error("The table specified is not a dynamic table and cannot be updated")}return b};a.getTableFromId=s});
|
||||
//# sourceMappingURL=dynamic.min.js.map
|
||||
|
|
File diff suppressed because one or more lines are too long
2
lib/table/amd/build/local/dynamic/events.min.js
vendored
Normal file
2
lib/table/amd/build/local/dynamic/events.min.js
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
define ("core_table/local/dynamic/events",["exports"],function(a){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;var b={tableContentRefreshed:function prefixEventName(a){return"core_table/dynamic:".concat(a)}("tableContentRefreshed")};a.default=b;return a.default});
|
||||
//# sourceMappingURL=events.min.js.map
|
1
lib/table/amd/build/local/dynamic/events.min.js.map
Normal file
1
lib/table/amd/build/local/dynamic/events.min.js.map
Normal file
|
@ -0,0 +1 @@
|
|||
{"version":3,"sources":["../../../src/local/dynamic/events.js"],"names":["tableContentRefreshed","prefixEventName","eventName"],"mappings":"uJA0Be,CACXA,qBAAqB,CAHD,QAAlBC,CAAAA,eAAkB,CAAAC,CAAS,qCAA0BA,CAA1B,EAGN,CAAgB,uBAAhB,CADZ,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Dynamic table selectors.\n *\n * @module core_table/local/dynamic/events\n * @package core_table\n * @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nconst prefixEventName = eventName => `core_table/dynamic:${eventName}`;\n\nexport default {\n tableContentRefreshed: prefixEventName('tableContentRefreshed'),\n};\n"],"file":"events.min.js"}
|
|
@ -1,2 +1,2 @@
|
|||
define ("core_table/local/dynamic/selectors",["exports"],function(a){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;a.default={main:{region:"[data-region=\"core_table/dynamic\"]"},table:{links:{sortableColumn:"a[data-sortable=\"1\"]"}},initialsBar:{links:{firstInitial:".firstinitial [data-initial]",lastInitial:".lastinitial [data-initial]"}},paginationBar:{links:{pageItem:".pagination [data-page-number]"}}};return a.default});
|
||||
define ("core_table/local/dynamic/selectors",["exports"],function(a){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;a.default={main:{region:"[data-region=\"core_table/dynamic\"]",fromRegionId:function fromRegionId(a){return"[data-region=\"core_table/dynamic\"][data-table-uniqueid=\"".concat(a,"\"]")}},table:{links:{sortableColumn:"a[data-sortable=\"1\"]"}},initialsBar:{links:{firstInitial:".firstinitial [data-initial]",lastInitial:".lastinitial [data-initial]"}},paginationBar:{links:{pageItem:".pagination [data-page-number]"}}};return a.default});
|
||||
//# sourceMappingURL=selectors.min.js.map
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"sources":["../../../src/local/dynamic/selectors.js"],"names":["main","region","table","links","sortableColumn","initialsBar","firstInitial","lastInitial","paginationBar","pageItem"],"mappings":"8JAuBe,CACXA,IAAI,CAAE,CACFC,MAAM,CAAE,sCADN,CADK,CAIXC,KAAK,CAAE,CACHC,KAAK,CAAE,CACHC,cAAc,CAAE,wBADb,CADJ,CAJI,CASXC,WAAW,CAAE,CACTF,KAAK,CAAE,CACHG,YAAY,CAAE,8BADX,CAEHC,WAAW,CAAE,6BAFV,CADE,CATF,CAeXC,aAAa,CAAE,CACXL,KAAK,CAAE,CACHM,QAAQ,CAAE,gCADP,CADI,CAfJ,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Dynamic table selectors.\n *\n * @module core_table/selectors\n * @package core_table\n * @copyright 2020 Simey Lameze <simey@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nexport default {\n main: {\n region: '[data-region=\"core_table/dynamic\"]',\n },\n table: {\n links: {\n sortableColumn: 'a[data-sortable=\"1\"]',\n },\n },\n initialsBar: {\n links: {\n firstInitial: '.firstinitial [data-initial]',\n lastInitial: '.lastinitial [data-initial]',\n },\n },\n paginationBar: {\n links: {\n pageItem: '.pagination [data-page-number]'\n }\n },\n};\n"],"file":"selectors.min.js"}
|
||||
{"version":3,"sources":["../../../src/local/dynamic/selectors.js"],"names":["main","region","fromRegionId","regionId","table","links","sortableColumn","initialsBar","firstInitial","lastInitial","paginationBar","pageItem"],"mappings":"8JAuBe,CACXA,IAAI,CAAE,CACFC,MAAM,CAAE,sCADN,CAEFC,YAAY,CAAE,sBAAAC,CAAQ,6EAA+DA,CAA/D,QAFpB,CADK,CAKXC,KAAK,CAAE,CACHC,KAAK,CAAE,CACHC,cAAc,CAAE,wBADb,CADJ,CALI,CAUXC,WAAW,CAAE,CACTF,KAAK,CAAE,CACHG,YAAY,CAAE,8BADX,CAEHC,WAAW,CAAE,6BAFV,CADE,CAVF,CAgBXC,aAAa,CAAE,CACXL,KAAK,CAAE,CACHM,QAAQ,CAAE,gCADP,CADI,CAhBJ,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Dynamic table selectors.\n *\n * @module core_table/selectors\n * @package core_table\n * @copyright 2020 Simey Lameze <simey@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nexport default {\n main: {\n region: '[data-region=\"core_table/dynamic\"]',\n fromRegionId: regionId => `[data-region=\"core_table/dynamic\"][data-table-uniqueid=\"${regionId}\"]`,\n },\n table: {\n links: {\n sortableColumn: 'a[data-sortable=\"1\"]',\n },\n },\n initialsBar: {\n links: {\n firstInitial: '.firstinitial [data-initial]',\n lastInitial: '.lastinitial [data-initial]',\n },\n },\n paginationBar: {\n links: {\n pageItem: '.pagination [data-page-number]'\n }\n },\n};\n"],"file":"selectors.min.js"}
|
|
@ -23,6 +23,7 @@
|
|||
*/
|
||||
import {fetch as fetchTableData} from 'core_table/local/dynamic/repository';
|
||||
import * as Selectors from 'core_table/local/dynamic/selectors';
|
||||
import Events from './local/dynamic/events';
|
||||
|
||||
let watching = false;
|
||||
|
||||
|
@ -85,7 +86,14 @@ export const refreshTableContent = tableRoot => {
|
|||
placeholder.innerHTML = data.html;
|
||||
tableRoot.replaceWith(...placeholder.childNodes);
|
||||
|
||||
return data;
|
||||
// Update the tableRoot.
|
||||
return getTableFromId(tableRoot.dataset.tableUniqueid);
|
||||
}).then(tableRoot => {
|
||||
tableRoot.dispatchEvent(new CustomEvent(Events.tableContentRefreshed, {
|
||||
bubbles: true,
|
||||
}));
|
||||
|
||||
return tableRoot;
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -132,7 +140,7 @@ export const updateTable = (tableRoot, {
|
|||
if (refreshContent) {
|
||||
return refreshTableContent(tableRoot);
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
return Promise.resolve(tableRoot);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -249,3 +257,25 @@ export const init = () => {
|
|||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch the table via its table region id.
|
||||
*
|
||||
* @param {String} tableRegionId
|
||||
* @returns {HTMLElement}
|
||||
*/
|
||||
export const getTableFromId = tableRegionId => {
|
||||
const tableRoot = document.querySelector(Selectors.main.fromRegionId(tableRegionId));
|
||||
|
||||
|
||||
if (!tableRoot) {
|
||||
// The table is not a dynamic table.
|
||||
throw new Error("The table specified is not a dynamic table and cannot be updated");
|
||||
}
|
||||
|
||||
return tableRoot;
|
||||
};
|
||||
|
||||
export {
|
||||
Events
|
||||
};
|
||||
|
|
29
lib/table/amd/src/local/dynamic/events.js
Normal file
29
lib/table/amd/src/local/dynamic/events.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Dynamic table selectors.
|
||||
*
|
||||
* @module core_table/local/dynamic/events
|
||||
* @package core_table
|
||||
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
const prefixEventName = eventName => `core_table/dynamic:${eventName}`;
|
||||
|
||||
export default {
|
||||
tableContentRefreshed: prefixEventName('tableContentRefreshed'),
|
||||
};
|
|
@ -24,6 +24,7 @@
|
|||
export default {
|
||||
main: {
|
||||
region: '[data-region="core_table/dynamic"]',
|
||||
fromRegionId: regionId => `[data-region="core_table/dynamic"][data-table-uniqueid="${regionId}"]`,
|
||||
},
|
||||
table: {
|
||||
links: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue