2015-12-06 00:07:37 +03:00
import BaseInput from './baseInput'
import Util from './util'
2015-12-15 01:42:22 +03:00
const BaseSelection = ( ( $ ) => {
2015-12-06 00:07:37 +03:00
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Constants
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
2015-12-11 20:07:03 +03:00
const Default = {
2015-12-17 18:47:55 +03:00
label : {
2015-12-11 20:07:03 +03:00
required : false
// Prioritized find order for resolving the label to be used as an mdb-label if not specified in the markup
// - a function(thisComponent); or
// - a string selector used like $mdbFormGroup.find(selector)
//
// Note this only runs if $mdbFormGroup.find(Selector.MDB_LABEL_WILDCARD) fails to find a label (as authored in the markup)
//
//selectors: [
// `.form-control-label`, // in the case of horizontal or inline forms, this will be marked
// `> label` // usual case for text inputs
//]
}
}
2015-12-06 00:07:37 +03:00
const Selector = {
LABEL : 'label'
}
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Class Definition
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
2015-12-15 01:42:22 +03:00
class BaseSelection extends BaseInput {
2015-12-06 00:07:37 +03:00
2015-12-07 20:40:42 +03:00
constructor ( $element , config , properties ) {
2015-12-06 06:24:05 +03:00
// 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
2015-12-11 20:07:03 +03:00
super ( $element , $ . extend ( true , { } , Default , config ) , properties )
2015-12-14 23:19:17 +03:00
this . decorateMarkup ( )
2015-12-06 00:07:37 +03:00
}
// ------------------------------------------------------------------------
// protected
2015-12-14 23:19:17 +03:00
decorateMarkup ( ) {
this . $element . after ( this . config . template )
}
2015-12-06 00:07:37 +03:00
// 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-09 22:13:55 +03:00
this . $element . closest ( Selector . LABEL ) . hover ( ( ) => {
2015-12-06 00:07:37 +03:00
this . addFormGroupFocus ( )
} , ( ) => {
this . removeFormGroupFocus ( )
} )
}
addChangeListener ( ) {
this . $element . change ( ( ) => {
this . $element . blur ( )
} )
}
// ------------------------------------------------------------------------
// private
}
2015-12-15 01:42:22 +03:00
return BaseSelection
2015-12-06 00:07:37 +03:00
} ) ( jQuery )
2015-12-15 01:42:22 +03:00
export default BaseSelection