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

90 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-12-02 03:28:36 +03:00
import Util from './util'
2015-12-01 21:43:01 +03:00
2015-12-02 03:28:36 +03:00
// Checkbox decorator, to be called after Input
2015-12-01 21:43:01 +03:00
const Checkbox = (($) => {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'checkbox'
const DATA_KEY = `mdb.${NAME}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Default = {
template: `<span class='checkbox-material'><span class='check'></span></span>`
}
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Checkbox {
constructor(element, config) {
this.$element = $(element)
2015-12-01 21:43:01 +03:00
this.config = $.extend({}, Default, config)
this.$element.after(this.config.template)
this.$formGroup = Util.findFormGroup(this.$element, false)
2015-12-02 03:28:36 +03:00
this._bindEventListeners()
2015-12-01 21:43:01 +03:00
}
dispose() {
$.removeData(this.$element, DATA_KEY)
this.$element = null
this.$formGroup = null
2015-12-01 21:43:01 +03:00
this.config = null
}
// ------------------------------------------------------------------------
// private
2015-12-02 03:28:36 +03:00
_bindEventListeners() {
// checkboxes didn't appear to bubble to the document, so we'll bind these directly
this.$formGroup.find('.checkbox label').hover(() => {
Util.addFormGroupFocus(this.$formGroup)
2015-12-02 03:28:36 +03:00
}, () => {
Util.removeFormGroupFocus(this.$formGroup)
2015-12-02 03:28:36 +03:00
})
2015-12-01 21:43:01 +03:00
this.$element.change(() => {
this.$element.blur()
2015-12-02 03:28:36 +03:00
})
}
2015-12-01 21:43:01 +03:00
// ------------------------------------------------------------------------
// static
static _jQueryInterface(config) {
return this.each(function () {
let $element = $(this)
let data = $element.data(DATA_KEY)
if (!data) {
data = new Checkbox(this, config)
$element.data(DATA_KEY, data)
}
})
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Checkbox._jQueryInterface
$.fn[NAME].Constructor = Checkbox
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Checkbox._jQueryInterface
}
return Checkbox
})(jQuery)
export default Checkbox