mdb-ui-kit/js/src/switch.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-12-01 21:47:00 +03:00
//import Util from './util'
// Switch decorator, to be called after Input
const Switch = (($) => {
2015-12-01 21:47:00 +03:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'switch'
2015-12-01 21:47:00 +03:00
const DATA_KEY = `mdb.${NAME}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Default = {
template: `<span class='switch-decorator'></span>`
2015-12-01 21:47:00 +03:00
}
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Switch {
2015-12-01 21:47:00 +03:00
constructor(element, config) {
this.$element = $(element)
2015-12-01 21:47:00 +03:00
this.config = $.extend({}, Default, config)
this.$element.after(this.config.template)
2015-12-01 21:47:00 +03:00
}
dispose() {
$.removeData(this.$element, DATA_KEY)
this.$element = null
2015-12-01 21:47:00 +03:00
this.config = null
}
// ------------------------------------------------------------------------
// private
// ------------------------------------------------------------------------
// static
static _jQueryInterface(config) {
return this.each(function () {
let $element = $(this)
let data = $element.data(DATA_KEY)
if (!data) {
data = new Switch(this, config)
2015-12-01 21:47:00 +03:00
$element.data(DATA_KEY, data)
}
})
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Switch._jQueryInterface
$.fn[NAME].Constructor = Switch
2015-12-01 21:47:00 +03:00
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Switch._jQueryInterface
2015-12-01 21:47:00 +03:00
}
return Switch
2015-12-01 21:47:00 +03:00
})(jQuery)
export default Switch