server/core/js/multiselect.js

259 lines
8.7 KiB
JavaScript
Raw Normal View History

2012-12-03 17:56:15 +00:00
/**
* @param 'createCallback' A function to be called when a new entry is created. Only argument to the function is the value of the option.
* @param 'createText' The placeholder text for the create action.
* @param 'title' The title to show if no options are selected.
* @param 'checked' An array containing values for options that should be checked. Any options which are already selected will be added to this array.
* @param 'labels' The corresponding labels to show for the checked items.
* @param 'oncheck' Callback function which will be called when a checkbox/radiobutton is selected. If the function returns false the input will be unchecked.
* @param 'onuncheck' @see 'oncheck'.
* @param 'singleSelect' If true radiobuttons will be used instead of checkboxes.
*/
2011-08-10 18:51:35 +00:00
(function( $ ){
var multiSelectId=-1;
$.fn.multiSelect=function(options){
multiSelectId++;
var settings = {
'createCallback':false,
'createText':false,
2012-12-03 17:56:15 +00:00
'singleSelect':false,
2011-08-10 18:51:35 +00:00
'title':this.attr('title'),
'checked':[],
2012-12-03 17:56:15 +00:00
'labels':[],
2011-08-10 18:51:35 +00:00
'oncheck':false,
'onuncheck':false,
'minWidth': 'default;',
2011-08-10 18:51:35 +00:00
};
$.extend(settings,options);
2012-02-22 20:38:15 +00:00
$.each(this.children(),function(i,option){
2012-12-03 17:56:15 +00:00
// If the option is selected, but not in the checked array, add it.
if($(option).attr('selected') && settings.checked.indexOf($(option).val()) == -1){
2012-02-22 20:38:15 +00:00
settings.checked.push($(option).val());
2012-12-03 17:56:15 +00:00
settings.labels.push($(option).text().trim());
}
// If the option is in the checked array but not selected, select it.
else if(settings.checked.indexOf($(option).val()) !== -1 && !$(option).attr('selected')){
$(option).attr('selected', 'selected');
settings.labels.push($(option).text().trim());
2012-02-22 20:38:15 +00:00
}
});
2011-08-10 18:51:35 +00:00
var button=$('<div class="multiselect button"><span>'+settings.title+'</span><span>▾</span></div>');
var span=$('<span/>');
span.append(button);
button.data('id',multiSelectId);
button.selectedItems=[];
this.hide();
this.before(span);
if(settings.minWidth=='default'){
settings.minWidth=button.width();
}
2011-08-10 18:51:35 +00:00
button.css('min-width',settings.minWidth);
settings.minOuterWidth=button.outerWidth()-2;
button.data('settings',settings);
2012-12-03 17:56:15 +00:00
if(!settings.singleSelect && settings.checked.length>0){
//button.children('span').first().text(settings.checked.join(', '));
button.children('span').first().text(settings.labels.join(', '));
} else if(settings.singleSelect) {
button.children('span').first().text(this.find(':selected').text());
}
2012-12-03 17:56:15 +00:00
var self = this;
self.menuDirection = 'down';
2011-08-10 18:51:35 +00:00
button.click(function(event){
2011-08-10 18:51:35 +00:00
var button=$(this);
if(button.parent().children('ul').length>0){
2012-12-03 17:56:15 +00:00
if(self.menuDirection === 'down') {
button.parent().children('ul').slideUp(400,function(){
button.parent().children('ul').remove();
button.removeClass('active');
});
} else {
button.parent().children('ul').fadeOut(400,function(){
button.parent().children('ul').remove();
button.removeClass('active').removeClass('up');
});
}
2011-08-10 18:51:35 +00:00
return;
}
var lists=$('ul.multiselectoptions');
lists.slideUp(400,function(){
lists.remove();
$('div.multiselect').removeClass('active');
button.addClass('active');
});
button.addClass('active');
event.stopPropagation();
2012-02-22 20:38:15 +00:00
var options=$(this).parent().next().children();
2011-08-10 18:51:35 +00:00
var list=$('<ul class="multiselectoptions"/>').hide().appendTo($(this).parent());
2012-12-03 17:56:15 +00:00
var inputType = settings.singleSelect ? 'radio' : 'checkbox';
2012-02-22 20:38:15 +00:00
function createItem(element,checked){
element=$(element);
var item=element.val();
2011-08-10 18:51:35 +00:00
var id='ms'+multiSelectId+'-option-'+item;
2012-12-03 17:56:15 +00:00
var input=$('<input type="' + inputType + '"/>');
2012-06-09 13:07:09 +00:00
input.attr('id',id);
2012-12-03 17:56:15 +00:00
if(settings.singleSelect) {
input.attr('name', 'ms'+multiSelectId+'-option');
}
2012-06-09 13:07:09 +00:00
var label=$('<label/>');
label.attr('for',id);
2012-12-03 17:56:15 +00:00
label.text(element.text() || item);
2011-08-10 18:51:35 +00:00
if(settings.checked.indexOf(item)!=-1 || checked){
input.attr('checked',true);
2011-08-10 18:51:35 +00:00
}
if(checked){
settings.checked.push(item);
}
input.change(function(){
2012-12-03 17:56:15 +00:00
var value = $(this).attr('id').substring(String('ms'+multiSelectId+'-option').length+1);
var label = $(this).next().text().trim();
if($(this).is(':checked')){
2012-12-03 17:56:15 +00:00
if(settings.singleSelect) {
settings.checked = [];
settings.labels = [];
$.each(self.find('option'), function() {
$(this).removeAttr('selected');
});
}
2012-02-22 20:38:15 +00:00
element.attr('selected','selected');
2012-12-03 22:38:48 +00:00
if(typeof settings.oncheck === 'function'){
2012-12-03 17:56:15 +00:00
if(settings.oncheck(value)===false){
$(this).attr('checked', false);
2011-08-10 18:51:35 +00:00
return;
}
}
2012-12-03 17:56:15 +00:00
settings.checked.push(value);
settings.labels.push(label);
2011-08-10 18:51:35 +00:00
}else{
2012-12-03 17:56:15 +00:00
var index=settings.checked.indexOf(value);
2012-02-22 20:38:15 +00:00
element.attr('selected',null);
2012-12-03 22:38:48 +00:00
if(typeof settings.onuncheck === 'function'){
2012-12-03 17:56:15 +00:00
if(settings.onuncheck(value)===false){
$(this).attr('checked',true);
2011-08-10 18:51:35 +00:00
return;
}
}
settings.checked.splice(index,1);
2012-12-03 17:56:15 +00:00
settings.labels.splice(index,1);
2011-08-10 18:51:35 +00:00
}
var oldWidth=button.width();
2012-12-03 17:56:15 +00:00
button.children('span').first().text(settings.labels.length > 0
? settings.labels.join(', ')
: settings.title);
var newOuterWidth=Math.max((button.outerWidth()-2),settings.minOuterWidth)+'px';
2011-08-10 18:51:35 +00:00
var newWidth=Math.max(button.width(),settings.minWidth);
var pos=button.position();
2011-08-10 18:51:35 +00:00
button.css('height',button.height());
button.css('white-space','nowrap');
button.css('width',oldWidth);
button.animate({'width':newWidth},undefined,undefined,function(){
button.css('width','');
});
list.animate({'width':newOuterWidth,'left':pos.left+3});
2011-08-10 18:51:35 +00:00
});
var li=$('<li></li>');
li.append(input).append(label);
return li;
}
$.each(options,function(index,item){
list.append(createItem(item));
});
button.parent().data('preventHide',false);
if(settings.createText){
var li=$('<li>+ <em>'+settings.createText+'<em></li>');
li.click(function(event){
li.empty();
var input=$('<input class="new">');
li.append(input);
input.focus();
2012-12-03 17:56:15 +00:00
input.css('width',button.innerWidth());
2011-08-10 18:51:35 +00:00
button.parent().data('preventHide',true);
input.keypress(function(event) {
if(event.keyCode == 13) {
event.preventDefault();
event.stopPropagation();
var value = $(this).val();
var exists = false;
$.each(options,function(index, item) {
2012-12-03 17:56:15 +00:00
if ($(item).val() == value || $(item).text() == value) {
exists = true;
return false;
}
});
if (exists) {
return false;
}
2011-08-10 18:51:35 +00:00
var li=$(this).parent();
$(this).remove();
li.text('+ '+settings.createText);
2012-12-03 17:56:15 +00:00
li.before(createItem(this, true));
if(self.menuDirection === 'up') {
var list = li.parent();
list.css('top', list.position().top-li.outerHeight());
}
2012-02-22 20:38:15 +00:00
var select=button.parent().next();
2012-06-09 13:07:09 +00:00
var option=$('<option selected="selected"/>');
select.append(option);
2012-12-03 17:56:15 +00:00
option.text($(this).val());
2011-08-10 18:51:35 +00:00
li.prev().children('input').trigger('click');
button.parent().data('preventHide',false);
2012-12-03 17:56:15 +00:00
if(typeof settings.createCallback === 'function'){
settings.createCallback($(this).val());
2011-08-10 18:51:35 +00:00
}
}
});
input.blur(function(){
event.preventDefault();
event.stopPropagation();
$(this).remove();
li.text('+ '+settings.createText);
setTimeout(function(){
button.parent().data('preventHide',false);
},100);
});
});
list.append(li);
}
var pos=button.position();
2012-12-03 17:56:15 +00:00
if($(document).height() > button.offset().top+button.outerHeight() + list.children().length * button.height()) {
list.css('top',pos.top+button.outerHeight()-5);
list.css('left',pos.left+3);
list.css('width',(button.outerWidth()-2)+'px');
list.addClass('down');
button.addClass('down');
list.slideDown();
} else {
list.css('top', pos.top - list.height());
list.css('left', pos.left+3);
list.css('width',(button.outerWidth()-2)+'px');
list.detach().insertBefore($(this));
list.addClass('up');
button.addClass('up');
list.fadeIn();
self.menuDirection = 'up';
}
2011-08-10 18:51:35 +00:00
list.click(function(event){
event.stopPropagation();
});
});
$(window).click(function(){
if(!button.parent().data('preventHide')){
2012-12-03 17:56:15 +00:00
// How can I save the effect in a var?
if(self.menuDirection === 'down') {
button.parent().children('ul').slideUp(400,function(){
button.parent().children('ul').remove();
button.removeClass('active').removeClass('down');
});
} else {
button.parent().children('ul').fadeOut(400,function(){
button.parent().children('ul').remove();
button.removeClass('active').removeClass('up');
});
}
2011-08-10 18:51:35 +00:00
}
});
return span;
};
})( jQuery );