Skip to content

Commit

Permalink
The grand renaming...
Browse files Browse the repository at this point in the history
I've replaced let, now everything derives from the new naming scheme.

An.object().named('foo').from( objects .... )
  • Loading branch information
cthulhuology committed Jul 17, 2009
1 parent 7259917 commit 80037d8
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 305 deletions.
288 changes: 275 additions & 13 deletions js/adhoc.js
@@ -1,16 +1,278 @@
A = An = {
// adhoc.js
//
// Copyright (C) 2009 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/>.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Adhoc Javascript
//
// This library of extensions turns Javascript into a real Object Oriented Functional Language.
// Within the following functions are routines that serve as an alternative to traditional flow
// control structures, utilities to ease string and arry concatenatination. An object cloning,
// replication, and component model. A property based object clasification system, and better
// support for object introspection. Also support for componetized collective objects allow
// for multiple message dispatch. Finally, it also a few small DOM manipulations functions.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Syntax Helpers

var A = An = {
object: function() { return a({}) },
string: function() { return "" },
string: function() { return '' },
array: function() { return [] },
box: function() { return a(Box) },
widget: function() { return a(Widget) },
component: function() { return a(Component) },
event: function() { return an(Event) },
sound: function(src) { return a(Sound,src) },
image: function(src) { return an(Image,src) },
movie: function(src) { return a(Movide,src) },
block: function() { return a(Block).says('') },
circle: function() { return a(Circle) },
rectangle: function() { return a(Rectangle) },
text: function() { return a(Text) },
};

////////////////////////////////////////////////////////////////////////////////////////////////////
// String prototype extensions

String.prototype.last = function() { return this.substring(this.length-1) }

String.prototype.first = function() { return this.substring(0,1) }

String.prototype.decode = function() { return unescape(this) }

String.prototype.encode = function() { return escape(this) }

String.prototype.append = function() {
var retval = this;
for (var i = 0; i < arguments.length; ++i) retval += arguments[i].toString();
return retval;
}

String.prototype.contains = function(s) { return 0 <= this.indexOf(s) }

String.prototype.name = function() { return this }

String.prototype.lowered = function() {
return ''.append(this.charAt(0).tolower(),this.substring(1, this.length-1));
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Object prototype extensions

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

Object.prototype.clone = function() {
var Proto = function() {};
Proto.prototype = this;
return new Proto();
}

Object.prototype.copy = function(o) {
var $self = this;
o.each(function(v,k) { $self[k] = v });
return this;
}

Object.prototype.from = function() {
this['init'] = function() { return this };
for (var i = 0; i < arguments.length; ++i) this.copy(arguments[i]);
return this;
}

Object.prototype.has = function(v,k) {
this[k.last() == "*" ? k : k.append('*')] = v;
return this;
}

Object.prototype.contains = function(e) {
var retval = false;
this.each(function(v,k) { if (k == e) return retval = true });
return retval;
}

Object.prototype.each = function(f) {
for (var k in this) if (this.hasOwnProperty(k) && k != 'prototype') f(this[k],k);
return this;
}

Object.prototype.all = function(f) {
for (var k in this) if (k != 'prototype') f(this[k],k);
return this;
}

Object.prototype.which = function(f) {
var w = [];
this.each(function(v,k) { if (f(v,k)) w.push(v) });
return w;
}

Object.prototype.parts = function() { return this.which(function(v,k) { return k.last() == "*" }) }

Object.prototype.can = function(k) { return (typeof(this[k]) == "function") }

Object.prototype.slots = function() {
var i = 0;
this.each(function(v,k) { if (k && v) ++i });
return i;
}

Object.prototype.its = function(k) {
return k.last() != '*' ? this[k.append('*')] : this[k];
}

Object.prototype.of = function(x,k) {
var args = [ arguments[1], arguments[2], arguments[3], arguments[4], arguments[5] ];
var $s = this;
x.parts().every(function(p,i) { if (p.is($s) && p.can(k)) p[k](args[1],args[2],args[3],args[4])});
return this;
}

Object.prototype.name = function() {
var retval = null;
var $self = this;
window.each(function(v,k) { if (v === $self) return retval = k });
return retval;
}

Object.prototype.plural = Object.prototype.named = function(x) {
A[x.name()] = function(y) { return a(x.name().lowered(),y) };
return window[x.name()] = this;
}

Object.prototype.is = function(x) {
var $self = this;
var retval = true;
x.all(function(v,k) { if (x.can(k) && !$self.can(k)) return retval = false });
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) {
for (var i = 0; i < this.length; ++i) f(this[i],i);
return this;
}

Array.prototype.map = function(f) {
var retval = [];
this.every(function(x,i) { retval.push(f(x)) });
return retval;
}

Array.prototype.apply = function(f,o) {
var retval = o;
this.every(function(x,i) { retval = f(retval,x) });
return retval;
}

Array.prototype.contains = function(e) {
var retval = false;
this.every(function(x,i) { if (x == e) return retval = true });
return retval;
}

Array.prototype.append = function(a) {
var $self = this;
a.every(function(x,i) { $self.push(x) });
return this;
}

Array.prototype.and = function() {
for (var i = 0; i < arguments.length; ++i) this.push(arguments[i]);
return this;
}

Array.prototype.except = function (e) {
for (var i = 0; i < this.length; ++i) if (this[i] == e) this.splice(i,1);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// HTML Element Extensions
Element.prototype.add = function(e) {
this.appendChild(e);
return this;
}

Object.prototype.listen = Element.prototype.listen = function(e,f) {
this.addEventListener(e,f,false);
return this;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// DOM Functions
function $(x) { return document.getElementById(x) }
function $_(x) { return document.createElement(x) }

0 comments on commit 80037d8

Please sign in to comment.