Last active
December 14, 2015 14:29
-
-
Save dhigginbotham/5101103 to your computer and use it in GitHub Desktop.
script loader with obj array pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var colors = require('colors'); | |
//scripts.schema.js | |
var scriptLoader = {}; | |
scriptLoader.files = [ | |
//header scripts | |
{path:'//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', name:'jquery', where:'head', url:'/', type: 'js'}, | |
{path:'//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js', name:'bootstrap', where:'head', url: '/', type: 'js'}, | |
{path:'/socket.io/socket.io.js', name:'socket-io', where:'head', url: '/', type: 'js'}, | |
{path:'/js/vendor/d3.v2.js', name:'d3', where:'head', url: '/dashboard', type: 'js'}, | |
{path:'/js/facebook.client.js', name:'facebook-client', where:'head', url: '/', type: 'js'}, | |
{path:'/js/vendor/rickshaw.min.js', name:'rickshaw', where:'head', url: '/dashboard', type: 'js'}, | |
//header css | |
{path:'/css/style.css', name: 'stylesheet', where: 'head', url: '/', type: 'css'}, | |
{path:'/css/flexslider.vendor.css', name: 'flexslider-css', where: 'head', url: '/p/view/{ln}', type: 'css'}, | |
//footer scripts | |
{path:'/js/lib/helpers.client.js', name:'helpers-client', where:'foot', url: '/', type: 'js'}, | |
{path:'/js/fb.meta.js', name:'fb-meta', where:'foot', url: '/', type: 'js'}, | |
{path:'/js/vendor/jquery.flexslider-min.js', name: 'flexslider-js', where: 'foot', url: '/p/view/{ln}', type: 'js'}, | |
{path:'/js/view.client.js', name: 'view-client', where: 'foot', url: '/p/view/{ln}', type: 'js'}, | |
{path:'/js/top5.client.js', name: 'top5', where:'foot', url: '/dashboard', type: 'js'}, | |
{path:'/js/create.client.js', name:'create', where:'foot', url: '/p/create', type: 'js'}, | |
{path:'/js/account.client.js', name:'account', where:'foot', url: '/account', type: 'js'}, | |
{path:'/js/rickshaw.client.js', name:'rickshaw-client', where:'foot', url: '/dashboard', type: 'js'}, | |
{path:'/js/socket.client.js', name:'socket-io-client', where:'foot', url: '/', type: 'js'}, | |
//admin analytics | |
{path:'/js/admin.client.js', name:'admin', where:'foot', url: '/admin', type: 'js'}, | |
]; | |
exports.ManageScriptLoader = function(request, type, callback) { | |
var internals = scriptLoader.files; | |
var loaded = { | |
head: [], | |
foot: [] | |
}; | |
var loadingLoop = scriptLoader.loop; | |
loadingLoop(request, type, function(t) { | |
for ( var j=0;j<t.length;++j) { | |
var scriptLoop = t[j]; | |
if ( request.url.pathname === scriptLoop.url || scriptLoop.url === '/') { | |
if ( scriptLoop.type === type && scriptLoop.where === scriptLoop.where) { | |
console.log(colors.cyan(' ' + scriptLoop.name + ' to ') + scriptLoop.where); | |
loaded[scriptLoop.where].push(scriptLoop); | |
} | |
} | |
} | |
}); | |
return (callback) ? callback(loaded) : loaded; | |
} | |
scriptLoader.loop = function (request, type, callback) { | |
var scripts = scriptLoader.files; | |
var regex = /\{([^}]+)\}/; | |
var t = []; | |
for(var i=0;i<scripts.length;++i) { | |
var f = scripts[i]; | |
f._split = f.url.split('/').pop(); | |
f._items = f.url.split('/'); | |
var rx = f._regex = f._split.match(regex); | |
if ( rx && request.params) { | |
f.url = (request.params[rx[0]]) ? request.params[rx[0]] : request.url.pathname; | |
t.push({slug: f._split, url: f.url, name: f.name, where: f.where, type: f.type, path: f.path, pathArray: f._items}); | |
} else { | |
t.push({slug: f._split, url: f.url, name: f.name, where: f.where, type: f.type, path: f.path, pathArray: f._items}); | |
} | |
} | |
return (callback) ? callback(t) : t; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment