Component-Builder-fork/media/uikit/js/components/form-select.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-11-12 00:33:10 +00:00
/*! UIkit 2.27.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2016-01-30 20:28:43 +00:00
(function(addon) {
var component;
2017-11-12 00:33:10 +00:00
if (window.UIkit2) {
component = addon(UIkit2);
2016-01-30 20:28:43 +00:00
}
2017-11-12 00:33:10 +00:00
if (typeof define == 'function' && define.amd) {
define('uikit-form-select', ['uikit'], function(){
return component || addon(UIkit2);
2016-01-30 20:28:43 +00:00
});
}
})(function(UI){
"use strict";
UI.component('formSelect', {
defaults: {
2017-11-12 00:33:10 +00:00
target: '>span:first',
activeClass: 'uk-active'
2016-01-30 20:28:43 +00:00
},
boot: function() {
// init code
UI.ready(function(context) {
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-form-select]', context).each(function(){
2016-01-30 20:28:43 +00:00
var ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!ele.data('formSelect')) {
UI.formSelect(ele, UI.Utils.options(ele.attr('data-uk-form-select')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
2017-11-12 00:33:10 +00:00
2016-01-30 20:28:43 +00:00
var $this = this;
this.target = this.find(this.options.target);
this.select = this.find('select');
// init + on change event
2017-11-12 00:33:10 +00:00
this.select.on({
change: (function(){
var select = $this.select[0], fn = function(){
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
try {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if($this.options.target === 'input') {
$this.target.val(select.options[select.selectedIndex].text);
} else {
$this.target.text(select.options[select.selectedIndex].text);
}
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
} catch(e) {}
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
$this.element[$this.select.val() ? 'addClass':'removeClass']($this.options.activeClass);
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
return fn;
};
return fn();
})(),
focus: function(){ $this.target.addClass('uk-focus') },
blur: function(){ $this.target.removeClass('uk-focus') },
mouseenter: function(){ $this.target.addClass('uk-hover') },
mouseleave: function(){ $this.target.removeClass('uk-hover') }
});
2016-01-30 20:28:43 +00:00
this.element.data("formSelect", this);
}
});
return UI.formSelect;
});