MDL-57471 forms: deprecate init_javascript_enhancement()

This was half-finished and only used for the smartselect enhancement. That
enhancement doesn't work with theme_boost and is better replaced just
using the searchableselector element.
This commit is contained in:
Dan Poltawski
2016-12-22 12:19:51 +00:00
parent c4cf1c60f5
commit 2fa2ed50eb
6 changed files with 12 additions and 487 deletions
+4 -202
View File
@@ -1462,209 +1462,11 @@ M.form = M.form || {};
/**
* Converts a nbsp indented select box into a multi drop down custom control much
* like the custom menu. It also selectable categories on or off.
*
* $form->init_javascript_enhancement('elementname','smartselect', array('selectablecategories'=>true|false, 'mode'=>'compact'|'spanning'));
*
* @param {YUI} Y
* @param {string} id
* @param {Array} options
* like the custom menu. Can no longer be used.
* @deprecated since Moodle 3.3
*/
M.form.init_smartselect = function(Y, id, options) {
if (!id.match(/^id_/)) {
id = 'id_'+id;
}
var select = Y.one('select#'+id);
if (!select) {
return false;
}
Y.use('event-delegate',function(){
var smartselect = {
id : id,
structure : [],
options : [],
submenucount : 0,
currentvalue : null,
currenttext : null,
shownevent : null,
cfg : {
selectablecategories : true,
mode : null
},
nodes : {
select : null,
loading : null,
menu : null
},
init : function(Y, id, args, nodes) {
if (typeof(args)=='object') {
for (var i in this.cfg) {
if (args[i] || args[i]===false) {
this.cfg[i] = args[i];
}
}
}
// Display a loading message first up
this.nodes.select = nodes.select;
this.currentvalue = this.nodes.select.get('selectedIndex');
this.currenttext = this.nodes.select.all('option').item(this.currentvalue).get('innerHTML');
var options = Array();
options[''] = {text:this.currenttext,value:'',depth:0,children:[]};
this.nodes.select.all('option').each(function(option, index) {
var rawtext = option.get('innerHTML');
var text = rawtext.replace(/^( )*/, '');
if (rawtext === text) {
text = rawtext.replace(/^(\s)*/, '');
var depth = (rawtext.length - text.length ) + 1;
} else {
var depth = ((rawtext.length - text.length )/12)+1;
}
option.set('innerHTML', text);
options['i'+index] = {text:text,depth:depth,index:index,children:[]};
}, this);
this.structure = [];
var structcount = 0;
for (var i in options) {
var o = options[i];
if (o.depth == 0) {
this.structure.push(o);
structcount++;
} else {
var d = o.depth;
var current = this.structure[structcount-1];
for (var j = 0; j < o.depth-1;j++) {
if (current && current.children) {
current = current.children[current.children.length-1];
}
}
if (current && current.children) {
current.children.push(o);
}
}
}
this.nodes.menu = Y.Node.create(this.generate_menu_content());
this.nodes.menu.one('.smartselect_mask').setStyle('opacity', 0.01);
this.nodes.menu.one('.smartselect_mask').setStyle('width', (this.nodes.select.get('offsetWidth')+5)+'px');
this.nodes.menu.one('.smartselect_mask').setStyle('height', (this.nodes.select.get('offsetHeight'))+'px');
if (this.cfg.mode == null) {
var formwidth = this.nodes.select.ancestor('form').get('offsetWidth');
if (formwidth < 400 || this.nodes.menu.get('offsetWidth') < formwidth*2) {
this.cfg.mode = 'compact';
} else {
this.cfg.mode = 'spanning';
}
}
if (this.cfg.mode == 'compact') {
this.nodes.menu.addClass('compactmenu');
} else {
this.nodes.menu.addClass('spanningmenu');
this.nodes.menu.delegate('mouseover', this.show_sub_menu, '.smartselect_submenuitem', this);
}
Y.one(document.body).append(this.nodes.menu);
var pos = this.nodes.select.getXY();
pos[0] += 1;
this.nodes.menu.setXY(pos);
this.nodes.menu.on('click', this.handle_click, this);
Y.one(window).on('resize', function(){
var pos = this.nodes.select.getXY();
pos[0] += 1;
this.nodes.menu.setXY(pos);
}, this);
},
generate_menu_content : function() {
var content = '<div id="'+this.id+'_smart_select" class="smartselect">';
content += this.generate_submenu_content(this.structure[0], true);
content += '</ul></div>';
return content;
},
generate_submenu_content : function(item, rootelement) {
this.submenucount++;
var content = '';
if (item.children.length > 0) {
if (rootelement) {
content += '<div class="smartselect_mask" href="#ss_submenu'+this.submenucount+'">&nbsp;</div>';
content += '<div id="ss_submenu'+this.submenucount+'" class="smartselect_menu">';
content += '<div class="smartselect_menu_content">';
} else {
content += '<li class="smartselect_submenuitem">';
var categoryclass = (this.cfg.selectablecategories)?'selectable':'notselectable';
content += '<a class="smartselect_menuitem_label '+categoryclass+'" href="#ss_submenu'+this.submenucount+'" value="'+item.index+'">'+item.text+'</a>';
content += '<div id="ss_submenu'+this.submenucount+'" class="smartselect_submenu">';
content += '<div class="smartselect_submenu_content">';
}
content += '<ul>';
for (var i in item.children) {
content += this.generate_submenu_content(item.children[i],false);
}
content += '</ul>';
content += '</div>';
content += '</div>';
if (rootelement) {
} else {
content += '</li>';
}
} else {
content += '<li class="smartselect_menuitem">';
content += '<a class="smartselect_menuitem_content selectable" href="#" value="'+item.index+'">'+item.text+'</a>';
content += '</li>';
}
return content;
},
select : function(e) {
var t = e.target;
e.halt();
this.currenttext = t.get('innerHTML');
this.currentvalue = t.getAttribute('value');
this.nodes.select.set('selectedIndex', this.currentvalue);
this.hide_menu();
},
handle_click : function(e) {
var target = e.target;
if (target.hasClass('smartselect_mask')) {
this.show_menu(e);
} else if (target.hasClass('selectable') || target.hasClass('smartselect_menuitem')) {
this.select(e);
} else if (target.hasClass('smartselect_menuitem_label') || target.hasClass('smartselect_submenuitem')) {
this.show_sub_menu(e);
}
},
show_menu : function(e) {
e.halt();
var menu = e.target.ancestor().one('.smartselect_menu');
menu.addClass('visible');
this.shownevent = Y.one(document.body).on('click', this.hide_menu, this);
},
show_sub_menu : function(e) {
e.halt();
var target = e.target;
if (!target.hasClass('smartselect_submenuitem')) {
target = target.ancestor('.smartselect_submenuitem');
}
if (this.cfg.mode == 'compact' && target.one('.smartselect_submenu').hasClass('visible')) {
target.ancestor('ul').all('.smartselect_submenu.visible').removeClass('visible');
return;
}
target.ancestor('ul').all('.smartselect_submenu.visible').removeClass('visible');
target.one('.smartselect_submenu').addClass('visible');
},
hide_menu : function() {
this.nodes.menu.all('.visible').removeClass('visible');
if (this.shownevent) {
this.shownevent.detach();
}
}
};
smartselect.init(Y, id, options, {select:select});
});
M.form.init_smartselect = function() {
throw new Error('M.form.init_smartselect can not be used any more.');
};
/**