2015-12-05 23:00:40 +03:00
|
|
|
import BaseInput from './baseInput'
|
|
|
|
import TextInput from './textInput'
|
|
|
|
import FileInput from './fileInput'
|
|
|
|
import Radio from './radio'
|
|
|
|
import Switch from './switch'
|
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 = {
|
2015-12-05 23:00:40 +03:00
|
|
|
template: `<span class='checkbox-material'><span class='check'></span></span>`,
|
|
|
|
formGroup: {
|
|
|
|
autoCreate: true
|
|
|
|
},
|
|
|
|
invalidComponentMatches: [TextInput, FileInput, Radio, Switch]
|
2015-12-01 21:43:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-12-05 23:00:40 +03:00
|
|
|
class Checkbox extends BaseInput {
|
2015-12-01 21:43:01 +03:00
|
|
|
|
2015-12-05 00:00:57 +03:00
|
|
|
constructor(element, config) {
|
2015-12-05 23:00:40 +03:00
|
|
|
super(element, Default, config)
|
2015-12-04 04:09:01 +03:00
|
|
|
this.$element.after(this.config.template)
|
2015-12-01 21:43:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
2015-12-05 23:00:40 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2015-12-05 23:00:40 +03:00
|
|
|
// protected
|
|
|
|
|
|
|
|
// Demarcation element (e.g. first child of a form-group)
|
|
|
|
// Subclasses such as file inputs may have different structures
|
|
|
|
outerElement() {
|
|
|
|
// '.checkbox > label > input[type=checkbox]'
|
|
|
|
return this.$element.parent().parent()
|
|
|
|
}
|
|
|
|
|
|
|
|
rejectWithoutRequiredStructure() {
|
|
|
|
// '.checkbox > label > input[type=checkbox]'
|
|
|
|
Util.assert(this.$element.parent().prop('tagName') === 'label', `${component} parent element should be <label>.`)
|
|
|
|
Util.assert(this.outerElement().hasClass('checkbox'), `${component} grandparent element should have class .checkbox.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// protected
|
|
|
|
|
|
|
|
addFocusListener() {
|
2015-12-02 03:28:36 +03:00
|
|
|
// checkboxes didn't appear to bubble to the document, so we'll bind these directly
|
2015-12-04 04:09:01 +03:00
|
|
|
this.$formGroup.find('.checkbox label').hover(() => {
|
|
|
|
Util.addFormGroupFocus(this.$formGroup)
|
2015-12-02 03:28:36 +03:00
|
|
|
}, () => {
|
2015-12-04 04:09:01 +03:00
|
|
|
Util.removeFormGroupFocus(this.$formGroup)
|
2015-12-02 03:28:36 +03:00
|
|
|
})
|
2015-12-05 23:00:40 +03:00
|
|
|
}
|
2015-12-01 21:43:01 +03:00
|
|
|
|
2015-12-05 23:00:40 +03:00
|
|
|
addChangeListener() {
|
2015-12-04 04:09: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
|
|
|
|
2015-12-05 23:00:40 +03:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// 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
|