app/scripts/ui-lib/core.coffee

Core

The UILib class defines the core of the UI library.

author: Julien Ramboz
version: 1.0
requires: jQuery or Zepto DOM library

AMD loader

Try loading as AMD module or fall back to default loading

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

Class

The actual plugin class

class UILib

Initialisation

constructor: () ->

Attach functions to the JS library

$.getScript = $.getScript or @getScript $.getStyle = $.getStyle or @getStyle

Set global settings

@animations = true

Initializing stuff for the GUID

@uuid = 0

Globals

The javascript namespace for the library

namespace: window.UI_JS_NAMESPACE || "ui_"

The css namespace for the library

cssNamespace: window.UI_CSS_NAMESPACE || "ui-"

Helper functions

getGUID
Get a new GUID, adding the desired prefix.

  • prefix String: an optional prefix for the hash

returns the GUID as a String

getGUID: (prefix = 'id-') -> prefix + @uuid++

getScript Loads the specified script.

  • url String: the url to the script
  • success Function: an optional success callback function
  • error Function: an optional error callback function
getScript: (url, success, error) -> script = document.createElement("script") $script = $(script) script.src = url $("head").append(script) $script.bind("load", success) $script.bind("error", error)

getStyle
Loads the specified css style sheet.

  • url String: the url to the style sheet

return the added link tag as an Object

getStyle: (url) -> $(document.createElement("link")) .attr( "rel": "stylesheet" "href": url) .insertAfter($("head link[rel='stylesheet']").last())

getInstance
Get the widget instance for the specified element.

  • element Object: the element to get the widget for
  • widget String or String[]: the widget type, can be specified as a
                                  comma-separated list, or an array.
                                  Will continue searching for each type
                                  until an instance is found.

returns the widget instance as an Object

getInstance: (element, widget) -> if not $.isArray(widget) widget = widget.split(",") inst = null for i in widget inst = $(element).data("#{@namespace}#{i}") return inst if inst inst

allowAnimation
Enable/disable all automatic animations for the library.

  • status Boolean: optional status to set

returns the animation status

allowAnimation: (status) -> if status? @animations = status $(window).trigger("animations", {animate: @animations}) @animations and not (document.webkitHidden or document.mozHidden or document.msHidden or document.hidden)

Installation

Instantiate the library and add it to the global scope

window._ = UI_Lib = new UILib()