MDL-68506 core_table: Add a tableContentRefreshed event
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 24 Apr 2020 01:40:32 +0000 (09:40 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Fri, 24 Apr 2020 06:53:35 +0000 (14:53 +0800)
lib/table/amd/build/dynamic.min.js
lib/table/amd/build/dynamic.min.js.map
lib/table/amd/build/local/dynamic/events.min.js [new file with mode: 0644]
lib/table/amd/build/local/dynamic/events.min.js.map [new file with mode: 0644]
lib/table/amd/build/local/dynamic/selectors.min.js
lib/table/amd/build/local/dynamic/selectors.min.js.map
lib/table/amd/src/dynamic.js
lib/table/amd/src/local/dynamic/events.js [new file with mode: 0644]
lib/table/amd/src/local/dynamic/selectors.js

index 96cc2ca..6b39495 100644 (file)
Binary files a/lib/table/amd/build/dynamic.min.js and b/lib/table/amd/build/dynamic.min.js differ
index d457b4f..f0f92e5 100644 (file)
Binary files a/lib/table/amd/build/dynamic.min.js.map and b/lib/table/amd/build/dynamic.min.js.map differ
diff --git a/lib/table/amd/build/local/dynamic/events.min.js b/lib/table/amd/build/local/dynamic/events.min.js
new file mode 100644 (file)
index 0000000..2c0a787
Binary files /dev/null and b/lib/table/amd/build/local/dynamic/events.min.js differ
diff --git a/lib/table/amd/build/local/dynamic/events.min.js.map b/lib/table/amd/build/local/dynamic/events.min.js.map
new file mode 100644 (file)
index 0000000..400c659
Binary files /dev/null and b/lib/table/amd/build/local/dynamic/events.min.js.map differ
index f4dc44a..916c3ee 100644 (file)
Binary files a/lib/table/amd/build/local/dynamic/selectors.min.js and b/lib/table/amd/build/local/dynamic/selectors.min.js differ
index 73059fc..994561c 100644 (file)
Binary files a/lib/table/amd/build/local/dynamic/selectors.min.js.map and b/lib/table/amd/build/local/dynamic/selectors.min.js.map differ
index c3f264f..9a95950 100644 (file)
@@ -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
+};
diff --git a/lib/table/amd/src/local/dynamic/events.js b/lib/table/amd/src/local/dynamic/events.js
new file mode 100644 (file)
index 0000000..115ea69
--- /dev/null
@@ -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'),
+};
index 118f328..8b07a19 100644 (file)
@@ -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: {