mdb-ui-kit/js/baseSelection.js

112 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-08-11 17:59:31 +03:00
import BaseInput from "./baseInput";
import Util from "./util";
2017-08-11 17:59:31 +03:00
const BaseSelection = ($ => {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const Default = {
label: {
required: false
// Prioritized find order for resolving the label to be used as an bmd-label if not specified in the markup
// - a function(thisComponent); or
// - a string selector used like $bmdFormGroup.find(selector)
//
// Note this only runs if $bmdFormGroup.find(Selector.BMD_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
//]
}
2017-08-11 17:59:31 +03:00
};
const Selector = {
2017-08-11 17:59:31 +03:00
LABEL: "label"
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class BaseSelection extends BaseInput {
constructor($element, config, properties) {
// properties = {inputType: checkbox, outerClass: checkbox-inline}
// '.checkbox|switch|radio > label > input[type=checkbox|radio]'
// '.${this.outerClass} > label > input[type=${this.inputType}]'
2017-08-11 17:59:31 +03:00
super($element, $.extend(true, {}, Default, config), properties);
this.decorateMarkup();
}
// ------------------------------------------------------------------------
// protected
decorateMarkup() {
const $decorator = $(this.config.template);
2017-08-11 17:59:31 +03:00
this.$element.after($decorator);
// initialize ripples after decorator has been inserted into DOM
if (this.config.ripples !== false) {
2017-08-11 17:59:31 +03:00
$decorator.bmdRipples();
}
}
// Demarcation element (e.g. first child of a form-group)
outerElement() {
// .checkbox|switch|radio > label > input[type=checkbox|radio]
// label.checkbox-inline > input[type=checkbox|radio]
// .${this.outerClass} > label > input[type=${this.inputType}]
2017-08-11 17:59:31 +03:00
return this.$element.parent().closest(`.${this.outerClass}`);
}
rejectWithoutRequiredStructure() {
// '.checkbox|switch|radio > label > input[type=checkbox|radio]'
// '.${this.outerClass} > label > input[type=${this.inputType}]'
2017-08-11 17:59:31 +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}.`
);
}
addFocusListener() {
// checkboxes didn't appear to bubble to the document, so we'll bind these directly
2017-08-11 17:59:31 +03:00
this.$element.closest(Selector.LABEL).hover(
() => {
this.addFormGroupFocus();
},
() => {
this.removeFormGroupFocus();
}
);
}
addChangeListener() {
this.$element.change(() => {
2017-08-11 17:59:31 +03:00
this.$element.blur();
});
}
// ------------------------------------------------------------------------
// private
}
2017-08-11 17:59:31 +03:00
return BaseSelection;
})(jQuery);
2017-08-11 17:59:31 +03:00
export default BaseSelection;