2015-12-02 19:38:00 +03:00
|
|
|
/**
|
|
|
|
* $.bootstrapMaterialDesign(config) is a macro class to configure the components generally
|
|
|
|
* used in Material Design for Bootstrap. You may pass overrides to the configurations
|
|
|
|
* which will be passed into each component, or you may omit use of this class and
|
|
|
|
* configure each component separately.
|
|
|
|
*/
|
|
|
|
const BootstrapMaterialDesign = (($) => {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
const NAME = 'bootstrapMaterialDesign'
|
|
|
|
const DATA_KEY = `mdb.${NAME}`
|
2015-12-06 17:11:47 +03:00
|
|
|
const JQUERY_NAME = NAME // retain this full name since it is long enough not to conflict
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[JQUERY_NAME]
|
2015-12-02 19:38:00 +03:00
|
|
|
|
|
|
|
/**
|
2015-12-08 03:07:21 +03:00
|
|
|
* Global configuration:
|
|
|
|
* The global configuration hash will be mixed in to each components' config.
|
|
|
|
* e.g. calling $.bootstrapMaterialDesign({global: { validate: true } }) would pass `validate:true` to every component
|
2015-12-02 19:38:00 +03:00
|
|
|
*
|
2015-12-08 03:07:21 +03:00
|
|
|
*
|
|
|
|
* Component configuration:
|
2015-12-02 19:38:00 +03:00
|
|
|
* - selector: may be a string or an array. Any array will be joined with a comma to generate the selector
|
2015-12-02 21:12:45 +03:00
|
|
|
* - disable any component by defining it as false with an override. e.g. $.bootstrapMaterialDesign({ autofill: false })
|
|
|
|
*
|
|
|
|
* @see each individual component for more configuration settings.
|
2015-12-02 19:38:00 +03:00
|
|
|
*/
|
|
|
|
const Default = {
|
2015-12-08 03:07:21 +03:00
|
|
|
global: {
|
2015-12-11 20:07:03 +03:00
|
|
|
validate: false,
|
2015-12-17 18:47:55 +03:00
|
|
|
label: {
|
2015-12-12 00:13:33 +03:00
|
|
|
className: 'mdb-label-static' // default style of label to be used if not specified in the html markup
|
2015-12-11 20:07:03 +03:00
|
|
|
}
|
2015-12-08 03:07:21 +03:00
|
|
|
},
|
2015-12-02 19:38:00 +03:00
|
|
|
ripples: {
|
2015-12-17 02:19:02 +03:00
|
|
|
//selector: ['.btn:not(.btn-link):not(.ripple-none)'] // testing only
|
2015-12-02 19:38:00 +03:00
|
|
|
selector: [
|
2015-12-02 21:12:45 +03:00
|
|
|
'.btn:not(.btn-link):not(.ripple-none)',
|
|
|
|
'.card-image:not(.ripple-none)',
|
|
|
|
'.navbar a:not(.ripple-none)',
|
|
|
|
'.dropdown-menu a:not(.ripple-none)',
|
|
|
|
'.nav-tabs a:not(.ripple-none)',
|
|
|
|
'.pagination li:not(.active):not(.disabled) a:not(.ripple-none)',
|
2015-12-02 19:38:00 +03:00
|
|
|
'.ripple' // generic marker class to add ripple to elements
|
2015-12-02 21:12:45 +03:00
|
|
|
]
|
2015-12-02 19:38:00 +03:00
|
|
|
},
|
2015-12-06 02:45:38 +03:00
|
|
|
text: {
|
2015-12-07 20:40:42 +03:00
|
|
|
// omit inputs we have specialized components to handle
|
|
|
|
selector: [`input[type!='checkbox'][type!='radio'][type!='file']`]
|
2015-12-06 02:45:38 +03:00
|
|
|
},
|
2015-12-07 20:40:42 +03:00
|
|
|
file: {
|
|
|
|
selector: 'input[type=file]'
|
2015-12-02 19:38:00 +03:00
|
|
|
},
|
|
|
|
checkbox: {
|
|
|
|
selector: '.checkbox > label > input[type=checkbox]'
|
|
|
|
},
|
2015-12-06 06:24:05 +03:00
|
|
|
checkboxInline: {
|
|
|
|
selector: 'label.checkbox-inline > input[type=checkbox]'
|
|
|
|
},
|
2015-12-02 19:38:00 +03:00
|
|
|
radio: {
|
|
|
|
selector: '.radio > label > input[type=radio]'
|
|
|
|
},
|
2015-12-06 06:24:05 +03:00
|
|
|
radioInline: {
|
|
|
|
selector: 'label.radio-inline > input[type=radio]'
|
|
|
|
},
|
2015-12-07 20:40:42 +03:00
|
|
|
select: {
|
|
|
|
selector: ['select']
|
2015-12-02 19:38:00 +03:00
|
|
|
},
|
2015-12-16 03:53:26 +03:00
|
|
|
switch: {
|
|
|
|
selector: '.switch > label > input[type=checkbox]'
|
|
|
|
},
|
|
|
|
textarea: {
|
|
|
|
selector: ['textarea']
|
|
|
|
},
|
2015-12-02 21:12:45 +03:00
|
|
|
autofill: {
|
|
|
|
selector: 'body'
|
|
|
|
},
|
|
|
|
arrive: true,
|
|
|
|
// create an ordered component list for instantiation
|
|
|
|
instantiation: [
|
|
|
|
'ripples',
|
|
|
|
'checkbox',
|
2015-12-06 06:24:05 +03:00
|
|
|
'checkboxInline',
|
2015-12-06 02:45:38 +03:00
|
|
|
'file',
|
2015-12-02 21:12:45 +03:00
|
|
|
'radio',
|
2015-12-06 06:24:05 +03:00
|
|
|
'radioInline',
|
2015-12-06 02:45:38 +03:00
|
|
|
'switch',
|
|
|
|
'text',
|
|
|
|
'textarea',
|
|
|
|
'select',
|
2015-12-02 21:12:45 +03:00
|
|
|
'autofill'
|
|
|
|
]
|
2015-12-02 19:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class BootstrapMaterialDesign {
|
|
|
|
|
2015-12-07 20:40:42 +03:00
|
|
|
constructor($element, config) {
|
|
|
|
this.$element = $element
|
2015-12-11 20:07:03 +03:00
|
|
|
this.config = $.extend(true, {}, Default, config)
|
2015-12-02 21:12:45 +03:00
|
|
|
let $document = $(document)
|
|
|
|
|
2015-12-05 00:00:57 +03:00
|
|
|
for (let component of this.config.instantiation) {
|
2015-12-02 21:12:45 +03:00
|
|
|
|
|
|
|
// the component's config fragment is passed in directly, allowing users to override
|
|
|
|
let componentConfig = this.config[component]
|
2015-12-02 19:38:00 +03:00
|
|
|
|
2015-12-02 21:12:45 +03:00
|
|
|
// check to make sure component config is enabled (not `false`)
|
|
|
|
if (componentConfig) {
|
|
|
|
|
|
|
|
// assemble the selector as it may be an array
|
|
|
|
let selector = this._resolveSelector(componentConfig)
|
|
|
|
|
2015-12-08 03:07:21 +03:00
|
|
|
// mix in global options
|
2015-12-11 20:07:03 +03:00
|
|
|
componentConfig = $.extend(true, {}, this.config.global, componentConfig)
|
2015-12-08 03:07:21 +03:00
|
|
|
|
2015-12-06 17:11:47 +03:00
|
|
|
// create the jquery fn name e.g. 'mdbText' for 'text'
|
|
|
|
let jqueryFn = `mdb${component.charAt(0).toUpperCase() + component.slice(1)}`
|
|
|
|
|
2015-12-02 21:12:45 +03:00
|
|
|
// instantiate component on selector elements with config
|
2015-12-17 02:19:02 +03:00
|
|
|
// console.debug(`instantiating: $('${selector}')[${jqueryFn}](${componentConfig})`) // eslint-disable-line no-console
|
2015-12-06 17:11:47 +03:00
|
|
|
$(selector)[jqueryFn](componentConfig)
|
2015-12-02 21:12:45 +03:00
|
|
|
|
|
|
|
// add to arrive if present and enabled
|
|
|
|
if (document.arrive && this.config.arrive) {
|
|
|
|
$document.arrive(selector, (element) => { // eslint-disable-line no-loop-func
|
2015-12-06 17:11:47 +03:00
|
|
|
$(element)[jqueryFn](componentConfig)
|
2015-12-02 21:12:45 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 19:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
2015-12-04 04:09:01 +03:00
|
|
|
$.removeData(this.$element, DATA_KEY)
|
|
|
|
this.$element = null
|
2015-12-02 19:38:00 +03:00
|
|
|
this.config = null
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// private
|
|
|
|
|
2015-12-02 21:12:45 +03:00
|
|
|
_resolveSelector(componentConfig) {
|
|
|
|
let selector = componentConfig['selector']
|
|
|
|
if (Array.isArray(selector)) {
|
|
|
|
selector = selector.join(', ')
|
|
|
|
}
|
|
|
|
|
|
|
|
return selector
|
2015-12-02 19:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// static
|
|
|
|
static _jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
|
|
|
let $element = $(this)
|
|
|
|
let data = $element.data(DATA_KEY)
|
|
|
|
|
|
|
|
if (!data) {
|
2015-12-07 20:40:42 +03:00
|
|
|
data = new BootstrapMaterialDesign($element, config)
|
2015-12-02 19:38:00 +03:00
|
|
|
$element.data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-12-06 17:11:47 +03:00
|
|
|
$.fn[JQUERY_NAME] = BootstrapMaterialDesign._jQueryInterface
|
|
|
|
$.fn[JQUERY_NAME].Constructor = BootstrapMaterialDesign
|
|
|
|
$.fn[JQUERY_NAME].noConflict = () => {
|
|
|
|
$.fn[JQUERY_NAME] = JQUERY_NO_CONFLICT
|
2015-12-02 19:38:00 +03:00
|
|
|
return BootstrapMaterialDesign._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
return BootstrapMaterialDesign
|
|
|
|
|
|
|
|
})(jQuery)
|
|
|
|
|
|
|
|
export default BootstrapMaterialDesign
|