r711 - in funjs: . nbproject public_html public_html/funjs
Author: jruchaud Date: 2014-06-18 15:24:07 +0200 (Wed, 18 Jun 2014) New Revision: 711 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/711 Log: Initial import Added: funjs/config/ funjs/nbproject/ funjs/nbproject/project.properties funjs/nbproject/project.xml funjs/public_html/ funjs/public_html/funjs/ funjs/public_html/funjs/Ajax.js funjs/public_html/funjs/Boot.js funjs/public_html/funjs/Class.js funjs/public_html/funjs/Deferred.js funjs/public_html/funjs/HtmlUtils.js funjs/public_html/funjs/Routing.js funjs/public_html/funjs/TemplateEngine.js funjs/public_html/funjs/TemplateScriptReader.js funjs/public_html/hello_tpl.html funjs/public_html/index.html funjs/test/ Property changes on: funjs/nbproject ___________________________________________________________________ Added: svn:ignore + private Added: funjs/nbproject/project.properties =================================================================== --- funjs/nbproject/project.properties (rev 0) +++ funjs/nbproject/project.properties 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,7 @@ +config.folder=${file.reference.FunJs-config} +file.reference.FunJs-config=config +file.reference.FunJs-public_html=public_html +file.reference.FunJs-test=test +files.encoding=UTF-8 +site.root.folder=${file.reference.FunJs-public_html} +test.folder=${file.reference.FunJs-test} Added: funjs/nbproject/project.xml =================================================================== (Binary files differ) Property changes on: funjs/nbproject/project.xml ___________________________________________________________________ Added: svn:mime-type + application/xml Added: funjs/public_html/funjs/Ajax.js =================================================================== Added: funjs/public_html/funjs/Boot.js =================================================================== --- funjs/public_html/funjs/Boot.js (rev 0) +++ funjs/public_html/funjs/Boot.js 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,2 @@ +$scriptReader = new TemplateScriptReader(); +$template = new TemplateEngine(); Added: funjs/public_html/funjs/Class.js =================================================================== --- funjs/public_html/funjs/Class.js (rev 0) +++ funjs/public_html/funjs/Class.js 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,61 @@ +var Class = function() { + this.init && this.init.apply(this, arguments); +}; + +Class.extend = function(proto) { + var parent = this; + + var child = function() { + this.init && this.init.apply(this, arguments); + }; + child.extend = parent.extend; + + var Wrapper = new Function(); + Wrapper.prototype = parent.prototype; + + var wrapper = new Wrapper(); + for (var key in proto) { + wrapper[key] = proto[key]; + } + + child.prototype = wrapper; + child.prototype.super = wrapper.__proto__; + return child; +}; + +function unitTestsClass() { + + A = Class.extend({ + init : function() { + this.yes = "OK"; + }, + + test : function() { + console.log("test A"); + } + }); + + B = A.extend({ + m : function() { + console.log("test B"); + } + }); + + C = B.extend({ + tutu : 3, + + init : function() { + this.super.init(); + this.var = "my var"; + }, + + test : function() { + console.log(this.super.test()); + console.log("test C"); + } + }); + + a = new A(); + b = new B(); + c = new C(); +} Added: funjs/public_html/funjs/Deferred.js =================================================================== --- funjs/public_html/funjs/Deferred.js (rev 0) +++ funjs/public_html/funjs/Deferred.js 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,229 @@ +Deferred = Class.extend({ + RESOLVED : 1, + UNDETERMINED : 0, + REJECTED : -1, + + init : function () { + this.dones = []; + this.fails = []; + this.state = this.UNDETERMINED; + }, + + resolve : function () { + if (this.state) return this; + + this.args = arguments; + this.state = this.RESOLVED; + this._executes(this.dones); + + return this; + }, + + reject : function () { + if (this.state) return this; + + this.args = arguments; + this.state = this.REJECTED; + this._executes(this.fails); + + return this; + }, + + done : function (fn) { + if (this.state == this.RESOLVED) { + this._execute(fn); + } else { + this.dones.push(fn); + } + + return this; + }, + + fail : function (fn) { + if (this.state == this.REJECTED) { + this._execute(fn); + } else { + this.fails.push(fn); + } + return this; + }, + + always : function (fn) { + if (this.state) { + this._execute(fn); + } else { + this.dones.push(fn); + this.fails.push(fn); + } + return this; + }, + + _executes : function(fns) { + for (var i = 0, l = fns.length; i < l; i++) { + var fn = fns.shift(); + if (!this._execute(fn)) { + break; + } + } + }, + + _execute : function (fn) { + var r = fn.apply(this, this.args); + + if (r instanceof Deferred) { + r.dones = this.dones.concat(r.dones); + r.fails = this.fails.concat(r.fails); + + if (r.state == this.RESOLVED) { + r._executes(r.dones); + } else if (r.state == this.REJECTED) { + r._executes(r.fails); + } + return false; + + } else if (typeof r != "undefined") { + this.args = [r]; + } + return true; + } + +}); + +function unitTestsDeferred() { + // Test cases + var success = false; + var sep = "-----------------------------------"; + + console.log("*** Deferred - Cases 0 : resolving"); + var a = new Deferred(); + a.done(function(arg) { + console.log("done", arg); + success = true; + }) + .fail(function() { + success = true; + }); + a.resolve("Test 0"); + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + + console.log("*** Deferred - Cases 1 : rejecting"); + success = false; + var a = new Deferred(); + a.fail(function(arg) { + console.log("done", arg); + success = true; + }) + .done(function() { + success = false; + }); + a.reject("Test 1"); + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + + console.log("*** Deferred - Case 2 : default chaining"); + success = false; + var a = new Deferred(); + a.done(function(arg) { + console.log("done 1", arg); + }) + .done(console.log.bind(console)) + .done(function(arg) { + success = true; + }) + .fail(function(){ + success = false; + }); + a.resolve("Test 2"); + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + + console.log("*** Deferred - Case 3 : always (after resolve)"); + success = false; + var a = new Deferred(); + a.done(console.log.bind(console)) + .fail(console.log.bind(console)) + .always(function() { + success = true; + }); + a.resolve("Test 3"); + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + + console.log("*** Deferred - Case 4 : always (after reject)"); + success = false; + var a = new Deferred(); + a.done(console.log.bind(console)) + .fail(console.log.bind(console)) + .always(function() { + success = true; + }); + a.reject("Test 4"); + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + + console.log("*** Deferred - Case 5 : chaining and passing value"); + success = false; + var a = new Deferred(); + a.done(function(arg) { + return true; + }) + .done(function (arg) { + success = arg; + }); + a.resolve(); + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + + console.log("*** Deferred - Case 6 : chaining and passing deferred"); + success = false; + var a = new Deferred(); + a.done(function(arg) { + return new Deferred().reject(); + }) + .fail(function() { + success = true; + }) + .done(function () { + success = false; + }); + a.resolve(); + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + console.log("*** Deferred - Case 7 : multi calls"); + success = false; + var a = new Deferred(); + a.done(function(arg) { + console.log("done 1"); + }); + a.resolve(); + a.done(function () { + console.log("done 2"); + success = true; + }); + + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + + console.log("*** Deferred - Case 7 : multi calls and different arguments"); + success = false; + var a = new Deferred(); + a.done(function(arg) { + return true; + }); + a.resolve(false); + a.done(function (arg) { + success = arg; + }); + + if (success) console.log("*** Success ***"); else console.log("!!! failure !!!"); + console.log(sep); + +} Added: funjs/public_html/funjs/HtmlUtils.js =================================================================== --- funjs/public_html/funjs/HtmlUtils.js (rev 0) +++ funjs/public_html/funjs/HtmlUtils.js 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,10 @@ +$id = function(id) { + return document.getElementById(id); +}; + +$tag = function(tag) { + return document.getElementsByTagName(tag); +}; + +$log = console.log.bind(console); +$warm = console.warn.bind(console); Added: funjs/public_html/funjs/Routing.js =================================================================== Added: funjs/public_html/funjs/TemplateEngine.js =================================================================== --- funjs/public_html/funjs/TemplateEngine.js (rev 0) +++ funjs/public_html/funjs/TemplateEngine.js 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,17 @@ +TemplateEngine = Class.extend({ + + load : function(scriptId, node) { + return $scriptReader.read(scriptId) + .done(function(content) { + + if (typeof node == "string") { + var element = $id(node); + } else { + element = node; + } + $log(element) + element.innerHTML = content; + }); + } + +}); \ No newline at end of file Added: funjs/public_html/funjs/TemplateScriptReader.js =================================================================== --- funjs/public_html/funjs/TemplateScriptReader.js (rev 0) +++ funjs/public_html/funjs/TemplateScriptReader.js 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,8 @@ +TemplateScriptReader = Class.extend({ + + read : function(scriptId) { + var content = $id(scriptId).textContent; + return new Deferred().resolve(content); + } + +}); Added: funjs/public_html/hello_tpl.html =================================================================== --- funjs/public_html/hello_tpl.html (rev 0) +++ funjs/public_html/hello_tpl.html 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,3 @@ +<div id="hello"> + <p>Hello World!</p> +</div> Added: funjs/public_html/index.html =================================================================== --- funjs/public_html/index.html (rev 0) +++ funjs/public_html/index.html 2014-06-18 13:24:07 UTC (rev 711) @@ -0,0 +1,26 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>Fun Js Application Test</title> + + <script src="funjs/Class.js" type="text/javascript"></script> + <script src="funjs/HtmlUtils.js" type="text/javascript"></script> + <script src="funjs/Deferred.js" type="text/javascript"></script> + <script src="funjs/TemplateScriptReader.js" type="text/javascript"></script> + <script src="funjs/TemplateEngine.js" type="text/javascript"></script> + + <script src="funjs/Boot.js" type="text/javascript"></script> + + <script id="hello" type="text/html"> + <div id="hello"> + <p>Hello World!</p> + </div> + </script> + + </head> + <body> + <div id="body"></div> + <h1>Empty</h1> + </body> +</html>
participants (1)
-
jruchaud@users.nuiton.org