This repository has been archived by the owner on May 20, 2019. It is now read-only.
forked from bmatusiak/apf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapf-o3.js
480 lines (409 loc) · 13.7 KB
/
apf-o3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*
*/
//#ifdef __SUPPORT_O3
/**
* Ajax.org Platform for O3
*
* @author Ruben Daniels [email protected]
* @version 3.0
* @url http://www.ajax.org
*/
//Start of the Ajax.org Platform namespace
apf = {
SUCCESS : 1,
TIMEOUT : 2,
ERROR : 3,
OFFLINE : 4,
//#ifdef __DEBUG
debug : true,
debugType : "Memory",
debugFilter : "!teleport",
/* #else
debug : false,
#endif */
initialized : false,
crypto : {}, //namespace
_GET : {},
basePath : "./",
$asyncObjects : {},
isO3 : true,
supportNamespaces: true,
basePath : "",
verbose : 3,
/**
* {Object} contains several known and often used namespace URI's.
* @private
*
*/
ns : {
apf : "https://github.com/pylonide/pylon",
aml : "https://github.com/pylonide/pylon",
xsd : "http://www.w3.org/2001/XMLSchema",
xhtml : "http://www.w3.org/1999/xhtml",
xslt : "http://www.w3.org/1999/XSL/Transform",
xforms : "http://www.w3.org/2002/xforms",
ev : "http://www.w3.org/2001/xml-events"
},
start : function(strAml){
this.host = null;
this.hostPath = null;
this.CWD = null;
this.TAGNAME = "localName";
this.oHttp = new apf.http();
apf.runO3();
this.implement(apf.Class);
//if (strAml)
apf.window.init(strAml || "<a:application xmlns:a='https://github.com/pylonide/pylon' />");
},
importClass : function(ref, strip, win){
if (!ref)
throw new Error(apf.formatErrorString(1018, null,
"importing class",
"Could not load reference. Reference is null"));
if (!strip)
return apf.jsexec(ref.toString());
var q = ref.toString().replace(/^\s*function\s*\w*\s*\([^\)]*\)\s*\{/, "");
q = q.replace(/\}\s*$/, "");
//var q = ref.toString().split("\n");q.shift();q.pop();
//if(!win.execScript) q.shift();q.pop();
return apf.jsexec(q);
},
/**
* This method returns a string representation of the object
* @return {String} Returns a string representing the object.
* @method
*/
toString : function(){
return "[Ajax.org Platform (apf)]";
},
all : [],
/**
* This method implements all traits of another class to this object
* @param {Function} classRef Required Class reference
* @method
*/
implement : function(classRef){
for (var arg, i = 0, l = arguments.length; i < l; i++) {
arg = arguments[i]
//#ifdef __DEBUG
if (!arg) {
throw new Error(apf.formatErrorString(0, this,
"Implementing class",
"Could not implement from '" + classRef + "'",
this.$aml));
}
//#endif
arg.call(this);//classRef
}
return this;
},
lookup : function(uniqueId){
return this.all[uniqueId];
},
/**
* Set reference to an object by name
*/
setReference : function(name, o, global){
if (self[name] && self[name].hasFeature) return 0;
return (self[name] = o);
},
/**
* Extends an object with one or more other objects by copying all their
* properties.
* @param {Object} dest the destination object.
* @param {Object} src the object that is copies from.
* @return {Object} the destination object.
*/
extend : function(dest, src){
var prop, i, x = !dest.notNull;
if (arguments.length == 2) {
for (prop in src) {
if (x || src[prop])
dest[prop] = src[prop];
}
return dest;
}
for (i = 1; i < arguments.length; i++) {
src = arguments[i];
for (prop in src) {
if (x || src[prop])
dest[prop] = src[prop];
}
}
return dest;
},
/**
* The console outputs to the debug screen
*/
console : {
//#ifdef __DEBUG
write : function(msg, type, subtype, data, forceWin, nodate){
if (!Number.prototype.toPrettyDigit) {
Number.prototype.toPrettyDigit = function() {
var n = this.toString();
return (n.length == 1) ? "0" + n : n;
}
}
var dt = new Date(),
ms = String(dt.getMilliseconds());
while (ms.length < 3)
ms += "0";
var date = dt.getHours().toPrettyDigit() + ":"
+ dt.getMinutes().toPrettyDigit() + ":"
+ dt.getSeconds().toPrettyDigit() + "." + ms;
util.puts((nodate ? "" : date) + " " + msg + (data ? "Extra information:\n" + data : ""));
},
//#endif
debug : function(){
},
time : function(msg, subtype, data){
//#ifdef __DEBUG
this.write(msg, "time", subtype, data);
//#endif
},
log : function(msg, subtype, data){
//#ifdef __DEBUG
this.info(msg, subtype, data);
//#endif
},
info : function(msg, subtype, data){
//#ifdef __DEBUG
this.write(msg, "info", subtype, data);
//#endif
},
warn : function(msg, subtype, data){
//#ifdef __DEBUG
this.write(msg, "warn", subtype, data);
//#endif
},
error : function(msg, subtype, data){
//#ifdef __DEBUG
this.write("Error "+ "\033[35m"+msg+"\033[39m", "error",subtype,data);
//this.write("Error: " + msg + "\nStacktrace:\n" + new Error().stack, "error", subtype, data);
//#endif
},
dir : function(obj){
this.info(apf.vardump(obj, null, true));
},
teleport: function() {}
},
namespace : function(name, oNamespace){
eval("apf." + name + " = oNamespace");
},
formatErrorString : function(number, control, process, message, amlContext, outputname, output){
//#ifdef __DEBUG
var str = ["---- APF Error ----"];
if (amlContext) {
var amlStr = (amlContext.outerHTML || amlContext.xml || amlContext.serialize())
.replace(/\<\?xml\:namespace prefix = j ns = "http\:\/\/ajax.org\/2005\/aml" \/\>/g, "")
.replace(/xmlns:a="[^"]*"\s*/g, "");
}
if (control)
str.push("Control: '"
+ (control.name
|| (control.$aml ? control.getAttribute("id") : null)
|| "{Anonymous}")
+ "' [" + control.tagName + "]");
if (process)
str.push("Process: " + process.replace(/ +/g, " "));
if (message)
str.push("Message: [" + number + "] " + message.replace(/ +/g, " "));
if (outputname)
str.push(outputname + ": " + output);
if (amlContext)
str.push("\n===\n" + amlStr);
return str.join("\n");
//#endif
},
namespaces : {},
setNamespace : function(namespaceURI, oNamespace){
this.namespaces[namespaceURI] = oNamespace;
oNamespace.namespaceURI = namespaceURI;
},
/* Init */
/**
* Returns the directory portion of a url
* @param {String} url the url to retrieve from.
* @return {String} the directory portion of a url.
*/
getDirname : function(url){
return ((url || "").match(/^([^#]*\/)[^\/]*(?:$|\#)/) || {})[1]; //Mike will check out how to optimize this line
},
/**
* Returns the file portion of a url
* @param {String} url the url to retrieve from.
* @return {String} the file portion of a url.
*/
getFilename : function(url){
return ((url || "").split("?")[0].match(/(?:\/|^)([^\/]+)$/) || {})[1];
},
/**
* Returns an absolute url based on url.
* @param {String} base the start of the url to which relative url's work.
* @param {String} url the url to transform.
* @return {String} the absolute url.
*/
getAbsolutePath : function(base, url){
return !url || !base || url.match(/^\w+\:\/\//) ? url : base.replace(/\/$/, "") + "/" + url;
},
include : function(sourceFile, doBase){
if (doBase) {
var base = apf.basePath || "";
if (sourceFile.indexOf(base) !== 0)
sourceFile = base + sourceFile;
}
// apf.console.info("including js file: " + sourceFile);
//o3.js.include(sourceFile);
require("./" + sourceFile.replace(/\.js$/i, ""));
//var fd = fs.child(sourceFile);
//eval(fd.data, self);
},
$loader : {
setGlobalDefaults : function(){
},
script : function(){
for (var i = 0; i < arguments.length; i++) {
apf.include(arguments[i], true);
}
return this;
},
wait : function(f){
if (f) f();
return this;
}
},
Init : {
add : function(func, o){
func.call(o);
},
addConditional : function(func, o, strObj){
return func.call(o);
},
run : function(strObj){
}
},
unload : function(exclude){
var node,
i = 0,
l = this.all.length;
for (; i < l; i++) {
node = this.all[i];
if (node && node != exclude && node.destroy && !node.apf)
node.destroy(false);
}
if (apf.xmldb)
apf.xmldb.unbind(apf.window);
}
};
Function.prototype.toHTMLNode = function(highlight){
var code, line1, endLine1, line2, res;
TYPE_OBJECT = "Object";
TYPE_NUMBER = "Number";
TYPE_STRING = "String";
TYPE_ARRAY = "Array";
TYPE_DATE = "Date";
TYPE_REGEXP = "RegExp";
TYPE_BOOLEAN = "Boolean";
TYPE_FUNCTION = "Function";
TYPE_DOMNODE = "XMLNode";
TYPE_APFNODE = "AMLElement";
STATE_UNDEFINED = "undefined";
STATE_NULL = "null";
STATE_NAN = "NaN";
STATE_INFINITE = "infinite";
/**
* @private
*/
function getType(variable){
if (variable === null)
return STATE_NULL;
if (variable === undefined)
return STATE_UNDEFINED;
if (typeof variable == "number" && isNaN(variable))
return STATE_NAN;
if (typeof variable == "number" && !isFinite(variable))
return STATE_INFINITE;
if (typeof variable == "object") {
if (variable.hasFeature)
return TYPE_APFNODE;
if (variable.tagName || variable.nodeValue)
return TYPE_DOMNODE;
}
if (typeof variable.dataType == "undefined")
return TYPE_OBJECT;
return variable.dataType;
}
//anonymous
code = this.toString();
endLine1 = code.indexOf("\n");
line1 = code.slice(0, endLine1);
line2 = code.slice(endLine1+1);
res = /^function(\s+(.*?)\s*|\s*?)\((.*)\)(.*)$/.exec(line1);
if (res) {
var name = res[1];
var args = res[3];
var last = res[4]; //NOT USED?
if (this.arguments) {
var argName, namedArgs = args.split(",");
args = [];
for (var i = 0; i < this.arguments.length; i++) {
//if(i != 0 && arr[i]) args += ", ";
argName = (namedArgs[i] || "NOT_NAMED").trim();// args += "<b>" + arr[i] + "</b>";
args.push("[" + getType(this.arguments[i]) + "] " + argName);
//info.push("Value: " + apf.vardump(this.arguments[i], null, false));
}
}
return " " + (name && name.trim() || "anonymous") + " (" + args.join(", ") + ")";
}
else {
return this.toString();
}
};
$setTimeout = setTimeout;
$setInterval = setInterval;
apf.stacktrace = function(){
var list = [], seen = {}, loop, end;
//Opera doesnt support caller... weird...
//try {
loop = end = arguments.callee.caller.caller
? arguments.callee.caller.caller.caller
: arguments.callee.caller.caller;
if (loop) {
//try {
do {
if (seen[loop.toString()])
break; //recursion checker
seen[loop.toString()] = true;
//str += loop.toHTML();
list.push(loop.toHTMLNode());
loop = loop.caller;
}
while (list.length < 30 && loop && loop.caller && loop.caller.caller != loop);
//}
//catch(a) {
// list=[];
//}
}
//}
//catch(e){}
return list.join("\n");
};
//#endif