MDL-70580 core: Tree nodes should check if they were clicked directly
[moodle.git] / lib / amd / build / tree.min.js
CommitLineData
357909b2 1define ("core/tree",["jquery"],function(a){var b={ITEM:"[role=treeitem]",GROUP:"[role=treeitem]:has([role=group]), [role=treeitem][aria-owns], [role=treeitem][data-requires-ajax=true]",CLOSED_GROUP:"[role=treeitem]:has([role=group])[aria-expanded=false], [role=treeitem][aria-owns][aria-expanded=false], [role=treeitem][data-requires-ajax=true][aria-expanded=false]",FIRST_ITEM:"[role=treeitem]:first",VISIBLE_ITEM:"[role=treeitem]:visible",UNLOADED_AJAX_ITEM:"[role=treeitem][data-requires-ajax=true][data-loaded=false][aria-expanded=true]"},c=function(c,d){this.treeRoot=a(c);this.treeRoot.data("activeItem",null);this.selectCallback=d;this.keys={tab:9,enter:13,space:32,pageup:33,pagedown:34,end:35,home:36,left:37,up:38,right:39,down:40,asterisk:106};this.initialiseNodes(this.treeRoot);this.setActiveItem(this.treeRoot.find(b.FIRST_ITEM));this.refreshVisibleItemsCache();this.bindEventHandlers()};c.prototype.registerEnterCallback=function(a){this.enterCallback=a};c.prototype.refreshVisibleItemsCache=function(){this.treeRoot.data("visibleItems",this.treeRoot.find(b.VISIBLE_ITEM))};c.prototype.getVisibleItems=function(){return this.treeRoot.data("visibleItems")};c.prototype.setActiveItem=function(a){var b=this.treeRoot.data("activeItem");if(a===b){return}if(b){b.attr("tabindex","-1");b.attr("aria-selected","false")}a.attr("tabindex","0");a.attr("aria-selected","true");this.treeRoot.data("activeItem",a);if("function"==typeof this.selectCallback){this.selectCallback(a)}};c.prototype.isGroupItem=function(a){return a.is(b.GROUP)};c.prototype.getGroupFromItem=function(a){var b=this.treeRoot.find("#"+a.attr("aria-owns")),c=a.children("[role=group]");if(b.length>c.length){return b}else{return c}};c.prototype.isGroupCollapsed=function(a){return"false"===a.attr("aria-expanded")};c.prototype.isGroupCollapsible=function(a){return"false"!==a.attr("data-collapsible")};c.prototype.initialiseNodes=function(c){this.removeAllFromTabOrder(c);this.setAriaSelectedFalseOnItems(c);var d=this;c.find(b.UNLOADED_AJAX_ITEM).each(function(){var b=a(this);d.collapseGroup(b);d.expandGroup(b)})};c.prototype.removeAllFromTabOrder=function(b){b.find("*").attr("tabindex","-1");this.getGroupFromItem(a(b)).find("*").attr("tabindex","-1")};c.prototype.setAriaSelectedFalseOnItems=function(a){a.find(b.ITEM).attr("aria-selected","false")};c.prototype.expandAllGroups=function(){var c=this;this.treeRoot.find(b.CLOSED_GROUP).each(function(){var b=a(this);c.expandGroup(a(this)).done(function(){c.expandAllChildGroups(b)})})};c.prototype.expandAllChildGroups=function(c){var d=this;this.getGroupFromItem(c).find(b.CLOSED_GROUP).each(function(){var b=a(this);d.expandGroup(a(this)).done(function(){d.expandAllChildGroups(b)})})};c.prototype.expandGroup=function(b){var c=a.Deferred();if("false"!==b.attr("data-expandable")&&this.isGroupCollapsed(b)){if("true"===b.attr("data-requires-ajax")&&"true"!==b.attr("data-loaded")){b.attr("data-loaded",!1);var d=b.closest("[data-ajax-loader]").attr("data-ajax-loader"),e=this;b.addClass("loading");require([d],function(a){a.load(b).done(function(){b.attr("data-loaded",!0);e.initialiseNodes(b);e.finishExpandingGroup(b);b.removeClass("loading");c.resolve()})})}else{this.finishExpandingGroup(b);c.resolve()}}else{c.resolve()}return c};c.prototype.finishExpandingGroup=function(a){var b=this.getGroupFromItem(a);b.removeAttr("aria-hidden");a.attr("aria-expanded","true");this.refreshVisibleItemsCache()};c.prototype.collapseGroup=function(a){if(!this.isGroupCollapsible(a)||this.isGroupCollapsed(a)){return}var b=this.getGroupFromItem(a);b.attr("aria-hidden","true");a.attr("aria-expanded","false");this.refreshVisibleItemsCache()};c.prototype.toggleGroup=function(a){if("true"===a.attr("aria-expanded")){this.collapseGroup(a)}else{this.expandGroup(a)}};c.prototype.handleKeyDown=function(c){var d=a(c.target),e=this.getVisibleItems().index(d);if(c.altKey||c.ctrlKey||c.metaKey||c.shiftKey&&c.keyCode!=this.keys.tab){return}switch(c.keyCode){case this.keys.home:{this.getVisibleItems().first().focus();c.preventDefault();return}case this.keys.end:{this.getVisibleItems().last().focus();c.preventDefault();return}case this.keys.enter:{var f=d.children("a").length?d.children("a"):d.children().not(b.GROUP).find("a");if(f.length){if(f.first().data("overrides-tree-activation-key-handler")){f.first().triggerHandler(c)}else if("function"==typeof this.enterCallback){this.enterCallback(d)}else{window.location.href=f.first().attr("href")}}else if(this.isGroupItem(d)){this.toggleGroup(d,!0)}c.preventDefault();return}case this.keys.space:{if(this.isGroupItem(d)){this.toggleGroup(d,!0)}else if(d.children("a").length){var g=d.children("a").first();if(g.data("overrides-tree-activation-key-handler")){g.triggerHandler(c)}}c.preventDefault();return}case this.keys.left:{var h=function(b){b.getVisibleItems().filter(function(){return b.getGroupFromItem(a(this)).has(d).length}).focus()};if(this.isGroupItem(d)){if(this.isGroupCollapsed(d)){h(this)}else{this.collapseGroup(d)}}else{h(this)}c.preventDefault();return}case this.keys.right:{if(this.isGroupItem(d)){if(this.isGroupCollapsed(d)){this.expandGroup(d)}else{this.getGroupFromItem(d).find(b.ITEM).first().focus()}}c.preventDefault();return}case this.keys.up:{if(0<e){var i=this.getVisibleItems().eq(e-1);i.focus()}c.preventDefault();return}case this.keys.down:{if(e<this.getVisibleItems().length-1){var j=this.getVisibleItems().eq(e+1);j.focus()}c.preventDefault();return}case this.keys.asterisk:{this.expandAllGroups();c.preventDefault()}}};c.prototype.handleClick=function(b){if(b.altKey||b.ctrlKey||b.shiftKey||b.metaKey){return}var c=a(b.target);if(b.target!==b.currentTarget){return}c.focus();if(this.isGroupItem(c)){this.toggleGroup(c)}};c.prototype.handleFocus=function(b){this.setActiveItem(a(b.target))};c.prototype.bindEventHandlers=function(){this.treeRoot.on({click:this.handleClick.bind(this),keydown:this.handleKeyDown.bind(this),focus:this.handleFocus.bind(this)},b.ITEM)};return c});
195ea448 2//# sourceMappingURL=tree.min.js.map