Last active
September 26, 2019 19:09
-
-
Save davodey/ee53293f6e9f445da0fc8be8d0a89913 to your computer and use it in GitHub Desktop.
Pearson Alert Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (w, doc) { | |
doc.addEventListener("DOMContentLoaded", function(){ | |
const forEach = Array.prototype.forEach, | |
triggers = doc.querySelectorAll('button[data-trigger="alert"]'); | |
function generateAlert(opts) { | |
const alert = doc.createElement('pearson-alert'); | |
for (let attrName in opts) { | |
alert.setAttribute(attrName, opts[attrName]) | |
} | |
return alert; | |
} | |
function triggerAlert(e) { | |
const returnNode = '#' + e.target.id; | |
const type = e.target.dataset.type; | |
const level = e.target.dataset.level; | |
const animated = e.target.dataset.animated !== undefined; | |
const focusOnOpen = e.target.dataset.focusOnOpen !== undefined; | |
const opts = { | |
returnNode: returnNode, | |
type: type, | |
level: level | |
}; | |
if (animated) { | |
opts.animated = true; | |
} | |
if (focusOnOpen) { | |
opts.focusOnOpen = true; | |
} | |
const alert = generateAlert(opts); | |
alert.innerHTML = ( | |
' <h2 id="alertTitle" class="pe-label alert-title"> ' + | |
' <strong>Heads up!</strong> ' + | |
' </h2> ' + | |
' <p id="alertText" class="pe-paragraph alert-text"> You fired an alert' + | |
' <a href="#">Do Something !</a> ' + | |
' </p> ' | |
); | |
if (level === 'inline') { | |
e.target.parentNode.insertBefore(alert, e.target.nextSibling) | |
} else { | |
doc.body.appendChild(alert) | |
} | |
} | |
forEach.call(triggers, function (trigger) { | |
trigger.addEventListener('click', triggerAlert); | |
}) | |
w.addEventListener('dismiss', function (e) { | |
renderMessage(e.target); | |
}); | |
}); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment