assets/bowline.js in bowline-0.5.8 vs assets/bowline.js in bowline-0.6.0

- old
+ new

@@ -116,10 +116,87 @@ Additionally, jQuery plays nicely with other libraries using it's noConflict() method. So you're still free to use other JavaScript libraries without fear of conflicts. */ +var BowlineBound = function(klass){ + this.klass = klass; + this.options = {}; + this.elements = jQuery(); +}; + +BowlineBound.fn = BowlineBound.prototype; + +BowlineBound.fn.updateOptions = function(opts){ + this.options = jQuery.extend({}, this.options, opts); + this.singleton = this.options.singleton; +} + +BowlineBound.fn.push = function(element){ + this.elements = this.elements.add(element); + this.setup(); +} + +BowlineBound.fn.replace = function(items){ + if(this.singleton) { + this.elements.item("replace", items); + } else { + this.elements.items("replace", items); + } +} + +BowlineBound.fn.create = function(id, item){ + if(this.singleton) { + this.elements.item(item); + } else { + this.elements.items("add", item); + } +} + +BowlineBound.fn.update = function(id, item){ + if(this.singleton){ + this.elements.item(item); + } else { + this.findElement(id).item(item); + } +} + +BowlineBound.fn.remove = function(id){ + if(this.singleton) { + this.elements.item("replace", {}); + } else { + this.findElement(id).item("remove"); + } +} + +BowlineBound.fn.invoke = function(){ + var args = $.makeArray(arguments); + args.unshift(this.klass); + Bowline.invoke.apply(Bowline, args); +} + +BowlineBound.fn.findElement = function(id){ + // TODO - increase efficiency + var element = jQuery(); + jQuery.each(this.elements.items(true), function(){ + var sameItem = $(this).item().id == id; + if(sameItem) element = element.add(this); + }); + return element; +} + +BowlineBound.fn.setup = function(){ + if (this.hasSetup) return; + this.hasSetup = true; + var self = this; + jQuery(function(){ + Bowline.invoke(self.klass, "setup", function(opts){ + self.updateOptions(opts); + }); + }); +} + var Bowline = { callbacks: {}, uuid: 0, bounds: {}, trace: false, @@ -152,10 +229,12 @@ id: id, }; Bowline.log("New message:", msg); + Bowline.log(JSON.stringify(msg)) + if(Bowline.enabled) _app.call(JSON.stringify(msg)); }, // Usage: instanceInvoke(klass, id, method, *args) @@ -172,119 +251,103 @@ Bowline.invoke.apply(this, args); }, helper: function(){ var args = jQuery.makeArray(arguments); - args.unshift("Helper"); + args.unshift("Bowline::Helpers"); Bowline.invoke.apply(this, args); }, bind: function(el, klass, options){ el = jQuery(el); + + el.data("bowline", klass); el.chain(options); - el.data('bowline', klass); + if(!Bowline.bounds[klass]) - Bowline.bounds[klass] = []; + Bowline.bounds[klass] = new BowlineBound(klass); + Bowline.bounds[klass].push(el); - jQuery(function(){ - Bowline.invoke(klass, "setup", function(res){ - Bowline.populate(klass, res); - }); - }); + + // Shortcut + Bowline[klass] = Bowline.bounds[klass]; }, unbind: function(el, klass){ - var array = Bowline.bounds[klass] - if(!array) return; - array = jQuery.grep(array, - function(n){ return n != el } - ); - Bowline.bounds[klass] = array; + // TODO + // var array = Bowline.bounds[klass]; + // if(!array) return; + // array = jQuery.grep(array, + // function(n){ return n.el != el } + // ); + // Bowline.bounds[klass] = array; }, + openInspector: function(){ + if(Bowline.enabled) + _app.openInspector(); + }, + // Bowline functions invokeJS: function(str){ Bowline.log("Invoking:", str); - return JSON.stringify(eval(str)); + try { + return JSON.stringify(eval(str)); + } catch(e) { + Bowline.warn(e); + } }, invokeCallback: function(id, res){ Bowline.log("Callback:", id, res); - if(!Bowline.callbacks[id]) return true; + if(!Bowline.callbacks[id]) return; try { Bowline.callbacks[id](JSON.parse(res)); delete Bowline.callbacks[id]; } catch(e) { - Bowline.log(e) + Bowline.warn(e) } - return true; }, - populate: function(klass, items){ - if(!Bowline.bounds[klass]) return true; - jQuery.each(Bowline.bounds[klass], function(){ - this.items('replace', items); - }); - return true; + replace: function(klass, items){ + if(!Bowline.bounds[klass]) return; + Bowline.bounds[klass].replace(items); }, created: function(klass, id, item){ - if(!Bowline.bounds[klass]) return true; - if(!item.id) item.id = id; - jQuery.each(Bowline.bounds[klass], function(){ - this.items('add', item); - }); - return true; + if(!Bowline.bounds[klass]) return; + Bowline.bounds[klass].create(id, item); }, updated: function(klass, id, item){ - if(!Bowline.bounds[klass]) return true; + if(!Bowline.bounds[klass]) return; if(!item.id) item.id = id; - jQuery.each(Bowline.bounds[klass], function(){ - Bowline.findItem(this, id).item('replace', item); - }); - return true; + Bowline.bounds[klass].update(id, item); }, removed: function(klass, id){ - if(!Bowline.bounds[klass]) return true; - jQuery.each(Bowline.bounds[klass], function(){ - Bowline.findItem(this, id).item('remove'); - }); - return true; + if(!Bowline.bounds[klass]) return; + Bowline.bounds[klass].remove(id); }, trigger: function(klass, event, data){ - if(!Bowline.bounds[klass]) return true; - jQuery.each(Bowline.bounds[klass], function(){ - this.trigger(event, data); - }); - return true; + if(!Bowline.bounds[klass]) return; + Bowline.bounds[klass].elements.trigger(event, data); }, element: function(klass, id){ - var el = jQuery(); - jQuery.each(Bowline.bounds[klass], function(){ - el = el.add(findItem(this, id)); - }); - return el; + if(!Bowline.bounds[klass]) return; + return Bowline.bounds[klass].findElement(id); }, // System functions loaded: function(){ Bowline.windowInvoke("loaded!"); }, - findItem: function(el, id){ - var items = jQuery.grep(el.items(true), function(n, i){ - return jQuery(n).item().id == id; - }); - return(jQuery(items[0])); - }, - log: function(){ if( !Bowline.trace ) return; var args = jQuery.makeArray(arguments); args.unshift("(Bowline)"); console.log.apply(console, args); @@ -297,23 +360,23 @@ } }; (function($){ $.fn.invoke = function(){ - if($(this).chain('active')){ - var args = $.makeArray(arguments); - if($(this).data('bowline')){ - // Class method - var klass = $(this).data('bowline'); - args.unshift(klass); - Bowline.invoke.apply(Bowline, args); - } else { - // Instance method - var klass = $(this).item('root').data('bowline'); - var id = $(this).item().id; + if($(this).chain("active")){ + var args = $.makeArray(arguments); + var element = $(this).item("root"); + var klass = element.data("bowline"); + if(!klass) throw "Unknown class: " + klass; + + if(Bowline[klass].singleton){ + var id = element.item().id; args.unshift(id); args.unshift(klass); Bowline.instanceInvoke.apply(Bowline, args); + } else { + args.unshift(klass); + Bowline.invoke.apply(Bowline, args); } } else { throw 'Chain not active'; } }; \ No newline at end of file