Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quantity Selector #860

Merged
merged 35 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
aac64d5
feat #113 (quantity selector): create new component
MewenLeHo Oct 20, 2021
84f450c
review(docs)
julien-deramond Dec 27, 2021
d700d68
review(tests): add more unit tests
julien-deramond Dec 27, 2021
d2c27f6
feat #113 (quantity selector): fix history
MewenLeHo Jan 21, 2022
378043d
feat #113 (quantity selector): fix listeners change and load and add …
MewenLeHo Jan 26, 2022
297066b
feat #113 (quantity selector): fix typo and job
MewenLeHo Jan 26, 2022
5e297d9
feat #113 (quantity selector): add more unit tests
MewenLeHo Jan 27, 2022
3b7cac2
feat #113 (quantity selector): create new component
MewenLeHo Oct 20, 2021
6779f64
review(docs)
julien-deramond Dec 27, 2021
a1ba43f
review(tests): add more unit tests
julien-deramond Dec 27, 2021
951f222
feat #113 (quantity selector): fix history
MewenLeHo Jan 21, 2022
f9a0681
feat #113 (quantity selector): fix listeners change and load and add …
MewenLeHo Jan 26, 2022
c6ab966
feat #113 (quantity selector): fix typo and job
MewenLeHo Jan 26, 2022
8671426
feat #113 (quantity selector): add more unit tests
MewenLeHo Jan 27, 2022
bc72706
Fix bundlewatch conflict
julien-deramond Jan 27, 2022
4616a18
Use style inherited from Bootstrap
julien-deramond Jan 27, 2022
1dca654
Fix bundlewatch
julien-deramond Jan 27, 2022
bbe452a
Finish formatting JS file + clean up Sass variables modifications
julien-deramond Jan 27, 2022
a89537a
feat #113 (quantity selector): add jQueryInterface
MewenLeHo Jan 28, 2022
00c8dff
feat #113 (quantity selector): fix bundlewatch
MewenLeHo Jan 28, 2022
bfc8a87
feat #113 (quantity selector): add dispatchEvent on StepUp and StepDown
MewenLeHo Feb 1, 2022
8c1c4db
feat #113 (quantity selector): add jQuery part in doc
MewenLeHo Feb 3, 2022
f11981e
feat #113 (quantity selector): refactoring js
MewenLeHo Feb 3, 2022
ac462a1
feat #113 (quantity selector): back on refactoring js
MewenLeHo Feb 4, 2022
5a3fad2
feat #113 (quantity selector): fix bundlewatch
MewenLeHo Feb 4, 2022
cac060b
feat #113 (quantity selector): fix load event in unit tests
MewenLeHo Feb 8, 2022
77b8c57
feat #113 (quantity selector): add jQueryInterface unit tests
MewenLeHo Feb 8, 2022
de4c7ec
feat #113 (quantity selector): fix case
MewenLeHo Feb 8, 2022
9630d96
feat #113 (quantity selector): update javascript usage documentation
MewenLeHo Feb 8, 2022
cf147c4
feat #113 (quantity selector): fix bundlewatch
MewenLeHo Feb 8, 2022
126fa7c
feat #113 (quantity selector): fix documentation for js change event
MewenLeHo Feb 14, 2022
a648d24
feat #113 (quantity selector): fix bundlewatch
MewenLeHo Feb 14, 2022
34ad178
feat #113 (quantity selector): minor fixes after review
MewenLeHo Feb 16, 2022
b1f6cc9
review: change order of event handlers to be coherent with other files
julien-deramond Feb 24, 2022
34598e2
review
julien-deramond Feb 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .bundlewatch.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"path": "./dist/css/boosted-reboot.min.css",
"maxSize": "3.3 kB"
"maxSize": "3.31 kB"
},
{
"path": "./dist/css/boosted-utilities.css",
Expand All @@ -26,35 +26,35 @@
},
{
"path": "./dist/css/boosted.css",
"maxSize": "29.4 kB"
"maxSize": "29.74 kB"
},
{
"path": "./dist/css/boosted.min.css",
"maxSize": "27.1 kB"
"maxSize": "27.46 kB"
},
{
"path": "./dist/js/boosted.bundle.js",
"maxSize": "46.7 kB"
},
{
"path": "./dist/js/boosted.bundle.min.js",
"maxSize": "23.8 kB"
"maxSize": "24.05 kB"
},
{
"path": "./dist/js/boosted.esm.js",
"maxSize": "31.25 kB"
},
{
"path": "./dist/js/boosted.esm.min.js",
"maxSize": "19.3 kB"
"maxSize": "19.49 kB"
},
{
"path": "./dist/js/boosted.js",
"maxSize": "32.0 kB"
"maxSize": "32.04 kB"
},
{
"path": "./dist/js/boosted.min.js",
"maxSize": "17.1 kB"
"maxSize": "17.24 kB"
}
],
"ci": {
Expand Down
1 change: 1 addition & 0 deletions js/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { default as Dropdown } from './src/dropdown'
export { default as Modal } from './src/modal'
export { default as Offcanvas } from './src/offcanvas'
export { default as Popover } from './src/popover'
export { default as QuantitySelector } from './src/quantity-selector' // Boosted mod
export { default as ScrollSpy } from './src/scrollspy'
export { default as Tab } from './src/tab'
export { default as Toast } from './src/toast'
Expand Down
2 changes: 2 additions & 0 deletions js/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Dropdown from './src/dropdown'
import Modal from './src/modal'
import Offcanvas from './src/offcanvas'
import Popover from './src/popover'
import QuantitySelector from './src/quantity-selector' // Boosted mod
import ScrollSpy from './src/scrollspy'
import Tab from './src/tab'
import Toast from './src/toast'
Expand All @@ -28,6 +29,7 @@ export default {
Modal,
Offcanvas,
Popover,
QuantitySelector, // Boosted mod
ScrollSpy,
Tab,
Toast,
Expand Down
153 changes: 153 additions & 0 deletions js/src/quantity-selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
julien-deramond marked this conversation as resolved.
Show resolved Hide resolved
* --------------------------------------------------------------------------
* Boosted (v5.1.3): quantity-selector.js
* Licensed under MIT (https://github.com/Orange-OpenSource/Orange-Boosted-Bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/

import { defineJQueryPlugin } from './util/index'
import EventHandler from './dom/event-handler'
import BaseComponent from './base-component'
import SelectorEngine from './dom/selector-engine'

/**
julien-deramond marked this conversation as resolved.
Show resolved Hide resolved
* Constants
*/

const NAME = 'quantityselector'
const DATA_KEY = 'bs.quantityselector'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'

const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
const EVENT_CHANGE_DATA_API = `change${EVENT_KEY}${DATA_API_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`

const SELECTOR_STEP_UP_BUTTON = '[data-bs-step="up"]'
const SELECTOR_STEP_DOWN_BUTTON = '[data-bs-step="down"]'
const SELECTOR_COUNTER_INPUT = '[data-bs-step="counter"]'
const SELECTOR_INPUT_GROUP = '.input-group'

/**
* Class definition
*/

class QuantitySelector extends BaseComponent {
// Getters
static get NAME() {
return NAME
}

// Public
ValueOnLoad(element) {
const counterInput = element.querySelector(SELECTOR_COUNTER_INPUT)
const btnUp = element.querySelector(SELECTOR_STEP_UP_BUTTON)
const btnDown = element.querySelector(SELECTOR_STEP_DOWN_BUTTON)

const min = counterInput.getAttribute('min')
const max = counterInput.getAttribute('max')
const step = Number(counterInput.getAttribute('step'))
louismaximepiton marked this conversation as resolved.
Show resolved Hide resolved

if (Number(counterInput.value) - step < min) {
btnDown.setAttribute('disabled', '')
}

if (Number(counterInput.value) + step > max) {
btnUp.setAttribute('disabled', '')
}
}

// Static
static StepUp(event) {
const parent = event.target.closest(SELECTOR_INPUT_GROUP)
const counterInput = parent.querySelector(SELECTOR_COUNTER_INPUT)

const max = counterInput.getAttribute('max')
const step = Number(counterInput.getAttribute('step'))
const round = Number(counterInput.getAttribute('data-bs-round'))

const eventChange = new Event('change')

if (Number(counterInput.value) < max) {
counterInput.value = (Number(counterInput.value) + step).toFixed(round).toString()
}

counterInput.dispatchEvent(eventChange)
}

static StepDown(event) {
const parent = event.target.closest(SELECTOR_INPUT_GROUP)
const counterInput = parent.querySelector(SELECTOR_COUNTER_INPUT)

const min = counterInput.getAttribute('min')
const step = Number(counterInput.getAttribute('step'))
const round = Number(counterInput.getAttribute('data-bs-round'))

const eventChange = new Event('change')

if (Number(counterInput.value) > min) {
counterInput.value = (Number(counterInput.value) - step).toFixed(round).toString()
}

counterInput.dispatchEvent(eventChange)
}

static CheckIfDisabledOnChange(event) {
const parent = event.target.closest(SELECTOR_INPUT_GROUP)
const counterInput = parent.querySelector(SELECTOR_COUNTER_INPUT)
const btnUp = parent.querySelector(SELECTOR_STEP_UP_BUTTON)
const btnDown = parent.querySelector(SELECTOR_STEP_DOWN_BUTTON)

const min = counterInput.getAttribute('min')
const max = counterInput.getAttribute('max')
const step = Number(counterInput.getAttribute('step'))

btnUp.removeAttribute('disabled', '')
btnDown.removeAttribute('disabled', '')

if (Number(counterInput.value) - step < min) {
btnDown.setAttribute('disabled', '')
}

if (Number(counterInput.value) + step > max) {
btnUp.setAttribute('disabled', '')
}
}

static jQueryInterface(config) {
return this.each(function () {
const data = QuantitySelector.getOrCreateInstance(this, config)

if (typeof config !== 'string') {
return
}

if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}

data[config]()
})
}
}

/**
* Data API implementation
*/

EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
for (const el of SelectorEngine.find(SELECTOR_INPUT_GROUP)) {
QuantitySelector.getOrCreateInstance(el).ValueOnLoad(el)
}
})
EventHandler.on(document, EVENT_CHANGE_DATA_API, SELECTOR_COUNTER_INPUT, QuantitySelector.CheckIfDisabledOnChange)
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_STEP_UP_BUTTON, QuantitySelector.StepUp)
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_STEP_DOWN_BUTTON, QuantitySelector.StepDown)

/**
* jQuery
*/

defineJQueryPlugin(QuantitySelector)

export default QuantitySelector
Loading