;(function(factory) {
if (typeof define === 'function' && define.amd) {
//AMD support
define(['jquery','fscroll'], factory);
} else {
//Browser global
factory(jQuery);
}
})(function($, fscroll) {
var Fselect = function(el, conf) {
this.conf = conf;
this.init(el, conf);
}
var timesheet;
//var isTouch = Modernizr.touch;
var isTouch = false;
Fselect.DEFAULTS = {
"maxHeight" : 250
}
Fselect.prototype.init = function(el, conf) {
var $el = $(el),
self = this;
this.isOpen = false;
this.key = '';
this.dataList = [];
this.textList = [];
this.isOpen = false;
this.$el = $el;
this.isDisabled = $el.prop('disabled');
this.render();
var $simSelect = $el.nextAll('.sim-select'),
$simHead = $simSelect.find('.sim-head'),
$simList = $simSelect.find('.sim-list');
this.$simSelect = $simSelect;
if(this.isDisabled) $simSelect.addClass('disabled');
$simSelect.on('focus', function(){
$simSelect.addClass('focus');
}).on('blur', function(){
$simSelect.removeClass('focus');
self.$el.val($simHead.find('.current').attr('str'));
self.close();
}).on('keyup', function(e){
var keylist = [13, 32],
arrowList = [38, 40];
if ($.inArray(e.keyCode, keylist) != -1) {
e.preventDefault();
self.open();
return;
}else if($.inArray(e.keyCode, arrowList) != -1){
e.preventDefault();
return;
}
var selectEL = $(this).prev();
selectEL.data('fselect').search(e, $simHead);
}).on('keydown', function(e){
var keylist = [13, 32],
arrowList = [38, 40];
if ($.inArray(e.keyCode, keylist) != -1) {
e.preventDefault();
return;
}else if($.inArray(e.keyCode, arrowList) != -1){
e.preventDefault();
var selectEL = $(this).prev();
selectEL.data('fselect').search(e, $simHead);
}
})
$simSelect.attr('tabindex', 0);
$el.attr('tabindex', 1);
$simSelect.on('click', '.sim-head', function(e) {
e.stopPropagation();
clearTimeout(timesheet);
$('select').not($el).each(function(){
$(this).next().removeClass('open').removeClass('focus');
$(this).next().find('.sim-list').hide();
$(this).parent().css('z-index', '100');
});
if($(this).closest('.sim-select').hasClass('disabled')) return;
self.open();
}).on('click', '.sim-list li', function(e) {
var $this = $(this),
val = $this.attr('str');
$el.data('currentoptdisabled', true);
if($this.hasClass('disabled')) {
$el.data('currentoptdisabled', false);
return;
}
$this.siblings().removeClass('hover').end().addClass('hover');
if($this.find('.so-title').length){
$simHead.find('.current').attr('str', val).text($this.find('.so-title').text());
}else{
$simHead.find('.current').attr('str', val).text($el.find('option').eq($this.index()).text());
}
$simSelect.parent().css('z-index', '100');
$simList.hide();
if ($el.data('change') == "alltime" || $simHead.find('.current').attr('str') != $el.val()) {
$el.val($simHead.find('.current').attr('str')).trigger('change');
}
if(self.conf && self.conf.afterClose && typeof self.conf.afterClose === 'function') self.conf.afterClose();
$simSelect.removeClass('open');
if(self.conf && self.conf.afterSelected && typeof self.conf.afterSelected === 'function') self.conf.afterSelected($el);
return false;
}).on('mouseenter', '.sim-list li', function() {
$(this).siblings().removeClass('hover')
.end().addClass('hover');
}).on('mouseleave', '.sim-list li', function() {
$(this).removeClass('hover');
}).on('mouseleave', '.sim-list ul', function() {
var val = $simHead.find('.current').attr('str');
$(this).find('li[str="'+ val +'"]').addClass('hover');
});
$el.on('change', function(e) {
if(isTouch) {
var $this = $(this),
$head = $this.nextAll('.sim-select').find('.sim-head .current');
$head.text($this.find('option').filter(':selected').text());
}
});
}
Fselect.prototype.checkAttr = function($el, i, selectList){
var self = this,
selected = $el.find("option").eq(i).is(':selected') ? 'hover' : '',
disabled = $el.find('option').eq(i).is(':disabled') ? 'disabled' : '',
unavailable = $el.find('option').eq(i).prop('unavailable') ? 'unavailable' : '',
hide = $el.find('option').eq(i).data('hideoption') ? 'hide' : '',
soContent = '';
var attrsArr = [selected, disabled, unavailable, hide],
attrs = attrsArr.join(' ');
var $text = self.$el.find("option").eq(i),
_text = $text.text();
if(self.conf.renderLetter) {
if($text.data('letter')) {
_text = $text.data('letter') + " - " + _text;
}
}
if (i == $el.find("option").length-1) selected += ' last';
if ($el.data('intercontent')) {
if($text.data('content')){
soContent = "
"+ $text.data('content') +"
";
}
selectList += ""+ _text +""+ soContent +"";
} else {
selectList += ""+ _text +"";
}
return selectList;
}
Fselect.prototype.render = function() {
var self = this,
$el = this.$el,
upward = $el.data('upward') ? 'upward' : '',
selectList = '',
selected = $el.val() ? $el.find("option[value = '"+ $el.val() +"']").text() : $el.find("option").eq(0).text(),
selectStr = $el.val() ? $el.val() : $el.find("option").eq(0).val();
for(var i = 0, len = $el.find('option').length; i");
var str = "";
str += "
";
str += ""+ selected +"";
str += "
";
str += "
";
str += "
";
$el.after(str);
if(self.conf.afterRender) $.extend({}, self.conf).afterRender(self, self.conf);
if(!$el.next().find('li.hover').length) {
$el.next().find('li:first').addClass('hover');
}
if(!isTouch) {
$el.css('z-index', 8);
$el.css('visibility', 'hidden');
}else {
if($el.data('intercontent')) {
$el.css('z-index', 8);
$el.css('visibility', 'hidden');
}
}
}
Fselect.prototype.open = function() {
var self = this,
$el = self.$el,
$select = self.$simSelect,
$simSelect = $el.nextAll('.sim-select'),
$simHead = $simSelect.find('.sim-head'),
$simList = $simSelect.find('.sim-list');
if(!$select.find('ul li.hover').length) {
$select.find('ul li').eq(0).addClass('hover');
}
var $curLi = $select.find('ul li.hover');
$(document).off('mouseup.fselect').on('mouseup.fselect', function(e){
var $target = $(e.target);
if ($target.closest('.sim-select').length < 1 && !$select.find('ul').data('fscroll')) {
$target.closest('.sim-select').find('ul').data('fscroll', false);
$('select').fselect('close');
}
});
if(isTouch && !$el.data('intercontent')) {
if($simSelect.hasClass('open')){
$simSelect.removeClass('open');
}else {
$simSelect.addClass('open');
}
}else {
if($simSelect.hasClass('open')){
$simSelect.parent().css('z-index', '100');
$simList.hide(0, function(){
$simSelect.removeClass('open');
this.isOpen = false;
if($simHead.find('.current').attr('str') !== $el.val()) {
$el.val($simHead.find('.current').attr('str')).trigger('change');
}
});
}else {
$simSelect.parent().css('z-index', '101');
$simList.show(0, function(){
$simSelect.addClass('open');
if (!self.liHeight) {
self.liHeight = $curLi.outerHeight()
}
self.isOpen = true;
if($select.find('.sbarContent').length) {
var $sbarContent = $select.find('.sbarContent'),
contentHeight = $sbarContent.height(),
scrollTop = $curLi.position().top,
ulHeight = $select.find('ul').height();
$select.find('ul').css({
'overflow-y' : 'hidden'
}).fscroll({
scrollTo: function(fscroll, conf) {
fscroll.setScrollContainer(scrollTop / contentHeight * ulHeight, scrollTop);
conf.offsetTop = scrollTop / contentHeight * ulHeight;
}
});
}else {
if(isTouch) {
$select.find('ul').css({
'overflow-y' : 'auto'
}).scrollTop($curLi.position().top);
}else {
$select.find('ul').css({
'overflow-y' : 'hidden'
}).fscroll({
minHeight: self.conf.maxHeight,
scrollTo: function(fscroll, conf) {
var $sbarContent = $select.find('.sbarContent'),
contentHeight = $sbarContent.height(),
scrollTop = $curLi.position().top,
ulHeight = $select.find('ul').height();
fscroll.setScrollContainer(scrollTop / contentHeight * ulHeight, scrollTop);
conf.offsetTop = scrollTop / contentHeight * ulHeight;
}
});
}
}
});
}
}
}
Fselect.prototype.returnIndex = function(evt, keyCode, key){
var keyIndex = -1,
len = key.length;
switch(keyCode) {
case 38 :
evt.preventDefault();
keyIndex = this.keyIndex - 1;
if(keyIndex < 0) {
keyIndex = this.textList.length-1;
}
break;
case 40 :
evt.preventDefault();
keyIndex = this.keyIndex + 1;
if(keyIndex == this.textList.length) {
keyIndex = 0;
}
break;
default :
var indexs = 0,
firstIndex = 0;
matchIndex = 0;
for(var i = 0; i < this.textList.length; i++) {
if(this.textList[i].substring(0, len).toLowerCase() === key.toLowerCase()) {
if(firstIndex == 0) firstIndex = i;
indexs++;
}else {
this.$el.find('option').eq(i).data('selected', false);
}
}
for(var i = 0; i < this.textList.length; i++) {
if(this.textList[i].substring(0, len).toLowerCase() === key.toLowerCase()) {
matchIndex ++
if(!this.$el.find('option').eq(i).data('selected')) {
keyIndex = i;
this.$el.find('option').eq(i).data('selected', true);
if(matchIndex == indexs) {
this.$el.find('option').data('selected', false);
}
break;
}
}
}
break;
}
this.keyIndex = keyIndex;
}
Fselect.prototype.search = function(evt, $simHead) {
this.keyIndex = this.$simSelect.find('li.hover').index();
var self = this,
keyCode = evt.which,
$select = this.$simSelect,
currentKey = String.fromCharCode(keyCode);
this.key = currentKey;
/*muilt letter
* this.key += currentKey;
* clearTimeout(spellKey);
* var spellKey = setTimeout(function(){
* self.key = '';
* }, 500);
*/
this.returnIndex(evt, keyCode, this.key);
if(this.keyIndex != -1) {
var currentVal = this.dataList[this.keyIndex],
currentText = this.textList[this.keyIndex];
if(!$select.find('ul li.hover').length) {
$select.find('ul li').eq(0).addClass('hover');
}
var $curLi = $select.find('ul li.hover');
$select.find('li').eq(this.keyIndex).addClass('hover').siblings().removeClass('hover');
$select.find('.sim-head .current').attr('str', currentVal).text(self.$el.find('option').eq(this.keyIndex).text());
if($simHead.find('.current').attr('str') != this.$el.val()) {
this.$el.val($simHead.find('.current').attr('str')).trigger('change');
}
if(this.$simSelect.find('.sbarContent').length) {
var $sbarContent = $select.find('.sbarContent'),
contentHeight = $sbarContent.height(),
scrollTop = $select.find('ul li.hover').position().top,
ulHeight = $select.find('ul').height();
$select.find('ul').fscroll({
scrollTo: function(fscroll, setting) {
fscroll.setScrollContainer(scrollTop / contentHeight * ulHeight, scrollTop);
setting.offsetTop = scrollTop / contentHeight * ulHeight;
}
});
}else {
$select.find('ul').scrollTop($curLi.position().top);
}
}
}
Fselect.prototype.close = function(){
$('.sim-select.open').parent().css('z-index', '100');
$('.sim-select.open').find('.sim-list').hide();
$('.sim-select.open').removeClass('open');
if(this.conf && this.conf.afterClose && typeof this.conf.afterClose === 'function') this.conf.afterClose();
}
Fselect.prototype.update = function(opts) {
if(opts && opts.afterSelected) this.conf = opts;
var $el = this.$el,
selectList = '',
$simSelect = $el.nextAll('.sim-select'),
$simHead = $simSelect.find('.sim-head'),
$simList = $simSelect.find('.sim-list'),
selected = ($el.val() !== null) ? $el.find("option[value = '"+ $el.val() +"']").text() : $el.find("option").eq(0).text(),
selectStr = ($el.val() !== null) ? $el.val() : $el.find("option").eq(0).attr('value');
self.isDisabled = $el.prop('disabled');
if(self.isDisabled) $simSelect.addClass('disabled');
else $simSelect.removeClass('disabled');
this.dataList.splice(0, this.dataList.length)
this.textList.splice(0, this.textList.length)
for(var i = 0, len = $el.find('option').length; i'+ selectList + '');
}
Fselect.prototype.updateOpts = function(opts) {
$.extend(this.conf, typeof opts === 'object' && opts);
}
$.fn.fselect = function(opts) {
node = this;
this.each(function() {
var $this = $(this),
data = $this.data('fselect'),
conf = $.extend({}, Fselect.DEFAULTS, typeof opts === 'object' && opts);
if (!data) {
$this.data('fselect', (data = new Fselect(this, conf)));
}
if (typeof opts == 'string') data[opts]();
});
return this;
}
});