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

Notification

The Notification class provides a simple notification widget.

author: Julien Ramboz version: 1.0 usage: Notification 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", "alert"], widget else widget @$, _, _.AbstractWidget, _.Alert ) notification = ($, _, AbstractWidget, Alert) -> "use strict"

Widget

The actual widget class

class Notification extends Alert

Default options for the widget

@defaultOptions:

timeout: the time after which the notification will be removed (in ms)

timeout: 10000

wrapper: id of the wrapper element

wrapper: "notifications"

sticky: whether the notification is a sticky and thus cannot be removed

sticky: false

Initialisation

Initializer function.

initialize: (options) -> super options @wrapper = @findWrapper() if @options.sticky isnt true and @options.sticky isnt "true" @timeoutID = setTimeout( Notification.removeNotification, @options.timeout, @) @element = @element.detach() @wrapper.append(@element)

Accessibility markup

Add aria attributes

aria: () -> super() @wrapper.attr("data-role", "notifications")

Event handling

Attach evenets to the widget

bindEvents: () -> super()

Remove the notification

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

Remove the notification

@removeNotification: (inst) -> if inst.options.sticky isnt true and inst.options.sticky isnt "true" clearTimeout(inst.timeoutID) inst.element.remove() inst.destroy()

Structure discovery

Find the widget structure using the specified configuration

Notifications wrapper

Find the wrapper for the notifications, using the selector specified using the wrapper option

findWrapper: () -> $wrapper = $("#"+@options.wrapper) if not $wrapper.length $page = $("#page") if $page.length $wrapper = @createWrapper().insertBefore(page) else $wrapper = @createWrapper().prependTo($("body")) $wrapper

Structure creation

Create elements required by the structure if they are not present

Wrapper element

createWrapper: () -> $(document.createElement("section")).attr "id": @options.wrapper "aria-live": "polite"

Key handling

Cleanup

Cleanup the widget and remove remaining references

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

Installation

Install the widget into the JS library

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