Created
January 29, 2011 01:52
-
-
Save patrick99e99/801410 to your computer and use it in GitHub Desktop.
This file contains 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 Core = (function($) { | |
var self; | |
// private methods: | |
var mathTest = function(method, arr) { | |
return Math[method].apply(Math, arr); | |
}; | |
return { | |
modules: {}, | |
plugins: {}, | |
init: function() { | |
self = this; | |
this.addCurryToFunction(); | |
this.initializePlugins(); | |
this.initializeModules(); | |
}, | |
initializePlugins: function() { | |
for (var name in this.plugins) { | |
this.plugins[name].init(); | |
} | |
}, | |
initializeModules: function() { | |
for (var name in this.modules) { | |
var module = this.modules[name]; | |
// create new sandbox for module | |
var sandbox = new Sandbox(name); | |
module.sandbox = sandbox; | |
module.instance = module.constructor(sandbox).init(); | |
} | |
}, | |
loadPlugin: function(plugin_name, fn) { | |
this.plugins[plugin_name] = fn(); | |
}, | |
registerModule: function(module_name, fn) { | |
this.modules[module_name] = { constructor: fn, | |
instance: null, | |
listening_list: {} }; | |
// if the last module has been registered, initialize core and start all modules | |
if (module_name == 'creator') { | |
Core.init(); | |
} | |
}, | |
registerPlugin: function(name, obj) { | |
this.plugins[name] = obj; | |
}, | |
addToListeningList: function(name, fn, module_name, persist) { | |
var module = this.modules[module_name]; | |
if (module) { | |
module.listening_list[name] = {method: fn, persist: persist}; | |
} | |
}, | |
fireEvent: function(name, data) { | |
for (var mod_name in this.modules) { | |
var list = this.modules[mod_name].listening_list; | |
if (list[name] && list[name].method && this.isFunction(list[name].method)) { | |
list[name].method(data); | |
// remove event if it's not supposed to persist forever | |
if (!list[name].persist) { | |
delete list[name]; | |
} | |
} | |
} | |
}, | |
addCurryToFunction: function() { | |
// add .curry method to functions to allow assignment of arguments outside of function calls | |
if (typeof Function.prototype.curry === 'undefined') { | |
Function.prototype.curry = function() { | |
var func = this; | |
var a = Array.prototype.slice.call(arguments, 0); | |
return function () { | |
var a_len = a.length; | |
var length = arguments.length; | |
while (length--) { | |
a[a_len + length] = arguments[length]; | |
} | |
return func.apply(this, a); | |
}; | |
}; | |
} | |
}, | |
// library specific methods: | |
bind: function(element, evt, fn) { | |
return $(element).bind(evt, fn); | |
}, | |
isFunction: function(obj) { | |
return $.isFunction(obj); | |
}, | |
isString: function(obj) { | |
return (typeof obj === 'string'); | |
}, | |
isArray: function(obj) { | |
return $.isArray(obj); | |
}, | |
find: function(el_id) { | |
return (el_id instanceof jQuery) ? el_id : | |
this.isString(el_id) ? $('#' + el_id) : $(el_id); | |
}, | |
find_by_selector: function(selector) { | |
// if this were Prototype, we'd use $$() here... | |
return (selector instanceof jQuery) ? selector : $(selector); | |
}, | |
resetSelect: function(select) { | |
var values = this.getSelectOptionValues(select); | |
return this.setValue(select, values[0]); | |
}, | |
getSelectOptionValues: function(select) { | |
return this.find(select).find('option').map(function() { | |
return this.value; | |
}).get(); | |
}, | |
getValue: function(input) { | |
return this.find(input).val(); | |
}, | |
setValue: function(input, value) { | |
return this.find(input).val(value); | |
}, | |
ajaxUpdate: function(container, location, callback) { | |
return this.find(container).load(location, callback); | |
}, | |
makeDraggable: function(collection, options) { | |
return collection.draggable(options); | |
}, | |
makeDroppable: function(collection, options) { | |
return collection.droppable(options); | |
}, | |
hide: function(obj) { | |
// use find to wrap the element as a jQuery object if it's not already one | |
return this.find(obj).hide(); | |
}, | |
show: function(obj) { | |
// use find to wrap the element as a jQuery object if it's not already one | |
return this.find(obj).show(); | |
}, | |
hideAll: function(collection) { | |
return this.each(collection, function() { | |
self.hide(this); | |
}); | |
}, | |
showAll: function(collection) { | |
return this.each(collection, function() { | |
self.show(this); | |
}); | |
}, | |
each: function(collection, block) { | |
return this.find(collection).each(block); | |
}, | |
map: function(collection, block) { | |
return this.find(collection).map(block).get(); | |
}, | |
down: function(element, selector) { | |
return this.find(element).find(selector).eq(0); | |
}, | |
clone: function(element) { | |
return this.find(element).clone(); | |
}, | |
attr: function(element, attributes) { | |
return this.find(element).attr(attributes); | |
}, | |
insertHTML: function(element, html) { | |
element = this.find(element); | |
this.each(html, function() { | |
element.append(this); | |
}); | |
return element; | |
}, | |
randomNumber: function(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
}, | |
getText: function(element) { | |
// handle text from text areas as well as from standard elements | |
return (this.isTextArea(element)) ? this.find(element).val() : this.find(element).text(); | |
}, | |
setText: function(element, text) { | |
return (this.isTextArea(element)) ? this.find(element).val(text) : this.find(element).text(text); | |
}, | |
getHTML: function(element, html) { | |
return (this.find(element)).html(); | |
}, | |
setHTML: function(element, html) { | |
return (this.find(element)).html(html); | |
}, | |
isTextArea: function(element) { | |
return this.find(element).get(0).tagName.toLowerCase() == 'textarea'; | |
}, | |
hasClass: function(element, class_name) { | |
return this.find(element).hasClass(class_name); | |
}, | |
highestNumber: function(arr) { | |
return mathTest('max', arr); | |
}, | |
lowestNumber: function(arr) { | |
return mathTest('min', arr); | |
}, | |
extend: function(obj1, obj2) { | |
return $.extend(true, obj1, obj2); | |
}, | |
createElement: function(element, attributes) { | |
var el = $('<' + element + '>'); | |
return attributes ? el.attr(attributes) : el; | |
}, | |
join: function(array, separator) { | |
separator = separator ? separator : ", "; | |
var str = ''; | |
this.each(array, function(index, value) { | |
str += value; | |
if (index != array.length - 1) { | |
str += separator; | |
} | |
}); | |
return str; | |
}, | |
addClass: function(element, class_name) { | |
return this.find(element).addClass(class_name); | |
}, | |
removeClass: function(element, class_name) { | |
return this.find(element).removeClass(class_name); | |
}, | |
width: function(element) { | |
return this.find(element).width(); | |
}, | |
height: function(element) { | |
return this.find(element).height(); | |
}, | |
css: function(element, obj) { | |
return this.find(element).css(obj); | |
} | |
}; | |
})(jQuery); |
This file contains 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
Core.registerModule('photos', function(sandbox) { | |
var self; | |
var photos; | |
var utilities = sandbox.getPlugin('utilities'); | |
return { | |
init: function() { | |
self = this; | |
this.storePhotos(); | |
this.addActionsToPhotos(); | |
this.makePhotosDraggable(); | |
}, | |
storePhotos: function() { | |
photos = Core.find_by_selector('div.photo'); | |
}, | |
addActionsToPhotos: function() { | |
var actions = [{ text: 'Add to favorites', | |
url: '#' }, | |
{ text: 'Move to album', | |
url: '#' }, | |
{ text: 'Edit', | |
url: '#' }, | |
{ text: 'Rename', | |
url: '#' }, | |
{ text: 'Delete', | |
url: '#' }]; | |
utilities.addActions({collection: photos, data: actions, position: 'above'}); | |
}, | |
makePhotosDraggable: function() { | |
utilities.makeDraggables({collection: photos}); | |
}, | |
}; | |
}); |
This file contains 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
Core.loadPlugin('utilities', function() { | |
var self; | |
return { | |
init: function() { | |
self = this; | |
}, | |
addActions: function(obj) { | |
// create a tab in the top-right corner of objects, submenu-options appear when hovering over this tab | |
// quick and dirty recreation of this html: | |
// | |
// <ul class="submenu"> | |
// <li><img src="images/info_box.png" /> | |
// <ul> | |
// <li><a href="#">Add to favorites</a></li> | |
// <li><a href="#">Move</a></li> | |
// <li><a href="#">Edit</a></li> | |
// <li><a href="#">Rename</a></li> | |
// <li><a href="#">Delete</a></li> | |
// </li> | |
// </li> | |
// </ul> | |
var links = ''; | |
Core.each(obj.data, function() { | |
links += '<li><a href="' + this.url + '">' + this.text + '</a></li>'; | |
}); | |
// determine whether the submenu hover above or below the object... | |
var css_class = obj.position ? (' class="' + obj.position + '"') : ""; | |
var html = '<ul class="submenu"><li><img src="images/info_box.png" /><ul' + css_class + '>' + links + '</ul></li></ul>'; | |
Core.each(obj.collection, function() { | |
Core.insertHTML(this, html); | |
}); | |
}, | |
makeDraggables: function(obj) { | |
// make all assets (photos, movies, music, etc) draggable | |
Core.makeDraggable(obj.collection, { | |
revert: true, | |
zIndex: 999 | |
}); | |
}, | |
}; | |
}); |
This file contains 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 Sandbox = function(module_name) { | |
// connect the sandbox instance to it's module | |
this.module_name = module_name; | |
this.listen = function(name, fn, persist) { | |
Core.addToListeningList(name, fn, module_name, persist); | |
}; | |
this.notify = function(name, data) { | |
Core.fireEvent(name, data); | |
}; | |
this.getPlugin = function(name) { | |
return Core.plugins[name]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment