Skip to content

Commit

Permalink
Fix reset setter func, provide default reset func
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasyadi Abdoellah committed Jan 17, 2020
1 parent b823a15 commit 0a1d865
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h1>This is a sandbox</h1>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->


<script src="https://github.com/RasyadiAbdoellah/filterPackage/blob/master/index.js"></script>
<script src="./index.js"></script>
<script>
const itemFilter = new Filter({ day: {}, topic: {}, category: {} })

Expand Down
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Filter {
//this is binded because below methods are called in an event handler.
this.filter = this.filter.bind(this)
this.reset = this.reset.bind(this)

//points to original reset func
this.defaultReset = this.reset
}

static isEmptyObj(obj) {
Expand Down Expand Up @@ -140,22 +143,24 @@ class Filter {
this.__filterItems.forEach(item => item.classList.remove(this.hider))
}
}
reset(e) {
document.querySelectorAll(`.${this.selector}.${this.toggler}`).forEach(el => el.classList.remove(this.toggler))
this._filters = JSON.parse(JSON.stringify(this.startingState))
}

_altReset = (el) => {
el = el.parentNode.firstElementChild
do {
el.classList.remove(`.${this.toggler}`)
} while (el = el.nextElementSibling)
}

setReset(type) {
if (type === "all") {
this.reset = (el) => {
document.querySelectorAll(`.${this.selector} .${this.toggler}`).forEach(el => el.classList.remove(`.${this.toggler}`))
this.filters = JSON.parse(JSON.stringify(this.startingState))
}
this.reset = this.defaultReset
} else if (type === 'filter') {
this.reset = (el) => {
el = el.parentNode.firstElementChild
do {
el.classList.remove(`.${this.toggler}`)
} while (el = el.nextElementSibling)
}
this.reset = this._altReset
}

}


Expand All @@ -166,6 +171,7 @@ class Filter {
this.__filterItems.forEach((item, index) => {
item.dataset['key'] = index
})

//sets the reset function
this.setReset('all')

Expand Down

0 comments on commit 0a1d865

Please sign in to comment.