MDL-78204 availability: Remove 'showmore' from availability_info

- Remove 'showmore' logic from the base availability_info renderable.
Other components rendering the availability information should handle the showmore/showless behaviour themselves.

- Deprecate JS module and string.

- Move availability_info header generation logic to a new protected function to improve code readability.
This commit is contained in:
Mikel Martín 2023-06-08 11:04:32 +02:00
parent 3cd84747cb
commit 09d9f367b6
8 changed files with 35 additions and 64 deletions

View File

@ -2,6 +2,8 @@ define("core_availability/availability_more",["exports"],(function(_exports){Obj
/**
* Show more action for availablity information.
*
* @deprecated since 4.3 MDL-78204.
* @todo MDL-78489 This will be deleted in Moodle 4.7.
* @module core_availability/availability_more
* @copyright 2021 Bas Brands <bas@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

View File

@ -1 +1 @@
{"version":3,"file":"availability_more.min.js","sources":["../src/availability_more.js"],"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 * Show more action for availablity information.\n *\n * @module core_availability/availability_more\n * @copyright 2021 Bas Brands <bas@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\n/**\n * Availability info selectors.\n */\nconst Selectors = {\n regions: {\n availability: '[data-region=\"availability-multiple\"]',\n },\n actions: {\n showmorelink: '[data-action=\"showmore\"]'\n },\n classes: {\n hidden: 'd-none',\n visible: 'd-block',\n\n }\n};\n\n/**\n * Displays all the availability information in case part of it is hidden.\n *\n * @param {Event} event the triggered event\n */\nconst showMoreHandler = (event) => {\n const triggerElement = event.target.closest(Selectors.actions.showmorelink);\n if (triggerElement === null) {\n return;\n }\n const container = triggerElement.closest(Selectors.regions.availability);\n container.querySelectorAll('.' + Selectors.classes.hidden).forEach(function(node) {\n node.classList.remove(Selectors.classes.hidden);\n });\n container.querySelectorAll('.' + Selectors.classes.visible).forEach(function(node) {\n node.classList.remove(Selectors.classes.visible);\n node.classList.add(Selectors.classes.hidden);\n });\n event.preventDefault();\n};\n\n/**\n * Initialise the eventlister for the showmore action on availability information.\n *\n * @method init\n */\nexport const init = () => {\n const body = document.querySelector('body');\n if (!body.dataset.showmoreactive) {\n document.addEventListener('click', showMoreHandler);\n body.dataset.showmoreactive = 1;\n }\n};\n"],"names":["Selectors","availability","showmorelink","hidden","visible","showMoreHandler","event","triggerElement","target","closest","container","querySelectorAll","forEach","node","classList","remove","add","preventDefault","body","document","querySelector","dataset","showmoreactive","addEventListener"],"mappings":";;;;;;;;MA0BMA,kBACO,CACLC,aAAc,yCAFhBD,kBAIO,CACLE,aAAc,4BALhBF,kBAOO,CACLG,OAAQ,SACRC,QAAS,WAUXC,gBAAmBC,cACfC,eAAiBD,MAAME,OAAOC,QAAQT,kBAAkBE,iBACvC,OAAnBK,4BAGEG,UAAYH,eAAeE,QAAQT,kBAAkBC,cAC3DS,UAAUC,iBAAiB,IAAMX,kBAAkBG,QAAQS,SAAQ,SAASC,MACxEA,KAAKC,UAAUC,OAAOf,kBAAkBG,WAE5CO,UAAUC,iBAAiB,IAAMX,kBAAkBI,SAASQ,SAAQ,SAASC,MACzEA,KAAKC,UAAUC,OAAOf,kBAAkBI,SACxCS,KAAKC,UAAUE,IAAIhB,kBAAkBG,WAEzCG,MAAMW,gCAQU,WACVC,KAAOC,SAASC,cAAc,QAC/BF,KAAKG,QAAQC,iBACdH,SAASI,iBAAiB,QAASlB,iBACnCa,KAAKG,QAAQC,eAAiB"}
{"version":3,"file":"availability_more.min.js","sources":["../src/availability_more.js"],"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 * Show more action for availablity information.\n *\n * @deprecated since 4.3 MDL-78204.\n * @todo MDL-78489 This will be deleted in Moodle 4.7.\n * @module core_availability/availability_more\n * @copyright 2021 Bas Brands <bas@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\n/**\n * Availability info selectors.\n */\nconst Selectors = {\n regions: {\n availability: '[data-region=\"availability-multiple\"]',\n },\n actions: {\n showmorelink: '[data-action=\"showmore\"]'\n },\n classes: {\n hidden: 'd-none',\n visible: 'd-block',\n\n }\n};\n\n/**\n * Displays all the availability information in case part of it is hidden.\n *\n * @param {Event} event the triggered event\n */\nconst showMoreHandler = (event) => {\n const triggerElement = event.target.closest(Selectors.actions.showmorelink);\n if (triggerElement === null) {\n return;\n }\n const container = triggerElement.closest(Selectors.regions.availability);\n container.querySelectorAll('.' + Selectors.classes.hidden).forEach(function(node) {\n node.classList.remove(Selectors.classes.hidden);\n });\n container.querySelectorAll('.' + Selectors.classes.visible).forEach(function(node) {\n node.classList.remove(Selectors.classes.visible);\n node.classList.add(Selectors.classes.hidden);\n });\n event.preventDefault();\n};\n\n/**\n * Initialise the eventlister for the showmore action on availability information.\n *\n * @method init\n */\nexport const init = () => {\n const body = document.querySelector('body');\n if (!body.dataset.showmoreactive) {\n document.addEventListener('click', showMoreHandler);\n body.dataset.showmoreactive = 1;\n }\n};\n"],"names":["Selectors","availability","showmorelink","hidden","visible","showMoreHandler","event","triggerElement","target","closest","container","querySelectorAll","forEach","node","classList","remove","add","preventDefault","body","document","querySelector","dataset","showmoreactive","addEventListener"],"mappings":";;;;;;;;;;MA4BMA,kBACO,CACLC,aAAc,yCAFhBD,kBAIO,CACLE,aAAc,4BALhBF,kBAOO,CACLG,OAAQ,SACRC,QAAS,WAUXC,gBAAmBC,cACfC,eAAiBD,MAAME,OAAOC,QAAQT,kBAAkBE,iBACvC,OAAnBK,4BAGEG,UAAYH,eAAeE,QAAQT,kBAAkBC,cAC3DS,UAAUC,iBAAiB,IAAMX,kBAAkBG,QAAQS,SAAQ,SAASC,MACxEA,KAAKC,UAAUC,OAAOf,kBAAkBG,WAE5CO,UAAUC,iBAAiB,IAAMX,kBAAkBI,SAASQ,SAAQ,SAASC,MACzEA,KAAKC,UAAUC,OAAOf,kBAAkBI,SACxCS,KAAKC,UAAUE,IAAIhB,kBAAkBG,WAEzCG,MAAMW,gCAQU,WACVC,KAAOC,SAASC,cAAc,QAC/BF,KAAKG,QAAQC,iBACdH,SAASI,iBAAiB,QAASlB,iBACnCa,KAAKG,QAAQC,eAAiB"}

View File

@ -16,6 +16,8 @@
/**
* Show more action for availablity information.
*
* @deprecated since 4.3 MDL-78204.
* @todo MDL-78489 This will be deleted in Moodle 4.7.
* @module core_availability/availability_more
* @copyright 2021 Bas Brands <bas@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

View File

@ -40,12 +40,6 @@ class availability_info implements renderable, templatable {
/** @var core_availability_multiple_messages availabilitymessages the course format class */
protected $availabilitymessages;
/** @var int counts number of conditions */
protected $count = 0;
/** @var int Maximum number of lines of availability info */
protected const MAXVISIBLE = 4;
/**
* Constructor.
*
@ -53,7 +47,6 @@ class availability_info implements renderable, templatable {
*/
public function __construct(core_availability_multiple_messages $renderable) {
$this->availabilitymessages = $renderable;
$this->count = 0;
}
/**
@ -68,10 +61,6 @@ class availability_info implements renderable, templatable {
$template->id = uniqid();
if ($this->count >= self::MAXVISIBLE) {
$template->showmorelink = true;
}
return $template;
}
@ -82,12 +71,9 @@ class availability_info implements renderable, templatable {
*/
protected function get_item_base_template(): stdClass {
return (object)[
'hidden' => $this->count > self::MAXVISIBLE,
'abbreviate' => $this->count === self::MAXVISIBLE,
'id' => false,
'items' => [],
'hasitems' => false,
'showmorelink' => false,
];
}
@ -101,15 +87,9 @@ class availability_info implements renderable, templatable {
$template = $this->get_item_base_template();
$template->header = get_string(
'list_' . ($availability->root ? 'root_' : '') .
($availability->andoperator ? 'and' : 'or') .
($availability->treehidden ? '_hidden' : ''),
'availability'
);
$template->header = $this->get_item_header($availability);
foreach ($availability->items as $item) {
$this->count++;
if (is_string($item)) {
$simple_item = $this->get_item_base_template();
$simple_item->header = $item;
@ -123,4 +103,20 @@ class availability_info implements renderable, templatable {
return $template;
}
/**
* Get the item header.
* Depending on availability configuration this will return a string from a combined string identifier.
* For example: list_root_and_hidden, list_and, list_root_or_hidden, list_root_or, etc.
*
* @param core_availability_multiple_messages $availability the availability messages
* @return string the item header
*/
protected function get_item_header(core_availability_multiple_messages $availability): string {
$stridentifier = 'list_' . ($availability->root ? 'root_' : '') .
($availability->andoperator ? 'and' : 'or') .
($availability->treehidden ? '_hidden' : '');
return get_string($stridentifier, 'availability');
}
}

View File

@ -25,74 +25,36 @@
"header": "Not available unless:",
"items" : [
{
"showmorelink": 0,
"abbreviate" : 0,
"hidden" : 0,
"hasitems": 0,
"header": "You belong to Green Group"
},
{
"showmorelink": 0,
"abbreviate" : 0,
"hidden" : 0,
"hasitems": 0,
"header": "You belong to Red Group"
},
{
"showmorelink": 0,
"abbreviate" : 1,
"hidden" : 0,
"hasitems": 0,
"header": "You belong to Orange Group"
},
{
"showmorelink": 0,
"abbreviate" : 0,
"hidden" : 1,
"hasitems": 0,
"header": "You belong to Pink Group"
},
{
"showmorelink": 0,
"abbreviate" : 0,
"hidden" : 1,
"hasitems": 0,
"header": "You belong to Red Group"
}
],
"hasitems": 1,
"showmorelink": 1
"hasitems": 1
}
}}
{{{header}}}
{{#hasitems}}
<ul {{#id}} id="availability-tree-{{id}}" {{/id}} data-region="availability-multiple">
{{#items}}
<li class="{{#hidden}}d-none{{/hidden}}">
{{#abbreviate}}
<span class="d-none">
{{> core_availability/availability_info }}
</span><span class="d-block">...</span>
{{/abbreviate}}
{{^abbreviate}}
{{> core_availability/availability_info }}
{{/abbreviate}}
<li>
{{> core_availability/availability_info }}
</li>
{{/items}}
{{#showmorelink}}
<li data-action="showmore" class="d-block showmore">
<a role="button" aria-expanded="false" aria-controls="availability-tree-{{id}}" href="#" >
{{#str}}showmore, availability{{/str}}
</a>
</li>
{{/showmorelink}}
</ul>
{{/hasitems}}
{{#showmorelink}}
{{#js}}
require(['core_availability/availability_more'], function(availabilityMore) {
availabilityMore.init();
});
{{/js}}
{{/showmorelink}}

View File

@ -2,6 +2,12 @@ This files describes API changes in /availability/*.
The information here is intended only for developers.
=== 4.3 ===
* The `availability_info templatable` no longer exports 'showmore' information, and any component rendering avaialable_info
should handle showmore/showless behaviour by itself.
That means that 'showmorelink', 'hidden' and 'abbreviate' won't be exported for template in the root element neither its child items,
and `core_availability/availability_more` module has beed deprecated
=== 4.2 ===
* The form for editing availability conditions now supports "private" conditions, that should not be shown

View File

@ -66,5 +66,7 @@ $string['privacy:metadata'] = 'The Access restriction subsystem does not store a
$string['restrictaccess'] = 'Restrict access';
$string['restrictbygroup'] = 'Add group/grouping access restriction';
$string['setheading'] = '{$a->number} Set of {$a->count} restriction(s)';
$string['showmore'] = 'Show more';
$string['unknowncondition'] = 'Unknown condition (deleted condition plugin)';
// Deprecated since Moodle 4.3.
$string['showmore'] = 'Show more';

View File

@ -79,3 +79,4 @@ selectdevice,core_admin
selecttheme,core_admin
themenoselected,core_admin
unsettheme,core_admin
showmore,core_availability