/** * humane.js * Humanized Messages for Notifications * @author Marc Harter (@wavded) * @example * humane('hello world'); * See more usage examples at: http://wavded.github.com/humane-js/ */ ;(function (win,doc) { var humane, on, off, isArray, eventing = false, useTransitions = false, animationInProgress = false, humaneEl = null, timeout = null, useFilter = /msie [678]/i.test(navigator.userAgent), // sniff, sniff vendors = { Webkit: 'webkit', Moz: '', O: 'o', ms: 'MS' }, eventPrefix = "", isSetup = false, currentMessage = {}, noop = function(){}, events = { 'add': noop, 'show': noop, 'hide': noop }, queue = []; if ('addEventListener' in win) { on = function (obj,type,fn) { obj.addEventListener(type,fn,false) }; off = function (obj,type,fn) { obj.removeEventListener(type,fn,false) }; } else { on = function (obj,type,fn) { obj.attachEvent('on'+type,fn) }; off = function (obj,type,fn) { obj.detachEvent('on'+type,fn) }; } isArray = Array.isArray || function (obj) { return Object.prototype.toString.call(obj) === '[object Array]' }; function normalizeEvent(name) { return eventPrefix ? eventPrefix + name : name.toLowerCase(); } function getConfig(type, config) { return win.humane[type][config] !== void 0 ? win.humane[type][config] : win.humane[config]; } on (win,'load', setup); function setup() { humaneEl = doc.createElement('div'); humaneEl.id = 'humane'; humaneEl.className = 'humane'; doc.body.appendChild(humaneEl); for (vendor in vendors) { if (vendor + 'TransitionProperty' in humaneEl.style) { eventPrefix = vendors[vendor]; useTransitions = true; } } if (!useTransitions) animate = jsAnimateOpacity; // use js animation when no transition support isSetup = true; run(); } function run() { if (animationInProgress) return; if (!queue.length) return; after = null; animationInProgress = true; if (timeout) { clearTimeout(timeout); timeout = null; } var next = queue.shift(); currentMessage = { type: next[0], message: next[1], callback: next[2] }; var content = currentMessage.message, type = currentMessage.type; if ( getConfig(type, 'clickToClose') === true ) { on (humaneEl, 'click', remove); on (humaneEl, 'touchstart', remove); } var timeoutInMillis = getConfig(type, 'timeout'); if (timeoutInMillis > 0) { timeout = setTimeout(function(){ // allow notification to stay alive for timeout if (!eventing) { on (doc.body, 'mousemove', remove); on (doc.body, 'click', remove); on (doc.body, 'keypress', remove); on (doc.body, 'touchstart', remove); eventing = true; if( getConfig(type, 'waitForMove') !== true ) remove(); } }, timeoutInMillis); } events['show'](type,content,'show'); if ( isArray(content) ) content = '