|
| 1 | +import { getjQuery, onDOMContentLoaded } from '../mdb/util/index'; |
| 2 | +import Data from '../mdb/dom/data'; |
| 3 | +import EventHandler from '../mdb/dom/event-handler'; |
| 4 | +import Manipulator from '../mdb/dom/manipulator'; |
| 5 | +import SelectorEngine from '../mdb/dom/selector-engine'; |
| 6 | + |
| 7 | +import BSButton from '../bootstrap/mdb-prefix/button'; |
| 8 | + |
| 9 | +const NAME = 'button'; |
| 10 | +const DATA_KEY = `mdb.${NAME}`; |
| 11 | +const EVENT_KEY = `.${DATA_KEY}`; |
| 12 | + |
| 13 | +const EVENT_CLICK = `click${EVENT_KEY}`; |
| 14 | +const EVENT_TRANSITIONEND = 'transitionend'; |
| 15 | +const EVENT_MOUSEENTER = 'mouseenter'; |
| 16 | +const EVENT_MOUSELEAVE = 'mouseleave'; |
| 17 | +const EVENT_HIDE = `hide${EVENT_KEY}`; |
| 18 | +const EVENT_HIDDEN = `hidden${EVENT_KEY}`; |
| 19 | +const EVENT_SHOW = `show${EVENT_KEY}`; |
| 20 | +const EVENT_SHOWN = `shown${EVENT_KEY}`; |
| 21 | + |
| 22 | +const CLASS_NAME_ACTIVE = 'active'; |
| 23 | +const CLASS_NAME_SHOWN = 'shown'; |
| 24 | +const CLASS_NAME_FIXED_ACTION_BTN = 'fixed-action-btn'; |
| 25 | + |
| 26 | +const SELECTOR_BUTTON = '[data-mdb-toggle="button"]'; |
| 27 | +const SELECTOR_FIXED_CONTAINER = '.fixed-action-btn'; |
| 28 | +const SELECTOR_ACTION_BUTTON = '.fixed-action-btn:not(.smooth-scroll) > .btn-floating'; |
| 29 | +const SELECTOR_LIST_ELEMENT = 'ul .btn'; |
| 30 | +const SELECTOR_LIST = 'ul'; |
| 31 | + |
| 32 | +class Button extends BSButton { |
| 33 | + constructor(element) { |
| 34 | + super(element); |
| 35 | + this._fn = {}; |
| 36 | + |
| 37 | + if (this._element) { |
| 38 | + Data.setData(this._element, DATA_KEY, this); |
| 39 | + this._init(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Static |
| 44 | + static get NAME() { |
| 45 | + return NAME; |
| 46 | + } |
| 47 | + |
| 48 | + static jQueryInterface(config, options) { |
| 49 | + return this.each(function () { |
| 50 | + let data = Data.getData(this, DATA_KEY); |
| 51 | + const _config = typeof config === 'object' && config; |
| 52 | + if (!data && /dispose/.test(config)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (!data) { |
| 57 | + data = new Button(this, _config); |
| 58 | + } |
| 59 | + if (typeof config === 'string') { |
| 60 | + if (typeof data[config] === 'undefined') { |
| 61 | + throw new TypeError(`No method named "${config}"`); |
| 62 | + } |
| 63 | + data[config](options); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + // Getters |
| 69 | + get _actionButton() { |
| 70 | + return SelectorEngine.findOne(SELECTOR_ACTION_BUTTON, this._element); |
| 71 | + } |
| 72 | + |
| 73 | + get _buttonListElements() { |
| 74 | + return SelectorEngine.find(SELECTOR_LIST_ELEMENT, this._element); |
| 75 | + } |
| 76 | + |
| 77 | + get _buttonList() { |
| 78 | + return SelectorEngine.findOne(SELECTOR_LIST, this._element); |
| 79 | + } |
| 80 | + |
| 81 | + get _isTouchDevice() { |
| 82 | + return 'ontouchstart' in document.documentElement; |
| 83 | + } |
| 84 | + |
| 85 | + // Public |
| 86 | + show() { |
| 87 | + if (Manipulator.hasClass(this._element, CLASS_NAME_FIXED_ACTION_BTN)) { |
| 88 | + EventHandler.off(this._buttonList, EVENT_TRANSITIONEND); |
| 89 | + EventHandler.trigger(this._element, EVENT_SHOW); |
| 90 | + // EventHandler.on(this._buttonList, EVENT_TRANSITIONEND, this._bindListOpenTransitionEnd); |
| 91 | + this._bindListOpenTransitionEnd(); |
| 92 | + Manipulator.addStyle(this._element, { height: `${this._fullContainerHeight}px` }); |
| 93 | + this._toggleVisibility(true); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + hide() { |
| 98 | + if (Manipulator.hasClass(this._element, CLASS_NAME_FIXED_ACTION_BTN)) { |
| 99 | + EventHandler.off(this._buttonList, EVENT_TRANSITIONEND); |
| 100 | + EventHandler.trigger(this._element, EVENT_HIDE); |
| 101 | + // EventHandler.on(this._buttonList, EVENT_TRANSITIONEND, this._bindListHideTransitionEnd); |
| 102 | + this._bindListHideTransitionEnd(); |
| 103 | + this._toggleVisibility(false); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + dispose() { |
| 108 | + if (Manipulator.hasClass(this._element, CLASS_NAME_FIXED_ACTION_BTN)) { |
| 109 | + EventHandler.off(this._actionButton, EVENT_CLICK); |
| 110 | + this._actionButton.removeEventListener(EVENT_MOUSEENTER, this._fn.mouseenter); |
| 111 | + this._element.removeEventListener(EVENT_MOUSELEAVE, this._fn.mouseleave); |
| 112 | + } |
| 113 | + |
| 114 | + super.dispose(); |
| 115 | + } |
| 116 | + |
| 117 | + // Private |
| 118 | + _init() { |
| 119 | + if (Manipulator.hasClass(this._element, CLASS_NAME_FIXED_ACTION_BTN)) { |
| 120 | + this._saveInitialHeights(); |
| 121 | + this._setInitialStyles(); |
| 122 | + this._bindInitialEvents(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + _bindMouseEnter() { |
| 127 | + this._actionButton.addEventListener( |
| 128 | + EVENT_MOUSEENTER, |
| 129 | + // prettier-ignore |
| 130 | + this._fn.mouseenter = () => { |
| 131 | + if (!this._isTouchDevice) { |
| 132 | + this.show(); |
| 133 | + } |
| 134 | + } |
| 135 | + // prettier-ignore |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + _bindMouseLeave() { |
| 140 | + this._element.addEventListener( |
| 141 | + EVENT_MOUSELEAVE, |
| 142 | + // prettier-ignore |
| 143 | + this._fn.mouseleave = () => { |
| 144 | + this.hide(); |
| 145 | + } |
| 146 | + // prettier-ignore |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + _bindClick() { |
| 151 | + EventHandler.on(this._actionButton, EVENT_CLICK, () => { |
| 152 | + if (Manipulator.hasClass(this._element, CLASS_NAME_ACTIVE)) { |
| 153 | + this.hide(); |
| 154 | + } else { |
| 155 | + this.show(); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + _bindListHideTransitionEnd() { |
| 161 | + EventHandler.on(this._buttonList, EVENT_TRANSITIONEND, (event) => { |
| 162 | + if (event.propertyName === 'transform') { |
| 163 | + EventHandler.off(this._buttonList, EVENT_TRANSITIONEND); |
| 164 | + this._element.style.height = `${this._initialContainerHeight}px`; |
| 165 | + EventHandler.trigger(this._element, EVENT_HIDDEN); |
| 166 | + } |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + _bindListOpenTransitionEnd() { |
| 171 | + EventHandler.on(this._buttonList, EVENT_TRANSITIONEND, (event) => { |
| 172 | + if (event.propertyName === 'transform') { |
| 173 | + EventHandler.off(this._buttonList, EVENT_TRANSITIONEND); |
| 174 | + EventHandler.trigger(this._element, EVENT_SHOWN); |
| 175 | + } |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + _toggleVisibility(isVisible) { |
| 180 | + const action = isVisible ? 'addClass' : 'removeClass'; |
| 181 | + const listTranslate = isVisible ? 'translate(0)' : `translateY(${this._fullContainerHeight}px)`; |
| 182 | + Manipulator.addStyle(this._buttonList, { transform: listTranslate }); |
| 183 | + |
| 184 | + if (this._buttonListElements) { |
| 185 | + this._buttonListElements.forEach((el) => Manipulator[action](el, CLASS_NAME_SHOWN)); |
| 186 | + } |
| 187 | + Manipulator[action](this._element, CLASS_NAME_ACTIVE); |
| 188 | + } |
| 189 | + |
| 190 | + _getHeight(element) { |
| 191 | + const computed = window.getComputedStyle(element); |
| 192 | + const height = parseFloat(computed.getPropertyValue('height')); |
| 193 | + return height; |
| 194 | + } |
| 195 | + |
| 196 | + _saveInitialHeights() { |
| 197 | + this._initialContainerHeight = this._getHeight(this._element); |
| 198 | + this._initialListHeight = this._getHeight(this._buttonList); |
| 199 | + this._fullContainerHeight = this._initialContainerHeight + this._initialListHeight; |
| 200 | + } |
| 201 | + |
| 202 | + _bindInitialEvents() { |
| 203 | + this._bindClick(); |
| 204 | + this._bindMouseEnter(); |
| 205 | + this._bindMouseLeave(); |
| 206 | + } |
| 207 | + |
| 208 | + _setInitialStyles() { |
| 209 | + this._buttonList.style.marginBottom = `${this._initialContainerHeight}px`; |
| 210 | + this._buttonList.style.transform = `translateY(${this._fullContainerHeight}px)`; |
| 211 | + |
| 212 | + this._element.style.height = `${this._initialContainerHeight}px`; |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +/** |
| 217 | + * ------------------------------------------------------------------------ |
| 218 | + * Data Api implementation - auto initialization |
| 219 | + * ------------------------------------------------------------------------ |
| 220 | + */ |
| 221 | + |
| 222 | +SelectorEngine.find(SELECTOR_FIXED_CONTAINER).forEach((element) => { |
| 223 | + let instance = Button.getInstance(element); |
| 224 | + if (!instance) { |
| 225 | + instance = new Button(element); |
| 226 | + } |
| 227 | + return instance; |
| 228 | +}); |
| 229 | + |
| 230 | +SelectorEngine.find(SELECTOR_BUTTON).forEach((element) => { |
| 231 | + let instance = Button.getInstance(element); |
| 232 | + if (!instance) { |
| 233 | + instance = new Button(element); |
| 234 | + } |
| 235 | + return instance; |
| 236 | +}); |
| 237 | + |
| 238 | +/** |
| 239 | + * ------------------------------------------------------------------------ |
| 240 | + * jQuery |
| 241 | + * ------------------------------------------------------------------------ |
| 242 | + */ |
| 243 | + |
| 244 | +onDOMContentLoaded(() => { |
| 245 | + const $ = getjQuery(); |
| 246 | + |
| 247 | + if ($) { |
| 248 | + const JQUERY_NO_CONFLICT = $.fn[NAME]; |
| 249 | + $.fn[NAME] = Button.jQueryInterface; |
| 250 | + $.fn[NAME].Constructor = Button; |
| 251 | + $.fn[NAME].noConflict = () => { |
| 252 | + $.fn[NAME] = JQUERY_NO_CONFLICT; |
| 253 | + return Button.jQueryInterface; |
| 254 | + }; |
| 255 | + } |
| 256 | +}); |
| 257 | + |
| 258 | +export default Button; |
0 commit comments