diff --git a/lib/std.js b/lib/std.js index d4c5603..0bbfcf0 100644 --- a/lib/std.js +++ b/lib/std.js @@ -66,12 +66,12 @@ if (!Enumerator.prototype.toArray) { // Global APIs ///////////////////////////////////////////////////////////////////////////////// -global.GetResource = function(ResourceName) { +function GetResource(ResourceName) { return arguments.callee.caller.GetResource(ResourceName); } // [lib/std] the time of `sleep()' function is not accuracy #34 -global.sleep = function(ms, callback) { +function sleep(ms, callback) { var handler = null; var cur = Date.now(); @@ -99,32 +99,78 @@ global.sleep = function(ms, callback) { return { 'ms': end, 'handler': handler }; }; -global.doRepeat = function(ms, callback, error) { - var handler = null; +function repeat(target, callback, onError) { + switch (typeof target) { + case "number": + case "boolean": + var ms = target; + var handler = null; + var cur = Date.now(); + var end = cur + ms; - var cur = Date.now(); - var end = cur + ms; - - if (typeof WScript !== "undefined") { - while (ms === true ? true : (cur < end)) { - try { - sleep(callback()); - } catch (e) { - error(e); + if (typeof WScript !== "undefined") { + while (ms === true ? true : (cur < end)) { + try { + if (typeof callback === "function") + callback(cur) + ; + } catch (e) { + if (typeof onError === "function") + onError(e) + ; + } + cur = Date.now(); + } + end = Date.now(); + } else if (typeof window !== "undefined") { + if (typeof callback === "function") + handler = setInterval(callback, ms); + ; } - cur = Date.now(); - } - end = Date.now(); - } else if (typeof window !== "undefined") { - if (typeof callback === "function") - handler = setInterval(callback, ms); - ; - } - return { 'ms': end, 'handler': handler }; + return { 'ms': end, 'handler': handler }; + + case "object": + var arr = target; + for (var i = 0; i < arr.length; i++) { + try { + if (typeof callback === "function") + callback(i, arr[i]) + ; + } catch (e) { + if (typeof onError === "function") + onError(e) + ; + } + } + break; + } }; -global.range = function() { +function rotate(target, callback, onError) { + var arr = target; + var i = 0; + var stop = false; + + while (!stop) { + try { + if (typeof callback === "function") { + stop = callback(i, arr[i]); + } else { + stop = true; + } + } catch (e) { + if (typeof onError === "function") + stop = onError(e); + ; + } + + i++; + i = i % keywords.length; + } +}; + +function range() { var args = arguments; var N = [], start, end, step; @@ -155,7 +201,7 @@ global.range = function() { return N; }; -global.CHR = function(ord) { +function CHR(ord) { return String.fromCharCode(ord); }; @@ -167,7 +213,7 @@ global.CHR = function(ord) { // Emulate Server.CreateObject ///////////////////////////////////////////////////////////////////////////////// -exports.CreateObject = function(progId, serverName, callback) { +function CreateObject(progId, serverName, callback) { var progIds = []; var _CreateObject = function(p, s) { if (typeof(WScript) !== "undefined") { @@ -196,42 +242,22 @@ exports.CreateObject = function(progId, serverName, callback) { } }; +///////////////////////////////////////////////////////////////////////////////// +// Standard Event Object ///////////////////////////////////////////////////////////////////////////////// -var StdEvent = function(eventName) { - this.bubbles = false; // Not supported - this.cancelable = false; // Not supported - this.composed = false; // Not supported - this.currentTarget = null; // Not supported +function StdEvent(eventName) { this.defaultPrevented = false; - this.eventPhase = null; // TODO - this.isTrusted = true; // Not supported this.timeStamp = new Date(); - this.eventName = eventName; this.target = null; - // Not supported - this.composedPath = function() { - return null; - }; - this.preventDefault = function() { this.defaultPrevented = true; }; - - // Not supported - this.stopImmediatePropagation = function() { - return null; - }; - - // Not supported - this.setPropagation = function() { - return null; - }; }; -var StdEventableObject = function() { +function StdEventableObject() { this.dispatchEvent = function(event) { event.target = this; if(('on' + event.eventName) in this) this['on' + event.eventName](event); @@ -246,6 +272,13 @@ var StdEventableObject = function() { }; }; +global.GetResource = GetResource; +global.sleep = sleep; +global.repeat = repeat; +global.rotate = rotate; +global.range = range; +global.CHR = CHR; + exports.VERSIONINFO = "Standard Lib (std.js) version 0.4"; exports.global = global; exports.require = global.require;