Initial commit

This commit is contained in:
Khanh Ngo
2015-12-13 16:34:12 +07:00
commit 2dac8205f6
3113 changed files with 514935 additions and 0 deletions

View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 xavierfaucon
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,74 @@
# bootstrap-selectsplitter
## Presentation
Transforms SELECT containing one or more OPTGROUP in two chained SELECT.
This:
![image1](http://img4.hostingpics.net/pics/927121bootstrapselectsplitterimage1.png)
Becomes this:
![image2](http://img4.hostingpics.net/pics/997752bootstrapselectsplitterimage2.png)
## Demo
See the [online demo](http://jsfiddle.net/ae7fxdyy/).
## How to use
Create a SELECT tag containing one or more OPTGROUP:
```HTML
<select data-selectsplitter-selector>
<optgroup label="Category 1">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
<option value="3">Choice 3</option>
<option value="4">Choice 4</option>
</optgroup>
<optgroup label="Category 2">
<option value="5">Choice 5</option>
<option value="6">Choice 6</option>
<option value="7">Choice 7</option>
<option value="8">Choice 8</option>
</optgroup>
<optgroup label="Category 3">
<option value="5">Choice 9</option>
<option value="6">Choice 10</option>
<option value="7">Choice 11</option>
<option value="8">Choice 12</option>
</optgroup>
</select>
```
Add the dependency files (jQuery and Bootstrap 3 CSS):
```HTML
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="bootstrap-selectsplitter.js"></script>
```
Call the plugin:
```JavaScript
$('select[data-selectsplitter-selector]').selectsplitter();
```
## Bower
bower install bootstrap-selectsplitter
##CDN
```HTML
<script src="//cdn.jsdelivr.net/bootstrap.selectsplitter/0.1.0/bootstrap-selectsplitter.min.js"></script>
```
## Copyright and license
Copyright (C) 2015 Xavier Faucon
Licensed under the MIT license.

View File

@ -0,0 +1,196 @@
+function ($) {
'use strict';
// CLASS DEFINITION
// ===============================
// NB: only user input triggers event change on SELECT.
var SelectSplitter = function(element, options) {
this.init('selectsplitter', element, options);
};
SelectSplitter.DEFAULTS = {
template:
'<div class="row" data-selectsplitter-wrapper-selector>' +
'<div class="col-xs-12 col-sm-6">' +
'<select class="form-control" data-selectsplitter-firstselect-selector></select>' +
'</div>' +
' <!-- Add the extra clearfix for only the required viewport -->' +
'<div class="clearfix visible-xs-block"></div>' +
'<div class="col-xs-12 col-sm-6">' +
'<select class="form-control" data-selectsplitter-secondselect-selector></select>' +
'</div>' +
'</div>'
};
/* Note: Est appelé par la fonction définie en var */
SelectSplitter.prototype.init = function (type, element, options) {
// Initial variables.
var self = this;
self.type = type;
self.$element = $(element);
self.$element.hide();
self.options = $.extend( {}, SelectSplitter.DEFAULTS, options);
// Get categoryParent data from $element's OPTGROUP.
self.fullCategoryList = {};
var optgroupCount = 0;
var longestOptionCount = 0;
self.$element.find('optgroup').each(function() {
self.fullCategoryList[$(this).attr('label')] = {};
var $that = $(this);
var currentOptionCount = 0;
$(this).find('option').each(function() {
self.fullCategoryList[$that.attr('label')][$(this).attr('value')] = $(this).text();
currentOptionCount++;
});
if (currentOptionCount > longestOptionCount) { longestOptionCount = currentOptionCount ;}
optgroupCount++;
});
// Get OPTIONS for $firstSelect.
var optionsHtml = '';
for (var key in self.fullCategoryList) {
if (self.fullCategoryList.hasOwnProperty(key)) {
optionsHtml = optionsHtml + '<option>' + key + '</option>';
}
}
// Add template.
self.$element.after(self.options.template);
// Define selected elements.
self.$wrapper = self.$element.next('div[data-selectsplitter-wrapper-selector]'); // improved by keenthemes
self.$firstSelect = $('select[data-selectsplitter-firstselect-selector]', self.$wrapper); // improved by keenthemes
self.$secondSelect = $('select[data-selectsplitter-secondselect-selector]', self.$wrapper); // improved by keenthemes
// Define $firstSelect and $secondSelect size attribute
var selectSize = Math.max(optgroupCount, longestOptionCount);
selectSize = Math.min(selectSize, 10);
if (self.options.selectSize) {
selectSize = self.options.selectSize; // improved by keenthemes
}
self.$firstSelect.attr('size', selectSize);
self.$secondSelect.attr('size', selectSize);
// Fill $firstSelect with OPTIONS
self.$firstSelect.append(optionsHtml);
// Define events.
self.$firstSelect.on('change', $.proxy(self.updateParentCategory, self));
self.$secondSelect.on('change', $.proxy(self.updateChildCategory, self));
// Define main variables.
self.$selectedOption = '';
self.currentParentCategory = '';
self.currentChildCategory = '';
// Takes in consideration whether an option is already selected before initialization.
// Note: .val() always returns the last value if SELECT is new. Hence cannot use .val() at init.
// Note2: find(option:selected) retourne toujours une OPTION même lors du premier affichage.
if ( self.$element.find('option[selected=selected]').length) {
self.$selectedOption = self.$element.find('option[selected=selected]');
self.currentParentCategory = self.$selectedOption.closest('optgroup').attr('label');
self.currentChildCategory = self.$selectedOption.attr('value');
self.$firstSelect.find('option:contains('+ self.currentParentCategory +')').attr('selected', 'selected');
self.$firstSelect.trigger('change');
}
};
SelectSplitter.prototype.updateParentCategory = function () {
var self = this;
// Update main variables.
self.currentParentCategory = self.$firstSelect.val();
self.$secondSelect.empty();
// Définit la liste de I pour les icônes à afficher en fonction de la page.
var optionsHtml = '';
for (var key in self.fullCategoryList[self.currentParentCategory]) {
if (self.fullCategoryList[self.currentParentCategory].hasOwnProperty(key)) {
optionsHtml = optionsHtml + '<option value="' + key + '">' +
self.fullCategoryList[self.currentParentCategory][key] +
'</option>';
}
}
self.$secondSelect.append(optionsHtml);
if ( self.$selectedOption ) {
self.$secondSelect.find( 'option[value=' + self.$selectedOption.attr('value') + ']' ).attr('selected', 'selected');
}
};
SelectSplitter.prototype.updateChildCategory = function (event) {
var self = this;
// Update main variables.
self.currentChildCategory = $(event.target).val(); // Note: event.target returns the SELECT, hence we must use val().
// Remove selected items in $element SELECT, if any.
self.$element.find('option[selected=selected]').removeAttr('selected');
// Add selected attribute to the new selected OPTION.
self.$element.find('option[value=' + self.currentChildCategory + ']').attr('selected', 'selected'); // Note: Adding attr also updates val().
self.$element.trigger('change'); // Required for external plugins.
self.$selectedOption = self.$element.find('option[selected=selected]');
};
SelectSplitter.prototype.destroy = function () {
var self = this;
self.$wrapper.remove();
self.$element.removeData(self.type);
self.$element.show();
};
// PLUGIN DEFINITION
// =========================
function Plugin(option) {
return this.each(function() {
var $this = $(this);
var data = $this.data('selectsplitter');
var options = typeof option === 'object' && option;
if (!data && option == 'destroy') { return; }
if (!data) { $this.data('selectsplitter', ( data = new SelectSplitter(this, options) ) ); }
if (typeof option == 'string') { data[option](); }
});
}
$.fn.selectsplitter = Plugin;
/* http://stackoverflow.com/questions/10525600/what-is-the-purpose-of-fn-foo-constructor-foo-in-twitter-bootstrap-js */
$.fn.selectsplitter.Constructor = SelectSplitter;
}(jQuery);

View File

@ -0,0 +1 @@
+function(e){"use strict";function t(t){return this.each(function(){var l=e(this),s=l.data("selectsplitter"),o="object"==typeof t&&t;(s||"destroy"!=t)&&(s||l.data("selectsplitter",s=new r(this,o)),"string"==typeof t&&s[t]())})}var r=function(e,t){this.init("selectsplitter",e,t)};r.DEFAULTS={template:'<div class="row" data-selectsplitter-wrapper-selector><div class="col-xs-12 col-sm-6"><select class="form-control" data-selectsplitter-firstselect-selector></select></div> <!-- Add the extra clearfix for only the required viewport --><div class="clearfix visible-xs-block"></div><div class="col-xs-12 col-sm-6"><select class="form-control" data-selectsplitter-secondselect-selector></select></div></div>'},r.prototype.init=function(t,l,s){var o=this;o.type=t,o.$element=e(l),o.$element.hide(),o.options=e.extend({},r.DEFAULTS,s),o.fullCategoryList={};var a=0,n=0;o.$element.find("optgroup").each(function(){o.fullCategoryList[e(this).attr("label")]={};var t=e(this),r=0;e(this).find("option").each(function(){o.fullCategoryList[t.attr("label")][e(this).attr("value")]=e(this).text(),r++}),r>n&&(n=r),a++});var i="";for(var c in o.fullCategoryList)o.fullCategoryList.hasOwnProperty(c)&&(i=i+"<option>"+c+"</option>");o.$element.after(o.options.template),o.$wrapper=o.$element.next("div[data-selectsplitter-wrapper-selector]"),o.$firstSelect=e("select[data-selectsplitter-firstselect-selector]",o.$wrapper),o.$secondSelect=e("select[data-selectsplitter-secondselect-selector]",o.$wrapper);var p=Math.max(a,n);p=Math.min(p,10),o.options.selectSize&&(p=o.options.selectSize),o.$firstSelect.attr("size",p),o.$secondSelect.attr("size",p),o.$firstSelect.append(i),o.$firstSelect.on("change",e.proxy(o.updateParentCategory,o)),o.$secondSelect.on("change",e.proxy(o.updateChildCategory,o)),o.$selectedOption="",o.currentParentCategory="",o.currentChildCategory="",o.$element.find("option[selected=selected]").length&&(o.$selectedOption=o.$element.find("option[selected=selected]"),o.currentParentCategory=o.$selectedOption.closest("optgroup").attr("label"),o.currentChildCategory=o.$selectedOption.attr("value"),o.$firstSelect.find("option:contains("+o.currentParentCategory+")").attr("selected","selected"),o.$firstSelect.trigger("change"))},r.prototype.updateParentCategory=function(){var e=this;e.currentParentCategory=e.$firstSelect.val(),e.$secondSelect.empty();var t="";for(var r in e.fullCategoryList[e.currentParentCategory])e.fullCategoryList[e.currentParentCategory].hasOwnProperty(r)&&(t=t+'<option value="'+r+'">'+e.fullCategoryList[e.currentParentCategory][r]+"</option>");e.$secondSelect.append(t),e.$selectedOption&&e.$secondSelect.find("option[value="+e.$selectedOption.attr("value")+"]").attr("selected","selected")},r.prototype.updateChildCategory=function(t){var r=this;r.currentChildCategory=e(t.target).val(),r.$element.find("option[selected=selected]").removeAttr("selected"),r.$element.find("option[value="+r.currentChildCategory+"]").attr("selected","selected"),r.$element.trigger("change"),r.$selectedOption=r.$element.find("option[selected=selected]")},r.prototype.destroy=function(){var e=this;e.$wrapper.remove(),e.$element.removeData(e.type),e.$element.show()},e.fn.selectsplitter=t,e.fn.selectsplitter.Constructor=r}(jQuery);

View File

@ -0,0 +1,11 @@
{
"name": "bootstrap-selectsplitter",
"version": "0.1.0",
"main": [
"bootstrap-selectsplitter.js"
],
"dependencies": {
"jQuery": ">= 1.9.1",
"bootstrap": ">= 3.0"
}
}