From cb336968a73baf3bc5cb727262efbbbb66944bfd Mon Sep 17 00:00:00 2001 From: Meegwun Southall Date: Fri, 15 Mar 2013 11:13:49 -0700 Subject: [PATCH] Made some minor changes to CBDL, added some TODO issues that need to be resolved. Mainly to test commits from a mobile platform --- CBDL.js | 234 +++++++++++++++++++++++++----------------------------- cbdl.html | 30 ++++++- 2 files changed, 136 insertions(+), 128 deletions(-) diff --git a/CBDL.js b/CBDL.js index a272f41..6130156 100644 --- a/CBDL.js +++ b/CBDL.js @@ -6,6 +6,9 @@ var CBDL = CBDL || {}; CBDL.version = 0.1; +// TODO: completely redo requires & library includes. At the moment, it is broken and disallows multiple Apps from starting if they both require libraries. + + CBDL.dump = function() { console.log("CBDL version "+CBDL.version); } @@ -13,6 +16,63 @@ CBDL.dump = function() { /* =============================================================================== +CBDL.App + + Object that should be the base for all CBDL-based applications. Contains the +bare-minimum logic, members, and vars that any CBDL app should hold. + +Usage: + some_app = new CBDL.App(); + some_app.Init(); + +=============================================================================== +*/ +CBDL.App = CBDL.App || function() {}; + +CBDL.App.prototype.Main = function() {}; +CBDL.App.prototype.Go = function() { + if (this.requires) { + this._includes = new CBDL.Includes(this); + for (var require in this.requires) { + this._includes.add(this.requires[require]); + } + } + console.log(this.name); + if (this._includes) { + if (this._includes.state != CBDL.states.LOADED) { + this.includeCallback = function(return_value) { + if (return_value < 0) { // fail + console.log("CBDL error: includes failed to load!"); + return return_value; + } + this.Main(); + }; + } else if (this._includes.state == CBDL.states.LOADED) { + this.Main(); + } + } else { + this.Main(); + } +}; +CBDL.App.prototype.include = function(some_library) { + if (!this._includes) { + this._includes = new CBDL.Includes(this); + } + this._includes.add(some_library); +}; +/* called when all includes succeed or fail */ +CBDL.App.prototype.includeCallback = function(return_value) { + if (return_value < 0) { // fail + console.log("CBDL error: includes failed to load!"); + return return_value; + } else { + this.Main(); + } +}; + +/* +=============================================================================== + CBDL.Includes Object that manages includes for an App. Attempts to load all includes @@ -47,15 +107,33 @@ CBDL.Includes = function(app) { }; this.netLoad = function() { - var element = document.createElement("script"); - element.type = "text/javascript"; - element.src = _Includes.includes[_Includes.current]; - element.onload = function() { + var exists = false; + // Compare some_library string to every script.src so as to disallow + // multiple loading of the same library. + var script_list = document.head.getElementsByTagName('script'); + for(script in script_list) { + if (script_list[script].src) { + if (script_list[script].src.substring(script_list[script].src.length-_Includes.includes[_Includes.current].length) == _Includes.includes[_Includes.current]) { + exists = true; + } + } + } + + if (exists == false) { + var element = document.createElement("script"); + element.type = "text/javascript"; + element.src = _Includes.includes[_Includes.current]; + element.onload = function() { + _Includes.state = 0x00; + _Includes.current++; + _Includes.load(); // load next file + } + document.getElementsByTagName('head')[0].appendChild(element); + } else { _Includes.state = 0x00; _Includes.current++; _Includes.load(); // load next file } - document.getElementsByTagName('head')[0].appendChild(element); // if (!/*@cc_on!@*/false) { /* var request = new XMLHttpRequest(); } else { @@ -84,62 +162,6 @@ CBDL.Includes = function(app) { } -/* -=============================================================================== - -CBDL.App - - Object that should be the base for all CBDL-based applications. Contains the -bare-minimum logic, members, and vars that any CBDL app should hold. - -Usage: - some_app = new CBDL.App(); - some_app.Init(); - -=============================================================================== -*/ -CBDL.App = CBDL.App || function() {}; - -CBDL.App.prototype.Main = function() {}; -CBDL.App.prototype.Go = function() { - if (this.requires) { - this._includes = new CBDL.Includes(this); - for (var require in this.requires) { - this._includes.add(this.requires[require]); - } - } - if (this._includes) { - if (this._includes.state != CBDL.states.LOADED) { - this.includeCallback = function(return_value) { - if (return_value < 0) { // fail - console.log("CBDL error: includes failed to load!"); - return return_value; - } - this.Main(); - }; - } else if (this._includes.state == CBDL.states.LOADED) { - this.Main(); - } - } else { - this.Main(); - } -}; -CBDL.App.prototype.include = function(some_library) { - if (!this._includes) { - this._includes = new CBDL.Includes(this); - } - this._includes.add(some_library); -}; -/* called when all includes succeed or fail */ -CBDL.App.prototype.includeCallback = function(return_value) { - if (return_value < 0) { // fail - console.log("CBDL error: includes failed to load!"); - return return_value; - } else { - this.Main(); - } -}; - /* ================ CBDL.inherit(baseClass, newClass) @@ -151,51 +173,16 @@ CBDL.inherit = function(base_class, new_class) { base_class.call(new_class); new_class.constructor = new_class; } -CBDL.inherits = function(base_class, new_class) { + +CBDL.extend = function(base_class, new_class) { new_class.prototype = new base_class; new_class.prototype.constructor = base_class; // fix constructor } -CBDL.extends = function(base_class, new_class) { - new_class.prototype = new base_class; - new_class.prototype.constructor = base_class; // fix constructor -} - - -/* -================ -CBDL.startApp(function) - - Function which is used to start the CBDL-based application that is passed -as its argument. The application, being an Object, requires, at bare minimum, -onInit(), onExecute(), and onLoop() member functions. This function then calls -onInit(), and if all goes well, to call onExecute(). - What this effectively accomplishes is header/defines in the onInit, and a -base main() call in the onExecute(). - -Usage: - someApp = function() { - this.onInit = function() { - include("some_library.js"); - }; - this.onExecute = function() { - console.log("some_app executed!"); - }; - } - CBDL.startApp(new someApp); -================ -*/ -CBDL.startApp = function(app) { - if (!app.onInit || !app.onExecute) { - console.log("CBDL error: missing onInit or onExecute app member!"); - return -1; - } -} - /* =============================================================================== -CBDL.Loop(callback, interval) +CBDL.Loop(scope, callback, interval) Object that acts as a simple interface to creating and managing loops within the script. Attempts to call callback() after X interval has passed. Interval @@ -215,8 +202,8 @@ Or: =============================================================================== */ -CBDL.Loop = function(callback, interval) { - _Loop = this; +CBDL.Loop = function(scope, callback, interval) { + this.scope = scope; this.callback = callback; this.interval = (interval ? interval : 0); @@ -229,7 +216,14 @@ CBDL.Loop = function(callback, interval) { this.running = true; if (delay) { - setTimeout(function() { _Loop.loop(); }, delay); + setTimeout( + (function(_Loop) { + return function() { + _Loop.loop.call(_Loop); + } + })(this), + delay); + //setTimeout(_Loop.loop, _Loop.time_delay); } else { this.loop(); } @@ -239,36 +233,28 @@ CBDL.Loop = function(callback, interval) { }; this.loop = function() { if (this.running) { + this.time_start = new Date().getTime(); if (this.interval == 0) { // acquire time_delay from callback return - this.time_delay = this.callback(); + this.time_delay = this.callback.call(this.scope, this.time_start - this.time_end); + this.time_end = new Date().getTime(); } else { - this.time_start = new Date().getTime(); - this.callback(this.time_start-this.time_end); // return delta to callback + this.callback.call(this.scope, this.time_start-this.time_end); // return delta to callback this.time_end = new Date().getTime(); this.time_delay = this.time_end-this.time_start; this.time_delay = (this.time_delay > this.interval ? 0 : this.interval - this.time_delay); } - setTimeout(function() { _Loop.loop(); }, this.time_delay); + setTimeout( + (function(_Loop) { + return function() { + _Loop.loop.call(_Loop); + } + })(this), + this.time_delay); + //setTimeout(this.callLoop.call, this); } }; -} - -/* -================ -CBDL.newLoop(callback, interval) - - Creates and returns a new Loop object. Once the Loop's start member is -called, CBDL continually calls the callback at the specified interval -times. - -Usage: - new_loop = CBDL.newLoop(function() { console.log("test"); }, 500); - new_loop.start(); // begins printing "test" to the console every 500ms -================ -*/ -CBDL.newLoop = function(callback, interval) { - if (callback instanceof Object) { - return new CBDL.Loop(callback, interval); + this.callLoop = function() { + this.loop(); } } diff --git a/cbdl.html b/cbdl.html index fc75314..e963259 100644 --- a/cbdl.html +++ b/cbdl.html @@ -3,26 +3,48 @@