Skip to content

Commit 7e5f0fa

Browse files
author
David Monllao
committed
Merge branch 'MDL-62144-master' of git://github.com/damyon/moodle
2 parents 14f70ed + a986cb4 commit 7e5f0fa

13 files changed

+243
-14
lines changed

lang/en/moodle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
$string['action'] = 'Action';
2727
$string['actionchoice'] = 'What do you want to do with the file \'{$a}\'?';
2828
$string['actions'] = 'Actions';
29+
$string['actionsmenu'] = 'Actions menu';
2930
$string['active'] = 'Active';
3031
$string['activeusers'] = 'Active users';
3132
$string['activities'] = 'Activities';

lib/outputcomponents.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4133,6 +4133,12 @@ class action_menu implements renderable, templatable {
41334133
*/
41344134
public $actiontext = null;
41354135

4136+
/**
4137+
* The string to use for the accessible label for the menu.
4138+
* @var array
4139+
*/
4140+
public $actionlabel = null;
4141+
41364142
/**
41374143
* An icon to use for the toggling the secondary menu (dropdown).
41384144
* @var actionicon
@@ -4190,6 +4196,16 @@ public function __construct(array $actions = array()) {
41904196
}
41914197
}
41924198

4199+
/**
4200+
* Sets the label for the menu trigger.
4201+
*
4202+
* @param string $label The text
4203+
* @return null
4204+
*/
4205+
public function set_action_label($label) {
4206+
$this->actionlabel = $label;
4207+
}
4208+
41934209
/**
41944210
* Sets the menu trigger text.
41954211
*
@@ -4293,7 +4309,7 @@ public function get_primary_actions(core_renderer $output = null) {
42934309
$pixicon = '<b class="caret"></b>';
42944310
$linkclasses[] = 'textmenu';
42954311
} else {
4296-
$title = new lang_string('actions', 'moodle');
4312+
$title = new lang_string('actionsmenu', 'moodle');
42974313
$this->actionicon = new pix_icon(
42984314
't/edit_menu',
42994315
'',
@@ -4312,10 +4328,17 @@ public function get_primary_actions(core_renderer $output = null) {
43124328
if ($this->actiontext) {
43134329
$string = $this->actiontext;
43144330
}
4331+
$label = '';
4332+
if ($this->actionlabel) {
4333+
$label = $this->actionlabel;
4334+
} else {
4335+
$label = $title;
4336+
}
43154337
$actions = $this->primaryactions;
43164338
$attributes = array(
43174339
'class' => implode(' ', $linkclasses),
43184340
'title' => $title,
4341+
'aria-label' => $label,
43194342
'id' => 'action-menu-toggle-'.$this->instance,
43204343
'role' => 'menuitem'
43214344
);
@@ -4481,9 +4504,17 @@ public function export_for_template(renderer_base $output) {
44814504
if (!empty($this->menutrigger)) {
44824505
$primary->menutrigger = $this->menutrigger;
44834506
$primary->triggerextraclasses = $this->triggerextraclasses;
4507+
if ($this->actionlabel) {
4508+
$primary->title = $this->actionlabel;
4509+
} else if ($this->actiontext) {
4510+
$primary->title = $this->actiontext;
4511+
} else {
4512+
$primary->title = strip_tags($this->menutrigger);
4513+
}
44844514
} else {
4485-
$primary->title = get_string('actions');
4486-
$actionicon = new pix_icon('t/edit_menu', '', 'moodle', ['class' => 'iconsmall actionmenu', 'title' => '']);
4515+
$primary->title = get_string('actionsmenu');
4516+
$iconattributes = ['class' => 'iconsmall actionmenu', 'title' => $primary->title];
4517+
$actionicon = new pix_icon('t/edit_menu', '', 'moodle', $iconattributes);
44874518
}
44884519

44894520
if ($actionicon instanceof pix_icon) {

lib/outputrenderers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,7 @@ public function user_menu($user = null, $withlinks = null) {
34053405
$am->set_menu_trigger(
34063406
$returnstr
34073407
);
3408+
$am->set_action_label(get_string('usermenu'));
34083409
$am->set_alignment(action_menu::TR, action_menu::BR);
34093410
$am->set_nowrap_on_items();
34103411
if ($withlinks) {

lib/templates/action_menu_link.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
<a href="{{url}}" class="{{classes}}" {{#attributes}}{{name}}={{#quote}}{{value}}{{/quote}} {{/attributes}}{{#showtext}}aria-labelledby="actionmenuaction-{{instance}}"{{/showtext}}>{{#icon}}{{#pix}}{{key}}, {{component}}, {{title}}{{/pix}}{{/icon}}{{#showtext}}<span class="menu-action-text" id="actionmenuaction-{{instance}}">{{{text}}}</span>{{/showtext}}</a>
3333
{{/disabled}}
3434
{{#disabled}}
35-
<span class="currentlink" role="menuitem">{{#icon}}{{#pix}}{{key}}, {{component}}, {{title}}{{/pix}}{{/icon}}{{{text}}}</span>
35+
<span class="currentlink" role="button">{{#icon}}{{#pix}}{{key}}, {{component}}, {{title}}{{/pix}}{{/icon}}{{{text}}}</span>
3636
{{/disabled}}

lib/templates/action_menu_trigger.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
"triggerextraclasses": ""
2828
}
2929
}}
30-
<a href="#" class="{{triggerextraclasses}} toggle-display {{#menutrigger}}textmenu{{/menutrigger}}" id="action-menu-toggle-{{instance}}" title="{{title}}" role="menuitem">{{{actiontext}}}{{{menutrigger}}}{{#icon}}{{#pix}}{{key}}, {{component}}, {{title}}{{/pix}}{{/icon}}{{#rawicon}}{{{.}}}{{/rawicon}}{{#menutrigger}}<b class="caret"></b>{{/menutrigger}}</a>
30+
<a tabindex="0" class="{{triggerextraclasses}} toggle-display {{#menutrigger}}textmenu{{/menutrigger}}" id="action-menu-toggle-{{instance}}" aria-label="{{title}}" role="menuitem" aria-controls="action-menu-{{instance}}-menu">{{{actiontext}}}{{{menutrigger}}}{{#icon}}{{#pix}}{{key}}, {{component}}, {{title}}{{/pix}}{{/icon}}{{#rawicon}}{{{.}}}{{/rawicon}}{{#menutrigger}}<b class="caret"></b>{{/menutrigger}}</a>

lib/tests/user_menu_test.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public function test_custom_user_menu($data, $entrycount, $dividercount) {
8686
$this->setAdminUser();
8787
$PAGE->set_url('/');
8888
$CFG->theme = 'clean';
89+
$PAGE->reset_theme_and_output();
90+
$PAGE->initialise_theme_and_output();
8991

9092
// Set the configuration.
9193
set_config('customusermenuitems', $data);
@@ -94,7 +96,8 @@ public function test_custom_user_menu($data, $entrycount, $dividercount) {
9496
$dividercount += 2;
9597

9698
// The basic entry count will additionally include the wrapper menu, Dashboard, Profile, Logout and switch roles link.
97-
$entrycount += 4;
99+
// On clean theme only, the trigger is also a menuitem.
100+
$entrycount += 5;
98101

99102
$output = $OUTPUT->user_menu($USER);
100103
preg_match_all('/<a [^>]+role="menuitem"[^>]+>/', $output, $results);

theme/boost/amd/build/aria.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

theme/boost/amd/build/loader.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

theme/boost/amd/src/aria.js

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// This file is part of Moodle - http://moodle.org/
2+
//
3+
// Moodle is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Moodle is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
16+
/**
17+
* Enhancements to Bootstrap components for accessibility.
18+
*
19+
* @module theme_boost/aria
20+
* @copyright 2018 Damyon Wiese <damyon@moodle.com>
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
*/
23+
define(['jquery'], function($) {
24+
return {
25+
init: function() {
26+
// Drop downs from bootstrap don't support keyboard accessibility by default.
27+
var focusEnd = false,
28+
setFocusEnd = function() {
29+
focusEnd = true;
30+
},
31+
getFocusEnd = function() {
32+
var result = focusEnd;
33+
focusEnd = false;
34+
return result;
35+
};
36+
37+
// Special handling for "up" keyboard control.
38+
$('[data-toggle="dropdown"]').keydown(function(e) {
39+
var trigger = e.which || e.keyCode,
40+
expanded;
41+
42+
// Up key opens the menu at the end.
43+
if (trigger == 38) {
44+
// Focus the end of the menu, not the beginning.
45+
setFocusEnd();
46+
}
47+
48+
// Escape key only closes the menu, it doesn't open it.
49+
if (trigger == 27) {
50+
expanded = $(e.target).attr('aria-expanded');
51+
e.preventDefault();
52+
if (expanded == "false") {
53+
$(e.target).click();
54+
}
55+
}
56+
57+
// Space key or Enter key opens the menu.
58+
if (trigger == 32 || trigger == 13) {
59+
// Cancel random scroll.
60+
e.preventDefault();
61+
// Open the menu instead.
62+
$(e.target).click();
63+
}
64+
});
65+
66+
// Special handling for navigation keys when menu is open.
67+
var shiftFocus = function(element) {
68+
var delayedFocus = function() {
69+
$(this).focus();
70+
}.bind(element);
71+
setTimeout(delayedFocus, 50);
72+
};
73+
74+
$('.dropdown').on('shown.bs.dropdown', function(e) {
75+
// We need to focus on the first menuitem.
76+
var menu = $(e.target).find('[role="menu"]'),
77+
menuItems = false,
78+
foundMenuItem = false;
79+
80+
if (menu) {
81+
menuItems = $(menu).find('[role="menuitem"]');
82+
}
83+
if (menuItems && menuItems.length > 0) {
84+
if (getFocusEnd()) {
85+
foundMenuItem = menuItems[menuItems.length - 1];
86+
} else {
87+
// The first menu entry, pretty reasonable.
88+
foundMenuItem = menuItems[0];
89+
}
90+
}
91+
if (foundMenuItem) {
92+
shiftFocus(foundMenuItem);
93+
}
94+
});
95+
// Search for menu items by finding the first item that has
96+
// text starting with the typed character (case insensitive).
97+
$('.dropdown [role="menu"] [role="menuitem"]').keypress(function(e) {
98+
var trigger = String.fromCharCode(e.which || e.keyCode),
99+
menu = $(e.target).closest('[role="menu"]'),
100+
i = 0,
101+
menuItems = false,
102+
item,
103+
itemText;
104+
105+
if (!menu) {
106+
return;
107+
}
108+
menuItems = $(menu).find('[role="menuitem"]');
109+
if (!menuItems) {
110+
return;
111+
}
112+
113+
trigger = trigger.toLowerCase();
114+
for (i = 0; i < menuItems.length; i++) {
115+
item = $(menuItems[i]);
116+
itemText = item.text().trim().toLowerCase();
117+
if (itemText.indexOf(trigger) == 0) {
118+
shiftFocus(item);
119+
break;
120+
}
121+
}
122+
});
123+
124+
// Keyboard navigation for arrow keys, home and end keys.
125+
$('.dropdown [role="menu"] [role="menuitem"]').keydown(function(e) {
126+
var trigger = e.which || e.keyCode,
127+
next = false,
128+
menu = $(e.target).closest('[role="menu"]'),
129+
i = 0,
130+
menuItems = false;
131+
if (!menu) {
132+
return;
133+
}
134+
menuItems = $(menu).find('[role="menuitem"]');
135+
if (!menuItems) {
136+
return;
137+
}
138+
// Down key.
139+
if (trigger == 40) {
140+
for (i = 0; i < menuItems.length - 1; i++) {
141+
if (menuItems[i] == e.target) {
142+
next = menuItems[i + 1];
143+
break;
144+
}
145+
}
146+
if (!next) {
147+
// Wrap to first item.
148+
next = menuItems[0];
149+
}
150+
151+
} else if (trigger == 38) {
152+
// Up key.
153+
for (i = 1; i < menuItems.length; i++) {
154+
if (menuItems[i] == e.target) {
155+
next = menuItems[i - 1];
156+
break;
157+
}
158+
}
159+
if (!next) {
160+
// Wrap to last item.
161+
next = menuItems[menuItems.length - 1];
162+
}
163+
164+
} else if (trigger == 36) {
165+
// Home key.
166+
next = menuItems[0];
167+
168+
} else if (trigger == 35) {
169+
// End key.
170+
next = menuItems[menuItems.length - 1];
171+
}
172+
// Variable next is set if we do want to act on the keypress.
173+
if (next) {
174+
e.preventDefault();
175+
shiftFocus(next);
176+
}
177+
return;
178+
});
179+
$('.dropdown').on('hidden.bs.dropdown', function(e) {
180+
// We need to focus on the menu trigger.
181+
var trigger = $(e.target).find('[data-toggle="dropdown"]');
182+
if (trigger) {
183+
shiftFocus(trigger);
184+
}
185+
});
186+
}
187+
};
188+
});

theme/boost/amd/src/loader.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ define(['jquery', './tether', 'core/event'], function(jQuery, Tether, Event) {
2828
window.jQuery = jQuery;
2929
window.Tether = Tether;
3030

31-
require(['theme_boost/util',
31+
require(['theme_boost/aria',
32+
'theme_boost/util',
3233
'theme_boost/alert',
3334
'theme_boost/button',
3435
'theme_boost/carousel',
@@ -39,7 +40,7 @@ define(['jquery', './tether', 'core/event'], function(jQuery, Tether, Event) {
3940
'theme_boost/tab',
4041
'theme_boost/tooltip',
4142
'theme_boost/popover'],
42-
function() {
43+
function(Aria) {
4344

4445
// We do twice because: https://github.com/twbs/bootstrap/issues/10547
4546
jQuery('body').popover({
@@ -63,8 +64,11 @@ define(['jquery', './tether', 'core/event'], function(jQuery, Tether, Event) {
6364
selector: '[data-toggle="popover"]',
6465
trigger: 'focus'
6566
});
67+
6668
});
6769
});
70+
71+
Aria.init();
6872
});
6973

7074

0 commit comments

Comments
 (0)