chicago
Version:
A front-end JavaScript library for user-interface developers.
356 lines (298 loc) • 8.21 kB
JavaScript
/*!
* Chicago - Core
* Base core of the Chicago library
*
* Copyright (c) 2015 Erik Nielsen
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://nielse63.github.io/Chicago/
*
* Version: @VERSION
*
*/
(function( global, factory ) {
if( typeof window === "undefined" && global.window ) {
global = global.window;
}
if( typeof define == "function" && define.amd ) {
define("chicago", function() {
var chicago = global.Chicago || factory(global.jQuery, global, global.document);
chicago.load = function(res, req, onload, config) {
var resources = res.split(','), load = [], i, base = (config.config && config.config.chicago && config.config.chicago.base ? config.config.chicago.base : "").replace(/\/+$/g, "");
if( ! base ) {
throw new Error( "Please define base path to Chicago in the requirejs config." );
}
for( i = 0; i < resources.length; i += 1 ) {
var resource = resources[i].replace(/\./g, '/');
load.push( base + '/modules/' + resource );
}
req(load, function() {
onload(chicago);
});
};
return chicago;
});
}
if( ! global.jQuery ) {
throw new Error( 'Chicago requires jQuery' );
}
global.Chicago = factory( global.jQuery, global, global.document );
return global.Chicago;
})(typeof window !== "undefined" ? window : this, function( $, win, doc ) {
var _cTemp = win.Chicago;
var eventnames = {
// Ready/Load events
beforeready : 'beforeready.chicago.dom',
ready : 'ready.chicago.dom',
load : 'reloaded.chicago.dom',
// Global module events
init : 'init.chicago.module'
};
var _c = {
// Properties
version : '@VERSION',
doc : doc,
win : win,
$ : $,
$doc : $(doc),
$win : $(win),
$html : $('html'),
$head : $('html head'),
$body : $('body'),
utils : {},
support : {},
modules : {},
create : {},
css : {},
elements : {},
deprecated : {},
domReady : false,
langdirection : $('html').attr('dir') ? $('html').attr('dir') : 'ltr',
// Core Methods
// @source: https://github.com/jquery/jquery/blob/98cee73244d55910a1ac82bcf6cae04a7f650484/src/core.js#L118
extend : function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[ 0 ] || {},
i = 1,
length = arguments.length,
deep = false;
if( _c.utils.is.boolean( target ) ) {
deep = target;
target = arguments[ i ] || {};
i++;
}
if( typeof target !== "object" && ! _c.utils.is.function( target ) ) {
target = {};
}
for( ; i < length; i++ ) {
if(( options = arguments[ i ] ) != null ) {
for( name in options ) {
src = target[ name ];
copy = options[ name ];
if( target === copy ) {
continue;
}
if( deep && copy && ( _c.utils.is.object( copy ) || ( copyIsArray = _c.utils.is.array( copy ) ) ) ) {
if ( copyIsArray ) {
copyIsArray = false;
clone = src && _c.utils.is.array( src ) ? src : [];
} else {
clone = src && _c.utils.is.object( src ) ? src : {};
}
target[ name ] = _c.extend( deep, clone, copy );
} else if( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
return target;
},
noConflict : function() {
if( _cTemp ) {
win.Chicago = _cTemp;
$.Chicago = _cTemp;
$.fn.chicago = _cTemp.fn;
return _cTemp;
}
return this;
},
fn : function( command, options ) {
var args = arguments,
cmd = command.match(/^([a-z\-]+)(?:\.([a-z]+))?/i),
module = cmd[1],
method = cmd[2];
if( ! method && typeof options === 'string' ) {
method = options;
}
if( ! _c[module] ) {
$.error('Chicago module [' + module + '] does not exist.');
return this;
}
return this.each(function() {
var $this = _c.$(this);
var data = $this.data(module);
if(!data) {
$this.data(module, data = _c[module](this, (method ? void 0 : options)));
}
if(method) {
data[method].apply(data, Array.prototype.slice.call(args, 1));
}
});
},
on : function( evt, handler, data ) {
if( evt && evt.indexOf( eventnames.ready ) > -1 && _c.domReady ) {
return handler.apply( _c.$doc );
}
return _c.$doc.on( evt, handler, data );
},
trigger : function( evt, params ) {
return _c.$doc.trigger( evt, params );
},
// find : function( selector, context ) {
// if( context ) {
// return _c.$( context ).find( selector );
// }
// return _c.$body.find( selector );
// },
};
_c.ready = function( fn ) {
if(_c.domReady) {
return fn(doc);
}
return _c.on( eventnames.ready );
};
_c.module = function( name, methods ) {
// add method to create object
if( ! this.create[name] ) {
this.create[name] = function( _this ) {
return function( options ) {
return _this.factory.make( name, options );
};
}(this);
}
var dataKey = 'chicago.data.' + name,
fn = function( element, options ) {
this.element = element ? _c.$( element ) : null;
this.options = _c.extend( true, {}, this.defaults, options );
// _c.extend( this, methods );
if(this.element) {
this.element.data(dataKey, this);
// add active element placeholder
if( ! _c.elements[name] ) {
_c.elements[name] = null;
}
}
this.init();
this.trigger(eventnames.init, [name, this]);
return this;
};
this.extend(true, fn.prototype, {
// properties
defaults : {},
// init/destroy methods
boot : function() {},
init : function() {},
destroy : function() {},
// utility methods
on : function( evt, data, handler ) {
return _c.$(this.element || this).on(evt, data, handler);
},
one : function( evt, data, handler ) {
return _c.$(this.element || this).one(evt, data, handler);
},
off : function( evt ) {
return _c.$(this.element || this).off(evt);
},
trigger : function( evt, params ) {
return _c.$(this.element || this).trigger(evt, params);
},
proxy : function( obj, methods ) {
return methods.split(' ').forEach(function(_this) {
return function(method) {
if( ! _this[method] ) {
_this[method] = function() {
return obj[method].apply( obj, arguments );
};
}
};
}(this));
},
}, methods);
this.modules[name] = fn;
this[name] = function() {
var args = arguments,
ele,
options;
if( args.length ) {
switch( args.length ) {
case 1 :
if( _c.utils.is.string( args[0] ) || args[0].nodeType || _c.utils.is.jQueryObject( args[0] ) ) {
ele = $(args[0]);
} else {
options = args[0];
}
break;
case 2 :
ele = _c.$(args[0]);
options = args[1];
break;
}
}
if( ele && ele.data( dataKey ) ) {
return ele.data( dataKey );
}
return new _c.modules[name](ele, options);
};
if( ! this[name].template ) {
this[name].template = {};
}
this[name].template = _c.extend( this[name].template, methods.template );
if( _c.domReady ) {
_c.module.boot(name);
}
return fn;
};
_c.module.boot = function(name) {
if(_c.modules[name] && _c.modules[name].prototype && _c.modules[name].prototype.boot && ! _c.modules[name].booted) {
_c.modules[name].booted = true;
// boot the module
_c.modules[name].prototype.boot.apply(_c, []);
}
};
_c.bootComponents = function() {
for(var module in _c.modules) {
_c.module.boot(module);
}
};
// Bind custom dom-ready listener
_c.on(eventnames.ready, function() {
if( _c.domReady ) {
return;
}
_c.domReady = true;
});
// @import "support.js";
// @import "utils.js";
// @import "factory.js";
// Init
(function() {
// On doc ready
_c.$doc.on('ready', function() {
// reassign the body variable
_c.$body = _c.$('body');
_c.trigger( eventnames.beforeready );
_c.bootComponents();
// On resizeend, orientationchange, and window load
_c.$win.on('load resizeend orientationchange', _c.utils.debounce(function() {
_c.$doc.trigger( eventnames.load );
}, 100));
// Chicago is ready
_c.trigger( eventnames.ready );
});
})();
return _c;
});