2015-12-06 00:07:37 +03:00
import BaseInput from './baseInput'
import Util from './util'
const BaseToggle = ( ( $ ) => {
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Constants
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
2015-12-06 06:24:05 +03:00
const Default = { }
2015-12-06 00:07:37 +03:00
const Selector = {
LABEL : 'label'
}
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Class Definition
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
class BaseToggle extends BaseInput {
2015-12-06 06:24:05 +03:00
constructor ( element , config , properties ) {
// properties = {inputType: checkbox, outerClass: checkbox-inline}
2015-12-06 00:07:37 +03:00
// '.checkbox|switch|radio > label > input[type=checkbox|radio]'
// '.${this.outerClass} > label > input[type=${this.inputType}]'
2015-12-06 06:24:05 +03:00
super ( element , $ . extend ( { } , Default , config ) , properties )
this . $element . after ( this . config . template )
2015-12-06 00:07:37 +03:00
}
// ------------------------------------------------------------------------
// protected
// Demarcation element (e.g. first child of a form-group)
outerElement ( ) {
2015-12-06 06:24:05 +03:00
// .checkbox|switch|radio > label > input[type=checkbox|radio]
// label.checkbox-inline > input[type=checkbox|radio]
// .${this.outerClass} > label > input[type=${this.inputType}]
return this . $element . parent ( ) . closest ( ` . ${ this . outerClass } ` )
2015-12-06 00:07:37 +03:00
}
rejectWithoutRequiredStructure ( ) {
// '.checkbox|switch|radio > label > input[type=checkbox|radio]'
// '.${this.outerClass} > label > input[type=${this.inputType}]'
2015-12-06 06:24:05 +03:00
Util . assert ( this . $element , ! this . $element . parent ( ) . prop ( 'tagName' ) === 'label' , ` ${ this . constructor . name } 's ${ Util . describe ( this . $element ) } parent element should be <label>. ` )
Util . assert ( this . $element , ! this . outerElement ( ) . hasClass ( this . outerClass ) , ` ${ this . constructor . name } 's ${ Util . describe ( this . $element ) } outer 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