MDL-51957 competencies: Improvements to event handling
Add custom .on methods instead of expecting code outside the module to know which node to bind to to listen for events. Also keep the selectionChanged handler private to the competencyactions module. Finally - fix the broken move competency action.
This commit is contained in:
committed by
Frederic Massart
parent
85dbaf91ba
commit
24f0680ca6
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -1 +1 @@
|
||||
define(["jquery","core/notification","core/ajax","core/templates","tool_lp/dialogue","core/str","tool_lp/tree"],function(a,b,c,d,e,f,g){var h,i=function(b,c,d){h=this,h._eventNode=a("<div></div>"),h._frameworks=[],h._reset(),h._pageContextId=b,h._pageContextIncludes=d||"children",c&&(h._frameworkId=c,h._singleFramework=!0)};return i.prototype._competencies=null,i.prototype._disallowedCompetencyIDs=null,i.prototype._eventNode=null,i.prototype._frameworks=null,i.prototype._frameworkId=null,i.prototype._pageContextId=null,i.prototype._pageContextIncludes=null,i.prototype._popup=null,i.prototype._searchText="",i.prototype._selectedCompetency=null,i.prototype._singleFramework=!1,i.prototype._afterRender=function(){new g(h._find("[data-enhance=linktree]")),h._find("[data-enhance=linktree]").on("change",function(b,c){var d=c.data("id"),e=!0;b.preventDefault(),a.each(h._disallowedCompetencyIDs,function(a,b){return b==d?(e=!1,!1):void 0}),h._selectedCompetency=null,e&&(h._selectedCompetency=d),h._selectedCompetency?h._find('[data-region="competencylinktree"] [data-action="add"]').removeAttr("disabled"):h._find('[data-region="competencylinktree"] [data-action="add"]').attr("disabled","disabled")}),h._singleFramework||h._find('[data-action="chooseframework"]').change(function(b){h._frameworkId=a(b.target).val(),h._loadCompetencies().then(h._refresh)}),h._find('[data-region="filtercompetencies"] button').click(function(b){return b.preventDefault(),a(b.target).attr("disabled","disabled"),h._searchText=h._find('[data-region="filtercompetencies"] input').val()||"",h._refresh().always(function(){a(b.target).removeAttr("disabled")})}),h._find('[data-region="competencylinktree"] [data-action="cancel"]').click(function(a){a.preventDefault(),h.close()}),h._find('[data-region="competencylinktree"] [data-action="add"]').click(function(a){a.preventDefault(),h._selectedCompetency&&(h._trigger("save",{competencyId:h._selectedCompetency}),h.close())})},i.prototype.close=function(){h._popup.close(),h._reset()},i.prototype.display=function(){return h._render().done(function(a){return f.get_string("competencypicker","tool_lp").done(function(b){h._popup=new e(b,a,h._afterRender.bind(h))})}).fail(b.exception)},i.prototype._fetchCompetencies=function(a,d){return c.call([{methodname:"tool_lp_search_competencies",args:{searchtext:d,competencyframeworkid:a}}])[0].done(function(a){function b(a,c){for(var d=0;d<c.length;d++)c[d].parentid==a.id&&(a.haschildren=!0,c[d].children=[],c[d].haschildren=!1,a.children[a.children.length]=c[d],b(c[d],c))}var c,d,e=[];for(c=0;c<a.length;c++)d=a[c],"0"==d.parentid&&(d.children=[],d.haschildren=0,e[e.length]=d,b(d,a));h._competencies=e}).fail(b.exception)},i.prototype._find=function(b){return a(h._popup.getContent()).find(b)},i.prototype._getFramework=function(b){var c;return a.each(h._frameworks,function(a,d){return d.id==b?(c=d,!1):void 0}),c},i.prototype._loadCompetencies=function(){return h._fetchCompetencies(h._frameworkId,h._searchText)},i.prototype._loadFrameworks=function(){var d;return h._frameworks.length>0?a.when():(d=h._singleFramework?c.call([{methodname:"tool_lp_read_competency_framework",args:{id:this._frameworkId}}])[0].then(function(a){return[a]}):c.call([{methodname:"tool_lp_list_competency_frameworks",args:{sort:"shortname",context:{contextid:h._pageContextId},includes:h._pageContextIncludes}}])[0],d.done(function(a){h._frameworks=a}).fail(b.exception))},i.prototype.on=function(a,b){h._eventNode.on(a,b)},i.prototype._preRender=function(){return h._loadFrameworks().then(function(){return!h._frameworkId&&h._frameworks.length>0&&(h._frameworkId=h._frameworks[0].id),h._frameworkId?h._loadCompetencies():(h._frameworks=[],a.when())})},i.prototype._refresh=function(){return h._render().then(function(a){h._find('[data-region="competencylinktree"]').replaceWith(a),h._afterRender()})},i.prototype._render=function(){return h._preRender().then(function(){h._singleFramework||a.each(h._frameworks,function(a,b){b.id==h._frameworkId?b.selected=!0:b.selected=!1});var b={competencies:h._competencies,framework:h._getFramework(h._frameworkId),frameworks:h._frameworks,search:h._searchText,singleFramework:h._singleFramework};return d.render("tool_lp/competency_picker",b)})},i.prototype._reset=function(){h._competencies=[],h._disallowedCompetencyIDs=[],h._popup=null,h._searchText="",h._selectedCompetency=null},i.prototype.setDisallowedCompetencyIDs=function(a){h._disallowedCompetencyIDs=a},i.prototype._trigger=function(a,b){h._eventNode.trigger(a,[b])},i});
|
||||
define(["jquery","core/notification","core/ajax","core/templates","tool_lp/dialogue","core/str","tool_lp/tree"],function(a,b,c,d,e,f,g){var h,i=function(b,c,d){h=this,h._eventNode=a("<div></div>"),h._frameworks=[],h._reset(),h._pageContextId=b,h._pageContextIncludes=d||"children",c&&(h._frameworkId=c,h._singleFramework=!0)};return i.prototype._competencies=null,i.prototype._disallowedCompetencyIDs=null,i.prototype._eventNode=null,i.prototype._frameworks=null,i.prototype._frameworkId=null,i.prototype._pageContextId=null,i.prototype._pageContextIncludes=null,i.prototype._popup=null,i.prototype._searchText="",i.prototype._selectedCompetency=null,i.prototype._singleFramework=!1,i.prototype._afterRender=function(){var b=new g(h._find("[data-enhance=linktree]"));b.on("selectionchanged",function(b,c){var d=c.data("id"),e=!0;a.each(h._disallowedCompetencyIDs,function(a,b){return b==d?(e=!1,!1):void 0}),h._selectedCompetency=null,e&&(h._selectedCompetency=d),h._selectedCompetency?h._find('[data-region="competencylinktree"] [data-action="add"]').removeAttr("disabled"):h._find('[data-region="competencylinktree"] [data-action="add"]').attr("disabled","disabled")}),h._singleFramework||h._find('[data-action="chooseframework"]').change(function(b){h._frameworkId=a(b.target).val(),h._loadCompetencies().then(h._refresh)}),h._find('[data-region="filtercompetencies"] button').click(function(b){return b.preventDefault(),a(b.target).attr("disabled","disabled"),h._searchText=h._find('[data-region="filtercompetencies"] input').val()||"",h._refresh().always(function(){a(b.target).removeAttr("disabled")})}),h._find('[data-region="competencylinktree"] [data-action="cancel"]').click(function(a){a.preventDefault(),h.close()}),h._find('[data-region="competencylinktree"] [data-action="add"]').click(function(a){a.preventDefault(),h._selectedCompetency&&(h._trigger("save",{competencyId:h._selectedCompetency}),h.close())})},i.prototype.close=function(){h._popup.close(),h._reset()},i.prototype.display=function(){return h._render().done(function(a){return f.get_string("competencypicker","tool_lp").done(function(b){h._popup=new e(b,a,h._afterRender.bind(h))})}).fail(b.exception)},i.prototype._fetchCompetencies=function(a,d){return c.call([{methodname:"tool_lp_search_competencies",args:{searchtext:d,competencyframeworkid:a}}])[0].done(function(a){function b(a,c){for(var d=0;d<c.length;d++)c[d].parentid==a.id&&(a.haschildren=!0,c[d].children=[],c[d].haschildren=!1,a.children[a.children.length]=c[d],b(c[d],c))}var c,d,e=[];for(c=0;c<a.length;c++)d=a[c],"0"==d.parentid&&(d.children=[],d.haschildren=0,e[e.length]=d,b(d,a));h._competencies=e}).fail(b.exception)},i.prototype._find=function(b){return a(h._popup.getContent()).find(b)},i.prototype._getFramework=function(b){var c;return a.each(h._frameworks,function(a,d){return d.id==b?(c=d,!1):void 0}),c},i.prototype._loadCompetencies=function(){return h._fetchCompetencies(h._frameworkId,h._searchText)},i.prototype._loadFrameworks=function(){var d;return h._frameworks.length>0?a.when():(d=h._singleFramework?c.call([{methodname:"tool_lp_read_competency_framework",args:{id:this._frameworkId}}])[0].then(function(a){return[a]}):c.call([{methodname:"tool_lp_list_competency_frameworks",args:{sort:"shortname",context:{contextid:h._pageContextId},includes:h._pageContextIncludes}}])[0],d.done(function(a){h._frameworks=a}).fail(b.exception))},i.prototype.on=function(a,b){h._eventNode.on(a,b)},i.prototype._preRender=function(){return h._loadFrameworks().then(function(){return!h._frameworkId&&h._frameworks.length>0&&(h._frameworkId=h._frameworks[0].id),h._frameworkId?h._loadCompetencies():(h._frameworks=[],a.when())})},i.prototype._refresh=function(){return h._render().then(function(a){h._find('[data-region="competencylinktree"]').replaceWith(a),h._afterRender()})},i.prototype._render=function(){return h._preRender().then(function(){h._singleFramework||a.each(h._frameworks,function(a,b){b.id==h._frameworkId?b.selected=!0:b.selected=!1});var b={competencies:h._competencies,framework:h._getFramework(h._frameworkId),frameworks:h._frameworks,search:h._searchText,singleFramework:h._singleFramework};return d.render("tool_lp/competency_picker",b)})},i.prototype._reset=function(){h._competencies=[],h._disallowedCompetencyIDs=[],h._popup=null,h._searchText="",h._selectedCompetency=null},i.prototype.setDisallowedCompetencyIDs=function(a){h._disallowedCompetencyIDs=a},i.prototype._trigger=function(a,b){h._eventNode.trigger(a,[b])},i});
|
||||
+1
-1
@@ -1 +1 @@
|
||||
define(["core/ajax","core/notification","core/templates","tool_lp/tree","jquery"],function(a,b,c,d,e){var f=[],g=0,h="",i="",j=function(a,b){var c=0,d=!1;for(a.haschildren=!1,a.children=[],c=0;c<b.length;c++)d=b[c],d.parentid==a.id&&(a.haschildren=!0,a.children.push(d),j(d,b))},k=function(k){var l=e.Deferred(),m=a.call([{methodname:"tool_lp_search_competencies",args:{searchtext:k,competencyframeworkid:g,includerelated:!0}}]);return m[0].done(function(a){f=[];var g=0;for(g=0;g<a.length;g++)f[a[g].id]=a[g];var k=[],m=!1;for(g=0;g<a.length;g++)m=a[g],0===parseInt(m.parentid,10)&&(k.push(m),j(m,a));var n={shortname:h,competencies:k};c.render("tool_lp/competencies_tree_root",n).done(function(a,b){c.replaceNodeContents(e(i),e(a).html(),b),new d(i),l.resolve(f)}).fail(b.exception)}).fail(function(a){l.reject(a)}),l.promise()};return{init:function(a,c,d,e){g=a,h=c,i=e,k(d).fail(b.exception)},getCompetencyFrameworkId:function(){return g},getCompetency:function(a){return f[a]},getCompetencyLevel:function(a){var b=this.getCompetency(a),c=b.path.replace(/^\/|\/$/g,"").split("/").length;return c},reloadCompetencies:function(){return k("").fail(b.exception)},listCompetencies:function(){return f}}});
|
||||
define(["core/ajax","core/notification","core/templates","tool_lp/tree","jquery"],function(a,b,c,d,e){var f=[],g=0,h="",i="",j=function(a,b){var c=0,d=!1;for(a.haschildren=!1,a.children=[],c=0;c<b.length;c++)d=b[c],d.parentid==a.id&&(a.haschildren=!0,a.children.push(d),j(d,b))},k=function(k){var l=e.Deferred(),m=a.call([{methodname:"tool_lp_search_competencies",args:{searchtext:k,competencyframeworkid:g,includerelated:!0}}]);return m[0].done(function(a){f=[];var g=0;for(g=0;g<a.length;g++)f[a[g].id]=a[g];var k=[],m=!1;for(g=0;g<a.length;g++)m=a[g],0===parseInt(m.parentid,10)&&(k.push(m),j(m,a));var n={shortname:h,competencies:k};c.render("tool_lp/competencies_tree_root",n).done(function(a,b){c.replaceNodeContents(e(i),e(a).html(),b),new d(i),l.resolve(f)}).fail(b.exception)}).fail(function(a){l.reject(a)}),l.promise()};return{init:function(a,c,d,e){g=a,h=c,i=e,k(d).fail(b.exception)},on:function(a,b){e(i).on(a,b)},getCompetencyFrameworkId:function(){return g},getCompetency:function(a){return f[a]},getCompetencyLevel:function(a){var b=this.getCompetency(a),c=b.path.replace(/^\/|\/$/g,"").split("/").length;return c},reloadCompetencies:function(){return k("").fail(b.exception)},listCompetencies:function(){return f}}});
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
define(["jquery","core/url"],function(a,b){var c=a('<img alt="" src="'+b.imageUrl("t/expanded")+'"/>'),d=a('<img alt="" src="'+b.imageUrl("t/collapsed")+'"/>'),e=function(b){this.treeRoot=a(b),this.items=this.treeRoot.find("li"),this.parents=this.treeRoot.find("li:has(ul)"),this.visibleItems=null,this.activeItem=null,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.init(),this.bindEventHandlers()};return e.prototype.init=function(){this.parents.attr("aria-expanded","true"),this.parents.prepend(c.clone()),this.items.attr("role","tree-item"),this.items.attr("tabindex","-1"),this.parents.attr("role","group"),this.treeRoot.attr("role","tree"),this.visibleItems=this.treeRoot.find("li")},e.prototype.expandGroup=function(a){var b=a.children("ul");b.show().attr("aria-hidden","false"),a.attr("aria-expanded","true"),a.children("img").attr("src",c.attr("src")),this.visibleItems=this.treeRoot.find("li:visible")},e.prototype.collapseGroup=function(a){var b=a.children("ul");b.hide().attr("aria-hidden","true"),a.attr("aria-expanded","false"),a.children("img").attr("src",d.attr("src")),this.visibleItems=this.treeRoot.find("li:visible")},e.prototype.toggleGroup=function(a){"true"==a.attr("aria-expanded")?this.collapseGroup(a):this.expandGroup(a)},e.prototype.updateFocus=function(a){this.items.attr("aria-selected","false").attr("tabindex","-1"),a.attr("aria-selected","true").attr("tabindex",0),this.treeRoot.trigger("change",[a])},e.prototype.handleKeyDown=function(b,c){var d=this.visibleItems.index(b);if(c.altKey||c.ctrlKey||c.shiftKey&&c.keyCode!=this.keys.tab)return!0;switch(c.keyCode){case this.keys.home:return this.activeItem=this.parents.first(),this.activeItem.focus(),c.stopPropagation(),!1;case this.keys.end:return this.activeItem=this.visibleItems.last(),this.activeItem.focus(),c.stopPropagation(),!1;case this.keys.enter:case this.keys.space:return b.has("ul")&&this.toggleGroup(b,!0),c.stopPropagation(),!1;case this.keys.left:if(b.has("ul")&&"true"==b.attr("aria-expanded"))this.collapseGroup(b);else{var e=b.parent(),f=e.parent();this.activeItem=f,this.activeItem.focus()}return c.stopPropagation(),!1;case this.keys.right:return b.has("ul")&&"false"==b.attr("aria-expanded")?this.expandGroup(b):(this.activeItem=b.children("ul").children("li").first(),this.activeItem.focus()),c.stopPropagation(),!1;case this.keys.up:if(d>0){var g=this.visibleItems.eq(d-1);this.activeItem=g,g.focus()}return c.stopPropagation(),!1;case this.keys.down:if(d<this.visibleItems.length-1){var h=this.visibleItems.eq(d+1);this.activeItem=h,h.focus()}return c.stopPropagation(),!1;case this.keys.asterisk:var i=this;return this.parents.each(function(){i.expandGroup(a(this))}),c.stopPropagation(),!1}return!0},e.prototype.handleKeyPress=function(a,b){if(b.altKey||b.ctrlKey||b.shiftKey)return!0;switch(b.keyCode){case this.keys.tab:return!0;case this.keys.enter:case this.keys.home:case this.keys.end:case this.keys.left:case this.keys.right:case this.keys.up:case this.keys.down:return b.stopPropagation(),!1;default:var c=String.fromCharCode(b.which),d=!1,e=this.visibleItems.index(a),f=this.visibleItems.length,g=e+1;for(g==f&&(g=0);g!=e;){var h=this.visibleItems.eq(g),i=h.text().charAt(0);if(h.has("ul")&&(i=h.find("span").text().charAt(0)),i.toLowerCase()==c){d=!0;break}g+=1,g==f&&(g=0)}return d===!0&&(this.activeItem=this.visibleItems.eq(g),this.activeItem.focus()),b.stopPropagation(),!1}return!0},e.prototype.handleDblClick=function(a,b){return b.altKey||b.ctrlKey||b.shiftKey?!0:(this.activeItem=a,this.updateFocus(a),this.toggleGroup(a),b.stopPropagation(),!1)},e.prototype.handleClick=function(a,b){return b.altKey||b.ctrlKey||b.shiftKey?!0:(this.activeItem=a,this.updateFocus(a),b.stopPropagation(),!1)},e.prototype.handleBlur=function(){return!0},e.prototype.handleFocus=function(a){return null===this.activeItem&&(this.activeItem=a),this.updateFocus(this.activeItem),!0},e.prototype.bindEventHandlers=function(){var b=this;this.parents.dblclick(function(c){return b.handleDblClick(a(this),c)}),this.items.click(function(c){return b.handleClick(a(this),c)}),this.items.keydown(function(c){return b.handleKeyDown(a(this),c)}),this.items.keypress(function(c){return b.handleKeyPress(a(this),c)}),this.items.focus(function(c){return b.handleFocus(a(this),c)}),this.items.blur(function(c){return b.handleBlur(a(this),c)})},e});
|
||||
define(["jquery","core/url","core/log"],function(a,b,c){var d=a('<img alt="" src="'+b.imageUrl("t/expanded")+'"/>'),e=a('<img alt="" src="'+b.imageUrl("t/collapsed")+'"/>'),f=function(b){this.treeRoot=a(b),this.items=this.treeRoot.find("li"),this.parents=this.treeRoot.find("li:has(ul)"),this.visibleItems=null,this.activeItem=null,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.init(),this.bindEventHandlers()};return f.prototype.init=function(){this.parents.attr("aria-expanded","true"),this.parents.prepend(d.clone()),this.items.attr("role","tree-item"),this.items.attr("tabindex","-1"),this.parents.attr("role","group"),this.treeRoot.attr("role","tree"),this.visibleItems=this.treeRoot.find("li")},f.prototype.expandGroup=function(a){var b=a.children("ul");b.show().attr("aria-hidden","false"),a.attr("aria-expanded","true"),a.children("img").attr("src",d.attr("src")),this.visibleItems=this.treeRoot.find("li:visible")},f.prototype.collapseGroup=function(a){var b=a.children("ul");b.hide().attr("aria-hidden","true"),a.attr("aria-expanded","false"),a.children("img").attr("src",e.attr("src")),this.visibleItems=this.treeRoot.find("li:visible")},f.prototype.toggleGroup=function(a){"true"==a.attr("aria-expanded")?this.collapseGroup(a):this.expandGroup(a)},f.prototype.updateFocus=function(a){this.items.attr("aria-selected","false").attr("tabindex","-1"),a.attr("aria-selected","true").attr("tabindex",0),this.treeRoot.trigger("selectionchanged",[a])},f.prototype.handleKeyDown=function(b,c){var d=this.visibleItems.index(b);if(c.altKey||c.ctrlKey||c.shiftKey&&c.keyCode!=this.keys.tab)return!0;switch(c.keyCode){case this.keys.home:return this.activeItem=this.parents.first(),this.activeItem.focus(),c.stopPropagation(),!1;case this.keys.end:return this.activeItem=this.visibleItems.last(),this.activeItem.focus(),c.stopPropagation(),!1;case this.keys.enter:case this.keys.space:return b.has("ul")&&this.toggleGroup(b,!0),c.stopPropagation(),!1;case this.keys.left:if(b.has("ul")&&"true"==b.attr("aria-expanded"))this.collapseGroup(b);else{var e=b.parent(),f=e.parent();this.activeItem=f,this.activeItem.focus()}return c.stopPropagation(),!1;case this.keys.right:return b.has("ul")&&"false"==b.attr("aria-expanded")?this.expandGroup(b):(this.activeItem=b.children("ul").children("li").first(),this.activeItem.focus()),c.stopPropagation(),!1;case this.keys.up:if(d>0){var g=this.visibleItems.eq(d-1);this.activeItem=g,g.focus()}return c.stopPropagation(),!1;case this.keys.down:if(d<this.visibleItems.length-1){var h=this.visibleItems.eq(d+1);this.activeItem=h,h.focus()}return c.stopPropagation(),!1;case this.keys.asterisk:var i=this;return this.parents.each(function(){i.expandGroup(a(this))}),c.stopPropagation(),!1}return!0},f.prototype.handleKeyPress=function(a,b){if(b.altKey||b.ctrlKey||b.shiftKey)return!0;switch(b.keyCode){case this.keys.tab:return!0;case this.keys.enter:case this.keys.home:case this.keys.end:case this.keys.left:case this.keys.right:case this.keys.up:case this.keys.down:return b.stopPropagation(),!1;default:var c=String.fromCharCode(b.which),d=!1,e=this.visibleItems.index(a),f=this.visibleItems.length,g=e+1;for(g==f&&(g=0);g!=e;){var h=this.visibleItems.eq(g),i=h.text().charAt(0);if(h.has("ul")&&(i=h.find("span").text().charAt(0)),i.toLowerCase()==c){d=!0;break}g+=1,g==f&&(g=0)}return d===!0&&(this.activeItem=this.visibleItems.eq(g),this.activeItem.focus()),b.stopPropagation(),!1}return!0},f.prototype.on=function(a,b){"selectionchanged"!==a?c.warning('Invalid custom event name for tree. Only "selectionchanged" is supported.'):this.treeRoot.on(a,b)},f.prototype.handleDblClick=function(a,b){return b.altKey||b.ctrlKey||b.shiftKey?!0:(this.activeItem=a,this.updateFocus(a),this.toggleGroup(a),b.stopPropagation(),!1)},f.prototype.handleClick=function(a,b){return b.altKey||b.ctrlKey||b.shiftKey?!0:(this.activeItem=a,this.updateFocus(a),b.stopPropagation(),!1)},f.prototype.handleBlur=function(){return!0},f.prototype.handleFocus=function(a){return null===this.activeItem&&(this.activeItem=a),this.updateFocus(this.activeItem),!0},f.prototype.bindEventHandlers=function(){var b=this;this.parents.dblclick(function(c){return b.handleDblClick(a(this),c)}),this.items.click(function(c){return b.handleClick(a(this),c)}),this.items.keydown(function(c){return b.handleKeyDown(a(this),c)}),this.items.keypress(function(c){return b.handleKeyPress(a(this),c)}),this.items.focus(function(c){return b.handleFocus(a(this),c)}),this.items.blur(function(c){return b.handleBlur(a(this),c)})},f});
|
||||
@@ -101,7 +101,8 @@ define(['jquery',
|
||||
* @param {dialogue} popup The tool_lp/dialogue that was created.
|
||||
*/
|
||||
var initMovePopup = function(popup) {
|
||||
new Ariatree('[data-enhance=movetree]', function(target) {
|
||||
var tree = new Ariatree('[data-enhance=movetree]');
|
||||
tree.on('selectionchanged', function(evt, target) {
|
||||
moveTarget = $(target).data('id');
|
||||
});
|
||||
|
||||
@@ -146,8 +147,8 @@ define(['jquery',
|
||||
methodname: 'tool_lp_search_competencies',
|
||||
args: {
|
||||
competencyframeworkid: competency.competencyframeworkid,
|
||||
context: { contextid: pageContextId },
|
||||
searchtext: ''
|
||||
searchtext: '',
|
||||
includerelated: false
|
||||
}
|
||||
},{
|
||||
methodname: 'tool_lp_read_competency_framework',
|
||||
@@ -532,6 +533,67 @@ define(['jquery',
|
||||
return str.get_string('taxonomy_selected_' + getTaxonomyAtLevel(level), 'tool_lp');
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler when a node in the aria tree is selected.
|
||||
* @method selectionChanged
|
||||
*/
|
||||
var selectionChanged = function(evt, node) {
|
||||
var id = $(node).data('id'),
|
||||
btn = $('[data-region="competencyactions"] [data-action="add"]'),
|
||||
actionMenu = $('[data-region="competencyactionsmenu"]'),
|
||||
selectedTitle = $('[data-region="selected-competency"]'),
|
||||
level = 0,
|
||||
sublevel = 1;
|
||||
|
||||
menubar.closeAll();
|
||||
|
||||
if (typeof id === "undefined") {
|
||||
// Assume this is the root of the tree.
|
||||
// Here we are only getting the text from the top of the tree, to do it we clone the tree,
|
||||
// remove all children and then call text on the result.
|
||||
$('[data-region="competencyinfo"]').html(node.clone().children().remove().end().text());
|
||||
$('[data-region="competencyactions"]').data('competency', null);
|
||||
actionMenu.hide();
|
||||
|
||||
} else {
|
||||
var competency = treeModel.getCompetency(id);
|
||||
|
||||
competency.showdeleterelatedaction = true;
|
||||
competency.showrelatedcompetencies = true;
|
||||
|
||||
level = treeModel.getCompetencyLevel(id);
|
||||
if (!hasSubLevel(level)) {
|
||||
sublevel = false;
|
||||
} else {
|
||||
sublevel = level + 1;
|
||||
}
|
||||
|
||||
actionMenu.show();
|
||||
$('[data-region="competencyactions"]').data('competency', competency);
|
||||
templates.render('tool_lp/competency_summary', competency).then(function(html) {
|
||||
$('[data-region="competencyinfo"]').html(html);
|
||||
$('[data-action="deleterelation"]').on('click', deleteRelatedHandler);
|
||||
}, notification.exception);
|
||||
}
|
||||
|
||||
strSelectedTaxonomy(level).then(function(str) {
|
||||
selectedTitle.text(str);
|
||||
});
|
||||
|
||||
if (!sublevel) {
|
||||
btn.hide();
|
||||
} else {
|
||||
strAddTaxonomy(sublevel).then(function(str) {
|
||||
btn.show()
|
||||
.find('[data-region="term"]')
|
||||
.text(str);
|
||||
});
|
||||
}
|
||||
// We handled this event so consume it.
|
||||
evt.preventDefault();
|
||||
return false;
|
||||
};
|
||||
|
||||
return {
|
||||
/**
|
||||
* Initialise this page (attach event handlers etc).
|
||||
@@ -567,67 +629,9 @@ define(['jquery',
|
||||
$('[data-region="managecompetencies"] li').on('dragenter', dragEnter);
|
||||
$('[data-region="managecompetencies"] li').on('dragleave', dragLeave);
|
||||
$('[data-region="managecompetencies"] li').on('drop', dropOver);
|
||||
},
|
||||
|
||||
/**
|
||||
* Handler when a node in the aria tree is selected.
|
||||
* @method selectionChanged
|
||||
*/
|
||||
selectionChanged: function(evt, node) {
|
||||
var id = $(node).data('id'),
|
||||
btn = $('[data-region="competencyactions"] [data-action="add"]'),
|
||||
actionMenu = $('[data-region="competencyactionsmenu"]'),
|
||||
selectedTitle = $('[data-region="selected-competency"]'),
|
||||
level = 0,
|
||||
sublevel = 1;
|
||||
|
||||
menubar.closeAll();
|
||||
|
||||
if (typeof id === "undefined") {
|
||||
// Assume this is the root of the tree.
|
||||
// Here we are only getting the text from the top of the tree, to do it we clone the tree,
|
||||
// remove all children and then call text on the result.
|
||||
$('[data-region="competencyinfo"]').html(node.clone().children().remove().end().text());
|
||||
$('[data-region="competencyactions"]').data('competency', null);
|
||||
actionMenu.hide();
|
||||
|
||||
} else {
|
||||
var competency = treeModel.getCompetency(id);
|
||||
|
||||
competency.showdeleterelatedaction = true;
|
||||
competency.showrelatedcompetencies = true;
|
||||
|
||||
level = treeModel.getCompetencyLevel(id);
|
||||
if (!hasSubLevel(level)) {
|
||||
sublevel = false;
|
||||
} else {
|
||||
sublevel = level + 1;
|
||||
}
|
||||
|
||||
actionMenu.show();
|
||||
$('[data-region="competencyactions"]').data('competency', competency);
|
||||
templates.render('tool_lp/competency_summary', competency).then(function(html) {
|
||||
$('[data-region="competencyinfo"]').html(html);
|
||||
$('[data-action="deleterelation"]').on('click', deleteRelatedHandler);
|
||||
}, notification.exception);
|
||||
}
|
||||
|
||||
strSelectedTaxonomy(level).then(function(str) {
|
||||
selectedTitle.text(str);
|
||||
});
|
||||
|
||||
if (!sublevel) {
|
||||
btn.hide();
|
||||
} else {
|
||||
strAddTaxonomy(sublevel).then(function(str) {
|
||||
btn.show()
|
||||
.find('[data-region="term"]')
|
||||
.text(str);
|
||||
});
|
||||
}
|
||||
// We handled this event so consume it.
|
||||
evt.preventDefault();
|
||||
return false;
|
||||
model.on('selectionchanged', selectionChanged);
|
||||
}
|
||||
|
||||
};
|
||||
});
|
||||
|
||||
@@ -82,14 +82,12 @@ define(['jquery',
|
||||
Picker.prototype._afterRender = function() {
|
||||
|
||||
// Initialise the tree.
|
||||
new Tree(self._find('[data-enhance=linktree]'));
|
||||
var tree = new Tree(self._find('[data-enhance=linktree]'));
|
||||
|
||||
self._find('[data-enhance=linktree]').on('change', function(evt, target) {
|
||||
tree.on('selectionchanged', function(evt, target) {
|
||||
var compId = target.data('id'),
|
||||
valid = true;
|
||||
|
||||
evt.preventDefault();
|
||||
|
||||
$.each(self._disallowedCompetencyIDs, function(i, id) {
|
||||
if (id == compId) {
|
||||
valid = false;
|
||||
|
||||
@@ -123,6 +123,20 @@ define(['core/ajax', 'core/notification', 'core/templates', 'tool_lp/tree', 'jqu
|
||||
loadCompetencies(search).fail(notification.exception);
|
||||
},
|
||||
|
||||
/**
|
||||
* Add an event handler for custom events emitted by the tree.
|
||||
*
|
||||
* @param {String} eventname The name of the event - only "selectionchanged" for now
|
||||
* @param {Function} handler The handler for the event.
|
||||
*/
|
||||
on: function(eventname, handler) {
|
||||
// We can't use the tree on function directly
|
||||
// because the tree gets rebuilt whenever the search string changes,
|
||||
// instead we attach the listner to the root node of the tree which never
|
||||
// gets destroyed (same as "on()" code in the tree.js).
|
||||
$(treeSelector).on(eventname, handler);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the competency framework id this model was initiliased with.
|
||||
*
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
* @copyright 2015 Damyon Wiese <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
define(['jquery', 'core/url'], function($, url) {
|
||||
define(['jquery', 'core/url', 'core/log'], function($, url, log) {
|
||||
// Private variables and functions.
|
||||
/** @var {String} expandedImage The html for an expanded tree node twistie. */
|
||||
var expandedImage = $('<img alt="" src="' + url.imageUrl('t/expanded') + '"/>');
|
||||
@@ -146,7 +146,7 @@ define(['jquery', 'core/url'], function($, url) {
|
||||
Tree.prototype.updateFocus = function(item) {
|
||||
this.items.attr('aria-selected', 'false').attr('tabindex', '-1');
|
||||
item.attr('aria-selected', 'true').attr('tabindex', 0);
|
||||
this.treeRoot.trigger('change', [item]);
|
||||
this.treeRoot.trigger('selectionchanged', [item]);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -337,6 +337,21 @@ define(['jquery', 'core/url'], function($, url) {
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Attach an event listener to the tree.
|
||||
*
|
||||
* @method on
|
||||
* @param {String} eventname This is the name of the event to listen for. Only 'selectionchanged' is supported for now.
|
||||
* @param {Function} handler The function to call when the event is triggered.
|
||||
*/
|
||||
Tree.prototype.on = function(eventname, handler) {
|
||||
if (eventname !== 'selectionchanged') {
|
||||
log.warning('Invalid custom event name for tree. Only "selectionchanged" is supported.');
|
||||
} else {
|
||||
this.treeRoot.on(eventname, handler);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle a double click (expand/collapse).
|
||||
*
|
||||
|
||||
@@ -111,8 +111,6 @@ require(['tool_lp/tree', 'tool_lp/competencytree', 'tool_lp/competencyactions',
|
||||
'{{framework.shortname}}',
|
||||
'{{search}}',
|
||||
'[data-enhance=tree]');
|
||||
// Listen to the custom event triggered by the aria tree.
|
||||
$('[data-enhance=tree]').on('change', actions.selectionChanged);
|
||||
|
||||
actions.init(treeModel, {{pagecontextid}}, {{{framework.taxonomies}}});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user