mdb-ui-kit/js/base.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-08-11 17:59:31 +03:00
import Util from "./util";
2017-08-11 17:59:31 +03:00
const Base = ($ => {
const ClassName = {
2017-08-11 17:59:31 +03:00
BMD_FORM_GROUP: "bmd-form-group",
IS_FILLED: "is-filled",
IS_FOCUSED: "is-focused"
};
const Selector = {
BMD_FORM_GROUP: `.${ClassName.BMD_FORM_GROUP}`
2017-08-11 17:59:31 +03:00
};
2017-08-11 17:59:31 +03:00
const Default = {};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Base {
/**
*
* @param element
* @param config
* @param properties - anything that needs to be set as this[key] = value. Works around the need to call `super` before using `this`
*/
constructor($element, config, properties = {}) {
2017-08-11 17:59:31 +03:00
this.$element = $element;
this.config = $.extend(true, {}, Default, config);
// set properties for use in the constructor initialization
for (let key in properties) {
2017-08-11 17:59:31 +03:00
this[key] = properties[key];
}
}
dispose(dataKey) {
2017-08-11 17:59:31 +03:00
this.$element.data(dataKey, null);
this.$element = null;
this.config = null;
}
// ------------------------------------------------------------------------
// protected
addFormGroupFocus() {
2017-08-11 17:59:31 +03:00
if (!this.$element.prop("disabled")) {
this.$bmdFormGroup.addClass(ClassName.IS_FOCUSED);
}
}
removeFormGroupFocus() {
2017-08-11 17:59:31 +03:00
this.$bmdFormGroup.removeClass(ClassName.IS_FOCUSED);
}
removeIsFilled() {
2017-08-11 17:59:31 +03:00
this.$bmdFormGroup.removeClass(ClassName.IS_FILLED);
}
addIsFilled() {
2017-08-11 17:59:31 +03:00
this.$bmdFormGroup.addClass(ClassName.IS_FILLED);
}
// Find bmd-form-group
findMdbFormGroup(raiseError = true) {
2017-08-11 17:59:31 +03:00
let mfg = this.$element.closest(Selector.BMD_FORM_GROUP);
if (mfg.length === 0 && raiseError) {
2017-08-11 17:59:31 +03:00
$.error(
`Failed to find ${Selector.BMD_FORM_GROUP} for ${Util.describe(
this.$element
)}`
);
}
2017-08-11 17:59:31 +03:00
return mfg;
}
// ------------------------------------------------------------------------
// private
// ------------------------------------------------------------------------
// static
}
2017-08-11 17:59:31 +03:00
return Base;
})(jQuery);
2017-08-11 17:59:31 +03:00
export default Base;