2015-12-02 19:38:00 +03:00
|
|
|
//import Util from './util'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $.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.
|
|
|
|
*
|
|
|
|
* NOTE: If omitting use of this class, please note that the Input component must be
|
|
|
|
* initialized prior to other decorating components such as radio, checkbox,
|
|
|
|
* togglebutton, fileInput.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const BootstrapMaterialDesign = (($) => {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
const NAME = 'bootstrapMaterialDesign'
|
|
|
|
const DATA_KEY = `mdb.${NAME}`
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2015-12-02 21:12:45 +03:00
|
|
|
* Default macro configuration for each component (primarily selectors).
|
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 = {
|
|
|
|
ripples: {
|
|
|
|
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
|
|
|
},
|
|
|
|
input: {
|
|
|
|
selector: [
|
|
|
|
'input.form-control',
|
|
|
|
'textarea.form-control',
|
|
|
|
'select.form-control'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
checkbox: {
|
|
|
|
selector: '.checkbox > label > input[type=checkbox]'
|
|
|
|
},
|
|
|
|
togglebutton: {
|
|
|
|
selector: '.togglebutton > label > input[type=checkbox]'
|
|
|
|
},
|
|
|
|
radio: {
|
|
|
|
selector: '.radio > label > input[type=radio]'
|
|
|
|
},
|
|
|
|
fileInput: {
|
|
|
|
selector: 'input[type=file]'
|
|
|
|
},
|
2015-12-02 21:12:45 +03:00
|
|
|
autofill: {
|
|
|
|
selector: 'body'
|
|
|
|
},
|
|
|
|
arrive: true,
|
|
|
|
// create an ordered component list for instantiation
|
|
|
|
instantiation: [
|
|
|
|
'ripples',
|
|
|
|
'input',
|
|
|
|
'checkbox',
|
|
|
|
'togglebutton',
|
|
|
|
'radio',
|
|
|
|
'fileInput',
|
|
|
|
'autofill'
|
|
|
|
]
|
2015-12-02 19:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class BootstrapMaterialDesign {
|
|
|
|
|
|
|
|
constructor(element, config) {
|
|
|
|
this.element = element
|
|
|
|
this.config = $.extend({}, Default, config)
|
2015-12-02 21:12:45 +03:00
|
|
|
let $document = $(document)
|
|
|
|
|
|
|
|
for (let component in this.config.instantiation) {
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// instantiate component on selector elements with config
|
|
|
|
$(selector)[component](componentConfig)
|
|
|
|
|
|
|
|
// add to arrive if present and enabled
|
|
|
|
if (document.arrive && this.config.arrive) {
|
|
|
|
$document.arrive(selector, (element) => { // eslint-disable-line no-loop-func
|
|
|
|
$(element)[component](componentConfig)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 19:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
$.removeData(this.element, DATA_KEY)
|
|
|
|
this.element = null
|
|
|
|
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) {
|
|
|
|
data = new BootstrapMaterialDesign(this, config)
|
|
|
|
$element.data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
$.fn[NAME] = BootstrapMaterialDesign._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = BootstrapMaterialDesign
|
|
|
|
$.fn[NAME].noConflict = () => {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return BootstrapMaterialDesign._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
return BootstrapMaterialDesign
|
|
|
|
|
|
|
|
})(jQuery)
|
|
|
|
|
|
|
|
export default BootstrapMaterialDesign
|