MDL-68462 core_table: add support for reset table preferences

This commit is contained in:
Simey Lameze 2020-04-21 08:47:51 +08:00
parent ae346c8624
commit 68c46a28d6
8 changed files with 161 additions and 46 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,2 +1,2 @@
define ("core_table/local/dynamic/repository",["exports","core/ajax"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.fetch=void 0;a.fetch=function fetch(a,c,d){var e=3<arguments.length&&arguments[3]!==void 0?arguments[3]:{},f=e.sortBy,g=void 0===f?null:f,h=e.sortOrder,i=void 0===h?null:h,j=e.joinType,k=void 0===j?null:j,l=e.filters,m=void 0===l?{}:l,n=e.firstinitial,o=void 0===n?null:n,p=e.lastinitial,q=void 0===p?null:p,r=e.pageNumber,s=void 0===r?null:r,t=e.pageSize,u=void 0===t?null:t,v=e.hiddenColumns,w=void 0===v?{}:v;return(0,b.call)([{methodname:"core_table_dynamic_fetch",args:{component:a,handler:c,uniqueid:d,sortby:g,sortorder:i,jointype:k,filters:m,firstinitial:o,lastinitial:q,pagenumber:s,pagesize:u,hiddencolumns:w}}])[0]}});
define ("core_table/local/dynamic/repository",["exports","core/ajax"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.fetch=void 0;a.fetch=function fetch(a,c,d){var e=3<arguments.length&&arguments[3]!==void 0?arguments[3]:{},f=e.sortBy,g=void 0===f?null:f,h=e.sortOrder,i=void 0===h?null:h,j=e.joinType,k=void 0===j?null:j,l=e.filters,m=void 0===l?{}:l,n=e.firstinitial,o=void 0===n?null:n,p=e.lastinitial,q=void 0===p?null:p,r=e.pageNumber,s=void 0===r?null:r,t=e.pageSize,u=void 0===t?null:t,v=e.hiddenColumns,w=void 0===v?{}:v,x=4<arguments.length&&arguments[4]!==void 0?arguments[4]:!1;return(0,b.call)([{methodname:"core_table_dynamic_fetch",args:{component:a,handler:c,uniqueid:d,sortby:g,sortorder:i,jointype:k,filters:m,firstinitial:o,lastinitial:q,pagenumber:s,pagesize:u,hiddencolumns:w,resetpreferences:x}}])[0]}});
//# sourceMappingURL=repository.min.js.map

View File

@ -1 +1 @@
{"version":3,"sources":["../../../src/local/dynamic/repository.js"],"names":["fetch","component","handler","uniqueid","sortBy","sortOrder","joinType","filters","firstinitial","lastinitial","pageNumber","pageSize","hiddenColumns","methodname","args","sortby","sortorder","jointype","pagenumber","pagesize","hiddencolumns"],"mappings":"yKAyCqB,QAARA,CAAAA,KAAQ,CAACC,CAAD,CAAYC,CAAZ,CAAqBC,CAArB,CAWhB,8DADG,EACH,KAVGC,MAUH,CAVGA,CAUH,YAVY,IAUZ,OATGC,SASH,CATGA,CASH,YATe,IASf,OARGC,QAQH,CARGA,CAQH,YARc,IAQd,OAPGC,OAOH,CAPGA,CAOH,YAPa,EAOb,OANGC,YAMH,CANGA,CAMH,YANkB,IAMlB,OALGC,WAKH,CALGA,CAKH,YALiB,IAKjB,OAJGC,UAIH,CAJGA,CAIH,YAJgB,IAIhB,OAHGC,QAGH,CAHGA,CAGH,YAHc,IAGd,OAFGC,aAEH,CAFGA,CAEH,YAFmB,EAEnB,GACD,MAAO,WAAU,CAAC,CACdC,UAAU,2BADI,CAEdC,IAAI,CAAE,CACFb,SAAS,CAATA,CADE,CAEFC,OAAO,CAAPA,CAFE,CAGFC,QAAQ,CAARA,CAHE,CAIFY,MAAM,CAAEX,CAJN,CAKFY,SAAS,CAAEX,CALT,CAMFY,QAAQ,CAAEX,CANR,CAOFC,OAAO,CAAPA,CAPE,CAQFC,YAAY,CAAZA,CARE,CASFC,WAAW,CAAXA,CATE,CAUFS,UAAU,CAAER,CAVV,CAWFS,QAAQ,CAAER,CAXR,CAYFS,aAAa,CAAER,CAZb,CAFQ,CAAD,CAAV,EAgBH,CAhBG,CAiBV,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 * A javascript module to handle calendar ajax actions.\n *\n * @module core_calendar/repository\n * @class repository\n * @package core_calendar\n * @copyright 2017 Simey Lameze <lameze@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport {call as fetchMany} from 'core/ajax';\n\n/**\n * Fetch table view.\n *\n * @method fetch\n * @param {String} component The component\n * @param {String} handler The name of the handler\n * @param {String} uniqueid The unique id of the table\n * @param {Object} filters The filters to apply when searching\n * @param {String} firstinitial The first name initial to filter on\n * @param {String} lastinitial The last name initial to filter on\n * @param {String} pageNumber The page number\n * @param {Number} pageSize The page size\n * @param {Number} params parameters to request table\n * @return {Promise} Resolved with requested table view\n */\nexport const fetch = (component, handler, uniqueid, {\n sortBy = null,\n sortOrder = null,\n joinType = null,\n filters = {},\n firstinitial = null,\n lastinitial = null,\n pageNumber = null,\n pageSize = null,\n hiddenColumns = {}\n } = {}\n) => {\n return fetchMany([{\n methodname: `core_table_dynamic_fetch`,\n args: {\n component,\n handler,\n uniqueid,\n sortby: sortBy,\n sortorder: sortOrder,\n jointype: joinType,\n filters,\n firstinitial,\n lastinitial,\n pagenumber: pageNumber,\n pagesize: pageSize,\n hiddencolumns: hiddenColumns,\n },\n }])[0];\n};\n"],"file":"repository.min.js"}
{"version":3,"sources":["../../../src/local/dynamic/repository.js"],"names":["fetch","component","handler","uniqueid","sortBy","sortOrder","joinType","filters","firstinitial","lastinitial","pageNumber","pageSize","hiddenColumns","resetPreferences","methodname","args","sortby","sortorder","jointype","pagenumber","pagesize","hiddencolumns","resetpreferences"],"mappings":"yKA0CqB,QAARA,CAAAA,KAAQ,CAACC,CAAD,CAAYC,CAAZ,CAAqBC,CAArB,CAUoB,8DAAjC,EAAiC,KATjCC,MASiC,CATjCA,CASiC,YATxB,IASwB,OARjCC,SAQiC,CARjCA,CAQiC,YARrB,IAQqB,OAPjCC,QAOiC,CAPjCA,CAOiC,YAPtB,IAOsB,OANjCC,OAMiC,CANjCA,CAMiC,YANvB,EAMuB,OALjCC,YAKiC,CALjCA,CAKiC,YALlB,IAKkB,OAJjCC,WAIiC,CAJjCA,CAIiC,YAJnB,IAImB,OAHjCC,UAGiC,CAHjCA,CAGiC,YAHpB,IAGoB,OAFjCC,QAEiC,CAFjCA,CAEiC,YAFtB,IAEsB,OADjCC,aACiC,CADjCA,CACiC,YADjB,EACiB,GAA7BC,CAA6B,2DACrC,MAAO,WAAU,CAAC,CACdC,UAAU,2BADI,CAEdC,IAAI,CAAE,CACFd,SAAS,CAATA,CADE,CAEFC,OAAO,CAAPA,CAFE,CAGFC,QAAQ,CAARA,CAHE,CAIFa,MAAM,CAAEZ,CAJN,CAKFa,SAAS,CAAEZ,CALT,CAMFa,QAAQ,CAAEZ,CANR,CAOFC,OAAO,CAAPA,CAPE,CAQFC,YAAY,CAAZA,CARE,CASFC,WAAW,CAAXA,CATE,CAUFU,UAAU,CAAET,CAVV,CAWFU,QAAQ,CAAET,CAXR,CAYFU,aAAa,CAAET,CAZb,CAaFU,gBAAgB,CAAET,CAbhB,CAFQ,CAAD,CAAV,EAiBH,CAjBG,CAkBV,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 * A javascript module to handle calendar ajax actions.\n *\n * @module core_calendar/repository\n * @class repository\n * @package core_calendar\n * @copyright 2017 Simey Lameze <lameze@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport {call as fetchMany} from 'core/ajax';\n\n/**\n * Fetch table view.\n *\n * @method fetch\n * @param {String} component The component\n * @param {String} handler The name of the handler\n * @param {String} uniqueid The unique id of the table\n * @param {Object} filters The filters to apply when searching\n * @param {String} firstinitial The first name initial to filter on\n * @param {String} lastinitial The last name initial to filter on\n * @param {String} pageNumber The page number\n * @param {Number} pageSize The page size\n * @param {Number} params parameters to request table\n * @param {Bool} resetPreferences\n * @return {Promise} Resolved with requested table view\n */\nexport const fetch = (component, handler, uniqueid, {\n sortBy = null,\n sortOrder = null,\n joinType = null,\n filters = {},\n firstinitial = null,\n lastinitial = null,\n pageNumber = null,\n pageSize = null,\n hiddenColumns = {}\n } = {}, resetPreferences = false) => {\n return fetchMany([{\n methodname: `core_table_dynamic_fetch`,\n args: {\n component,\n handler,\n uniqueid,\n sortby: sortBy,\n sortorder: sortOrder,\n jointype: joinType,\n filters,\n firstinitial,\n lastinitial,\n pagenumber: pageNumber,\n pagesize: pageSize,\n hiddencolumns: hiddenColumns,\n resetpreferences: resetPreferences\n },\n }])[0];\n};\n"],"file":"repository.min.js"}

View File

@ -61,9 +61,10 @@ const getFiltersetFromTable = tableRoot => {
* Update the specified table based on its current values.
*
* @param {HTMLElement} tableRoot
* @param {Bool} resetContent
* @returns {Promise}
*/
export const refreshTableContent = tableRoot => {
export const refreshTableContent = (tableRoot, resetContent = false) => {
const filterset = getFiltersetFromTable(tableRoot);
return fetchTableData(
@ -80,7 +81,8 @@ export const refreshTableContent = tableRoot => {
pageNumber: tableRoot.dataset.tablePageNumber,
pageSize: tableRoot.dataset.tablePageSize,
hiddenColumns: JSON.parse(tableRoot.dataset.tableHiddenColumns),
}
},
resetContent,
)
.then(data => {
const placeholder = document.createElement('div');
@ -246,6 +248,14 @@ export const showColumn = (tableRoot, columnToShow, refreshContent = true) => {
updateTable(tableRoot, {hiddenColumns}, refreshContent);
};
/**
* Reset table preferences.
*
* @param {HTMLElement} tableRoot
* @returns {Promise}
*/
const resetTablePreferences = tableRoot => refreshTableContent(tableRoot, true);
/**
* Set up listeners to handle table updates.
*/
@ -305,6 +315,12 @@ export const init = () => {
showColumn(tableRoot, show.dataset.column);
}
const resetTablePreferencesLink = e.target.closest('.resettable a');
if (resetTablePreferencesLink) {
e.preventDefault();
resetTablePreferences(tableRoot);
}
});
};

View File

@ -37,6 +37,7 @@ import {call as fetchMany} from 'core/ajax';
* @param {String} pageNumber The page number
* @param {Number} pageSize The page size
* @param {Number} params parameters to request table
* @param {Bool} resetPreferences
* @return {Promise} Resolved with requested table view
*/
export const fetch = (component, handler, uniqueid, {
@ -49,8 +50,7 @@ export const fetch = (component, handler, uniqueid, {
pageNumber = null,
pageSize = null,
hiddenColumns = {}
} = {}
) => {
} = {}, resetPreferences = false) => {
return fetchMany([{
methodname: `core_table_dynamic_fetch`,
args: {
@ -66,6 +66,7 @@ export const fetch = (component, handler, uniqueid, {
pagenumber: pageNumber,
pagesize: pageSize,
hiddencolumns: hiddenColumns,
resetpreferences: resetPreferences
},
}])[0];
};

View File

@ -123,6 +123,12 @@ class fetch extends external_api {
null
)
),
'resetpreferences' => new external_value(
PARAM_BOOL,
'Whether the table preferences should be reset',
VALUE_REQUIRED,
null
),
]);
}
@ -140,6 +146,8 @@ class fetch extends external_api {
* @param string $lastinitial The last name initial to filter on
* @param int $pagenumber The page number.
* @param int $pagesize The number of records.
* @param string $jointype The join type.
* @param bool $resetpreferences Whether it is resetting table preferences or not.
*
* @return array
*/
@ -155,7 +163,8 @@ class fetch extends external_api {
?string $lastinitial = null,
?int $pagenumber = null,
?int $pagesize = null,
?array $hiddencolumns = null
?array $hiddencolumns = null,
?bool $resetpreferences = null
) {
global $PAGE;
@ -173,6 +182,7 @@ class fetch extends external_api {
'pagenumber' => $pagenumber,
'pagesize' => $pagesize,
'hiddencolumns' => $hiddencolumns,
'resetpreferences' => $resetpreferences,
] = self::validate_parameters(self::execute_parameters(), [
'component' => $component,
'handler' => $handler,
@ -186,6 +196,7 @@ class fetch extends external_api {
'pagenumber' => $pagenumber,
'pagesize' => $pagesize,
'hiddencolumns' => $hiddencolumns,
'resetpreferences' => $resetpreferences,
]);
$tableclass = "\\{$component}\\table\\{$handler}";
@ -238,6 +249,12 @@ class fetch extends external_api {
$instance->set_hidden_columns($hiddencolumns);
}
if ($resetpreferences === true) {
$instance->mark_table_to_reset();
}
$PAGE->set_url($instance->baseurl);
ob_start();
$instance->out($pagesize, true);
$tablehtml = ob_get_contents();

View File

@ -158,6 +158,9 @@ class flexible_table {
/** @var array $hiddencolumns List of hidden columns. */
protected $hiddencolumns;
/** @var $resetting bool Whether the table preferences is resetting. */
protected $resetting;
/**
* @var filterset The currently applied filerset
* This is required for dynamic tables, but can be used by other tables too if desired.
@ -485,6 +488,26 @@ class flexible_table {
$this->helpforheaders = $helpicons;
}
/**
* Mark the table preferences to be reset.
*/
public function mark_table_to_reset(): void {
$this->resetting = true;
}
/**
* Is the table marked for reset preferences?
*
* @return bool True if the table is marked to reset, false otherwise.
*/
protected function is_resetting_preferences(): bool {
if ($this->resetting === null) {
$this->resetting = optional_param($this->request[TABLE_VAR_RESET], false, PARAM_BOOL);
}
return $this->resetting;
}
/**
* Must be called after table is defined. Use methods above first. Cannot
* use functions below till after calling this method.
@ -497,43 +520,7 @@ class flexible_table {
return false;
}
// Load any existing user preferences.
if ($this->persistent) {
$this->prefs = json_decode(get_user_preferences('flextable_' . $this->uniqueid), true);
$oldprefs = $this->prefs;
} else if (isset($SESSION->flextable[$this->uniqueid])) {
$this->prefs = $SESSION->flextable[$this->uniqueid];
$oldprefs = $this->prefs;
}
// Set up default preferences if needed.
if (!$this->prefs or optional_param($this->request[TABLE_VAR_RESET], false, PARAM_BOOL)) {
$this->prefs = array(
'collapse' => array(),
'sortby' => array(),
'i_first' => '',
'i_last' => '',
'textsort' => $this->column_textsort,
);
}
if (!isset($oldprefs)) {
$oldprefs = $this->prefs;
}
$this->set_hide_show_preferences();
$this->set_sorting_preferences();
$this->set_initials_preferences();
// Save user preferences if they have changed.
if ($this->prefs != $oldprefs) {
if ($this->persistent) {
set_user_preference('flextable_' . $this->uniqueid, json_encode($this->prefs));
} else {
$SESSION->flextable[$this->uniqueid] = $this->prefs;
}
}
unset($oldprefs);
$this->initialise_table_preferences();
if (empty($this->baseurl)) {
debugging('You should set baseurl when using flexible_table.');
@ -1413,6 +1400,100 @@ class flexible_table {
$this->hiddencolumns = $columns;
}
/**
* Initialise table preferences.
*/
protected function initialise_table_preferences(): void {
// Load any existing user preferences.
if ($this->persistent) {
$this->prefs = json_decode(get_user_preferences('flextable_' . $this->uniqueid), true);
$oldprefs = $this->prefs;
} else if (isset($SESSION->flextable[$this->uniqueid])) {
$this->prefs = $SESSION->flextable[$this->uniqueid];
$oldprefs = $this->prefs;
}
// Set up default preferences if needed.
if (!$this->prefs || $this->is_resetting_preferences()) {
$this->prefs = [
'collapse' => [],
'sortby' => [],
'i_first' => '',
'i_last' => '',
'textsort' => $this->column_textsort,
];
}
if (!isset($oldprefs)) {
$oldprefs = $this->prefs;
}
// Save user preferences if they have changed.
if ($this->is_resetting_preferences()) {
$this->sortorder = null;
$this->sortby = null;
$this->ifirst = null;
$this->ilast = null;
}
if (($showcol = optional_param($this->request[TABLE_VAR_SHOW], '', PARAM_ALPHANUMEXT)) &&
isset($this->columns[$showcol])) {
$this->prefs['collapse'][$showcol] = false;
} else if (($hidecol = optional_param($this->request[TABLE_VAR_HIDE], '', PARAM_ALPHANUMEXT)) &&
isset($this->columns[$hidecol])) {
$this->prefs['collapse'][$hidecol] = true;
if (array_key_exists($hidecol, $this->prefs['sortby'])) {
unset($this->prefs['sortby'][$hidecol]);
}
}
// Now, update the column attributes for collapsed columns
foreach (array_keys($this->columns) as $column) {
if (!empty($this->prefs['collapse'][$column])) {
$this->column_style[$column]['width'] = '10px';
}
}
// Now, update the column attributes for collapsed columns
foreach (array_keys($this->columns) as $column) {
if (!empty($this->prefs['collapse'][$column])) {
$this->column_style[$column]['width'] = '10px';
}
}
$this->set_sorting_preferences();
$this->set_initials_preferences();
if (empty($this->baseurl)) {
debugging('You should set baseurl when using flexible_table.');
global $PAGE;
$this->baseurl = $PAGE->url;
}
if ($this->currpage == null) {
$this->currpage = optional_param($this->request[TABLE_VAR_PAGE], 0, PARAM_INT);
}
$this->save_preferences($oldprefs);
}
/**
* Save preferences.
*
* @param array $oldprefs Old preferences to compare against.
*/
protected function save_preferences($oldprefs): void {
if ($this->prefs != $oldprefs) {
if ($this->persistent) {
set_user_preference('flextable_' . $this->uniqueid, json_encode($this->prefs));
} else {
$SESSION->flextable[$this->uniqueid] = $this->prefs;
}
}
unset($oldprefs);
}
/**
* Set the preferred table sorting attributes.
*