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

94 lines
2.5 KiB
JavaScript
Raw Normal View History

import BaseToggle from './baseToggle'
import Text from './text'
import File from './file'
import Radio from './radio'
import Textarea from './textare'
import Select from './select'
2015-12-02 03:28:36 +03:00
import Util from './util'
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-decorator'><span class='check'></span></span>`
}
2015-12-01 21:43:01 +03:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Checkbox extends BaseToggle {
2015-12-01 21:43:01 +03:00
constructor(element, config, inputType = NAME, outerClass = NAME) {
super(element, $.extend({
invalidComponentMatches: [File, Radio, Text, Textarea, Select]
}, Default, config), inputType, outerClass)
}
2015-12-06 00:19:06 +03:00
dispose() {
super.dispose(DATA_KEY)
}
static matches($element) {
// '.checkbox > label > input[type=checkbox]'
if ($element.attr('type') === 'checkbox') {
return true
}
return false
}
static rejectMatch(component, $element) {
Util.assert(this.matches($element), `${component} component is invalid for type='checkbox'.`)
2015-12-01 21:43:01 +03:00
}
// ------------------------------------------------------------------------
// protected
// ------------------------------------------------------------------------
// protected
// ------------------------------------------------------------------------
// private
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