2
0
forked from Wavyzz/dolibarr

Clean code to manage multiselect jquery: All code is centralized

into function multipleselectarray (except loading of css).
Code is prepared to choose which plugin to use (only a if is required
to switch or add a new manager). Introduce working version with
multiple-select jquery plugin. Removed deprecated old jquery plugin.
This commit is contained in:
Laurent Destailleur
2014-11-04 00:33:50 +01:00
parent b6b6cced30
commit 0b41df7c35
12 changed files with 879 additions and 605 deletions

View File

@@ -0,0 +1,21 @@
(The MIT License)
Copyright (c) 2012-2014 Zhixin Wen <wenzhixin2010@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,96 @@
# Multiple Select
Multiple select is a jQuery plugin to select multiple elements with checkboxes :).
To get started checkout examples and documentation at http://wenzhixin.net.cn/p/multiple-select
## Contributors
Multiple select is due to the excellent work of the following contributors:
<table>
<tbody>
<tr><th align="left">文翼</th><td><a href="https://github.com/wenzhixin">GitHub/wenzhixin</a></td></tr>
<tr><th align="left">Gaurav Jassal</th><td><a href="https://github.com/creativeaura">GitHub/creativeaura</a></td><</tr>
<tr><th align="left">guli</th><td><a href="https://github.com/guli">GitHub/guli</a></td></tr>
<tr><th align="left">jwheadon</th><td><a href="https://github.com/jwheadon">GitHub/jwheadon</a></td></tr>
<tr><th align="left">yx</th><td><a href="https://github.com/qqfish">GitHub/qqfish</a></td></tr>
<tr><th align="left">Tobias Macey</th><td><a href="https://github.com/blarghmatey">GitHub/blarghmatey</a></td></tr>
<tr><th align="left">Jona Goldman</th><td><a href="https://github.com/jonagoldman">GitHub/jonagoldman</a></td></tr>
<tr><th align="left">Alex Jeffrey</th><td><a href="https://github.com/ajeffrey">GitHub/ajeffrey</a></td></tr>
</tbody>
</table>
## Changelog
### 1.1.0
* Fix #63: Add ```keepOpen``` option.
* Fix #62: Fix ```isOpen``` and ```filter``` options are both true bug.
* Fix #57: Fire onCheckAll event when literally select.
* Add data attributes for support.
* Fix #55: Add ```name``` option.
### 1.0.9
* Fix #42: Add ```styler``` option to custom item style.
* Fix firefox click bug.
* Add ```allSelected```, ```minumimCountSelected``` and ```countSelected``` options.
* Fix #35: Add ```onFocus``` and ```onBlur``` events.
* Fix #25: Add ```focus``` and ```blur``` methods.
* Fix #31: Trigger the onCheckAll & onUncheckAll events when use filter to select all.
### 1.0.8
* Update the license to The MIT License.
* Fix #47: Add ```No matches found``` message when there are no results found.
* Fix #43: Add ```position``` option.
### 1.0.7
* Fix #44: The filters not working bugs.
### 1.0.6
* Fix #21: Add ```single``` option.
* Add ```override``` option.
* Add ```container``` option.
* Fix #29: Update the optgroups select text.
* Fix #30: Image is not shown in Firefox 25.0.1.
* Fix #22: fix group filter problem.
### 1.0.5
* Update the button text witdh.
* Add keyboard support.
### 1.0.4
* Fix #12: Add ```width``` option.
* Fix #11: Add callback events.
* Add ```maxHeight``` option.
### 1.0.3
* Fix #4: Add ```filter``` option.
* Support mobile devices.
* Fix #6: Add ```refresh``` method.
### 1.0.2
* Fix #7: Add ```selected``` and ```disabled``` options.
* Fix #5: Add ```checkAll``` and ```uncheckAll``` methods.
### 1.0.1
* Fix #3: Add optgroups support.
* Add ```placeholder``` option.
* Fix #2: use prop method instead of attr.
### 1.0.0
* Initial release
## LICENSE
[The MIT License](https://github.com/wenzhixin/multiple-select/blob/master/LICENSE)

View File

@@ -0,0 +1,466 @@
/**
* @author zhixin wen <wenzhixin2010@gmail.com>
* @version 1.1.0
*
* http://wenzhixin.net.cn/p/multiple-select/
*/
(function($) {
'use strict';
function MultipleSelect($el, options) {
var that = this,
name = $el.attr('name') || options.name || '',
elWidth = $el.width();
this.$el = $el.hide();
this.options = options;
this.$parent = $('<div class="ms-parent"></div>');
this.$choice = $('<button type="button" class="ms-choice"><span class="placeholder">' +
options.placeholder + '</span><div></div></button>');
this.$drop = $('<div class="ms-drop ' + options.position + '"></div>');
this.$el.after(this.$parent);
this.$parent.append(this.$choice);
this.$parent.append(this.$drop);
if (this.$el.prop('disabled')) {
this.$choice.addClass('disabled');
}
this.$choice.css('width', elWidth + 'px');
this.$drop.css({
width: (options.width || elWidth) + 'px'
});
if (!this.options.keepOpen) {
$('body').click(function(e) {
if ($(e.target)[0] === that.$choice[0] ||
$(e.target).parents('.ms-choice')[0] === that.$choice[0]) {
return;
}
if (($(e.target)[0] === that.$drop[0] ||
$(e.target).parents('.ms-drop')[0] !== that.$drop[0]) &&
that.options.isOpen) {
that.close();
}
});
}
this.selectAllName = 'name="selectAll' + name + '"';
this.selectGroupName = 'name="selectGroup' + name + '"';
this.selectItemName = 'name="selectItem' + name + '"';
}
MultipleSelect.prototype = {
constructor : MultipleSelect,
init: function() {
var that = this,
html = [];
if (this.options.filter) {
html.push(
'<div class="ms-search">',
'<input type="text" autocomplete="off" autocorrect="off" autocapitilize="off" spellcheck="false">',
'</div>'
);
}
html.push('<ul>');
if (this.options.selectAll && !this.options.single) {
html.push(
'<li>',
'<label>',
'<input type="checkbox" ' + this.selectAllName + ' /> ',
'[' + this.options.selectAllText + ']',
'</label>',
'</li>'
);
}
$.each(this.$el.children(), function(i, elm) {
html.push(that.optionToHtml(i, elm));
});
html.push('<li class="ms-no-results">No matches found</li>');
html.push('</ul>');
this.$drop.html(html.join(''));
this.$drop.find('ul').css('max-height', this.options.maxHeight + 'px');
this.$drop.find('.multiple').css('width', this.options.multipleWidth + 'px');
this.$searchInput = this.$drop.find('.ms-search input');
this.$selectAll = this.$drop.find('input[' + this.selectAllName + ']');
this.$selectGroups = this.$drop.find('input[' + this.selectGroupName + ']');
this.$selectItems = this.$drop.find('input[' + this.selectItemName + ']:enabled');
this.$disableItems = this.$drop.find('input[' + this.selectItemName + ']:disabled');
this.$noResults = this.$drop.find('.ms-no-results');
this.events();
this.update();
if (this.options.isOpen) {
this.open();
}
},
optionToHtml: function(i, elm, group, groupDisabled) {
var that = this,
$elm = $(elm),
html = [],
multiple = this.options.multiple,
disabled,
type = this.options.single ? 'radio' : 'checkbox';
if ($elm.is('option')) {
var value = $elm.val(),
text = $elm.text(),
selected = $elm.prop('selected'),
style = this.options.styler(value) ? ' style="' + this.options.styler(value) + '"' : '';
disabled = groupDisabled || $elm.prop('disabled');
html.push(
'<li' + (multiple ? ' class="multiple"' : '') + style + '>',
'<label' + (disabled ? ' class="disabled"' : '') + '>',
'<input type="' + type + '" ' + this.selectItemName + ' value="' + value + '"' +
(selected ? ' checked="checked"' : '') +
(disabled ? ' disabled="disabled"' : '') +
(group ? ' data-group="' + group + '"' : '') +
'/> ',
text,
'</label>',
'</li>'
);
} else if (!group && $elm.is('optgroup')) {
var _group = 'group_' + i,
label = $elm.attr('label');
disabled = $elm.prop('disabled');
html.push(
'<li class="group">',
'<label class="optgroup' + (disabled ? ' disabled' : '') + '" data-group="' + _group + '">',
'<input type="checkbox" ' + this.selectGroupName +
(disabled ? ' disabled="disabled"' : '') + ' /> ',
label,
'</label>',
'</li>');
$.each($elm.children(), function(i, elm) {
html.push(that.optionToHtml(i, elm, _group, disabled));
});
}
return html.join('');
},
events: function() {
var that = this;
this.$choice.off('click').on('click', function(e) {
e.preventDefault();
that[that.options.isOpen ? 'close' : 'open']();
})
.off('focus').on('focus', this.options.onFocus)
.off('blur').on('blur', this.options.onBlur);
this.$parent.off('keydown').on('keydown', function(e) {
switch (e.which) {
case 27: // esc key
that.close();
that.$choice.focus();
break;
}
});
this.$searchInput.off('keyup').on('keyup', function() {
that.filter();
});
this.$selectAll.off('click').on('click', function() {
var checked = $(this).prop('checked'),
$items = that.$selectItems.filter(':visible');
if ($items.length === that.$selectItems.length) {
that[checked ? 'checkAll' : 'uncheckAll']();
} else { // when the filter option is true
that.$selectGroups.prop('checked', checked);
$items.prop('checked', checked);
that.options[checked ? 'onCheckAll' : 'onUncheckAll']();
that.update();
}
});
this.$selectGroups.off('click').on('click', function() {
var group = $(this).parent().attr('data-group'),
$items = that.$selectItems.filter(':visible'),
$children = $items.filter('[data-group="' + group + '"]'),
checked = $children.length !== $children.filter(':checked').length;
$children.prop('checked', checked);
that.updateSelectAll();
that.update();
that.options.onOptgroupClick({
label: $(this).parent().text(),
checked: checked,
children: $children.get()
});
});
this.$selectItems.off('click').on('click', function() {
that.updateSelectAll();
that.update();
that.updateOptGroupSelect();
that.options.onClick({
label: $(this).parent().text(),
value: $(this).val(),
checked: $(this).prop('checked')
});
});
},
open: function() {
if (this.$choice.hasClass('disabled')) {
return;
}
this.options.isOpen = true;
this.$choice.find('>div').addClass('open');
this.$drop.show();
if (this.options.container) {
var offset = this.$drop.offset();
this.$drop.appendTo($(this.options.container));
this.$drop.offset({ top: offset.top, left: offset.left });
}
if (this.options.filter) {
this.$searchInput.val('');
this.filter();
}
this.options.onOpen();
},
close: function() {
this.options.isOpen = false;
this.$choice.find('>div').removeClass('open');
this.$drop.hide();
if (this.options.container) {
this.$parent.append(this.$drop);
this.$drop.css({
'top': 'auto',
'left': 'auto'
})
}
this.options.onClose();
},
update: function() {
var selects = this.getSelects('text'),
$span = this.$choice.find('>span');
if (selects.length === this.$selectItems.length + this.$disableItems.length && this.options.allSelected) {
$span.removeClass('placeholder').html(this.options.allSelected);
} else if (selects.length > this.options.minumimCountSelected && this.options.countSelected) {
$span.removeClass('placeholder').html(this.options.countSelected
.replace('#', selects.length)
.replace('%', this.$selectItems.length + this.$disableItems.length));
} else if (selects.length) {
$span.removeClass('placeholder').html(selects.join(', '));
} else {
$span.addClass('placeholder').html(this.options.placeholder);
}
// set selects to select
this.$el.val(this.getSelects());
},
updateSelectAll: function() {
var $items = this.$selectItems.filter(':visible');
this.$selectAll.prop('checked', $items.length &&
$items.length === $items.filter(':checked').length);
if (this.$selectAll.prop('checked')) {
this.options.onCheckAll();
}
},
updateOptGroupSelect: function() {
var $items = this.$selectItems.filter(':visible');
$.each(this.$selectGroups, function(i, val) {
var group = $(val).parent().attr('data-group'),
$children = $items.filter('[data-group="' + group + '"]');
$(val).prop('checked', $children.length &&
$children.length === $children.filter(':checked').length);
});
},
//value or text, default: 'value'
getSelects: function(type) {
var that = this,
texts = [],
values = [];
this.$drop.find('input[' + this.selectItemName + ']:checked').each(function() {
texts.push($(this).parent().text());
values.push($(this).val());
});
if (type === 'text' && this.$selectGroups.length) {
texts = [];
this.$selectGroups.each(function() {
var html = [],
text = $.trim($(this).parent().text()),
group = $(this).parent().data('group'),
$children = that.$drop.find('[' + that.selectItemName + '][data-group="' + group + '"]'),
$selected = $children.filter(':checked');
if ($selected.length === 0) {
return;
}
html.push('[');
html.push(text);
if ($children.length > $selected.length) {
var list = [];
$selected.each(function() {
list.push($(this).parent().text());
});
html.push(': ' + list.join(', '));
}
html.push(']');
texts.push(html.join(''));
});
}
return type === 'text' ? texts : values;
},
setSelects: function(values) {
var that = this;
this.$selectItems.prop('checked', false);
$.each(values, function(i, value) {
that.$selectItems.filter('[value="' + value + '"]').prop('checked', true);
});
this.$selectAll.prop('checked', this.$selectItems.length ===
this.$selectItems.filter(':checked').length);
this.update();
},
enable: function() {
this.$choice.removeClass('disabled');
},
disable: function() {
this.$choice.addClass('disabled');
},
checkAll: function() {
this.$selectItems.prop('checked', true);
this.$selectGroups.prop('checked', true);
this.$selectAll.prop('checked', true);
this.update();
this.options.onCheckAll();
},
uncheckAll: function() {
this.$selectItems.prop('checked', false);
this.$selectGroups.prop('checked', false);
this.$selectAll.prop('checked', false);
this.update();
this.options.onUncheckAll();
},
focus: function() {
this.$choice.focus();
this.options.onFocus();
},
blur: function() {
this.$choice.blur();
this.options.onBlur();
},
refresh: function() {
this.init();
},
filter: function() {
var that = this,
text = $.trim(this.$searchInput.val()).toLowerCase();
if (text.length === 0) {
this.$selectItems.parent().show();
this.$disableItems.parent().show();
this.$selectGroups.parent().show();
} else {
this.$selectItems.each(function() {
var $parent = $(this).parent();
$parent[$parent.text().toLowerCase().indexOf(text) < 0 ? 'hide' : 'show']();
});
this.$disableItems.parent().hide();
this.$selectGroups.each(function() {
var $parent = $(this).parent();
var group = $parent.attr('data-group'),
$items = that.$selectItems.filter(':visible');
$parent[$items.filter('[data-group="' + group + '"]').length === 0 ? 'hide' : 'show']();
});
//Check if no matches found
if (this.$selectItems.filter(':visible').length) {
this.$selectAll.parent().show();
this.$noResults.hide();
} else {
this.$selectAll.parent().hide();
this.$noResults.show();
}
}
this.updateOptGroupSelect();
this.updateSelectAll();
}
};
$.fn.multipleSelect = function() {
var option = arguments[0],
args = arguments,
value,
allowedMethods = [
'getSelects', 'setSelects',
'enable', 'disable',
'checkAll', 'uncheckAll',
'focus', 'blur',
'refresh'
];
this.each(function() {
var $this = $(this),
data = $this.data('multipleSelect'),
options = $.extend({}, $.fn.multipleSelect.defaults,
$this.data(), typeof option === 'object' && option);
if (!data) {
data = new MultipleSelect($this, options);
$this.data('multipleSelect', data);
}
if (typeof option === 'string') {
if ($.inArray(option, allowedMethods) < 0) {
throw "Unknown method: " + option;
}
value = data[option](args[1]);
} else {
data.init();
}
});
return value ? value : this;
};
$.fn.multipleSelect.defaults = {
name: '',
isOpen: false,
placeholder: '',
selectAll: true,
selectAllText: 'Select all',
allSelected: 'All selected',
minumimCountSelected: 3,
countSelected: '# of % selected',
multiple: false,
multipleWidth: 80,
single: false,
filter: false,
width: undefined,
maxHeight: 250,
container: null,
position: 'bottom',
keepOpen: false,
styler: function() {return false;},
onOpen: function() {return false;},
onClose: function() {return false;},
onCheckAll: function() {return false;},
onUncheckAll: function() {return false;},
onFocus: function() {return false;},
onBlur: function() {return false;},
onOptgroupClick: function() {return false;},
onClick: function() {return false;}
};
})(jQuery);

View File

@@ -0,0 +1,183 @@
/**
* @author zhixin wen <wenzhixin2010@gmail.com>
*/
.ms-parent {
display: inline-block;
position: relative;
vertical-align: middle;
}
.ms-choice {
display: block;
height: 26px;
padding: 0;
overflow: hidden;
cursor: pointer;
border: 1px solid #aaa;
text-align: left;
white-space: nowrap;
line-height: 26px;
color: #444;
text-decoration: none;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
background-color: #fff;
}
.ms-choice.disabled {
background-color: #f4f4f4;
background-image: none;
border: 1px solid #ddd;
cursor: default;
}
.ms-choice > span {
position: absolute;
top: 0;
left: 0;
right: 20px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: block;
padding-left: 8px;
}
.ms-choice > span.placeholder {
color: #999;
}
.ms-choice > div {
position: absolute;
top: 0;
right: 0;
width: 20px;
height: 25px;
background: url('multiple-select.png') right top no-repeat;
}
.ms-choice > div.open {
background: url('multiple-select.png') left top no-repeat;
}
.ms-drop {
overflow: hidden;
display: none;
margin-top: -1px;
padding: 0;
position: absolute;
z-index: 1000;
background: #fff;
color: #000;
border: 1px solid #aaa;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.ms-drop.bottom {
top: 100%;
-webkit-box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
-moz-box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
}
.ms-drop.top {
bottom: 100%;
-webkit-box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
-moz-box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
}
.ms-search {
display: inline-block;
margin: 0;
min-height: 26px;
padding: 4px;
position: relative;
white-space: nowrap;
width: 100%;
z-index: 10000;
}
.ms-search input {
width: 100%;
height: auto !important;
min-height: 24px;
padding: 0 20px 0 5px;
margin: 0;
outline: 0;
font-family: sans-serif;
font-size: 1em;
border: 1px solid #aaa;
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
background: #fff url('multiple-select.png') no-repeat 100% -22px;
background: url('multiple-select.png') no-repeat 100% -22px, -webkit-gradient(linear, left bottom, left top, color-stop(0.85, white), color-stop(0.99, #eeeeee));
background: url('multiple-select.png') no-repeat 100% -22px, -webkit-linear-gradient(center bottom, white 85%, #eeeeee 99%);
background: url('multiple-select.png') no-repeat 100% -22px, -moz-linear-gradient(center bottom, white 85%, #eeeeee 99%);
background: url('multiple-select.png') no-repeat 100% -22px, -o-linear-gradient(bottom, white 85%, #eeeeee 99%);
background: url('multiple-select.png') no-repeat 100% -22px, -ms-linear-gradient(top, #ffffff 85%, #eeeeee 99%);
background: url('multiple-select.png') no-repeat 100% -22px, linear-gradient(top, #ffffff 85%, #eeeeee 99%);
}
.ms-search, .ms-search input {
-webkit-box-sizing: border-box;
-khtml-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
}
.ms-drop ul {
overflow: auto;
margin: 0;
padding: 5px 8px;
}
.ms-drop ul > li {
list-style: none;
display: list-item;
background-image: none;
position: static;
}
.ms-drop ul > li .disabled {
opacity: .35;
filter: Alpha(Opacity=35);
}
.ms-drop ul > li.multiple {
display: block;
float: left;
}
.ms-drop ul > li.group {
clear: both;
}
.ms-drop ul > li.multiple label {
width: 100%;
display: block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.ms-drop ul > li label.optgroup {
font-weight: bold;
}
.ms-drop input[type="checkbox"] {
vertical-align: middle;
}
.ms-drop .ms-no-results {
display: none;
}

View File

@@ -0,0 +1,28 @@
{
"name": "multiple-select",
"version": "1.1.0",
"title": "Multiple Select",
"description": "Multiple select is a jQuery plugin to select multiple elements with checkboxes :).",
"author": {
"name": "zhixin wen",
"email": "wenzhixin2010@gmail.com",
"url": "http://wenzhixin.net.cn/"
},
"licenses": [
{
"type": "MIT License",
"url": "http://opensource.org/licenses/MIT"
}
],
"dependencies": {
"jquery": ">=1.7"
},
"keywords": ["multiple.select", "select.list", "multiple.choose", "checkbox"],
"homepage": "http://wenzhixin.net.cn/p/multiple-select/",
"demo": "http://wenzhixin.net.cn/p/multiple-select/#examples",
"bugs": {
"url": "https://github.com/wenzhixin/multiple-select/issues"
},
"docs": "http://wenzhixin.net.cn/p/multiple-select/#documentation",
"download": "https://github.com/wenzhixin/multiple-select/archive/master.zip"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB