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

95 lines
2.7 KiB
JavaScript
Raw Normal View History

import BaseSelection from './baseSelection'
import Text from './text'
import File from './file'
import Radio from './radio'
import Textarea from './textarea'
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_NAME = `mdb${NAME.charAt(0).toUpperCase() + NAME.slice(1)}`
const JQUERY_NO_CONFLICT = $.fn[JQUERY_NAME]
2015-12-01 21:43:01 +03:00
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 BaseSelection {
2015-12-01 21:43:01 +03:00
2015-12-07 20:40:42 +03:00
constructor($element, config, properties = {inputType: NAME, outerClass: NAME}) {
super($element, $.extend(true, {
invalidComponentMatches: [File, Radio, Text, Textarea, Select]
}, Default, config), properties)
}
dispose(dataKey = DATA_KEY) {
super.dispose(dataKey)
2015-12-06 00:19:06 +03:00
}
static matches($element) {
// '.checkbox > label > input[type=checkbox]'
if ($element.attr('type') === 'checkbox') {
return true
}
return false
}
static rejectMatch(component, $element) {
Util.assert(this.$element, this.matches($element), `${component} component element ${Util.describe($element)} 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) {
2015-12-07 20:40:42 +03:00
data = new Checkbox($element, config)
2015-12-01 21:43:01 +03:00
$element.data(DATA_KEY, data)
}
})
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[JQUERY_NAME] = Checkbox._jQueryInterface
$.fn[JQUERY_NAME].Constructor = Checkbox
$.fn[JQUERY_NAME].noConflict = () => {
$.fn[JQUERY_NAME] = JQUERY_NO_CONFLICT
2015-12-01 21:43:01 +03:00
return Checkbox._jQueryInterface
}
return Checkbox
})(jQuery)
export default Checkbox