From 123e298fb15920559de5bdf0e961d1a31d917cd3 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Thu, 19 Nov 2020 13:52:19 +0530 Subject: [PATCH 1/2] Update data to map --- js/src/alert.js | 8 ++-- js/src/button.js | 10 ++-- js/src/carousel.js | 12 ++--- js/src/collapse.js | 14 +++--- js/src/dom/data.js | 52 ++++++--------------- js/src/dropdown.js | 10 ++-- js/src/modal.js | 10 ++-- js/src/popover.js | 6 +-- js/src/scrollspy.js | 8 ++-- js/src/tab.js | 10 ++-- js/src/toast.js | 8 ++-- js/src/tooltip.js | 25 +++++----- js/tests/unit/dom/data.spec.js | 85 ++-------------------------------- 13 files changed, 76 insertions(+), 182 deletions(-) diff --git a/js/src/alert.js b/js/src/alert.js index 724cda0173b2..1e025717b60e 100644 --- a/js/src/alert.js +++ b/js/src/alert.js @@ -49,7 +49,7 @@ class Alert { this._element = element if (this._element) { - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } } @@ -73,7 +73,7 @@ class Alert { } dispose() { - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) this._element = null } @@ -113,7 +113,7 @@ class Alert { static jQueryInterface(config) { return this.each(function () { - let data = Data.getData(this, DATA_KEY) + let data = Data.getData(this) if (!data) { data = new Alert(this) @@ -136,7 +136,7 @@ class Alert { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } diff --git a/js/src/button.js b/js/src/button.js index 42376943debd..8cc307ec1b8b 100644 --- a/js/src/button.js +++ b/js/src/button.js @@ -36,7 +36,7 @@ const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}` class Button { constructor(element) { this._element = element - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -53,7 +53,7 @@ class Button { } dispose() { - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) this._element = null } @@ -61,7 +61,7 @@ class Button { static jQueryInterface(config) { return this.each(function () { - let data = Data.getData(this, DATA_KEY) + let data = Data.getData(this) if (!data) { data = new Button(this) @@ -74,7 +74,7 @@ class Button { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } @@ -89,7 +89,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => { const button = event.target.closest(SELECTOR_DATA_TOGGLE) - let data = Data.getData(button, DATA_KEY) + let data = Data.getData(button) if (!data) { data = new Button(button) } diff --git a/js/src/carousel.js b/js/src/carousel.js index c663efbeacdc..a35e6e509f1b 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -122,7 +122,7 @@ class Carousel { this._pointerEvent = Boolean(window.PointerEvent) this._addEventListeners() - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -219,7 +219,7 @@ class Carousel { dispose() { EventHandler.off(this._element, EVENT_KEY) - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) this._items = null this._config = null @@ -526,7 +526,7 @@ class Carousel { // Static static carouselInterface(element, config) { - let data = Data.getData(element, DATA_KEY) + let data = Data.getData(element) let _config = { ...Default, ...Manipulator.getDataAttributes(element) @@ -585,14 +585,14 @@ class Carousel { Carousel.carouselInterface(target, config) if (slideIndex) { - Data.getData(target, DATA_KEY).to(slideIndex) + Data.getData(target).to(slideIndex) } event.preventDefault() } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } @@ -608,7 +608,7 @@ EventHandler.on(window, EVENT_LOAD_DATA_API, () => { const carousels = SelectorEngine.find(SELECTOR_DATA_RIDE) for (let i = 0, len = carousels.length; i < len; i++) { - Carousel.carouselInterface(carousels[i], Data.getData(carousels[i], DATA_KEY)) + Carousel.carouselInterface(carousels[i], Data.getData(carousels[i])) } }) diff --git a/js/src/collapse.js b/js/src/collapse.js index 2ddcb0c047a0..6fbd2f6e801d 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -101,7 +101,7 @@ class Collapse { this.toggle() } - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -151,7 +151,7 @@ class Collapse { const container = SelectorEngine.findOne(this._selector) if (actives) { const tempActiveData = actives.find(elem => container !== elem) - activesData = tempActiveData ? Data.getData(tempActiveData, DATA_KEY) : null + activesData = tempActiveData ? Data.getData(tempActiveData) : null if (activesData && activesData._isTransitioning) { return @@ -170,7 +170,7 @@ class Collapse { } if (!activesData) { - Data.setData(elemActive, DATA_KEY, null) + Data.setData(elemActive, null) } }) } @@ -266,7 +266,7 @@ class Collapse { } dispose() { - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) this._config = null this._parent = null @@ -339,7 +339,7 @@ class Collapse { // Static static collapseInterface(element, config) { - let data = Data.getData(element, DATA_KEY) + let data = Data.getData(element) const _config = { ...Default, ...Manipulator.getDataAttributes(element), @@ -370,7 +370,7 @@ class Collapse { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } @@ -391,7 +391,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function ( const selectorElements = SelectorEngine.find(selector) selectorElements.forEach(element => { - const data = Data.getData(element, DATA_KEY) + const data = Data.getData(element) let config if (data) { // update parent attribute diff --git a/js/src/dom/data.js b/js/src/dom/data.js index 6e7e6c72f53d..7615d503aa02 100644 --- a/js/src/dom/data.js +++ b/js/src/dom/data.js @@ -12,55 +12,29 @@ */ const mapData = (() => { - const storeData = new Map() - let id = 1 + const map = new Map() return { - set(element, key, data) { - if (typeof element.bsKey === 'undefined') { - element.bsKey = { - key, - id - } - id++ - } - - storeData.set(element.bsKey.id, data) + set(element, data) { + map.set(element, data) }, - get(element, key) { - if (!element || typeof element.bsKey === 'undefined') { - return null - } - - const keyProperties = element.bsKey - if (keyProperties.key === key) { - return storeData.get(keyProperties.id) - } - - return null + get(element) { + return map.get(element) || null }, - delete(element, key) { - if (typeof element.bsKey === 'undefined') { - return - } - - const keyProperties = element.bsKey - if (keyProperties.key === key) { - storeData.delete(keyProperties.id) - delete element.bsKey - } + delete(element) { + map.delete(element) } } })() const Data = { - setData(instance, key, data) { - mapData.set(instance, key, data) + setData(instance, data) { + mapData.set(instance, data) }, - getData(instance, key) { - return mapData.get(instance, key) + getData(instance) { + return mapData.get(instance) }, - removeData(instance, key) { - mapData.delete(instance, key) + removeData(instance) { + mapData.delete(instance) } } diff --git a/js/src/dropdown.js b/js/src/dropdown.js index c262997ce940..449d55b9041b 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -105,7 +105,7 @@ class Dropdown { this._inNavbar = this._detectNavbar() this._addEventListeners() - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -229,7 +229,7 @@ class Dropdown { } dispose() { - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) EventHandler.off(this._element, EVENT_KEY) this._element = null this._menu = null @@ -345,7 +345,7 @@ class Dropdown { // Static static dropdownInterface(element, config) { - let data = Data.getData(element, DATA_KEY) + let data = Data.getData(element) const _config = typeof config === 'object' ? config : null if (!data) { @@ -377,7 +377,7 @@ class Dropdown { for (let i = 0, len = toggles.length; i < len; i++) { const parent = Dropdown.getParentFromElement(toggles[i]) - const context = Data.getData(toggles[i], DATA_KEY) + const context = Data.getData(toggles[i]) const relatedTarget = { relatedTarget: toggles[i] } @@ -491,7 +491,7 @@ class Dropdown { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } diff --git a/js/src/modal.js b/js/src/modal.js index 548b1d801fc2..86597316cbb7 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -92,7 +92,7 @@ class Modal { this._ignoreBackdropClick = false this._isTransitioning = false this._scrollbarWidth = 0 - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -208,7 +208,7 @@ class Modal { */ EventHandler.off(document, EVENT_FOCUSIN) - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) this._config = null this._element = null @@ -541,7 +541,7 @@ class Modal { static jQueryInterface(config, relatedTarget) { return this.each(function () { - let data = Data.getData(this, DATA_KEY) + let data = Data.getData(this) const _config = { ...Default, ...Manipulator.getDataAttributes(this), @@ -565,7 +565,7 @@ class Modal { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } @@ -595,7 +595,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function ( }) }) - let data = Data.getData(target, DATA_KEY) + let data = Data.getData(target) if (!data) { const config = { ...Manipulator.getDataAttributes(target), diff --git a/js/src/popover.js b/js/src/popover.js index 46b693e6963b..3699ac9d1702 100644 --- a/js/src/popover.js +++ b/js/src/popover.js @@ -140,7 +140,7 @@ class Popover extends Tooltip { static jQueryInterface(config) { return this.each(function () { - let data = Data.getData(this, DATA_KEY) + let data = Data.getData(this) const _config = typeof config === 'object' ? config : null if (!data && /dispose|hide/.test(config)) { @@ -149,7 +149,7 @@ class Popover extends Tooltip { if (!data) { data = new Popover(this, _config) - Data.setData(this, DATA_KEY, data) + Data.setData(this, data) } if (typeof config === 'string') { @@ -163,7 +163,7 @@ class Popover extends Tooltip { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index de456e0805ce..5d0aa0ff01b0 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -82,7 +82,7 @@ class ScrollSpy { this.refresh() this._process() - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -141,7 +141,7 @@ class ScrollSpy { } dispose() { - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) EventHandler.off(this._scrollElement, EVENT_KEY) this._element = null @@ -285,7 +285,7 @@ class ScrollSpy { static jQueryInterface(config) { return this.each(function () { - let data = Data.getData(this, DATA_KEY) + let data = Data.getData(this) const _config = typeof config === 'object' && config if (!data) { @@ -303,7 +303,7 @@ class ScrollSpy { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } diff --git a/js/src/tab.js b/js/src/tab.js index 56269e2facba..1933cad97c61 100644 --- a/js/src/tab.js +++ b/js/src/tab.js @@ -60,7 +60,7 @@ class Tab { constructor(element) { this._element = element - Data.setData(this._element, DATA_KEY, this) + Data.setData(this._element, this) } // Getters @@ -128,7 +128,7 @@ class Tab { } dispose() { - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) this._element = null } @@ -206,7 +206,7 @@ class Tab { static jQueryInterface(config) { return this.each(function () { - const data = Data.getData(this, DATA_KEY) || new Tab(this) + const data = Data.getData(this) || new Tab(this) if (typeof config === 'string') { if (typeof data[config] === 'undefined') { @@ -219,7 +219,7 @@ class Tab { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } @@ -232,7 +232,7 @@ class Tab { EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) { event.preventDefault() - const data = Data.getData(this, DATA_KEY) || new Tab(this) + const data = Data.getData(this) || new Tab(this) data.show() }) diff --git a/js/src/toast.js b/js/src/toast.js index 1e50b0669347..d6ca0977e400 100644 --- a/js/src/toast.js +++ b/js/src/toast.js @@ -66,7 +66,7 @@ class Toast { this._config = this._getConfig(config) this._timeout = null this._setListeners() - Data.setData(element, DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -159,7 +159,7 @@ class Toast { } EventHandler.off(this._element, EVENT_CLICK_DISMISS) - Data.removeData(this._element, DATA_KEY) + Data.removeData(this._element) this._element = null this._config = null @@ -192,7 +192,7 @@ class Toast { static jQueryInterface(config) { return this.each(function () { - let data = Data.getData(this, DATA_KEY) + let data = Data.getData(this) const _config = typeof config === 'object' && config if (!data) { @@ -210,7 +210,7 @@ class Toast { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 7b115a69bead..f332f38f19c2 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -141,7 +141,7 @@ class Tooltip { this.tip = null this._setListeners() - Data.setData(element, this.constructor.DATA_KEY, this) + Data.setData(element, this) } // Getters @@ -194,15 +194,14 @@ class Tooltip { } if (event) { - const dataKey = this.constructor.DATA_KEY - let context = Data.getData(event.delegateTarget, dataKey) + let context = Data.getData(event.delegateTarget) if (!context) { context = new this.constructor( event.delegateTarget, this._getDelegateConfig() ) - Data.setData(event.delegateTarget, dataKey, context) + Data.setData(event.delegateTarget, context) } context._activeTrigger.click = !context._activeTrigger.click @@ -225,7 +224,7 @@ class Tooltip { dispose() { clearTimeout(this._timeout) - Data.removeData(this.element, this.constructor.DATA_KEY) + Data.removeData(this.element) EventHandler.off(this.element, this.constructor.EVENT_KEY) EventHandler.off(this.element.closest(`.${CLASS_NAME_MODAL}`), 'hide.bs.modal', this._hideModalHandler) @@ -284,7 +283,7 @@ class Tooltip { this._addAttachmentClass(attachment) const container = this._getContainer() - Data.setData(tip, this.constructor.DATA_KEY, this) + Data.setData(tip, this) if (!this.element.ownerDocument.documentElement.contains(this.tip)) { container.appendChild(tip) @@ -588,15 +587,14 @@ class Tooltip { } _enter(event, context) { - const dataKey = this.constructor.DATA_KEY - context = context || Data.getData(event.delegateTarget, dataKey) + context = context || Data.getData(event.delegateTarget) if (!context) { context = new this.constructor( event.delegateTarget, this._getDelegateConfig() ) - Data.setData(event.delegateTarget, dataKey, context) + Data.setData(event.delegateTarget, context) } if (event) { @@ -628,15 +626,14 @@ class Tooltip { } _leave(event, context) { - const dataKey = this.constructor.DATA_KEY - context = context || Data.getData(event.delegateTarget, dataKey) + context = context || Data.getData(event.delegateTarget) if (!context) { context = new this.constructor( event.delegateTarget, this._getDelegateConfig() ) - Data.setData(event.delegateTarget, dataKey, context) + Data.setData(event.delegateTarget, context) } if (event) { @@ -765,7 +762,7 @@ class Tooltip { static jQueryInterface(config) { return this.each(function () { - let data = Data.getData(this, DATA_KEY) + let data = Data.getData(this) const _config = typeof config === 'object' && config if (!data && /dispose|hide/.test(config)) { @@ -787,7 +784,7 @@ class Tooltip { } static getInstance(element) { - return Data.getData(element, DATA_KEY) + return Data.getData(element) } } diff --git a/js/tests/unit/dom/data.spec.js b/js/tests/unit/dom/data.spec.js index c80f32db0f5b..e471ede39716 100644 --- a/js/tests/unit/dom/data.spec.js +++ b/js/tests/unit/dom/data.spec.js @@ -14,36 +14,6 @@ describe('Data', () => { clearFixture() }) - describe('setData', () => { - it('should set data in an element by adding a bsKey attribute', () => { - fixtureEl.innerHTML = '
' - - const div = fixtureEl.querySelector('div') - const data = { - test: 'bsData' - } - - Data.setData(div, 'test', data) - expect(div.bsKey).toBeDefined() - }) - - it('should change data if something is already stored', () => { - fixtureEl.innerHTML = '
' - - const div = fixtureEl.querySelector('div') - const data = { - test: 'bsData' - } - - Data.setData(div, 'test', data) - - data.test = 'bsData2' - Data.setData(div, 'test', data) - - expect(div.bsKey).toBeDefined() - }) - }) - describe('getData', () => { it('should return stored data', () => { fixtureEl.innerHTML = '
' @@ -53,8 +23,8 @@ describe('Data', () => { test: 'bsData' } - Data.setData(div, 'test', data) - expect(Data.getData(div, 'test')).toEqual(data) + Data.setData(div, data) + expect(Data.getData(div)).toEqual(data) }) it('should return null on undefined element', () => { @@ -67,20 +37,7 @@ describe('Data', () => { const div = fixtureEl.querySelector('div') - expect(Data.getData(div, 'test')).toEqual(null) - }) - - it('should return null when an element have nothing stored with the provided key', () => { - fixtureEl.innerHTML = '
' - - const div = fixtureEl.querySelector('div') - const data = { - test: 'bsData' - } - - Data.setData(div, 'test', data) - - expect(Data.getData(div, 'test2')).toEqual(null) + expect(Data.getData(div)).toEqual(null) }) }) @@ -90,42 +47,8 @@ describe('Data', () => { const div = fixtureEl.querySelector('div') - Data.removeData(div, 'test') + Data.removeData(div) expect().nothing() }) - - it('should should do nothing if it\'s not a valid key provided', () => { - fixtureEl.innerHTML = '
' - - const div = fixtureEl.querySelector('div') - const data = { - test: 'bsData' - } - - Data.setData(div, 'test', data) - - expect(div.bsKey).toBeDefined() - - Data.removeData(div, 'test2') - - expect(div.bsKey).toBeDefined() - }) - - it('should remove data if something is stored', () => { - fixtureEl.innerHTML = '
' - - const div = fixtureEl.querySelector('div') - const data = { - test: 'bsData' - } - - Data.setData(div, 'test', data) - - expect(div.bsKey).toBeDefined() - - Data.removeData(div, 'test') - - expect(div.bsKey).toBeUndefined() - }) }) }) From 61c1c5db369c2ac9208002d9a466169ce75e5018 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Fri, 20 Nov 2020 18:40:25 +0530 Subject: [PATCH 2/2] Remvoe IIFE from data.js --- js/src/dom/data.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/js/src/dom/data.js b/js/src/dom/data.js index 7615d503aa02..535b783842b4 100644 --- a/js/src/dom/data.js +++ b/js/src/dom/data.js @@ -11,31 +11,16 @@ * ------------------------------------------------------------------------ */ -const mapData = (() => { - const map = new Map() - return { - set(element, data) { - map.set(element, data) - }, - get(element) { - return map.get(element) || null - }, - delete(element) { - map.delete(element) - } - } -})() +const mapData = new Map() -const Data = { - setData(instance, data) { - mapData.set(instance, data) +export default { + setData(element, data) { + mapData.set(element, data) }, - getData(instance) { - return mapData.get(instance) + getData(element) { + return mapData.get(element) || null }, - removeData(instance) { - mapData.delete(instance) + removeData(element) { + mapData.delete(element) } } - -export default Data