Blame view
sources/core/js/multiselect.js
10.8 KB
|
03e52840d
|
1 |
/** |
|
6d9380f96
|
2 3 4 5 6 |
* @param 'createCallback' A function to be called when a new entry is created. * Two arguments are supplied to this function: * The select element used and the value of the option. If the function * returns false addition will be cancelled. If it returns * anything else it will be used as the value of the newly added option. |
|
03e52840d
|
7 8 |
* @param 'createText' The placeholder text for the create action. * @param 'title' The title to show if no options are selected. |
|
6d9380f96
|
9 10 |
* @param 'checked' An array containing values for options that should be * checked. Any options which are already selected will be added to this array. |
|
03e52840d
|
11 |
* @param 'labels' The corresponding labels to show for the checked items. |
|
6d9380f96
|
12 13 |
* @param 'oncheck' Callback function which will be called when a * checkbox/radiobutton is selected. If the function returns false the input will be unchecked. |
|
03e52840d
|
14 |
* @param 'onuncheck' @see 'oncheck'. |
|
6d9380f96
|
15 16 |
* @param 'singleSelect' If true radiobuttons will be used instead of * checkboxes. |
|
03e52840d
|
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
*/
(function( $ ){
var multiSelectId=-1;
$.fn.multiSelect=function(options) {
multiSelectId++;
var settings = {
'createCallback':false,
'createText':false,
'singleSelect':false,
'selectedFirst':false,
'sort':true,
'title':this.attr('title'),
'checked':[],
'labels':[],
'oncheck':false,
'onuncheck':false,
|
|
31b7f2792
|
33 |
'minWidth': 'default;' |
|
03e52840d
|
34 |
}; |
|
6d9380f96
|
35 |
var slideDuration = 200; |
|
03e52840d
|
36 37 38 39 |
$(this).attr('data-msid', multiSelectId);
$.extend(settings,options);
$.each(this.children(),function(i,option) {
// If the option is selected, but not in the checked array, add it.
|
|
6d9380f96
|
40 41 42 43 |
if (
$(option).attr('selected') &&
settings.checked.indexOf($(option).val()) === -1
) {
|
|
03e52840d
|
44 45 46 47 |
settings.checked.push($(option).val()); settings.labels.push($(option).text().trim()); } // If the option is in the checked array but not selected, select it. |
|
6d9380f96
|
48 49 50 51 |
else if (
settings.checked.indexOf($(option).val()) !== -1 &&
!$(option).attr('selected')
) {
|
|
03e52840d
|
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
$(option).attr('selected', 'selected');
settings.labels.push($(option).text().trim());
}
});
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();
}
button.css('min-width',settings.minWidth);
settings.minOuterWidth=button.outerWidth()-2;
button.data('settings',settings);
if(!settings.singleSelect && settings.checked.length>0) {
button.children('span').first().text(settings.labels.join(', '));
} else if(settings.singleSelect) {
button.children('span').first().text(this.find(':selected').text());
}
var self = this;
self.menuDirection = 'down';
button.click(function(event){
var button=$(this);
if(button.parent().children('ul').length>0) {
if(self.menuDirection === 'down') {
|
|
6d9380f96
|
83 |
button.parent().children('ul').slideUp(slideDuration,function() {
|
|
03e52840d
|
84 85 86 87 |
button.parent().children('ul').remove();
button.removeClass('active down');
});
} else {
|
|
6d9380f96
|
88 |
button.parent().children('ul').fadeOut(slideDuration,function() {
|
|
03e52840d
|
89 90 91 92 93 94 95 |
button.parent().children('ul').remove();
button.removeClass('active up');
});
}
return;
}
var lists=$('ul.multiselectoptions');
|
|
6d9380f96
|
96 |
lists.slideUp(slideDuration,function(){
|
|
03e52840d
|
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
lists.remove();
$('div.multiselect').removeClass('active');
button.addClass('active');
});
button.addClass('active');
event.stopPropagation();
var options=$(this).parent().next().children();
var list=$('<ul class="multiselectoptions"/>').hide().appendTo($(this).parent());
var inputType = settings.singleSelect ? 'radio' : 'checkbox';
function createItem(element, checked){
element=$(element);
var item=element.val();
var id='ms'+multiSelectId+'-option-'+item;
var input=$('<input type="' + inputType + '"/>');
input.attr('id',id);
if(settings.singleSelect) {
input.attr('name', 'ms'+multiSelectId+'-option');
}
var label=$('<label/>');
label.attr('for',id);
label.text(element.text() || item);
|
|
6d9380f96
|
118 |
if(settings.checked.indexOf(item) !== -1 || checked) {
|
|
03e52840d
|
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
input.attr('checked', true);
}
if(checked){
if(settings.singleSelect) {
settings.checked = [item];
settings.labels = [item];
} else {
settings.checked.push(item);
settings.labels.push(item);
}
}
input.change(function(){
var value = $(this).attr('id').substring(String('ms'+multiSelectId+'-option').length+1);
var label = $(this).next().text().trim();
if($(this).is(':checked')) {
if(settings.singleSelect) {
settings.checked = [];
settings.labels = [];
$.each(self.find('option'), function() {
$(this).removeAttr('selected');
});
}
element.attr('selected','selected');
if(typeof settings.oncheck === 'function') {
if(settings.oncheck(value)===false) {
$(this).attr('checked', false);
return;
}
}
settings.checked.push(value);
settings.labels.push(label);
$(this).parent().addClass('checked');
} else {
var index=settings.checked.indexOf(value);
element.attr('selected',null);
if(typeof settings.onuncheck === 'function') {
if(settings.onuncheck(value)===false) {
$(this).attr('checked',true);
return;
}
}
$(this).parent().removeClass('checked');
settings.checked.splice(index,1);
settings.labels.splice(index,1);
}
var oldWidth=button.width();
|
|
6d9380f96
|
165 |
button.children('span').first().text(settings.labels.length > 0
|
|
03e52840d
|
166 167 |
? settings.labels.join(', ')
: settings.title);
|
|
6d9380f96
|
168 169 170 171 |
var newOuterWidth = Math.max( (button.outerWidth() - 2), settings.minOuterWidth ) + 'px'; |
|
03e52840d
|
172 173 |
var newWidth=Math.max(button.width(),settings.minWidth); var pos=button.position(); |
|
03e52840d
|
174 175 176 177 |
button.css('width',oldWidth);
button.animate({'width':newWidth},undefined,undefined,function(){
button.css('width','');
});
|
|
6d9380f96
|
178 179 |
list.animate({'width':newOuterWidth,'left':pos.left});
self.change();
|
|
03e52840d
|
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
});
var li=$('<li></li>');
li.append(input).append(label);
if(input.is(':checked')) {
li.addClass('checked');
}
return li;
}
$.each(options,function(index,item){
list.append(createItem(item));
});
button.parent().data('preventHide',false);
if(settings.createText){
var li=$('<li class="creator">+ '+settings.createText+'</li>');
li.click(function(event){
li.empty();
var input=$('<input type="text" class="new">');
li.append(input);
input.focus();
input.css('width',button.innerWidth());
button.parent().data('preventHide',true);
input.keypress(function(event) {
|
|
6d9380f96
|
202 |
if(event.keyCode === 13) {
|
|
03e52840d
|
203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
event.preventDefault();
event.stopPropagation();
var value = $(this).val();
var exists = false;
$.each(options,function(index, item) {
if ($(item).val() == value || $(item).text() == value) {
exists = true;
return false;
}
});
if (exists) {
return false;
}
var li=$(this).parent();
|
|
31b7f2792
|
217 |
var val = $(this).val(); |
|
03e52840d
|
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
var select=button.parent().next();
if(typeof settings.createCallback === 'function') {
var response = settings.createCallback(select, val);
if(response === false) {
return false;
} else if(typeof response !== 'undefined') {
val = response;
}
}
if(settings.singleSelect) {
$.each(select.find('option:selected'), function() {
$(this).removeAttr('selected');
});
}
$(this).remove();
li.text('+ '+settings.createText);
li.before(createItem(this));
var option=$('<option selected="selected"/>');
option.text($(this).val()).val(val).attr('selected', 'selected');
select.append(option);
li.prev().children('input').prop('checked', true).trigger('change');
button.parent().data('preventHide',false);
|
|
6d9380f96
|
240 |
button.children('span').first().text(settings.labels.length > 0
|
|
03e52840d
|
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
? settings.labels.join(', ')
: settings.title);
if(self.menuDirection === 'up') {
var list = li.parent();
list.css('top', list.position().top-li.outerHeight());
}
}
});
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 doSort = function(list, selector) {
var rows = list.find('li'+selector).get();
if(settings.sort) {
rows.sort(function(a, b) {
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
});
}
$.each(rows, function(index, row) {
list.append(row);
});
};
if(settings.sort && settings.selectedFirst) {
doSort(list, '.checked');
doSort(list, ':not(.checked)');
} else if(settings.sort && !settings.selectedFirst) {
doSort(list, '');
}
list.append(list.find('li.creator'));
var pos=button.position();
|
|
6d9380f96
|
283 284 285 |
if(($(document).height() > (button.offset().top + button.outerHeight() + list.children().length * button.height()) && $(document).height() - button.offset().top > (button.offset().top+button.outerHeight() + list.children().length * button.height())) || $(document).height() / 2 > button.offset().top |
|
03e52840d
|
286 287 288 |
) {
list.css({
top:pos.top+button.outerHeight()-5,
|
|
6d9380f96
|
289 |
left:pos.left, |
|
03e52840d
|
290 291 292 293 294 |
width:(button.outerWidth()-2)+'px',
'max-height':($(document).height()-(button.offset().top+button.outerHeight()+10))+'px'
});
list.addClass('down');
button.addClass('down');
|
|
6d9380f96
|
295 |
list.slideDown(slideDuration); |
|
03e52840d
|
296 297 298 299 |
} else {
list.css('max-height', $(document).height()-($(document).height()-(pos.top)+50)+'px');
list.css({
top:pos.top - list.height(),
|
|
6d9380f96
|
300 |
left:pos.left, |
|
03e52840d
|
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
width:(button.outerWidth()-2)+'px'
});
list.detach().insertBefore($(this));
list.addClass('up');
button.addClass('up');
list.fadeIn();
self.menuDirection = 'up';
}
list.click(function(event) {
event.stopPropagation();
});
});
$(window).click(function() {
if(!button.parent().data('preventHide')) {
// How can I save the effect in a var?
if(self.menuDirection === 'down') {
|
|
6d9380f96
|
318 |
button.parent().children('ul').slideUp(slideDuration,function() {
|
|
03e52840d
|
319 320 321 322 |
button.parent().children('ul').remove();
button.removeClass('active down');
});
} else {
|
|
6d9380f96
|
323 |
button.parent().children('ul').fadeOut(slideDuration,function() {
|
|
03e52840d
|
324 325 326 327 328 329 330 331 332 333 |
button.parent().children('ul').remove();
button.removeClass('active up');
});
}
}
});
return span;
};
})( jQuery );
|