Skip to content

Commit

Permalink
fix/issue-395/globalAlert (#403)
Browse files Browse the repository at this point in the history
* Add cookie functionality, update examples in blank
  • Loading branch information
laurenhitchon authored Jun 12, 2024
1 parent d668275 commit 80bf5e6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/components/global-alert/_global-alert.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="nsw-global-alert{{#if type}} nsw-global-alert--{{type}}{{/if}} js-global-alert" role="alert">
<div class="nsw-global-alert{{#if type}} nsw-global-alert--{{type}}{{/if}} js-global-alert" role="alert" data-cookie-name="{{cookie}}">
<div class="nsw-global-alert__wrapper">
<div class="nsw-global-alert__content">
<div class="nsw-global-alert__title">{{title}}</div>
Expand Down
10 changes: 5 additions & 5 deletions src/components/global-alert/blank.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ model: json/global-alert.json
---

{{>_global-alert model}}
{{>_global-alert model type="light"}}
{{>_global-alert model type="critical"}}
{{>_global-alert model link="true"}}
{{>_global-alert model link="true" type="light"}}
{{>_global-alert model link="true" type="critical"}}
{{>_global-alert model type="light" cookie="nsw-alert-light"}}
{{>_global-alert model type="critical" cookie="nsw-alert-critical"}}
{{>_global-alert model link="true" cookie="nsw-alert-link"}}
{{>_global-alert model link="true" type="light" cookie="nsw-alert-light-link"}}
{{>_global-alert model link="true" type="critical" cookie="nsw-alert-critical-link"}}
41 changes: 35 additions & 6 deletions src/components/global-alert/global-alert.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
class GlobalAlert {
constructor(element) {
this.messageElement = element
this.closeButton = element.querySelector('.js-close-alert')
this.closeMessageEvent = (e) => this.closeMessage(e)
this.element = element
this.closeButton = this.element.querySelector('button.js-close-alert')
this.cookieName = this.element.getAttribute('data-cookie-name') || false
}

init() {
this.controls()
if (this.cookieName && this.constructor.getCookie(this.cookieName)) {
this.element.hidden = true
return
}
if (this.closeButton) {
this.controls()
}
}

controls() {
this.closeButton.addEventListener('click', this.closeMessageEvent, false)
this.closeButton.addEventListener('click', () => {
this.closeMessage()
})
}

closeMessage() {
this.messageElement.hidden = true
this.element.hidden = true
if (this.cookieName) {
this.constructor.setCookie(this.cookieName, 'dismissed', 7)
}
}

static setCookie(name, value, days) {
const date = new Date()
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000))
const expires = `expires=${date.toUTCString()}`
document.cookie = `${name}=${value};${expires};path=/`
}

static getCookie(name) {
const nameEQ = `${name}=`
const ca = document.cookie.split(';')
for (let i = 0; i < ca.length; i += 1) {
let c = ca[i]
while (c.charAt(0) === ' ') c = c.substring(1, c.length)
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length)
}
return null
}
}

Expand Down

0 comments on commit 80bf5e6

Please sign in to comment.