app/scripts/ui-lib/widgets/alert.coffee

Alert

The Alert class provides a simple disposable alert message.

author: Julien Ramboz version: 1.0 usage: Alert examples

AMD loader

Try loading as AMD module or fall back to default loading

((widget) -> if typeof define is "function" and define.amd define ["jslib", "core", "widget"], widget else widget @$, _, _.AbstractWidget ) alert = ($, _, AbstractWidget) -> "use strict"

Widget

The actual widget class

class Alert extends AbstractWidget

defaultOptions

Default options for the widget

Initialisation

Initializer function.

initialize: (options) -> super options @close = @findClose()

Accessibility markup

Add aria attributes

aria: () -> @element.attr("role", "alert") @close.attr "role": "button" "data-role": "close" "tabindex": "0"

Event handling

Attach evenets to the widget

bindEvents: () -> @handleKeys(@element) @close.on "click", (e) => return if e.target.getAttribute("aria-disabled") is "true" @removeNotification()

Remove the notification

removeNotification: () -> Alert.removeNotification(@)

Remove the notification

@removeNotification: (inst) -> inst.element.remove() inst.destroy()

Structure discovery

Find the widget structure using the specified configuration

Close button

Find the close button, specified using the data-role="close" attribute, or auto-generated

findClose: () -> $close = @element.find("[data-role='close']") return $close if $close.length $close = @createClose() @element.prepend($close) $close

Structure creation

Create elements required by the structure if they are not present

Close button

createClose: () -> $(document.createElement("span"))

Key handling

Handle escape key press

keyEscape: (e) -> @removeNotification()

Handle space key press

keySpace: (e) -> @keyEnter e

Handle enter key press

keyEnter: (e) -> if e.target.getAttribute("data-role") is "close" @removeNotification()

Cleanup

Cleanup the widget and remove remaining references

destructor: () -> super() @close = null @timeoutID = null

Installation

Install the widget into the JS library

Alert.install("Alert", () -> $("[data-widget='alert']")["#{_.namespace}alert"]() )