forked from jeresig/env-js
/
window.js
563 lines (501 loc) · 14.7 KB
/
window.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**
* @todo: document
*/
var Window,
Screen,
History,
Navigator,
__this__ = this;
/*
* Envjs window.1.2.0.0
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
*/
(function(){
/**
* @author john resig
*/
// Helper method for extending one object with another.
function __extend__(a,b) {
for ( var i in b ) {
var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
if ( g || s ) {
if ( g ) a.__defineGetter__(i, g);
if ( s ) a.__defineSetter__(i, s);
} else
a[i] = b[i];
} return a;
};
/**
* @todo: document
*/
__extend__(HTMLFrameElement.prototype,{
get contentDocument(){
return this.contentWindow?
this.contentWindow.document:
null;
},
set src(value){
var event;
this.setAttribute('src', value);
if (value && value.length > 0){
//console.log('loading frame %s', value);
Envjs.loadFrame(this, Envjs.location(value));
//console.log('event frame load %s', value);
event = this.ownerDocument.createEvent('HTMLEvents');
event.initEvent("load", false, false);
this.dispatchEvent( event, false );
}
}
});
/*
* history.js
*
*/
History = function(owner){
var $current = 0,
$history = [null],
$owner = owner;
return {
get length(){
return $history.length;
},
back : function(count){
if(count){
go(-count);
}else{
go(-1);
}
},
get current(){
return this.item($current);
},
get previous(){
return this.item($current-1);
},
forward : function(count){
if(count){
go(count);
}else{go(1);}
},
go : function(target){
if(typeof target == "number"){
target = $current + target;
if(target > -1 && target < $history.length){
if($history[target].type == "hash"){
if($owner.location){
$owner.location.hash = $history[target].value;
}
}else{
if($owner.location){
$owner.location = $history[target].value;
}
}
$current = target;
}
}else{
//TODO: walk through the history and find the 'best match'?
}
},
item: function(index){
if(index < history.length)
return $history[index];
else
return null;
},
add: function(newLocation, type){
//not a standard interface, we expose it to simplify
//history state modifications
if(newLocation !== $history[$current]){
$history.slice(0, $current);
$history.push({
type: type||"href",
value: value
});
}
}
};
};
/*
* navigator.js
* Browser Navigator
*/
Navigator = function(){
return {
get appCodeName(){
return Envjs.appCodeName;
},
get appName(){
return Envjs.appName;
},
get appVersion(){
return Envjs.version +" ("+
this.platform +"; "+
"U; "+//?
Envjs.os_name+" "+Envjs.os_arch+" "+Envjs.os_version+"; "+
Envjs.lang+"; "+
"rv:"+Envjs.revision+
")";
},
get cookieEnabled(){
return true;
},
get mimeTypes(){
return [];
},
get platform(){
return Envjs.platform;
},
get plugins(){
return [];
},
get userAgent(){
return this.appCodeName + "/" + this.appVersion + " " + this.appName;
},
javaEnabled : function(){
return Envjs.javaEnabled;
}
};
};
/**
* Screen
* @param {Object} __window__
*/
Screen = function(__window__){
var $availHeight = 600,
$availWidth = 800,
$colorDepth = 16,
$pixelDepth = 24,
$height = 600,
$width = 800;
$top = 0;
$left = 0;
$availTop = 0;
$availLeft = 0;
__extend__( __window__, {
moveBy : function(dx,dy){
//TODO - modify $locals to reflect change
},
moveTo : function(x,y) {
//TODO - modify $locals to reflect change
},
/*print : function(){
//TODO - good global to modify to ensure print is not misused
};*/
resizeBy : function(dw, dh){
__window__resizeTo($width + dw, $height + dh);
},
resizeTo : function(width, height){
$width = (width <= $availWidth) ? width : $availWidth;
$height = (height <= $availHeight) ? height : $availHeight;
},
scroll : function(x,y){
//TODO - modify $locals to reflect change
},
scrollBy : function(dx, dy){
//TODO - modify $locals to reflect change
},
scrollTo : function(x,y){
//TODO - modify $locals to reflect change
}
});
return {
get top(){
return $top;
},
get left(){
return $left;
},
get availTop(){
return $availTop;
},
get availLeft(){
return $availleft;
},
get availHeight(){
return $availHeight;
},
get availWidth(){
return $availWidth;
},
get colorDepth(){
return $colorDepth;
},
get pixelDepth(){
return $pixelDepth;
},
get height(){
return $height;
},
get width(){
return $width;
}
};
};
//These descriptions of window properties are taken loosely David Flanagan's
//'JavaScript - The Definitive Guide' (O'Reilly)
/**
* Window
* @param {Object} scope
* @param {Object} parent
* @param {Object} opener
*/
Window = function(scope, parent, opener){
// the window property is identical to the self property and to this obj
var proxy = new Envjs.proxy(scope, parent);
scope.__proxy__ = proxy;
scope.__defineGetter__('window', function(){
return proxy;
});
var $uuid = new Date().getTime()+'-'+Math.floor(Math.random()*1000000000000000);
__windows__[$uuid] = scope;
//console.log('opening window %s', $uuid);
// every window has one-and-only-one .document property which is always
// an [object HTMLDocument]. also, only window.document objects are
// html documents, all other documents created by the window.document are
// [object XMLDocument]
var $htmlImplementation = new DOMImplementation();
$htmlImplementation.namespaceAware = true;
$htmlImplementation.errorChecking = false;
// read only reference to the Document object
var $document = new HTMLDocument($htmlImplementation);
//The version of this application
var $version = "0.1";
//This should be hooked to git or svn or whatever
var $revision = "0.0.0.0";
// A read-only reference to the Window object that contains this window
// or frame. If the window is a top-level window, parent refers to
// the window itself. If this window is a frame, this property refers
// to the window or frame that contains it.
var $parent = parent;
/**> $cookies - see cookie.js <*/
// read only boolean specifies whether the window has been closed
var $closed = false;
// a read/write string that specifies the default message that
// appears in the status line
var $defaultStatus = "Done";
// IE only, refers to the most recent event object - this maybe be
// removed after review
var $event = null;
// a read-only reference to the History object
var $history = new History();
// a read-only reference to the Location object. the location object does
// expose read/write properties
var $location = new Location('about:blank', $document, $history);
// The name of window/frame. Set directly, when using open(), or in frameset.
// May be used when specifying the target attribute of links
var $name = null;
// a read-only reference to the Navigator object
var $navigator = new Navigator();
// a read/write reference to the Window object that contained the script
// that called open() to open this browser window. This property is valid
// only for top-level window objects.
var $opener = opener?opener:null;
// read-only properties that specify the height and width, in pixels
var $innerHeight = 600, $innerWidth = 800;
// Read-only properties that specify the total height and width, in pixels,
// of the browser window. These dimensions include the height and width of
// the menu bar, toolbars, scrollbars, window borders and so on. These
// properties are not supported by IE and IE offers no alternative
// properties;
var $outerHeight = $innerHeight,
$outerWidth = $innerWidth;
// Read-only properties that specify the number of pixels that the current
// document has been scrolled to the right and down. These are not
// supported by IE.
var $pageXOffset = 0, $pageYOffset = 0;
// a read-only reference to the Screen object that specifies information
// about the screen: the number of available pixels and the number of
// available colors.
var $screen = new Screen(scope);
// read only properties that specify the coordinates of the upper-left
// corner of the screen.
var $screenX = 1,
$screenY = 1;
var $screenLeft = $screenX,
$screenTop = $screenY;
// a read/write string that specifies the current status line.
var $status = '';
__extend__(scope, EventTarget.prototype);
return __extend__( scope, {
get closed(){
return $closed;
},
get defaultStatus(){
return $defaultStatus;
},
set defaultStatus(defaultStatus){
$defaultStatus = defaultStatus;
},
get document(){
return $document;
},
set document(doc){
$document = doc;
},
/*
deprecated ie specific property probably not good to support
get event(){
return $event;
},
*/
get frames(){
// A read-only array of window objects
if(HTMLCollection){
return new HTMLCollection($document.getElementsByTagName('frame'));
}else{
return __setArray__({}, []);
}
},
get length(){
// should be frames.length,
return this.frames.length;
},
get history(){
return $history;
},
get innerHeight(){
return $innerHeight;
},
get innerWidth(){
return $innerWidth;
},
get clientHeight(){
return $innerHeight;
},
get clientWidth(){
return $innerWidth;
},
get location(){
return $location;
},
set location(uri){
if($location.href == uri){
$location.reload();
}else if($location.href == 'about:blank'){
$location.assign(uri);
}else{
$location.replace(uri);
}
},
get name(){
return $name;
},
set name(newName){
$name = newName;
},
get navigator(){
return $navigator;
},
get opener(){
return $opener;
},
get outerHeight(){
return $outerHeight;
},
get outerWidth(){
return $outerWidth;
},
get pageXOffest(){
return $pageXOffset;
},
get pageYOffset(){
return $pageYOffset;
},
get parent(){
return $parent;
},
get screen(){
return $screen;
},
get screenLeft(){
return $screenLeft;
},
get screenTop(){
return $screenTop;
},
get screenX(){
return $screenX;
},
get screenY(){
return $screenY;
},
get self(){
return scope;
},
get status(){
return $status;
},
set status(status){
$status = status;
},
// a read-only reference to the top-level window that contains this window.
// If this window is a top-level window it is simply a reference to itself.
// If this window is a frame, the top property refers to the top-level
// window that contains the frame.
get top(){
var _parent = $parent;
while(scope && _parent && scope !== _parent){
if(_parent === _parent.parent)break;
_parent = _parent.parent;
//console.log('scope %s _parent %s', scope, _parent);
}
return _parent || null;
},
get window(){
return proxy;
},
toString : function(){
return '[Window]';
},
getComputedStyle : function(element, pseudoElement){
if(CSS2Properties){
return element?
element.style:new CSS2Properties({cssText:""});
}
},
open: function(url, name, features, replace){
if (features)
console.log("'features argument not yet implemented");
var _window = {},
open;
if(replace && name){
for(open in __windows__){
if(open.name === name)
_window = open;
}
}
new Window(_window, _window, this);
if(name)
_window.name = name;
_window.document.async = false;
_window.location.assign(url);
return _window;
},
close: function(){
delete __windows__[$uuid];
},
alert : function(message){
Envjs.alert(message);
},
confirm : function(question){
Envjs.confirm(question);
},
prompt : function(message, defaultMsg){
Envjs.prompt(message, defaultMsg);
},
onload: function(){},
onunload: function(){}
});
};
var __windows__ = {};
//finally pre-supply the window with the window-like environment
new Window(__this__, __this__);
/**
* @author john resig & the envjs team
* @uri http://www.envjs.com/
* @copyright 2008-2010
* @license MIT
*/
})();