2015-12-06 00:07:37 +03:00
import BaseInput from './baseInput'
import Util from './util'
const BaseToggle = ( ( $ ) => {
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Constants
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
const Default = {
2015-12-06 03:55:29 +03:00
formGroup : {
required : false
}
2015-12-06 00:07:37 +03:00
}
const Selector = {
LABEL : 'label'
}
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Class Definition
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
class BaseToggle extends BaseInput {
constructor ( element , config , inputType , outerClass ) {
2015-12-06 02:45:38 +03:00
super ( element , $ . extend ( { } , Default , config ) )
2015-12-06 00:07:37 +03:00
this . $element . after ( this . config . template )
// '.checkbox|switch|radio > label > input[type=checkbox|radio]'
// '.${this.outerClass} > label > input[type=${this.inputType}]'
this . inputType = inputType
this . outerClass = outerClass
}
// ------------------------------------------------------------------------
// protected
// Demarcation element (e.g. first child of a form-group)
outerElement ( ) {
// '.checkbox|switch|radio > label > input[type=checkbox|radio]'
// '.${this.outerClass} > label > input[type=${this.inputType}]'
return this . $element . parent ( ) . parent ( )
}
rejectWithoutRequiredStructure ( ) {
// '.checkbox|switch|radio > label > input[type=checkbox|radio]'
// '.${this.outerClass} > label > input[type=${this.inputType}]'
2015-12-06 00:19:06 +03:00
Util . assert ( this . $element . parent ( ) . prop ( 'tagName' ) === 'label' , ` ${ this . constructor . name } 's ${ this . $element } parent element should be <label>. ` )
Util . assert ( this . outerElement ( ) . hasClass ( this . outerClass ) , ` ${ this . constructor . name } 's ${ this . $element } grandparent element should have class . ${ this . outerClass } . ` )
2015-12-06 00:07:37 +03:00
}
addFocusListener ( ) {
// checkboxes didn't appear to bubble to the document, so we'll bind these directly
2015-12-06 03:55:29 +03:00
this . $mdbFormGroup . find ( Selector . LABEL ) . hover ( ( ) => {
2015-12-06 00:07:37 +03:00
this . addFormGroupFocus ( )
} , ( ) => {
this . removeFormGroupFocus ( )
} )
}
addChangeListener ( ) {
this . $element . change ( ( ) => {
this . $element . blur ( )
} )
}
// ------------------------------------------------------------------------
// private
}
return BaseToggle
} ) ( jQuery )
export default BaseToggle