diff --git a/contentbank/amd/build/sort.min.js b/contentbank/amd/build/sort.min.js index 5d0babfc933c2..bd655b7259506 100644 --- a/contentbank/amd/build/sort.min.js +++ b/contentbank/amd/build/sort.min.js @@ -1,2 +1,2 @@ -define ("core_contentbank/sort",["exports","core_contentbank/selectors","core/str","core/prefetch"],function(a,b,c,d){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;b=e(b);d=e(d);function e(a){return a&&a.__esModule?a:{default:a}}var f=function(){var a=document.querySelector(b.default.regions.contentbank);d.default.prefetchStrings("contentbank",["sortbyx","sortbyxreverse","contentname","lastmodified","size","type"]);g(a)};a.init=f;var g=function(a){var c=document.querySelector(b.default.regions.filearea),d=c.querySelectorAll(b.default.elements.listitem),e=a.querySelector(b.default.actions.viewgrid),f=a.querySelector(b.default.actions.viewlist);e.addEventListener("click",function(){a.classList.remove("view-list");a.classList.add("view-grid");e.classList.add("active");f.classList.remove("active")});f.addEventListener("click",function(){a.classList.remove("view-grid");a.classList.add("view-list");f.classList.add("active");e.classList.remove("active")});var g=a.querySelector(b.default.actions.sortname);g.addEventListener("click",function(){var b=h(a,g);j(c,d,"data-file",b)});var i=a.querySelector(b.default.actions.sortdate);i.addEventListener("click",function(){var b=h(a,i);j(c,d,"data-timemodified",b)});var k=a.querySelector(b.default.actions.sortsize);k.addEventListener("click",function(){var b=h(a,k);j(c,d,"data-bytes",b)});var l=a.querySelector(b.default.actions.sorttype);l.addEventListener("click",function(){var b=h(a,l);j(c,d,"data-type",b)})},h=function(a,c){var d=a.querySelectorAll(b.default.elements.sortbutton);d.forEach(function(a){if(a!==c){a.classList.remove("dir-asc");a.classList.remove("dir-desc");a.classList.add("dir-none");i(a,!1)}});var e=!0;if(c.classList.contains("dir-none")){c.classList.remove("dir-none");c.classList.add("dir-asc")}else if(c.classList.contains("dir-asc")){c.classList.remove("dir-asc");c.classList.add("dir-desc");e=!1}else if(c.classList.contains("dir-desc")){c.classList.remove("dir-desc");c.classList.add("dir-asc")}i(c,e);return e},i=function(a,b){var d=b?"sortbyxreverse":"sortbyx";return(0,c.get_string)(a.dataset.string,"contentbank").then(function(a){return(0,c.get_string)(d,"core",a)}).then(function(b){a.setAttribute("title",b);return b}).catch()},j=function(a,b,c,d){var e=[].slice.call(b).sort(function(e,a){var b=e.getAttribute(c),f=a.getAttribute(c);if(!isNaN(b)){b=parseInt(b);f=parseInt(f)}if(d){return b>f?1:-1}else{return bf?1:-1}else{return b.\n\n/**\n * Content bank UI actions.\n *\n * @module core_contentbank/sort\n * @package core_contentbank\n * @copyright 2020 Bas Brands \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport selectors from 'core_contentbank/selectors';\nimport {get_string as getString} from 'core/str';\nimport Prefetch from 'core/prefetch';\n\n/**\n * Set up the contentbank views.\n *\n * @method init\n */\nexport const init = () => {\n const contentBank = document.querySelector(selectors.regions.contentbank);\n Prefetch.prefetchStrings('contentbank', ['sortbyx', 'sortbyxreverse', 'contentname',\n 'lastmodified', 'size', 'type']);\n registerListenerEvents(contentBank);\n};\n\n/**\n * Register contentbank related event listeners.\n *\n * @method registerListenerEvents\n * @param {HTMLElement} contentBank The DOM node of the content bank\n */\nconst registerListenerEvents = (contentBank) => {\n\n // The search.\n const fileArea = document.querySelector(selectors.regions.filearea);\n const shownItems = fileArea.querySelectorAll(selectors.elements.listitem);\n\n // The view buttons.\n const viewGrid = contentBank.querySelector(selectors.actions.viewgrid);\n const viewList = contentBank.querySelector(selectors.actions.viewlist);\n\n viewGrid.addEventListener('click', () => {\n contentBank.classList.remove('view-list');\n contentBank.classList.add('view-grid');\n viewGrid.classList.add('active');\n viewList.classList.remove('active');\n });\n\n viewList.addEventListener('click', () => {\n contentBank.classList.remove('view-grid');\n contentBank.classList.add('view-list');\n viewList.classList.add('active');\n viewGrid.classList.remove('active');\n });\n\n // Sort by file name alphabetical\n const sortByName = contentBank.querySelector(selectors.actions.sortname);\n sortByName.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortByName);\n updateSortOrder(fileArea, shownItems, 'data-file', ascending);\n });\n\n // Sort by date.\n const sortByDate = contentBank.querySelector(selectors.actions.sortdate);\n sortByDate.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortByDate);\n updateSortOrder(fileArea, shownItems, 'data-timemodified', ascending);\n });\n\n // Sort by size.\n const sortBySize = contentBank.querySelector(selectors.actions.sortsize);\n sortBySize.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortBySize);\n updateSortOrder(fileArea, shownItems, 'data-bytes', ascending);\n });\n\n // Sort by type\n const sortByType = contentBank.querySelector(selectors.actions.sorttype);\n sortByType.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortByType);\n updateSortOrder(fileArea, shownItems, 'data-type', ascending);\n });\n};\n\n/**\n * Update the sort button view.\n *\n * @method updateSortButtons\n * @param {HTMLElement} contentBank The DOM node of the contentbank button\n * @param {HTMLElement} sortButton The DOM node of the sort button\n * @return {Bool} sort ascending\n */\nconst updateSortButtons = (contentBank, sortButton) => {\n const sortButtons = contentBank.querySelectorAll(selectors.elements.sortbutton);\n\n sortButtons.forEach((button) => {\n if (button !== sortButton) {\n button.classList.remove('dir-asc');\n button.classList.remove('dir-desc');\n button.classList.add('dir-none');\n\n updateButtonTitle(button, false);\n }\n });\n\n let ascending = true;\n\n if (sortButton.classList.contains('dir-none')) {\n sortButton.classList.remove('dir-none');\n sortButton.classList.add('dir-asc');\n } else if (sortButton.classList.contains('dir-asc')) {\n sortButton.classList.remove('dir-asc');\n sortButton.classList.add('dir-desc');\n ascending = false;\n } else if (sortButton.classList.contains('dir-desc')) {\n sortButton.classList.remove('dir-desc');\n sortButton.classList.add('dir-asc');\n }\n\n updateButtonTitle(sortButton, ascending);\n\n return ascending;\n};\n\n/**\n * Update the button title.\n *\n * @method updateButtonTitle\n * @param {HTMLElement} button Button to update\n * @param {Bool} ascending Sort direction\n * @return {Promise} string promise\n */\nconst updateButtonTitle = (button, ascending) => {\n\n const sortString = (ascending ? 'sortbyxreverse' : 'sortbyx');\n\n return getString(button.dataset.string, 'contentbank')\n .then(columnName => {\n return getString(sortString, 'core', columnName);\n })\n .then(sortByString => {\n button.setAttribute('title', sortByString);\n return sortByString;\n })\n .catch();\n};\n\n/**\n * Update the sort order of the itemlist and update the DOM\n *\n * @method updateSortOrder\n * @param {HTMLElement} fileArea the Dom container for the itemlist\n * @param {Array} itemList Nodelist of Dom elements\n * @param {String} attribute, the attribut to sort on\n * @param {Bool} ascending, Sort Ascending\n */\nconst updateSortOrder = (fileArea, itemList, attribute, ascending) => {\n const sortList = [].slice.call(itemList).sort(function(a, b) {\n\n let aa = a.getAttribute(attribute);\n let bb = b.getAttribute(attribute);\n if (!isNaN(aa)) {\n aa = parseInt(aa);\n bb = parseInt(bb);\n }\n\n if (ascending) {\n return aa > bb ? 1 : -1;\n } else {\n return aa < bb ? 1 : -1;\n }\n });\n sortList.forEach(listItem => fileArea.appendChild(listItem));\n};\n"],"file":"sort.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/sort.js"],"names":["init","contentBank","document","querySelector","selectors","regions","contentbank","Prefetch","prefetchStrings","registerListenerEvents","fileArea","filearea","shownItems","querySelectorAll","elements","listitem","viewGrid","actions","viewgrid","viewList","viewlist","addEventListener","classList","remove","add","setViewListPreference","sortByName","sortname","ascending","updateSortButtons","updateSortOrder","sortByDate","sortdate","sortBySize","sortsize","sortByType","sorttype","request","methodname","args","preferences","type","value","Ajax","call","catch","Notification","exception","sortButton","sortButtons","sortbutton","forEach","button","updateButtonTitle","contains","sortString","dataset","string","then","columnName","sortByString","setAttribute","itemList","attribute","sortList","slice","sort","a","b","aa","getAttribute","bb","isNaN","parseInt","listItem","appendChild"],"mappings":"uNAwBA,OAEA,OACA,OACA,O,mDAOO,GAAMA,CAAAA,CAAI,CAAG,UAAM,CACtB,GAAMC,CAAAA,CAAW,CAAGC,QAAQ,CAACC,aAAT,CAAuBC,UAAUC,OAAV,CAAkBC,WAAzC,CAApB,CACAC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AAI3C,CAAJ,CAAe,CACX,MAAOyC,CAAAA,CAAE,CAAGE,CAAL,CAAU,CAAV,CAAc,CAAC,CACzB,CAFD,IAEO,CACH,MAAOF,CAAAA,CAAE,CAAGE,CAAL,CAAU,CAAV,CAAc,CAAC,CACzB,CACJ,CAdgB,CAAjB,CAeAP,CAAQ,CAACb,OAAT,CAAiB,SAAAuB,CAAQ,QAAIhE,CAAAA,CAAQ,CAACiE,WAAT,CAAqBD,CAArB,CAAJ,CAAzB,CACH,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 .\n\n/**\n * Content bank UI actions.\n *\n * @module core_contentbank/sort\n * @package core_contentbank\n * @copyright 2020 Bas Brands \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport selectors from './selectors';\nimport {get_string as getString} from 'core/str';\nimport Prefetch from 'core/prefetch';\nimport Ajax from 'core/ajax';\nimport Notification from 'core/notification';\n\n/**\n * Set up the contentbank views.\n *\n * @method init\n */\nexport const init = () => {\n const contentBank = document.querySelector(selectors.regions.contentbank);\n Prefetch.prefetchStrings('contentbank', ['sortbyx', 'sortbyxreverse', 'contentname',\n 'lastmodified', 'size', 'type']);\n registerListenerEvents(contentBank);\n};\n\n/**\n * Register contentbank related event listeners.\n *\n * @method registerListenerEvents\n * @param {HTMLElement} contentBank The DOM node of the content bank\n */\nconst registerListenerEvents = (contentBank) => {\n\n // The search.\n const fileArea = document.querySelector(selectors.regions.filearea);\n const shownItems = fileArea.querySelectorAll(selectors.elements.listitem);\n\n // The view buttons.\n const viewGrid = contentBank.querySelector(selectors.actions.viewgrid);\n const viewList = contentBank.querySelector(selectors.actions.viewlist);\n\n viewGrid.addEventListener('click', () => {\n contentBank.classList.remove('view-list');\n contentBank.classList.add('view-grid');\n viewGrid.classList.add('active');\n viewList.classList.remove('active');\n setViewListPreference(false);\n });\n\n viewList.addEventListener('click', () => {\n contentBank.classList.remove('view-grid');\n contentBank.classList.add('view-list');\n viewList.classList.add('active');\n viewGrid.classList.remove('active');\n setViewListPreference(true);\n });\n\n // Sort by file name alphabetical\n const sortByName = contentBank.querySelector(selectors.actions.sortname);\n sortByName.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortByName);\n updateSortOrder(fileArea, shownItems, 'data-file', ascending);\n });\n\n // Sort by date.\n const sortByDate = contentBank.querySelector(selectors.actions.sortdate);\n sortByDate.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortByDate);\n updateSortOrder(fileArea, shownItems, 'data-timemodified', ascending);\n });\n\n // Sort by size.\n const sortBySize = contentBank.querySelector(selectors.actions.sortsize);\n sortBySize.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortBySize);\n updateSortOrder(fileArea, shownItems, 'data-bytes', ascending);\n });\n\n // Sort by type\n const sortByType = contentBank.querySelector(selectors.actions.sorttype);\n sortByType.addEventListener('click', () => {\n const ascending = updateSortButtons(contentBank, sortByType);\n updateSortOrder(fileArea, shownItems, 'data-type', ascending);\n });\n};\n\n\n/**\n * Set the contentbank user preference in list view\n *\n * @param {Bool} viewList view ContentBank as list.\n * @return {Promise} Repository promise.\n */\nconst setViewListPreference = function(viewList) {\n\n // If the given status is not hidden, the preference has to be deleted with a null value.\n if (viewList === false) {\n viewList = null;\n }\n\n const request = {\n methodname: 'core_user_update_user_preferences',\n args: {\n preferences: [\n {\n type: 'core_contentbank_view_list',\n value: viewList\n }\n ]\n }\n };\n\n return Ajax.call([request])[0].catch(Notification.exception);\n};\n\n/**\n * Update the sort button view.\n *\n * @method updateSortButtons\n * @param {HTMLElement} contentBank The DOM node of the contentbank button\n * @param {HTMLElement} sortButton The DOM node of the sort button\n * @return {Bool} sort ascending\n */\nconst updateSortButtons = (contentBank, sortButton) => {\n const sortButtons = contentBank.querySelectorAll(selectors.elements.sortbutton);\n\n sortButtons.forEach((button) => {\n if (button !== sortButton) {\n button.classList.remove('dir-asc');\n button.classList.remove('dir-desc');\n button.classList.add('dir-none');\n\n updateButtonTitle(button, false);\n }\n });\n\n let ascending = true;\n\n if (sortButton.classList.contains('dir-none')) {\n sortButton.classList.remove('dir-none');\n sortButton.classList.add('dir-asc');\n } else if (sortButton.classList.contains('dir-asc')) {\n sortButton.classList.remove('dir-asc');\n sortButton.classList.add('dir-desc');\n ascending = false;\n } else if (sortButton.classList.contains('dir-desc')) {\n sortButton.classList.remove('dir-desc');\n sortButton.classList.add('dir-asc');\n }\n\n updateButtonTitle(sortButton, ascending);\n\n return ascending;\n};\n\n/**\n * Update the button title.\n *\n * @method updateButtonTitle\n * @param {HTMLElement} button Button to update\n * @param {Bool} ascending Sort direction\n * @return {Promise} string promise\n */\nconst updateButtonTitle = (button, ascending) => {\n\n const sortString = (ascending ? 'sortbyxreverse' : 'sortbyx');\n\n return getString(button.dataset.string, 'contentbank')\n .then(columnName => {\n return getString(sortString, 'core', columnName);\n })\n .then(sortByString => {\n button.setAttribute('title', sortByString);\n return sortByString;\n })\n .catch();\n};\n\n/**\n * Update the sort order of the itemlist and update the DOM\n *\n * @method updateSortOrder\n * @param {HTMLElement} fileArea the Dom container for the itemlist\n * @param {Array} itemList Nodelist of Dom elements\n * @param {String} attribute, the attribut to sort on\n * @param {Bool} ascending, Sort Ascending\n */\nconst updateSortOrder = (fileArea, itemList, attribute, ascending) => {\n const sortList = [].slice.call(itemList).sort(function(a, b) {\n\n let aa = a.getAttribute(attribute);\n let bb = b.getAttribute(attribute);\n if (!isNaN(aa)) {\n aa = parseInt(aa);\n bb = parseInt(bb);\n }\n\n if (ascending) {\n return aa > bb ? 1 : -1;\n } else {\n return aa < bb ? 1 : -1;\n }\n });\n sortList.forEach(listItem => fileArea.appendChild(listItem));\n};\n"],"file":"sort.min.js"} \ No newline at end of file diff --git a/contentbank/amd/src/sort.js b/contentbank/amd/src/sort.js index bfa61c8a0a3a0..24f4f79fbbdd7 100644 --- a/contentbank/amd/src/sort.js +++ b/contentbank/amd/src/sort.js @@ -22,9 +22,11 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -import selectors from 'core_contentbank/selectors'; +import selectors from './selectors'; import {get_string as getString} from 'core/str'; import Prefetch from 'core/prefetch'; +import Ajax from 'core/ajax'; +import Notification from 'core/notification'; /** * Set up the contentbank views. @@ -59,6 +61,7 @@ const registerListenerEvents = (contentBank) => { contentBank.classList.add('view-grid'); viewGrid.classList.add('active'); viewList.classList.remove('active'); + setViewListPreference(false); }); viewList.addEventListener('click', () => { @@ -66,6 +69,7 @@ const registerListenerEvents = (contentBank) => { contentBank.classList.add('view-list'); viewList.classList.add('active'); viewGrid.classList.remove('active'); + setViewListPreference(true); }); // Sort by file name alphabetical @@ -97,6 +101,35 @@ const registerListenerEvents = (contentBank) => { }); }; + +/** + * Set the contentbank user preference in list view + * + * @param {Bool} viewList view ContentBank as list. + * @return {Promise} Repository promise. + */ +const setViewListPreference = function(viewList) { + + // If the given status is not hidden, the preference has to be deleted with a null value. + if (viewList === false) { + viewList = null; + } + + const request = { + methodname: 'core_user_update_user_preferences', + args: { + preferences: [ + { + type: 'core_contentbank_view_list', + value: viewList + } + ] + } + }; + + return Ajax.call([request])[0].catch(Notification.exception); +}; + /** * Update the sort button view. * diff --git a/contentbank/classes/output/bankcontent.php b/contentbank/classes/output/bankcontent.php index 6574b0409c7fa..5ac92e93a7aca 100644 --- a/contentbank/classes/output/bankcontent.php +++ b/contentbank/classes/output/bankcontent.php @@ -97,6 +97,7 @@ public function export_for_template(renderer_base $output): stdClass { 'type' => $mimetype ); } + $data->viewlist = get_user_preferences('core_contentbank_view_list'); $data->contents = $contentdata; $data->tools = $this->toolbar; return $data; diff --git a/contentbank/classes/privacy/provider.php b/contentbank/classes/privacy/provider.php index d654ab37bc54c..c0c49c5c9ac44 100644 --- a/contentbank/classes/privacy/provider.php +++ b/contentbank/classes/privacy/provider.php @@ -44,7 +44,8 @@ class provider implements \core_privacy\local\metadata\provider, \core_privacy\local\request\core_userlist_provider, - \core_privacy\local\request\plugin\provider { + \core_privacy\local\request\plugin\provider, + \core_privacy\local\request\user_preference_provider { /** * Returns meta data about this system. @@ -65,6 +66,26 @@ public static function get_metadata(collection $collection): collection { return $collection; } + /** + * Export all user preferences for the contentbank + * + * @param int $userid The userid of the user whose data is to be exported. + */ + public static function export_user_preferences(int $userid) { + $preference = get_user_preferences('core_contentbank_view_list', null, $userid); + if (isset($preference)) { + writer::export_user_preference( + 'core_contentbank', + 'core_contentbank_view_list', + $preference, + get_string('privacy:request:preference:set', 'core_contentbank', (object) [ + 'name' => 'core_contentbank_view_list', + 'value' => $preference, + ]) + ); + } + } + /** * Get the list of contexts that contain user information for the specified user. * diff --git a/contentbank/lib.php b/contentbank/lib.php new file mode 100644 index 0000000000000..e709df77df0cd --- /dev/null +++ b/contentbank/lib.php @@ -0,0 +1,39 @@ +. + +/** + * Library functions for contentbank + * + * @package core_contentbank + * @copyright 2020 Bas Brands + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Get the current user preferences that are available + * + * @return Array preferences configuration + */ +function core_contentbank_user_preferences() { + return [ + 'core_contentbank_view_list' => [ + 'choices' => array(0, 1), + 'type' => PARAM_INT, + 'null' => NULL_NOT_ALLOWED, + 'default' => 'none' + ], + ]; +} diff --git a/contentbank/templates/bankcontent.mustache b/contentbank/templates/bankcontent.mustache index 0826a65e482e5..aaa3857789bbd 100644 --- a/contentbank/templates/bankcontent.mustache +++ b/contentbank/templates/bankcontent.mustache @@ -48,7 +48,8 @@ } }} -
+
{{>core_contentbank/bankcontent/search}} diff --git a/contentbank/templates/bankcontent/toolbar.mustache b/contentbank/templates/bankcontent/toolbar.mustache index 04c762a132026..76fe0877174de 100644 --- a/contentbank/templates/bankcontent/toolbar.mustache +++ b/contentbank/templates/bankcontent/toolbar.mustache @@ -38,12 +38,12 @@ {{#pix}} {{{ icon }}} {{/pix}} {{{ name }}} {{/tools}} - -