diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js index 2f8ac9ccda6..92900e08575 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js @@ -746,37 +746,51 @@ EditorToolbarNav.prototype = { // Prevent the default browser behaviour. e.preventDefault(); - var buttons = this.toolbar.all('button'); - // On cursor moves we loops through the buttons. - var found = false, - index = 0, + var buttons = this.toolbar.all('button'), direction = 1, - checkCount = 0, - group, + button, current = e.target.ancestor('button', true); - // Determine which button is currently selected. - while (!found && index < buttons.size()) { - if (buttons.item(index) === current) { - found = true; - } else { - index++; - } - } - - if (!found) { - Y.log("Unable to find this button in the list of buttons", 'debug', LOGNAME); - return; - } - if (e.keyCode === 37) { // Moving left so reverse the direction. direction = -1; } - // Try to find the next - do { + button = this._findFirstFocusable(buttons, current, direction); + if (button) { + button.focus(); + } else { + Y.log("Unable to find a button to focus on", 'debug', LOGNAME); + } + }, + + /** + * Find the first focusable button. + * + * @param {NodeList} buttons A list of nodes. + * @param {Node} startAt The node in the list to start the search from. + * @param {Number} direction The direction in which to search (1 or -1). + * @return {Node | Undefined} The Node or undefined. + * @method _findFirstFocusable + * @private + */ + _findFirstFocusable: function(buttons, startAt, direction) { + var checkCount = 0, + group, + candidate, + button, + index; + + // Determine which button to start the search from. + index = buttons.indexOf(startAt); + if (index < -1) { + Y.log("Unable to find the button in the list of buttons", 'debug', LOGNAME); + index = 0; + } + + // Try to find the next. + while (checkCount < buttons.size()) { index += direction; if (index < 0) { index = buttons.size() - 1; @@ -784,21 +798,57 @@ EditorToolbarNav.prototype = { // Handle wrapping. index = 0; } - next = buttons.item(index); - group = next.ancestor('.atto_group'); + + candidate = buttons.item(index); // Add a counter to ensure we don't get stuck in a loop if there's only one visible menu item. checkCount++; - // Loop while: - // * we are not in a loop and have not already checked every button; and - // * we are on a different button; and - // * both the next button and the group it is in are not hidden. - } while (checkCount < buttons.size() && next !== current && (next.hasAttribute('hidden') || group.hasAttribute('hidden'))); - if (next) { - next.focus(); - this._setTabFocus(next); + // Loop while: + // * we haven't checked every button; + // * the button is hidden or disabled; + // * the group is hidden. + if (candidate.hasAttribute('hidden') || candidate.hasAttribute('disabled')) { + continue; + } + group = candidate.ancestor('.atto_group'); + if (group.hasAttribute('hidden')) { + continue; + } + + button = candidate; + break; } + + return button; + }, + + /** + * Check the tab focus. + * + * When we disable or hide a button, we should call this method to ensure that the + * focus is not currently set on an inaccessible button, otherwise tabbing to the toolbar + * would be impossible. + * + * @method checkTabFocus + * @chainable + */ + checkTabFocus: function() { + if (this._tabFocus) { + if (this._tabFocus.hasAttribute('disabled') || this._tabFocus.hasAttribute('hidden') + || this._tabFocus.ancestor('.atto_group').hasAttribute('hidden')) { + // Find first available button. + button = this._findFirstFocusable(this.toolbar.all('button'), this._tabFocus, -1); + if (button) { + if (this._tabFocus.compareTo(document.activeElement)) { + // We should also move the focus, because the inaccessible button also has the focus. + button.focus(); + } + this._setTabFocus(button); + } + } + } + return this; }, /** diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js index 5efbe55293f..ff90434356d 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js @@ -1,2 +1,2 @@ -YUI.add("moodle-editor_atto-editor",function(e,t){function i(){i.superclass.constructor.apply(this,arguments)}function s(){}function o(){}function u(){}function a(){}function f(){}function l(){}function c(){}var n="moodle-editor_atto-editor",r={CONTENT:"editor_atto_content",CONTENTWRAPPER:"editor_atto_content_wrap",TOOLBAR:"editor_atto_toolbar",WRAPPER:"editor_atto",HIGHLIGHT:"highlight"};e.extend(i,e.Base,{BLOCK_TAGS:["address","article","aside","audio","blockquote","canvas","dd","div","dl","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","noscript","ol","output","p","pre","section","table","tfoot","ul","video"],PLACEHOLDER_CLASS:"atto-tmp-class",ALL_NODES_SELECTOR:"[style],font[face]",FONT_FAMILY:"fontFamily",_wrapper:null,editor:null,textarea:null,textareaLabel:null,plugins:null,initializer:function(){var t;this.textarea=e.one(document.getElementById(this.get("elementid")));if(!this.textarea)return;this._wrapper=e.Node.create('
'),t=e.Handlebars.compile('
'),this.editor=e.Node.create(t({elementid:this.get("elementid"),CSS:r})),this.textareaLabel=e.one('[for="'+this.get("elementid")+'"]'),this.textareaLabel&&(this.textareaLabel.generateID(),this.editor.setAttribute("aria-labelledby",this.textareaLabel.get("id"))),this.setupToolbar();var n=e.Node.create('
');n.appendChild(this.editor),this._wrapper.appendChild(n),this.editor.setStyle("minHeight",20*this.textarea.getAttribute("rows")+8+"px"),e.UA.ie===0&&this.editor.setStyle("height",20*this.textarea.getAttribute("rows")+8+"px"),this.disableCssStyling(),this.textarea.get("parentNode").insert(this._wrapper,this.textarea),this.textarea.hide(),this.updateFromTextArea(),this.publishEvents(),this.setupSelectionWatchers(),this.setupPlugins()},focus:function(){return this.editor.focus(),this},publishEvents:function(){return this.publish("change",{broadcast:!0,preventable:!0}),this.publish("pluginsloaded",{fireOnce:!0}),this.publish("atto:selectionchanged",{prefix:"atto"}),e.delegate(["mouseup","keyup","focus"],this._hasSelectionChanged,document.body,"#"+this.editor.get("id"),this),this},setupPlugins:function(){this.plugins={};var t=this.get("plugins"),n,r,i,s,o;for(n in t){r=t[n];if(!r.plugins)continue;for(i in r.plugins){s=r.plugins[i],o=e.mix({name:s.name,group:r.group,editor:this.editor,toolbar:this.toolbar,host:this},s);if(typeof e.M["atto_"+s.name]=="undefined")continue;this.plugins[s.name]=new e.M["atto_"+s.name].Button(o)}}return this.fire("pluginsloaded"),this},enablePlugins:function(e){this._setPluginState(!0,e)},disablePlugins:function(e){this._setPluginState(!1,e)},_setPluginState:function(t,n){var r="disableButtons";t&&(r="enableButtons"),n?this.plugins[n][r]():e.Object.each(this.plugins,function(e){e[r]()},this)}},{NS:"editor_atto",ATTRS:{elementid:{value:null,writeOnce:!0},plugins:{value:{},writeOnce:!0}}}),e.augment(i,e.EventTarget),e.namespace("M.editor_atto").Editor=i,e.namespace("M.editor_atto.Editor").init=function(t){return new e.M.editor_atto.Editor(t)},s.ATTRS={},s.prototype={updateFromTextArea:function(){this.editor.setHTML(""),this.editor.append(this.textarea.get("value")),this.cleanEditorHTML(),this.editor.getHTML()===""&&(e.UA.ie&&e.UA.ie<10?this.editor.setHTML("

"):this.editor.setHTML("


"))},updateOriginal:function(){this.textarea.set("value",this.getCleanHTML()),this.textarea.simulate("change"),this.fire("change")}},e.Base.mix(e.M.editor_atto.Editor,[s]),o.ATTRS={},o.prototype={getCleanHTML:function(){var t=this.editor.cloneNode(!0);return e.each(t.all('[id^="yui"]'),function(e){e.removeAttribute("id")}),t.all(".atto_control").remove(!0),this._cleanHTML(t.get("innerHTML"))},cleanEditorHTML:function(){var e=this.editor.get("innerHTML");return this.editor.set("innerHTML",this._cleanHTML(e)),this},_cleanHTML:function(e){var t=[{regex://gi,replace:""},{regex:/<\\?\?xml[^>]*>/gi,replace:""},{regex:/<\/?\w+:[^>]*>/gi,replace:""},{regex:/\s*MSO[-:][^;"']*;?/gi,replace:""},{regex:/]*>( |\s)*<\/span>/gi,replace:""},{regex:/class="Mso[^"]*"/gi,replace:""},{regex:/<(\/?title|\/?meta|\/?style|\/?st\d|\/?head|\/?font|\/?html|\/?body|!\[)[^>]*?>/gi,replace:""},{regex:new RegExp(String.fromCharCode(8220),"gi"),replace:'"'},{regex:new RegExp(String.fromCharCode(8216),"gi"),replace:"'"},{regex:new RegExp(String.fromCharCode(8217),"gi"),replace:"'"},{regex:new RegExp(String.fromCharCode(8211),"gi"),replace:"-"},{regex:new RegExp(String.fromCharCode(8212),"gi"),replace:"--"},{regex:new RegExp(String.fromCharCode(189),"gi"),replace:"1/2"},{regex:new RegExp(String.fromCharCode(188),"gi"),replace:"1/4"},{regex:new RegExp(String.fromCharCode(190),"gi"),replace:"3/4"},{regex:new RegExp(String.fromCharCode(169),"gi"),replace:"(c)"},{regex:new RegExp(String.fromCharCode(174),"gi"),replace:"(r)"},{regex:new RegExp(String.fromCharCode(8230),"gi"),replace:"..."}],n=0;for(n=0;n'),this._wrapper.appendChild(this.toolbar),this.textareaLabel&&this.toolbar.setAttribute("aria-labelledby",this.textareaLabel.get("id")),this.setupToolbarNavigation(),this}},e.Base.mix(e.M.editor_atto.Editor,[u]),a.ATTRS={},a.prototype={_tabFocus:null,setupToolbarNavigation:function(){return this._wrapper.delegate("key",this.toolbarKeyboardNavigation,"down:37,39","."+r.TOOLBAR,this),this},toolbarKeyboardNavigation:function(e){e.preventDefault();var t=this.toolbar.all("button"),n=!1,r=0,i=1,s=0,o,u=e.target.ancestor("button",!0);while(!n&&r=t.size -()&&(r=0),next=t.item(r),o=next.ancestor(".atto_group"),s++;while(s0,i=!1,s=this.editor,o=function(e){return s.contains(e)};return t.each(function(t){if(n){if(!r||!t.ancestor(e,!0,o))r=!1}else!i&&t.ancestor(e,!0,o)&&(i=!0)},this),n?r:i},getSelectedNodes:function(){var t=new e.NodeList,n,r,i,s,o;r=rangy.getSelection(),r.rangeCount?i=r.getRangeAt(0):i=rangy.createRange(),i.collapsed&&(i=i.cloneRange(),i.selectNode(i.commonAncestorContainer)),n=i.getNodes();for(o=0;o

"),i.get("childNodes").each(function(e){u.append(e.remove())}),i.append(u),o=u),t&&t!==""&&(f=e.Node.create("<"+t+">"),f.setAttrs(o.getAttrs()),o.get("childNodes").each(function(e){e.remove(),f.append(e)}),o.replace(f),o=f),n&&o.setAttrs(n);var l=this.getSelectionFromNode(o);return this.setSelection(l),o}},e.Base.mix(e.M.editor_atto.Editor,[l]),c.ATTRS={filepickeroptions:{value:{}}},c.prototype={canShowFilepicker:function(e){return typeof this.get("filepickeroptions")[e]!="undefined"},showFilepicker:function(t,n,r){var i=this;e.use("core_filepicker",function(e){var s=e.clone(i.get("filepickeroptions")[t],!0);s.formcallback=n,r&&(s.magicscope=r),M.core_filepicker.show(e,s)})}},e.Base.mix(e.M.editor_atto.Editor,[c])},"@VERSION@",{requires:["node","io","overlay","escape","event","event-simulate","event-custom","yui-throttle","moodle-core-notification-dialogue","moodle-editor_atto-rangy","handlebars"]}); +YUI.add("moodle-editor_atto-editor",function(e,t){function i(){i.superclass.constructor.apply(this,arguments)}function s(){}function o(){}function u(){}function a(){}function f(){}function l(){}function c(){}var n="moodle-editor_atto-editor",r={CONTENT:"editor_atto_content",CONTENTWRAPPER:"editor_atto_content_wrap",TOOLBAR:"editor_atto_toolbar",WRAPPER:"editor_atto",HIGHLIGHT:"highlight"};e.extend(i,e.Base,{BLOCK_TAGS:["address","article","aside","audio","blockquote","canvas","dd","div","dl","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","noscript","ol","output","p","pre","section","table","tfoot","ul","video"],PLACEHOLDER_CLASS:"atto-tmp-class",ALL_NODES_SELECTOR:"[style],font[face]",FONT_FAMILY:"fontFamily",_wrapper:null,editor:null,textarea:null,textareaLabel:null,plugins:null,initializer:function(){var t;this.textarea=e.one(document.getElementById(this.get("elementid")));if(!this.textarea)return;this._wrapper=e.Node.create('
'),t=e.Handlebars.compile('
'),this.editor=e.Node.create(t({elementid:this.get("elementid"),CSS:r})),this.textareaLabel=e.one('[for="'+this.get("elementid")+'"]'),this.textareaLabel&&(this.textareaLabel.generateID(),this.editor.setAttribute("aria-labelledby",this.textareaLabel.get("id"))),this.setupToolbar();var n=e.Node.create('
');n.appendChild(this.editor),this._wrapper.appendChild(n),this.editor.setStyle("minHeight",20*this.textarea.getAttribute("rows")+8+"px"),e.UA.ie===0&&this.editor.setStyle("height",20*this.textarea.getAttribute("rows")+8+"px"),this.disableCssStyling(),this.textarea.get("parentNode").insert(this._wrapper,this.textarea),this.textarea.hide(),this.updateFromTextArea(),this.publishEvents(),this.setupSelectionWatchers(),this.setupPlugins()},focus:function(){return this.editor.focus(),this},publishEvents:function(){return this.publish("change",{broadcast:!0,preventable:!0}),this.publish("pluginsloaded",{fireOnce:!0}),this.publish("atto:selectionchanged",{prefix:"atto"}),e.delegate(["mouseup","keyup","focus"],this._hasSelectionChanged,document.body,"#"+this.editor.get("id"),this),this},setupPlugins:function(){this.plugins={};var t=this.get("plugins"),n,r,i,s,o;for(n in t){r=t[n];if(!r.plugins)continue;for(i in r.plugins){s=r.plugins[i],o=e.mix({name:s.name,group:r.group,editor:this.editor,toolbar:this.toolbar,host:this},s);if(typeof e.M["atto_"+s.name]=="undefined")continue;this.plugins[s.name]=new e.M["atto_"+s.name].Button(o)}}return this.fire("pluginsloaded"),this},enablePlugins:function(e){this._setPluginState(!0,e)},disablePlugins:function(e){this._setPluginState(!1,e)},_setPluginState:function(t,n){var r="disableButtons";t&&(r="enableButtons"),n?this.plugins[n][r]():e.Object.each(this.plugins,function(e){e[r]()},this)}},{NS:"editor_atto",ATTRS:{elementid:{value:null,writeOnce:!0},plugins:{value:{},writeOnce:!0}}}),e.augment(i,e.EventTarget),e.namespace("M.editor_atto").Editor=i,e.namespace("M.editor_atto.Editor").init=function(t){return new e.M.editor_atto.Editor(t)},s.ATTRS={},s.prototype={updateFromTextArea:function(){this.editor.setHTML(""),this.editor.append(this.textarea.get("value")),this.cleanEditorHTML(),this.editor.getHTML()===""&&(e.UA.ie&&e.UA.ie<10?this.editor.setHTML("

"):this.editor.setHTML("


"))},updateOriginal:function(){this.textarea.set("value",this.getCleanHTML()),this.textarea.simulate("change"),this.fire("change")}},e.Base.mix(e.M.editor_atto.Editor,[s]),o.ATTRS={},o.prototype={getCleanHTML:function(){var t=this.editor.cloneNode(!0);return e.each(t.all('[id^="yui"]'),function(e){e.removeAttribute("id")}),t.all(".atto_control").remove(!0),this._cleanHTML(t.get("innerHTML"))},cleanEditorHTML:function(){var e=this.editor.get("innerHTML");return this.editor.set("innerHTML",this._cleanHTML(e)),this},_cleanHTML:function(e){var t=[{regex://gi,replace:""},{regex:/<\\?\?xml[^>]*>/gi,replace:""},{regex:/<\/?\w+:[^>]*>/gi,replace:""},{regex:/\s*MSO[-:][^;"']*;?/gi,replace:""},{regex:/]*>( |\s)*<\/span>/gi,replace:""},{regex:/class="Mso[^"]*"/gi,replace:""},{regex:/<(\/?title|\/?meta|\/?style|\/?st\d|\/?head|\/?font|\/?html|\/?body|!\[)[^>]*?>/gi,replace:""},{regex:new RegExp(String.fromCharCode(8220),"gi"),replace:'"'},{regex:new RegExp(String.fromCharCode(8216),"gi"),replace:"'"},{regex:new RegExp(String.fromCharCode(8217),"gi"),replace:"'"},{regex:new RegExp(String.fromCharCode(8211),"gi"),replace:"-"},{regex:new RegExp(String.fromCharCode(8212),"gi"),replace:"--"},{regex:new RegExp(String.fromCharCode(189),"gi"),replace:"1/2"},{regex:new RegExp(String.fromCharCode(188),"gi"),replace:"1/4"},{regex:new RegExp(String.fromCharCode(190),"gi"),replace:"3/4"},{regex:new RegExp(String.fromCharCode(169),"gi"),replace:"(c)"},{regex:new RegExp(String.fromCharCode(174),"gi"),replace:"(r)"},{regex:new RegExp(String.fromCharCode(8230),"gi"),replace:"..."}],n=0;for(n=0;n'),this._wrapper.appendChild(this.toolbar),this.textareaLabel&&this.toolbar.setAttribute("aria-labelledby",this.textareaLabel.get("id")),this.setupToolbarNavigation(),this}},e.Base.mix(e.M.editor_atto.Editor,[u]),a.ATTRS={},a.prototype={_tabFocus:null,setupToolbarNavigation:function(){return this._wrapper.delegate("key",this.toolbarKeyboardNavigation,"down:37,39","."+r.TOOLBAR,this),this},toolbarKeyboardNavigation:function(e){e.preventDefault();var t=this.toolbar.all("button"),n=1,r,i=e.target.ancestor("button",!0);e.keyCode===37&&(n=-1),r=this._findFirstFocusable(t,i,n),r&&r.focus()},_findFirstFocusable:function(e,t,n){var r=0,i,s,o,u;u=e. +indexOf(t),u<-1&&(u=0);while(r=e.size()&&(u=0),s=e.item(u),r++;if(s.hasAttribute("hidden")||s.hasAttribute("disabled"))continue;i=s.ancestor(".atto_group");if(i.hasAttribute("hidden"))continue;o=s;break}return o},checkTabFocus:function(){return this._tabFocus&&(this._tabFocus.hasAttribute("disabled")||this._tabFocus.hasAttribute("hidden")||this._tabFocus.ancestor(".atto_group").hasAttribute("hidden"))&&(button=this._findFirstFocusable(this.toolbar.all("button"),this._tabFocus,-1),button&&(this._tabFocus.compareTo(document.activeElement)&&button.focus(),this._setTabFocus(button))),this},_setTabFocus:function(e){return this._tabFocus&&this._tabFocus.setAttribute("tabindex","-1"),this._tabFocus=e,this._tabFocus.setAttribute("tabindex",0),this.toolbar.setAttribute("aria-activedescendant",this._tabFocus.generateID()),this}},e.Base.mix(e.M.editor_atto.Editor,[a]),f.ATTRS={},f.prototype={_selections:null,_lastSelection:null,_focusFromClick:!1,setupSelectionWatchers:function(){return this.on("atto:selectionchanged",this.saveSelection,this),this.editor.on("focus",this.restoreSelection,this),this.editor.on("mousedown",function(){this._focusFromClick=!0},this),this.editor.on("blur",function(){this._focusFromClick=!1,this.updateOriginal()},this),this},isActive:function(){var e=rangy.createRange(),t=rangy.getSelection();return t.rangeCount?(e.selectNode(this.editor.getDOMNode()),e.intersectsRange(t.getRangeAt(0))):!1},getSelectionFromNode:function(e){var t=rangy.createRange();return t.selectNode(e.getDOMNode()),[t]},saveSelection:function(){this.isActive()&&(this._selections=this.getSelection())},restoreSelection:function(){this._focusFromClick||this._selections&&this.setSelection(this._selections),this._focusFromClick=!1},getSelection:function(){return rangy.getSelection().getAllRanges()},selectionContainsNode:function(e){return rangy.getSelection().containsNode(e.getDOMNode(),!0)},selectionFilterMatches:function(e,t,n){typeof n=="undefined"&&(n=!0),t||(t=this.getSelectedNodes());var r=t.size()>0,i=!1,s=this.editor,o=function(e){return s.contains(e)};return t.each(function(t){if(n){if(!r||!t.ancestor(e,!0,o))r=!1}else!i&&t.ancestor(e,!0,o)&&(i=!0)},this),n?r:i},getSelectedNodes:function(){var t=new e.NodeList,n,r,i,s,o;r=rangy.getSelection(),r.rangeCount?i=r.getRangeAt(0):i=rangy.createRange(),i.collapsed&&(i=i.cloneRange(),i.selectNode(i.commonAncestorContainer)),n=i.getNodes();for(o=0;o

"),i.get("childNodes").each(function(e){u.append(e.remove())}),i.append(u),o=u),t&&t!==""&&(f=e.Node.create("<"+t+">"),f.setAttrs(o.getAttrs()),o.get("childNodes").each(function(e){e.remove(),f.append(e)}),o.replace(f),o=f),n&&o.setAttrs(n);var l=this.getSelectionFromNode(o);return this.setSelection(l),o}},e.Base.mix(e.M.editor_atto.Editor,[l]),c.ATTRS={filepickeroptions:{value:{}}},c.prototype={canShowFilepicker:function(e){return typeof this.get("filepickeroptions")[e]!="undefined"},showFilepicker:function(t,n,r){var i=this;e.use("core_filepicker",function(e){var s=e.clone(i.get("filepickeroptions")[t],!0);s.formcallback=n,r&&(s.magicscope=r),M.core_filepicker.show(e,s)})}},e.Base.mix(e.M.editor_atto.Editor,[c])},"@VERSION@",{requires:["node","io","overlay","escape","event","event-simulate","event-custom","yui-throttle","moodle-core-notification-dialogue","moodle-editor_atto-rangy","handlebars"]}); diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js index 477a74c4efa..d7338231357 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js @@ -743,36 +743,49 @@ EditorToolbarNav.prototype = { // Prevent the default browser behaviour. e.preventDefault(); - var buttons = this.toolbar.all('button'); - // On cursor moves we loops through the buttons. - var found = false, - index = 0, + var buttons = this.toolbar.all('button'), direction = 1, - checkCount = 0, - group, + button, current = e.target.ancestor('button', true); - // Determine which button is currently selected. - while (!found && index < buttons.size()) { - if (buttons.item(index) === current) { - found = true; - } else { - index++; - } - } - - if (!found) { - return; - } - if (e.keyCode === 37) { // Moving left so reverse the direction. direction = -1; } - // Try to find the next - do { + button = this._findFirstFocusable(buttons, current, direction); + if (button) { + button.focus(); + } else { + } + }, + + /** + * Find the first focusable button. + * + * @param {NodeList} buttons A list of nodes. + * @param {Node} startAt The node in the list to start the search from. + * @param {Number} direction The direction in which to search (1 or -1). + * @return {Node | Undefined} The Node or undefined. + * @method _findFirstFocusable + * @private + */ + _findFirstFocusable: function(buttons, startAt, direction) { + var checkCount = 0, + group, + candidate, + button, + index; + + // Determine which button to start the search from. + index = buttons.indexOf(startAt); + if (index < -1) { + index = 0; + } + + // Try to find the next. + while (checkCount < buttons.size()) { index += direction; if (index < 0) { index = buttons.size() - 1; @@ -780,21 +793,57 @@ EditorToolbarNav.prototype = { // Handle wrapping. index = 0; } - next = buttons.item(index); - group = next.ancestor('.atto_group'); + + candidate = buttons.item(index); // Add a counter to ensure we don't get stuck in a loop if there's only one visible menu item. checkCount++; - // Loop while: - // * we are not in a loop and have not already checked every button; and - // * we are on a different button; and - // * both the next button and the group it is in are not hidden. - } while (checkCount < buttons.size() && next !== current && (next.hasAttribute('hidden') || group.hasAttribute('hidden'))); - if (next) { - next.focus(); - this._setTabFocus(next); + // Loop while: + // * we haven't checked every button; + // * the button is hidden or disabled; + // * the group is hidden. + if (candidate.hasAttribute('hidden') || candidate.hasAttribute('disabled')) { + continue; + } + group = candidate.ancestor('.atto_group'); + if (group.hasAttribute('hidden')) { + continue; + } + + button = candidate; + break; } + + return button; + }, + + /** + * Check the tab focus. + * + * When we disable or hide a button, we should call this method to ensure that the + * focus is not currently set on an inaccessible button, otherwise tabbing to the toolbar + * would be impossible. + * + * @method checkTabFocus + * @chainable + */ + checkTabFocus: function() { + if (this._tabFocus) { + if (this._tabFocus.hasAttribute('disabled') || this._tabFocus.hasAttribute('hidden') + || this._tabFocus.ancestor('.atto_group').hasAttribute('hidden')) { + // Find first available button. + button = this._findFirstFocusable(this.toolbar.all('button'), this._tabFocus, -1); + if (button) { + if (this._tabFocus.compareTo(document.activeElement)) { + // We should also move the focus, because the inaccessible button also has the focus. + button.focus(); + } + this._setTabFocus(button); + } + } + } + return this; }, /** diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js index 910bcd2d7c1..df9caaeb555 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js @@ -882,6 +882,7 @@ EditorPluginButtons.prototype = { }, this); } + this.get('host').checkTabFocus(); return this; }, diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js index c3911c462d1..8a284419be5 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js @@ -1,2 +1,2 @@ YUI.add("moodle-editor_atto-plugin",function(e,t){function n(){n.superclass.constructor.apply(this,arguments)}function l(){}function c(){}var r=".atto_group.",i="_group";e.extend(n,e.Base,{name:null,editor:null,toolbar:null,initializer:function(e){this.name=e.name,this.toolbar=e.toolbar,this.editor=e.editor,this.buttons={},this.buttonNames=[],this.buttonStates={},this.menus={},this._primaryKeyboardShortcut=[],this._buttonHandlers=[],this._menuHideHandlers=[]},markUpdated:function(){return this.get("host").updateOriginal()}},{NAME:"editorPlugin",ATTRS:{host:{writeOnce:!0},group:{writeOnce:!0,getter:function(t){var n=this.toolbar.one(r+t+i);return n||(n=e.Node.create('
'),this.toolbar.append(n)),n}}}}),e.namespace("M.editor_atto").EditorPlugin=n;var s='',o="disabled",u="highlight",a="moodle-editor_atto-editor-plugin",f={EDITORWRAPPER:".editor_atto_content"};l.ATTRS={},l.prototype={buttons:null,buttonNames:null,buttonStates:null,menus:null,DISABLED:0,ENABLED:1,_buttonHandlers:null,_menuHideHandlers:null,_primaryKeyboardShortcut:null,addButton:function(t){var n=this.get("group"),r=this.name,i="atto_"+r+"_button",s,o=this.get("host");t.exec&&(i=i+"_"+t.exec),t.buttonName?i=i+"_"+t.buttonName:t.buttonName=t.exec||r,t.buttonClass=i,t=this._normalizeIcon(t),t.title||(t.title="pluginname");var u=M.util.get_string(t.title,"atto_"+r);s=e.Node.create('"),s.setAttribute("title",u),n.append(s);var a=this.toolbar.getAttribute("aria-activedescendant");a||(s.setAttribute("tabindex","0"),this.toolbar.setAttribute("aria-activedescendant",s.generateID()),this.get("host")._tabFocus=s),t=this._normalizeCallback(t),this._buttonHandlers.push(this.toolbar.delegate("click",t.callback,"."+i,this)),t.keys&&(typeof t.keyDescription!="undefined"&&(this._primaryKeyboardShortcut[i]=t.keyDescription),this._addKeyboardListener(t.callback,t.keys,i),this._primaryKeyboardShortcut[i]&&s.setAttribute("title",M.util.get_string("plugin_title_shortcut","editor_atto",{title:u,shortcut:this._primaryKeyboardShortcut[i]})));if(t.tags){var f=null;typeof t.tagMatchRequiresAll=="boolean"&&(f=t.tagMatchRequiresAll),this._buttonHandlers.push(o.on("atto:selectionchanged",function(e){o.selectionFilterMatches(t.tags,e.selectedNodes,f)?this.highlightButtons(t.buttonName):this.unHighlightButtons(t.buttonName)},this))}return this.buttonNames.push(t.buttonName),this.buttons[t.buttonName]=s,this.buttonStates[t.buttonName]=this.ENABLED,s},addBasicButton:function(e){return e.exec?(e.icon||(e.icon="e/"+e.exec),e.callback=function(){document.execCommand(e.exec,!1,null),this.markUpdated()},this.addButton(e)):null},addToolbarMenu:function(t){var n=this.get("group"),r=this.name,i="atto_"+r+"_button",o,u;t.buttonName?i=i+"_"+t.buttonName:t.buttonName=r,t.buttonClass=i,t=this._normalizeIcon(t),t.title||(t.title="pluginname");var a=M.util.get_string(t.title,"atto_"+r);t.menuColor||(t.menuColor="transparent");var f=e.Handlebars.compile(s);return o=e.Node.create(f({buttonClass:i,config:t,title:a})),n.append(o),u=this.toolbar.getAttribute("aria-activedescendant"),u||(o.setAttribute("tabindex","0"),this.toolbar.setAttribute("aria-activedescendant",o.generateID())),this._buttonHandlers.push(this.toolbar.delegate("click",this._showToolbarMenu,"."+i,this,t),this.toolbar.delegate("key",this._showToolbarMenuAndFocus,"40, 32, enter","."+i,this,t)),this.buttonNames.push(t.buttonName),this.buttons[t.buttonName]=o,this.buttonStates[t.buttonName]=this.ENABLED,o},_showToolbarMenu:function(t,n){t.preventDefault();if(!this.isEnabled())return;if(t.currentTarget.ancestor("button",!0).hasAttribute(o))return;var r;this.menus[n.buttonClass]||(n.overlayWidth||(n.overlayWidth="14"),n.innerOverlayWidth||(n.innerOverlayWidth=parseInt(n.overlayWidth,10)-2+"em"),n.overlayWidth=parseInt(n.overlayWidth,10)+"em",this.menus[n.buttonClass]=new e.M.editor_atto.Menu(n),this.menus[n.buttonClass].get("contentBox").delegate("click",this._chooseMenuItem,".atto_menuentry a",this,n));var i=this.buttons[n.buttonName];i.focus(),this.get("host")._setTabFocus(i),r=this.menus[n.buttonClass],r.set("focusAfterHide",i),r.on("focusAfterHide",function(e){console.log(e)}),r.show(),r.align(this.buttons[n.buttonName],[e.WidgetPositionAlign.TL,e.WidgetPositionAlign.BL])},_showToolbarMenuAndFocus:function(e,t){this._showToolbarMenu(e,t),this.menus[t.buttonClass].get("boundingBox").one("a").focus()},_chooseMenuItem:function(e,t,n){var r=e.target.ancestor("a",!0).getData("index"),i=this._normalizeCallback(t.items[r],t.globalItemConfig);n=this.menus[t.buttonClass],n.set("preventHideMenu",!0),i.callback(e,i._callback,i.callbackArgs),n.set("preventHideMenu",!1),console.log("Menu item chosen"),n.set("focusAfterHide",this.get("host").editor),n.hide(e)},_normalizeCallback:function(t,n){return t._callbackNormalized?t:(n||(n={}),typeof t.wrapCallback=="undefined"&&(t.wrapCallback=n.wrapCallback||!0),t.wrapCallback?(t._callback=t.callback||n.callback,t.callback=e.rbind(this._callbackWrapper,this,t._callback,t.callbackArgs)):t.callback=t.callback||n.callback,t._callbackNormalized=!0,t)},_normalizeIcon:function(e){return e.iconurl||(e.iconComponent||(e.iconComponent="core"),e.iconurl=M.util.image_url(e.icon,e.iconComponent)),e},_callbackWrapper:function(e,t,n){e.preventDefault();if(!this.isEnabled())return;var r=e.currentTarget.ancestor("button",!0);if(r&&r.hasAttribute(o))return;this.get("host").isActive()||this.get("host").focus(),r&&this.get("host")._setTabFocus(r -);var i=[e,n];return t.apply(this,i)},_addKeyboardListener:function(t,n,r){var i="key",s=f.EDITORWRAPPER,o;if(e.Lang.isArray(n))return e.Array.each(n,function(e){this._addKeyboardListener(t,e)},this),this;typeof n=="object"?(n.eventtype&&(i=n.eventtype),n.container&&(s=n.container),o=n.keyCodes):(o=this._getKeyEvent()+n+this._getDefaultMetaKey(),typeof this._primaryKeyboardShortcut[r]=="undefined"&&(this._primaryKeyboardShortcut[r]=this._getDefaultMetaKeyDescription(n))),this._buttonHandlers.push(this.editor.delegate(i,t,o,s,this))},isEnabled:function(){var t=e.Object.some(this.buttonStates,function(e){return e===this.ENABLED},this);return t},disableButtons:function(e){return this._setButtonState(!1,e)},enableButtons:function(e){return this._setButtonState(!0,e)},_setButtonState:function(t,n){var r="setAttribute";return t&&(r="removeAttribute"),n?this.buttons[n]&&(this.buttons[n][r](o,o),this.buttonStates[n]=t?this.ENABLED:this.DISABLED):e.Array.each(this.buttonNames,function(e){this.buttons[e][r](o,o),this.buttonStates[e]=t?this.ENABLED:this.DISABLED},this),this},highlightButtons:function(e){return this._changeButtonHighlight(!0,e)},unHighlightButtons:function(e){return this._changeButtonHighlight(!1,e)},_changeButtonHighlight:function(t,n){var r="addClass";return t||(r="removeClass"),n?this.buttons[n]&&this.buttons[n][r](u):e.Object.each(this.buttons,function(e){e[r](u)},this),this},_getDefaultMetaKey:function(){return e.UA.os==="macintosh"?"+meta":"+ctrl"},_getDefaultMetaKeyDescription:function(t){return e.UA.os==="macintosh"?M.util.get_string("editor_command_keycode","editor_atto",String.fromCharCode(t).toLowerCase()):M.util.get_string("editor_control_keycode","editor_atto",String.fromCharCode(t).toLowerCase())},_getKeyEvent:function(){return"down:"}},e.Base.mix(e.M.editor_atto.EditorPlugin,[l]),c.ATTRS={},c.prototype={_dialogue:null,getDialogue:function(t){t=t||{};var n=!1;t.focusAfterHide&&(n=t.focusAfterHide,delete t.focusAfterHide);if(!this._dialogue){var r=e.merge({visible:!1,modal:!0,close:!0,draggable:!0},t);this._dialogue=new M.core.dialogue(r)}return n!==!1&&(n===!0?this._dialogue.set("focusAfterHide",this.buttons[this.buttonNames[0]]):typeof n=="string"?this._dialogue.set("focusAfterHide",this.buttons[n]):this._dialogue.set("focusAfterHide",n)),this._dialogue}},e.Base.mix(e.M.editor_atto.EditorPlugin,[c])},"@VERSION@",{requires:["node","base","escape","event","event-outside","handlebars","event-custom"]}); +);var i=[e,n];return t.apply(this,i)},_addKeyboardListener:function(t,n,r){var i="key",s=f.EDITORWRAPPER,o;if(e.Lang.isArray(n))return e.Array.each(n,function(e){this._addKeyboardListener(t,e)},this),this;typeof n=="object"?(n.eventtype&&(i=n.eventtype),n.container&&(s=n.container),o=n.keyCodes):(o=this._getKeyEvent()+n+this._getDefaultMetaKey(),typeof this._primaryKeyboardShortcut[r]=="undefined"&&(this._primaryKeyboardShortcut[r]=this._getDefaultMetaKeyDescription(n))),this._buttonHandlers.push(this.editor.delegate(i,t,o,s,this))},isEnabled:function(){var t=e.Object.some(this.buttonStates,function(e){return e===this.ENABLED},this);return t},disableButtons:function(e){return this._setButtonState(!1,e)},enableButtons:function(e){return this._setButtonState(!0,e)},_setButtonState:function(t,n){var r="setAttribute";return t&&(r="removeAttribute"),n?this.buttons[n]&&(this.buttons[n][r](o,o),this.buttonStates[n]=t?this.ENABLED:this.DISABLED):e.Array.each(this.buttonNames,function(e){this.buttons[e][r](o,o),this.buttonStates[e]=t?this.ENABLED:this.DISABLED},this),this.get("host").checkTabFocus(),this},highlightButtons:function(e){return this._changeButtonHighlight(!0,e)},unHighlightButtons:function(e){return this._changeButtonHighlight(!1,e)},_changeButtonHighlight:function(t,n){var r="addClass";return t||(r="removeClass"),n?this.buttons[n]&&this.buttons[n][r](u):e.Object.each(this.buttons,function(e){e[r](u)},this),this},_getDefaultMetaKey:function(){return e.UA.os==="macintosh"?"+meta":"+ctrl"},_getDefaultMetaKeyDescription:function(t){return e.UA.os==="macintosh"?M.util.get_string("editor_command_keycode","editor_atto",String.fromCharCode(t).toLowerCase()):M.util.get_string("editor_control_keycode","editor_atto",String.fromCharCode(t).toLowerCase())},_getKeyEvent:function(){return"down:"}},e.Base.mix(e.M.editor_atto.EditorPlugin,[l]),c.ATTRS={},c.prototype={_dialogue:null,getDialogue:function(t){t=t||{};var n=!1;t.focusAfterHide&&(n=t.focusAfterHide,delete t.focusAfterHide);if(!this._dialogue){var r=e.merge({visible:!1,modal:!0,close:!0,draggable:!0},t);this._dialogue=new M.core.dialogue(r)}return n!==!1&&(n===!0?this._dialogue.set("focusAfterHide",this.buttons[this.buttonNames[0]]):typeof n=="string"?this._dialogue.set("focusAfterHide",this.buttons[n]):this._dialogue.set("focusAfterHide",n)),this._dialogue}},e.Base.mix(e.M.editor_atto.EditorPlugin,[c])},"@VERSION@",{requires:["node","base","escape","event","event-outside","handlebars","event-custom"]}); diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js index 957c3166ddf..ffbba1aefdb 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js @@ -878,6 +878,7 @@ EditorPluginButtons.prototype = { }, this); } + this.get('host').checkTabFocus(); return this; }, diff --git a/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js b/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js index 80dc30320db..786eb89c047 100644 --- a/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js +++ b/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js @@ -739,6 +739,7 @@ EditorPluginButtons.prototype = { }, this); } + this.get('host').checkTabFocus(); return this; }, diff --git a/lib/editor/atto/yui/src/editor/js/toolbar-keyboardnav.js b/lib/editor/atto/yui/src/editor/js/toolbar-keyboardnav.js index 91469a18dd3..3c6dd30ab81 100644 --- a/lib/editor/atto/yui/src/editor/js/toolbar-keyboardnav.js +++ b/lib/editor/atto/yui/src/editor/js/toolbar-keyboardnav.js @@ -70,37 +70,51 @@ EditorToolbarNav.prototype = { // Prevent the default browser behaviour. e.preventDefault(); - var buttons = this.toolbar.all('button'); - // On cursor moves we loops through the buttons. - var found = false, - index = 0, + var buttons = this.toolbar.all('button'), direction = 1, - checkCount = 0, - group, + button, current = e.target.ancestor('button', true); - // Determine which button is currently selected. - while (!found && index < buttons.size()) { - if (buttons.item(index) === current) { - found = true; - } else { - index++; - } - } - - if (!found) { - Y.log("Unable to find this button in the list of buttons", 'debug', LOGNAME); - return; - } - if (e.keyCode === 37) { // Moving left so reverse the direction. direction = -1; } - // Try to find the next - do { + button = this._findFirstFocusable(buttons, current, direction); + if (button) { + button.focus(); + } else { + Y.log("Unable to find a button to focus on", 'debug', LOGNAME); + } + }, + + /** + * Find the first focusable button. + * + * @param {NodeList} buttons A list of nodes. + * @param {Node} startAt The node in the list to start the search from. + * @param {Number} direction The direction in which to search (1 or -1). + * @return {Node | Undefined} The Node or undefined. + * @method _findFirstFocusable + * @private + */ + _findFirstFocusable: function(buttons, startAt, direction) { + var checkCount = 0, + group, + candidate, + button, + index; + + // Determine which button to start the search from. + index = buttons.indexOf(startAt); + if (index < -1) { + Y.log("Unable to find the button in the list of buttons", 'debug', LOGNAME); + index = 0; + } + + // Try to find the next. + while (checkCount < buttons.size()) { index += direction; if (index < 0) { index = buttons.size() - 1; @@ -108,21 +122,57 @@ EditorToolbarNav.prototype = { // Handle wrapping. index = 0; } - next = buttons.item(index); - group = next.ancestor('.atto_group'); + + candidate = buttons.item(index); // Add a counter to ensure we don't get stuck in a loop if there's only one visible menu item. checkCount++; - // Loop while: - // * we are not in a loop and have not already checked every button; and - // * we are on a different button; and - // * both the next button and the group it is in are not hidden. - } while (checkCount < buttons.size() && next !== current && (next.hasAttribute('hidden') || group.hasAttribute('hidden'))); - if (next) { - next.focus(); - this._setTabFocus(next); + // Loop while: + // * we haven't checked every button; + // * the button is hidden or disabled; + // * the group is hidden. + if (candidate.hasAttribute('hidden') || candidate.hasAttribute('disabled')) { + continue; + } + group = candidate.ancestor('.atto_group'); + if (group.hasAttribute('hidden')) { + continue; + } + + button = candidate; + break; } + + return button; + }, + + /** + * Check the tab focus. + * + * When we disable or hide a button, we should call this method to ensure that the + * focus is not currently set on an inaccessible button, otherwise tabbing to the toolbar + * would be impossible. + * + * @method checkTabFocus + * @chainable + */ + checkTabFocus: function() { + if (this._tabFocus) { + if (this._tabFocus.hasAttribute('disabled') || this._tabFocus.hasAttribute('hidden') + || this._tabFocus.ancestor('.atto_group').hasAttribute('hidden')) { + // Find first available button. + button = this._findFirstFocusable(this.toolbar.all('button'), this._tabFocus, -1); + if (button) { + if (this._tabFocus.compareTo(document.activeElement)) { + // We should also move the focus, because the inaccessible button also has the focus. + button.focus(); + } + this._setTabFocus(button); + } + } + } + return this; }, /**