// EssentialJS v0.7.0 // Copyright (c)2014 - 2016 Roberto Dip // Distributed under MIT license // http://roperzh.github.io/essential.js (function(e,t){if(typeof define==="function"&&define.amd){define([],t)}else if(typeof module==="object"&&module.exports){module.exports=t()}else{e.returnExports=t()}})(this,function(){var e={rootElement:document,Core:{},start:function(e){this.loadBehaviors({application:e})}, // If no context is provided the default is `Essential.rootElement` loadBehaviors:function(e){e.context=e.context||this.rootElement;var t=this.initializeBehaviors(e.application,e.context);this.launchBehaviors(t)},initializeBehaviors:function(e,t){var n=this.Core.crawl(t),r=Object.keys(n),i=[],o=-1;while(r[++o]){var a=r[o],s=this.Core.camelize(a),c=e[s];if(typeof c!=="undefined"){var l=n[a],u=-1;while(l[++u]){var h=c.new(l[u],true);i.push(h)}}}return i},launchBehaviors:function(e){var t=e.sort(this.Core.SortMethods.byPriority),n=-1;while(t[++n]){t[n].start()}}};if(!Object.getOwnPropertyDescriptors){Object.getOwnPropertyDescriptors=function(e){var t={};Object.getOwnPropertyNames(e).forEach(function(n){t[n]=Object.getOwnPropertyDescriptor(e,n)});return t}}var t={"new":function(){var e=Object.create(this);if(e.constructor){e.constructor.apply(e,arguments)}return e},extend:function(e){var t=Object.create(this,Object.getOwnPropertyDescriptors(e));t.super=this;return t}};Function.prototype.extend=function(e){var n=this;var r=t.extend.call(n.prototype,t);return r.extend(e)};function n(e,t){t=t||{bubbles:false,cancelable:false,detail:undefined};var n=document.createEvent("CustomEvent");n.initCustomEvent(e,t.bubbles,t.cancelable,t.detail);return n}n.prototype=window.Event.prototype;window.CustomEvent=n;e.Behavior=t.extend({constructor:function(e,t,n){this.el=e;if(!t){this.start(n)}},start:function(e){if(typeof this.init==="function"){this.init(e)}this.delegateEvents();this.listenChannels()},delegateEvents:function(){e.Core.mapEvents.call(this,this.events,this.el)},listenChannels:function(){e.Core.mapEvents.call(this,this.channels,document)}, // this parameter can be ommited. Default value is `document` // defaults to true // defaults to false // event can be accesed later via `event.detail` emit:function(e){e.context=e.context||this.el;e.data=e.data||{};e.bubbles=e.bubbles||true;e.cancelable=e.cancelable||false;var t=new n(e.channel,{bubbles:e.bubbles,cancelable:e.cancelable,detail:e.data});e.context.dispatchEvent(t)},priority:0}); // name a valid CSS selector must be placed, for example `"click #element"` e.Core.mapEvents=function(t,n){if(typeof t==="undefined"){return}var r=/^(\S+)\s*(.*)$/;for(var i in t){var o=t[i];var a=i.match(r);var s=a[1],c=a[2],l=c?n.querySelectorAll(c):[n];if(typeof this[o]==="undefined"){continue}e.Core.bind(s,l,this[o].bind(this))}};e.Core.bind=function(e,t,n){var r=-1;while(t[++r]){var i=t[r];if(i.addEventListener){t[r].addEventListener(e,n)}else{i.attachEvent("on"+e,n)}}};e.Core.SPECIAL_CHARS_REGEXP=/([\:\-\_]+(.))/g;e.Core.FIRST_LETTER_REGEXP=/^[a-z]/g;e.Core.camelize=function(t){return t.replace(e.Core.FIRST_LETTER_REGEXP,function(e){return e.toUpperCase()}).replace(e.Core.SPECIAL_CHARS_REGEXP,function(e,t,n){return n.toUpperCase()})};e.Core.crawl=function(e){var t=e.querySelectorAll("[data-behavior], [behavior]"),n=-1,r={};while(t[++n]){var i=t[n],o=i.getAttribute("data-behavior")||i.getAttribute("behavior"),a=o.split(" "),s=-1;while(a[++s]){var c=a[s];if(r[c]){r[c].push(i)}else{r[c]=[i]}}}return r};e.Core.SortMethods={byPriority:function(e,t){return t.priority-e.priority}};return e});