Skip to content

Commit

Permalink
fixed shitweasel ref
Browse files Browse the repository at this point in the history
  • Loading branch information
cthulhuology committed Jul 4, 2009
1 parent 40e89da commit 94bc4ac
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 94 deletions.
73 changes: 14 additions & 59 deletions js/phos.js
Expand Up @@ -28,6 +28,7 @@
// Event Functions
document.onkeypress = function() { return false }; // Hack to break backspace
document.oncontextmenu = function(e) { return false }; // Hack to remove popup menu
window.onresize = function() { document.location.href = document.location.href };

var _doc = document;
var _root = window;
Expand All @@ -37,12 +38,11 @@ function nop() {}

function boot() {
_body = document.getElementsByTagName('body')[0];
if (navigator.userAgent.contains('Firefox')) use('/js/shitweasel.js');
Display.init();
Keyboard.init();
Mouse.init();
Screen.init();
navigator.userAgent.contains('Firefox') ? use('js/shitwasel.js') : Objects.init();
navigator.userAgent.contains('Firefox') ? use('js/shitweasel.js') : Objects.init();
App.run();
}

Expand All @@ -55,57 +55,11 @@ var Objects = let({
}
});

////////////////////////////////////////////////////////////////////////////////////////////////////
// Network Functions
Object.prototype.request = function(cb) {
this._request = XMLHttpRequest ? new XMLHttpRequest(): _doc.createRequest();
this._request.onreadystatechange = function () {
if (this.readyState != 4 || typeof(cb) != "function") return;
if (this.status == 404) cb(null);
if (this.status == 200) cb(this.responseText);
};
return this._request;
}
Object.prototype.post = function(url,cb) {
var data = this.toString();
this.request(cb);
this._request.open("POST",url,true);
this._request.setRequestHeader('Content-Type','appliaction/x-www-from-urlencoded');
this._request.send(data);
return this;
}
Object.prototype.get = function(url,cb) {
this.request(function(txt) { if (typeof(cb) == "function") cb(txt) });
this._request.open("GET",url,true);
this._request.send("");
return this;
};
Object.prototype.download = function() {
document.location.href = "data:application/json,".append(this.toString().encode());
return this;
}

var loaded = [];
Object.prototype.use = function() {
var urls = [];
for (var i = 0; i < arguments.length; ++i) urls[i] = arguments[i];
var url = urls.shift();
var cb = function(txt) {
if (!txt) alert('Failed to load '.append(url));
try {
eval('('.append(txt,')'))
var url = urls.shift();
if (url) get(url,cb);
} catch(e) { alert('Load error: '.append(e,':',txt)) }
};
return this.get(url,cb);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Box Object
var Box = Boxes = let({
x: 0, y: 0, w: 0, h: 0,
init: function() { return this.clone() },
init: function() { return this.clone().copy({x:0,y:0,w:0,h:0}) },
on: function(o) {
var x = o.x ? o.x : 0;
var y = o.y ? o.y : 0;
Expand Down Expand Up @@ -146,6 +100,11 @@ var Box = Boxes = let({
this.y = Math.min(h - this.h,this.y);
return this;
},
towards: function(x,y) {
var dx = x - this.x;
var dy = y - this.y;
return this.to(dx ? dx/Math.abs(dx): 0,dy ? dy/Math.abs(dy) : 0);
},
});

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -154,7 +113,7 @@ var Widget = Widgets = let(Box, {
visible: true,
draw: function() {}, // Override to draw
tick: function() {}, // Override to update based on time
init: function() { return this.clone().instance() }, // Override to initialize
init: function() { return this.clone().copy({visible:true}).instance() }, // Override to init
free: function() { return this.remove() }, // Override this method for custom code
remove: function() {
App.widgets.except(this);
Expand All @@ -163,7 +122,7 @@ var Widget = Widgets = let(Box, {
instance: function() {
App.widgets.push(this);
return this;
},
},
add : function(o) {
if (!this.children) return;
this.children.push(o);
Expand Down Expand Up @@ -209,7 +168,6 @@ var Display = let(Widget, {
this.canvas = $('canvas');
if (this.canvas) return this.at(0,0).by(this.canvas.width,this.canvas.height).instance();
return this.at(0,0).by(window.innerWidth, window.innerHeight).create().instance();

},
up: function(e) { this.moving = false },
down: function(e) { if (!e.overlaps([Display])) this.moving = e },
Expand Down Expand Up @@ -336,7 +294,6 @@ var Screen = let(Box,{
////////////////////////////////////////////////////////////////////////////////////////////////////
// Event Object
var Event = Events = let(Box,{
key: 0,
init: function(e) {
return Event.clone().copy({
button: e.button,
Expand Down Expand Up @@ -421,20 +378,20 @@ var Mouse = let(Device, {
////////////////////////////////////////////////////////////////////////////////////////////////////
// App Object
var App = let(Device, {
delay: 40,
widgets: [],
delay: 40,
run: function () {
Screen.clear();
this.dispatch('tick',{}).dispatch('draw',{});
this.timer = setTimeout("App.run()",this.delay);
},
fps: function() { return Math.floor(1000/this.delay) },
});

////////////////////////////////////////////////////////////////////////////////////////////////////
// Resource Object
var Resource = Resources = let(Box,{
loaded: false,
init: function() { return Resource.clone() },
init: function() { return Resource.clone().copy({loaded:false}) },
load: function(t,i,cb) {
var $self = this;
$self.data = $_(t);
Expand Down Expand Up @@ -469,10 +426,8 @@ var Image = Images = let(Widget,Resource, {
////////////////////////////////////////////////////////////////////////////////////////////////////
// Movie Object
var Movie = Movies = let(Widget,Resource,{
div: $_('div'),
attached: false,
init: function(name) {
var i = this.clone();
var i = this.clone().copy({attached:false, div: $_('div')});
i.load('video',name,function($self) {
if ($self.attached) return;
$self.attached = true;
Expand Down
2 changes: 2 additions & 0 deletions js/phosphor-app.js
Expand Up @@ -66,6 +66,8 @@ var YouTube = let(Widget,{
this.d.style.top = this.y;
this.d.style.left = this.x;
},
play: function() { },
pause: function() { },
free: function() { _body.removeChild(this.d); }
});

Expand Down
15 changes: 4 additions & 11 deletions js/phosphor-widgets.js
Expand Up @@ -38,7 +38,7 @@ var Rectangle = Rectangles = let(Graphic,{
////////////////////////////////////////////////////////////////////////////////////////////////////
// Circle
var Circle = Circles = let(Graphic,{
rad: 20,
init: function() { return this.clone().radius(20).instance() },
draw: function() {
var r = Screen.rad;
if(this.visible) Screen.color(this.r,this.g,this.b).radius(this.rad).at(this.x+this.rad,this.y+this.rad).circle().radius(r).white() },
Expand All @@ -52,9 +52,7 @@ var Circle = Circles = let(Graphic,{
////////////////////////////////////////////////////////////////////////////////////////////////////
// Text
var Text = Texts = let(Widget,{
print: function(p) {
Screen.color(this.r,this.g,this.b).at(this.x,this.y).print(p).white();
}
print: function(p) { Screen.color(this.r,this.g,this.b).at(this.x,this.y).print(p).white() }
});

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -66,10 +64,7 @@ var Drawing = Drawings = let(Widget,{
////////////////////////////////////////////////////////////////////////////////////////////////////
// Picker
var Picker = let(Circle,{
init: function() {
return this.clone().instance().radius(128);
},
gamma: 255, cr: 0, cb: 0,
init: function() { return this.clone().copy({ gamma: 255, cr: 0, cb: 0 }).instance().radius(128) },
move: function(e) {
this.cr = Math.min(255,Math.max(0,e.x - this.x))/255;
this.cb = Math.min(255,Math.max(0,e.y - this.y))/255;
Expand All @@ -78,7 +73,5 @@ var Picker = let(Circle,{
this.color(Math.floor(this.gamma*this.cr),
Math.floor(this.gamma*this.cg),Math.floor(this.gamma*this.cb));
},
down: function(e) {
if (e.on(this) && that.can('color')) that.color(this.r,this.g,this.b);
},
down: function(e) { if (e.on(this) && that.can('color')) that.color(this.r,this.g,this.b) },
});
40 changes: 17 additions & 23 deletions js/phosphor.js
Expand Up @@ -32,7 +32,7 @@ Object.prototype.display = function(x,y) {
var w = [];
var $self = this;
this.each(function(v,k) {
if (!k || !v) return;
if (!k || !typeof(v)) return;
w.push(a(Block).says(v.parameters ? k + v.parameters() : k).at(x,y).by(200,20).copy({childof: $self }));
y += 28;
});
Expand Down Expand Up @@ -68,23 +68,22 @@ Widget.down = function(e) {
this.contents = this.contents ? this.contents.collapse() : this.display(this.x,this.y+this.h)
};
Widget.move = function(e) {
if (!this.moving) return;
var dx = e.x - this.moving.x;
var dy = e.y - this.moving.y;
this.to(dx,dy);
if (this.parent) this.clamp(this.parent.x,this.parent.y,this.parent.x+this.parent.w,this.parent.y+this.parent.h);
if (this.contents) this.contents.every(function(v,i) { v.to(dx,dy)});
if (this.children) this.children.every(function(v,i) { v.to(dx,dy)});
this.moving = e;

if (!this.moving) return;
var dx = e.x - this.moving.x;
var dy = e.y - this.moving.y;
this.to(dx,dy);
if (this.parent) this.clamp(this.parent.x,this.parent.y,this.parent.x+this.parent.w,this.parent.y+this.parent.h);
if (this.contents) this.contents.every(function(v,i) { v.to(dx,dy)});
if (this.children) this.children.every(function(v,i) { v.to(dx,dy)});
this.moving = e;
};
Widget.up = function(e) {
this.moving = false;
if (!e.on(this)) return;
var o = this.overlaps([Display,Phosphor,this]);
if (!o || !o.is('Graphic') || this == o) return;
if (o.children.contains(this)) return;
o.add(this);
this.moving = false;
if (!e.on(this)) return;
var o = this.overlaps([Display,Phosphor,this]);
if (!o || !o.is('Graphic') || this == o) return;
if (o.children.contains(this)) return;
o.add(this);
};

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -115,12 +114,8 @@ var HotKey = let({
////////////////////////////////////////////////////////////////////////////////////////////////////
// Block Object
var Block = let(Widget,{
bg: "gray",
moving: false,
editing: false,
expanded: false,
content: false,
init: function() { return this.clone().instance() },
init: function() { return this.clone().copy({
bg: "gray", moving: false, editing: false, expanded: false, content: false }).instance() },
says: function(t) { this.content = t; return this },
evaluate: function() {
if (!this.content) return "";
Expand Down Expand Up @@ -196,7 +191,6 @@ var Block = let(Widget,{
this.free();
}
if (!o.childof) {
alert('setting');
if (!window.contains(o.content)) window[o.content] = {};
o.evaluate()[this.content] = true;
if (o.expanded) o.expanded.collapse();
Expand Down
65 changes: 64 additions & 1 deletion js/selfish.js
Expand Up @@ -47,7 +47,7 @@ String.prototype.contains = function(s) { return 0 <= this.indexOf(s) }
////////////////////////////////////////////////////////////////////////////////////////////////////
// Object prototype extensions

Object.prototype.a = Object.prototype.an = function(x,v) { return x.can('init') ? x.init(v) : x.clone() };
Object.prototype.a = Object.prototype.an = function(x,v) { return x.can('init') ? x.init(v).copy({from:x}) : x.clone().copy({from: x}) };
Object.prototype.the = function(x) { return x };

Object.prototype.clone = function() {
Expand Down Expand Up @@ -130,12 +130,75 @@ Object.prototype.is = function(x) {
return retval;
}

Object.prototype.implements = function() {
var $self = this;
var retval = [];
Objects.each(function(v,k) { if ($self.is(v)) retval.push(k) });
return retval;
}

Object.prototype.any = function(f) {
var retval = null;
this.each(function(v,k) { if (f(v,k)) return retval = v });
return retval;
}

Object.prototype.module = function() {
var ots = Object.prototype.toString;
Object.prototype.toString = function() {
var retval = '{ ';
this.each(function(v,k) {
if (typeof(v) == 'function') retval = retval.append(k,': ',v,', ');
if (typeof(v) == 'string') retval = retval.append(k,': "', v, '", ');
if (typeof(v) == 'number') retval = retval.append(k,': ', v, ', ');
if (typeof(v) == 'boolean') retval = retval.append(k,': ', v, ', ');
if (typeof(v) == 'object') retval = retval.append(k, ': ', v.name(), ', ');
});
return retval.append('}');
};
var retval = this.toString();
Object.prototype.toString = ots;
return retval;
}

Object.prototype.use = function() {
var urls = [];
for (var i = 0; i < arguments.length; ++i) urls[i] = arguments[i];
var url = urls.shift();
var cb = function(txt) {
if (!txt) alert('Failed to load '.append(url));
try {
eval('('.append(txt,')'))
var url = urls.shift();
if (url) get(url,cb);
} catch(e) { alert('Load error: '.append(e,':',txt)) }
};
return this.get(url,cb);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Network Functions
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','appliaction/x-www-from-urlencoded');
_request.send(data ? data : '');
return this;
}
Object.prototype.post = function(url,cb) { return this.request("POST",url,this.toString(),cb) }
Object.prototype.get = function(url,cb) { return this.request("GET",url,cb) }

Object.prototype.download = function() {
document.location.href = "data:application/json,".append(this.toString().encode());
return this;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
// Array extensions
Array.prototype.every = function(f) {
Expand Down
3 changes: 3 additions & 0 deletions js/shitweasel.js
Expand Up @@ -4,6 +4,9 @@ function() {
// this.init = false;
// };
// Objects.init();
Keyboard.keymap[61] = '=+';
Keyboard.keymap[107] = '++';
Keyboard.keymap[109] = '-_';
Screen.frame = function() {
this.ctx.beginPath();
this.ctx.moveTo(this.x,this.y);
Expand Down

0 comments on commit 94bc4ac

Please sign in to comment.