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

72 lines
1.8 KiB
JavaScript
Raw Normal View History

import Checkbox from './checkbox'
2015-12-01 21:47:00 +03:00
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 extends Checkbox {
2015-12-01 21:47:00 +03:00
constructor(element, config) {
super(element, $.extend({}, Default, config), 'checkbox', NAME)
// selector: '.switch > label > input[type=checkbox]'
2015-12-01 21:47:00 +03:00
}
dispose() {
super.dispose(DATA_KEY)
2015-12-01 21:47:00 +03:00
}
// ------------------------------------------------------------------------
// protected
2015-12-01 21:47:00 +03:00
// ------------------------------------------------------------------------
// 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