/
phos.js
110 lines (99 loc) · 4.11 KB
/
phos.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
////////////////////////////////////////////////////////////////////////////////////////////////////
// phos.js
//
// Copyright (C) 2009,2010 David J. Goehrig
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Phos
// This library simulates a very simple cross-platform computing system. It effectively hides
// the implementation of the browser interface from the higher level code. You can think of it
// as a Hardware Abstraction Layer for your web browser. It provides Javascript objects which
// represent the various hardware devices that you might want to use, as well as, network
// resources. It is designed around the concept of a simple object oriented statemachine.
//
function $(x) { return document.getElementById(x) }
function $_(x) { return document.createElement(x) }
document.goto = function(x) { document.location.href = x }
var _doc = document;
var _root = window;
var _body = null;
var here; // the last mouse position
var that; // the last thing selected
var these; // the targets of a selection
var editing; // the block we're editing
var clipboard; // the last object we cut or copied
var A;
var An = A = {
object: function() { return a({}) },
string: function() { return '' },
array: function() { return [] },
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Event Functions
_doc.onkeypress = function() { return false }; // Hack to break backspace
_doc.oncontextmenu = function(e) { return false }; // Hack to remove popup menu
_root.onresize = function() { _doc.location.href = _doc.location.href };
function nop() {}
// Object Prototype Extensions necessary for bootstrapping
Object.prototype.request = function(method,url,cb,data) {
var _request = XMLHttpRequest ? new XMLHttpRequest(): _doc.createRequest();
_request.onreadystatechange = function () {
if (this.readyState != 4 || typeof(cb) != "function") return;
if (this.status == 404) cb(null);
if (this.status == 200) cb(this.responseText);
};
_request.open(method,url,true);
_request.setRequestHeader('Content-Type','application/x-www-from-urlencoded');
_request.send(data ? data : '');
return this;
}
Object.prototype.get = function(url,cb) { return this.request("GET",url,cb) }
Object.prototype.use = function() {
var modules = [];
modules.push.apply(modules,arguments);
var module = modules.shift();
var url = '/object/' + module;
var cb = function(txt) {
if (!txt) return;
try {
eval('( function () { ' + txt + ' } )')();
} catch(e) {
alert('Load error: ' + e + ':' + txt); ;
}
if (modules.length) {
var module = modules.shift();
var url = '/object/' + module;
if (module) this.get(url,cb);
}
};
return this.get(url,cb)
}
_root.onload = function() {
_body = _doc.getElementsByTagName('body')[0];
use('Object','Array','String','Box','Widget','Component','Event','Device','Resource',
'Display','Keyboard','Mouse','Screen','Text','Sound','Image','Movie',
'Help','Inventory','Trash','Objects','Mirror','Phosphor',
'Names','HotKey','Block',
'Graphic','Rectangle','Circle',
'Blog','Search','App',
navigator.userAgent.match(/AppleWebKit\/534/) ? 'WebKit' :
navigator.userAgent.match(/Safari/) ? 'Safari' :
navigator.userAgent.match(/Chrome/) ? 'Chrome' :
navigator.userAgent.match(/Firefox/) ? 'Firefox' :
'Opera');
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// End