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

73 lines
1.9 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_NAME = `mdb${NAME.charAt(0).toUpperCase() + NAME.slice(1)}`
const JQUERY_NO_CONFLICT = $.fn[JQUERY_NAME]
2015-12-01 21:47:00 +03:00
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[JQUERY_NAME] = Switch._jQueryInterface
$.fn[JQUERY_NAME].Constructor = Switch
$.fn[JQUERY_NAME].noConflict = () => {
$.fn[JQUERY_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