Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor for cleanness

  • Loading branch information...
commit f8c789b047d52a8041927eadcf6ee7579676aac7 1 parent 6760630
@dtudury authored
View
6 index.html
@@ -10,13 +10,17 @@
<script language="javascript" src="js/utils/deepPrint.js"></script>
<script language="javascript" src="js/cryptopotamus/Model.js"></script>
<script language="javascript" src="js/cryptopotamus/View.js"></script>
+ <script language="javascript" src="js/cryptopotamus/view/Base.js"></script>
+ <script language="javascript" src="js/cryptopotamus/view/Text.js"></script>
+ <script language="javascript" src="js/cryptopotamus/view/TextInputPrompted.js"></script>
+ <script language="javascript" src="js/cryptopotamus/view/PasswordInputPrompted.js"></script>
<script language="javascript" src="js/cryptopotamus/view/Div.js"></script>
<script language="javascript" src="js/cryptopotamus/view/FancyDiv.js"></script>
<script language="javascript" src="js/cryptopotamus/view/FancierDiv.js"></script>
<script language="javascript" src="js/cryptopotamus/Controller.js"></script>
<script language="javascript" src="js/cryptopotamus/handle_onload.js"></script>
</head>
- <body onload="IMPORT_FUNCTION( 'cryptopotamus.handle_onload')()">
+ <body onload="IMPORT( 'cryptopotamus.handle_onload')()">
<div id="content"></div>
</body>
</html>
View
108 js/CORE.js
@@ -4,66 +4,68 @@
*****************************************************************************/
(function() {
-
- var _name_space = {};
- var _packaged_functions = {};
-
var global = ( function(){ return this;}).call();
+ var _name_space = {};
- /*****************************************************************************
- *
- *****************************************************************************/
- global.PACKAGE_FUNCTION = function( qualified_name, in_function) {
- _packaged_functions[ qualified_name] = in_function;
- };
-
- global.IMPORT_FUNCTION = function( qualified_name) {
- if( !_name_space[ qualified_name]) {
+ var is_initializing = false;
+ function initialize( in_constructor) {
+ var was_initializing = is_initializing;
+ is_initializing = true;
+ var instance = new in_constructor();
+ is_initializing = was_initializing;
+ return instance;
+ }
+ global.CLASS = function( in_name) {
+ if( !_name_space[ in_name]) {
var _class = function() {
- return _packaged_functions[ qualified_name].apply( this, [].slice.call( arguments));
+ if( _class.function_only) return _class.mixins[ 0].apply( this, [].slice.call( arguments));
+ if( this.super_constructor) this.SUPER = initialize( this.super_constructor);
+ for( var i = 0; i < _class.mixins.length; i++) _class.mixins[ i].call( this);
+ if( this.constructor != _class) this.CONSTRUCTOR = this.constructor;
+ if( !is_initializing && this.CONSTRUCTOR) {
+ this.CONSTRUCTOR.apply( this, [].slice.call( arguments));
+ }
};
- _name_space[ qualified_name] = _class;
- }
- return _name_space[ qualified_name];
- };
-
-
- /*****************************************************************************
- *
- *****************************************************************************/
- global.IMPORT = function( qualified_name) {
- if( !_name_space[ qualified_name]) {
- var _class = function() {
- if( !_class.prototype.super_class) {
- var temp_prototype = new _class.super_class();
- for( var name in temp_prototype) _class.prototype[ name] = temp_prototype[ name];
- _class.prototype.constructor = _class;
- _class.prototype.super_class = _class.super_class.prototype;
- if( _class.instance_definition) _class.instance_definition( _class.prototype);
+ _class.mixins = [];
+ _class.uninitialized = false;
+ _class.EXTENDS = function( in_super_name){
+ _class.super_name = in_super_name;
+ _class.uninitialized = true;
+ return _class;
+ };
+ _class.DEFINITION = function( in_function){
+ _class.mixins.push( in_function);
+ return _class;
+ };
+ _class.STATIC = function( in_definition) {
+ var temp_instance = new in_definition();
+ for( var member in temp_instance) {
+ _class[ member] = temp_instance[ member];
+ if( _class[ member] instanceof Function) {
+ FUNCTION( in_name + "." + member).DEFINITION( _class[ member]);
+ }
}
- if( _class.prototype.constructor == _class) _class.prototype.constructor = _class.super_class.prototype.constructor;
- if( _class.prototype.constructor) _class.prototype.constructor.apply( _class.prototype, [].slice.call( arguments));
+ return _class;
};
- _class.define_instance = function( in_definition) { _class.instance_definition = in_definition; return _class;};
- _class.define_static = function( in_definition) { if( in_definition) in_definition( _class); return _class;};
- _name_space[ qualified_name] = _class;
+ _name_space[ in_name] = _class;
}
- return _name_space[ qualified_name];
+ return _name_space[ in_name];
};
-
- /*****************************************************************************
- *
- *****************************************************************************/
- global.PACKAGE = function( qualified_name, super_class, instance_definition, static_definition) {
- if( !super_class) super_class = function() {};
-
- var _class = IMPORT( qualified_name);
- _class.qualified_name = qualified_name;
- _class.super_class = super_class;
- _class.define_instance( instance_definition);
- _class.define_static( static_definition);
- _name_space[ qualified_name] = _class;
- return _class;
+ global.FUNCTION = function( in_name) {
+ var wrapper_class = CLASS( in_name);
+ wrapper_class.function_only = true;
+ return wrapper_class;
+ };
+ global.IMPORT = function( in_name) {
+ var wrapper_class = CLASS( in_name);
+ if( wrapper_class.uninitialized) {
+ var super_class = CLASS( wrapper_class.super_name);
+ wrapper_class.prototype = initialize( super_class);
+ wrapper_class.prototype.constructor = wrapper_class;
+ wrapper_class.prototype.super_constructor = super_class;
+ wrapper_class.uninitialized = false;
+ }
+ return wrapper_class;
};
-
+
})();
View
7 js/cryptopotamus/Controller.js
@@ -1,15 +1,16 @@
-PACKAGE( "cryptopotamus.Controller").define_static( function( _) {
+CLASS( "cryptopotamus.Controller")
+.STATIC( function() {
var model = IMPORT( "cryptopotamus.Model");
- _.update_salt = function( in_event) {
+ this.update_salt = function( in_event) {
var _event = window.event || in_event;
if (!_event.target) _event.target = _event.srcElement;
model.set_salt( _event.target.value);
};
- _.generate_all = function( in_event) {
+ this.generate_all = function( in_event) {
window.alert( "generate based on " + model.get_salt());
};
View
13 js/cryptopotamus/Model.js
@@ -1,18 +1,19 @@
-PACKAGE( "cryptopotamus.Model").define_static( function( _) {
+CLASS( "cryptopotamus.Model")
+.STATIC( function() {
var PubSub = IMPORT( "utils.PubSub");
- _.publisher = new PubSub();
+ this.publisher = new PubSub();
- _.NEW_SALT = "newsalt";
+ this.NEW_SALT = "newsalt";
var _salt;
- _.set_salt = function( salt) {
+ this.set_salt = function( salt) {
if( _salt == salt) return;
_salt = salt;
- _.publisher.sendMessage( _.NEW_SALT);
+ this.publisher.sendMessage( this.NEW_SALT);
};
- _.get_salt = function() { return _salt;};
+ this.get_salt = function() { return _salt;};
var _configs = [];
});
View
25 js/cryptopotamus/View.js
@@ -1,17 +1,18 @@
-PACKAGE( "cryptopotamus.View").define_static( function( _) {
+CLASS( "cryptopotamus.View")
+.STATIC( function() {
var controller = IMPORT( "cryptopotamus.Controller");
- var configure = IMPORT_FUNCTION( "utils.configure");
+ var configure = IMPORT( "utils.configure");
- _.new_top_nav = function() {
- var div = _.new_div();
- div.appendChild( _.new_text( "salt: "));
- div.appendChild( _.new_password_input( controller.update_salt));
- div.appendChild( _.new_button_input( controller.generate_all, "generate all"));
+ this.new_top_nav = function() {
+ var div = this.new_div();
+ div.appendChild( this.new_text( "salt: "));
+ div.appendChild( this.new_password_input( controller.update_salt));
+ div.appendChild( this.new_button_input( controller.generate_all, "generate all"));
return div;
};
- _.new_text_input = function( value, size) {
+ this.new_text_input = function( value, size) {
var input = document.createElement( "input");
var _defaults = { value: "text here", size: 20};
var _no_prompt = { onfocus: null, onblur: null};
@@ -27,7 +28,7 @@ PACKAGE( "cryptopotamus.View").define_static( function( _) {
configure( configuration, _prompt);
return configure( input, configuration, _defaults);
};
- _.new_button_input = function( onclick, value) {
+ this.new_button_input = function( onclick, value) {
var input = document.createElement( "input");
var configuration = { type: "button", value: value, onclick: onclick};
return configure( input, configuration);
@@ -59,13 +60,13 @@ PACKAGE( "cryptopotamus.View").define_static( function( _) {
};
return configure( input, configuration);
};
- _.new_password_input = function( onchange, size) {
+ this.new_password_input = function( onchange, size) {
return _new_password_input_prompt();
};
- _.new_div = function() {
+ this.new_div = function() {
return document.createElement( "div");
};
- _.new_text = function( text) {
+ this.new_text = function( text) {
return document.createTextNode( text);
};
});
View
230 js/cryptopotamus/handle_onload.js
@@ -2,31 +2,227 @@
*
*****************************************************************************/
-PACKAGE_FUNCTION( "cryptopotamus.handle_onload", function() {
-
+FUNCTION( "cryptopotamus.handle_onload")
+.DEFINITION( function() {
+
var view = IMPORT( "cryptopotamus.View");
- var Div = IMPORT( "cryptopotamus.view.Div");
- var FancyDiv = IMPORT( "cryptopotamus.view.FancyDiv");
- var FancierDiv = IMPORT( "cryptopotamus.view.FancierDiv");
-
- var div;
- /*
- div = new Div( content);
- console.log( "\n\n\n");
- div = new FancyDiv( content);
- console.log( "\n\n\n");
- */
- div = new FancierDiv( content);
- console.log( "\n\n\n");
- div = new FancierDiv( content);
- console.log( "\n\n\n");
+ var Text = IMPORT( "cryptopotamus.view.Text");
+ var TextInputPrompted = IMPORT( "cryptopotamus.view.TextInputPrompted");
+ var PasswordInputPrompted = IMPORT( "cryptopotamus.view.PasswordInputPrompted");
var content = document.getElementById( "content");
content.appendChild( view.new_top_nav());
content.appendChild( view.new_text_input( "name here"));
+
+ new Text( content, "hey there");
+ new TextInputPrompted( content, "type your text");
+ new PasswordInputPrompted( content, "type your password");
+
+
+
+/*
+
+ var named_functions = {};
+ var is_initializing = false;
+ function initialize( in_constructor) {
+ var was_initializing = is_initializing;
+ is_initializing = true;
+ var instance = new in_constructor();
+ is_initializing = was_initializing;
+ return instance;
+ }
+ function CLASS( in_name) {
+ if( !named_functions[ in_name]) {
+ named_functions[ in_name] = function() {
+ if( this.super_constructor) this.SUPER = initialize( this.super_constructor);
+ for( var i = 0; i < arguments.callee.mixins.length; i++) arguments.callee.mixins[ i].call( this);
+ if( this.constructor != arguments.callee) this.CONSTRUCTOR = this.constructor;
+ if( !is_initializing && this.CONSTRUCTOR) {
+ this.CONSTRUCTOR.apply( this, [].slice.call( arguments));
+ }
+ };
+ named_functions[ in_name].mixins = [];
+ named_functions[ in_name].uninitialized = false;
+ named_functions[ in_name].EXTENDS = function( in_super_name){
+ named_functions[ in_name].super_name = in_super_name;
+ named_functions[ in_name].uninitialized = true;
+ return named_functions[ in_name];
+ };
+ named_functions[ in_name].DEFINITION = function( in_function){
+ named_functions[ in_name].mixins.push( in_function);
+ return named_functions[ in_name];
+ };
+ }
+ return named_functions[ in_name];
+ }
+ function retreive( in_name) {
+ var wrapper_class = CLASS( in_name);
+ if( wrapper_class.uninitialized) {
+ var super_class = retreive( wrapper_class.super_name);
+ wrapper_class.prototype = initialize( super_class);
+ wrapper_class.prototype.constructor = wrapper_class;
+ wrapper_class.prototype.super_constructor = super_class;
+ wrapper_class.uninitialized = false;
+ }
+ return wrapper_class;
+ }
+
+ CLASS( "Mammal")
+ .EXTENDS( "Animal")
+ .DEFINITION( function() {
+ var SUPER = this.SUPER;
+ console.log( "creating Mammal");
+ this.talk = function() {
+ console.log( "grunt");
+ SUPER.talk();
+ };
+ });
+
+ CLASS( "Animal")
+ .DEFINITION( function() {
+ console.log( "creating Animal");
+ this.is_alive = true;
+ this.is_cuddly = false;
+ this.talk = function() {
+ console.log( "breath");
+ };
+ this.constructor = function() {
+ console.log( "I'm not a vegetable " + [].slice.call( arguments));
+ };
+ });
+
+ var dog_mixin = function() {
+ this.talk = function() {
+ console.log( "woof");
+ this.SUPER.talk();
+ };
+ };
+
+ var cat_mixin = function() {
+ this.talk = function() {
+ console.log( "meow");
+ this.SUPER.talk();
+ this.SUPER.SUPER.talk();
+ this.SUPER.SUPER.talk = function() {
+ console.log( "scratch");
+ };
+ this.SUPER.talk();
+
+ };
+ };
+
+ var pet_mixin = function() {
+ this.is_cuddly = true;
+ this.constructor = function( in_name) {
+ console.log( "I'm " + in_name);
+ };
+ };
+
+ CLASS( "Cat")
+ .EXTENDS("Mammal")
+ .DEFINITION( cat_mixin)
+ .DEFINITION( function() {
+ console.log( "creating Cat");
+ });
+
+ CLASS( "Dog")
+ .EXTENDS( "Mammal")
+ .DEFINITION( dog_mixin)
+ .DEFINITION( pet_mixin)
+ .DEFINITION( function() {
+ console.log( "creating Dog");
+ });
+
+
+ var Cat = retreive( "Cat");
+ var Dog = retreive( "Dog");
+ var Mammal = retreive( "Mammal");
+ var Animal = retreive( "Animal");
+
+ var felix = new Cat( "felix");
+ felix.is_alive = false;
+
+ console.log( "felix is alive: " + felix.is_alive);
+ console.log( felix instanceof Cat);
+ console.log( felix instanceof Dog);
+ console.log( felix instanceof Mammal);
+ console.log( felix instanceof Animal);
+
+ felix.talk();
+ console.log( "is cuddly: " + felix.is_cuddly);
+
+ var spot = new Dog( "spot");
+ console.log( "spot is alive: " + spot.is_alive);
+ console.log( spot instanceof Cat);
+ console.log( spot instanceof Dog);
+ console.log( spot instanceof Mammal);
+ console.log( spot instanceof Animal);
+ console.log( spot instanceof Object);
+
+ spot.talk();
+ console.log( "is cuddly: " + spot.is_cuddly);
+
+*/
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ /*
+ var adder = function( a, b) {
+ this.sum = a + b;
+ console.log( this.sum);
+ };
+ var test = new adder( 1, 1);
+ console.log( test.sum);
+ console.log( test instanceof adder);
+
+ function construct( constructor, args) {
+ var module = function() { constructor.apply( this, args);};
+ module.prototype = constructor.prototype;
+ return new module();
+ }
+
+ var construction = construct( adder, [ 2, 2]);
+ console.log( construction.sum);
+ console.log( construction instanceof adder);
+
+ var spoofer = function() {
+ // adder.apply( this, [ 3, 3]);
+ spoofer.prototype = new adder();
+// this.__proto__ = adder.prototype;
+ };
+ //spoofer.prototype = adder.prototype;
+ console.log( "=====");
+ var spoof = new spoofer();
+ console.log( spoof.constructor.prototype === adder.prototype);
+ console.log( spoofer.prototype === adder.prototype);
+ console.log( spoof.__proto__.__proto__ === adder.prototype);
+ console.log( spoof instanceof adder);
+ console.log( "-----");
+ spoof = new spoofer();
+ console.log( spoof.constructor.prototype === adder.prototype);
+ console.log( spoofer.prototype === adder.prototype);
+ var temp = {};
+ spoof.__proto__ = temp;
+ adder.prototype = temp;
+ console.log( spoof.__proto__.__proto__ === adder.prototype);
+ console.log( spoof instanceof adder);
+ */
/*
var announce_new_salt = function() {
//window.alert( "new salt: " + m.get_salt());
View
37 js/cryptopotamus/view/Div.js
@@ -1,35 +1,14 @@
-PACKAGE( "cryptopotamus.view.Div").define_instance( function( _) {
+CLASS( "cryptopotamus.view.Div")
+.EXTENDS( "cryptopotamus.view.Base")
+.DEFINITION( function() {
- var deepPrint = IMPORT_FUNCTION( "utils.deepPrint");
- var configure = IMPORT_FUNCTION( "utils.configure");
-
- _.parent = null;
- _.node = null;
-
- _.constructor = function( in_parent) {
- _.parent = in_parent;
- _.node = _.get_node();
-// console.log( "config: Div");
- console.log( "config: " + deepPrint( this.get_configuration()));
- };
-
- console.log( "setting foo to bar");
-
- _.foo = "bar";
-
- _.get_node = function() {
- return document.createElement( "div");
- };
-
- _.get_configuration = function() {
- return { a: "1", b: "2"};
+ //override
+ this.create_node = function() {
+ this.node = document.createElement( "div");
};
- _.extend_configuration = function( in_config) {
- var config = {};
- configure( config, this.super_class.get_configuration());
- configure( config, in_config);
- return config;
+ this.get_configuration = function() {
+ return this.extend_configuration( { a: "1", b: "2"});
};
});
View
6 js/cryptopotamus/view/FancierDiv.js
@@ -1,6 +1,8 @@
-PACKAGE( "cryptopotamus.view.FancierDiv", IMPORT( "cryptopotamus.view.FancyDiv")).define_instance( function( _) {
+CLASS( "cryptopotamus.view.FancierDiv")
+.EXTENDS( "cryptopotamus.view.FancyDiv")
+.DEFINITION( function() {
- _.get_configuration = function() {
+ this.get_configuration = function() {
return this.extend_configuration( { c: "14", d: "15"});
};
});
View
7 js/cryptopotamus/view/FancyDiv.js
@@ -1,6 +1,9 @@
-PACKAGE( "cryptopotamus.view.FancyDiv", IMPORT( "cryptopotamus.view.Div")).define_instance( function( _) {
+CLASS( "cryptopotamus.view.FancyDiv")
+.EXTENDS( "cryptopotamus.view.Div")
+.DEFINITION( IMPORT( "utils.PubSub"))
+.DEFINITION( function() {
- _.get_configuration = function() {
+ this.get_configuration = function() {
return this.extend_configuration( { b: "7", c: "8"});
};
});
View
16 js/utils/Closure.js
@@ -3,9 +3,10 @@
*
*****************************************************************************/
-PACKAGE( "utils.Closure").define_instance( function( _) {
- _.constructor = function( target, executable) {
- var args = [].slice.call( arguments, 2);
+CLASS( "utils.Closure")
+.DEFINITION( function() {
+ this.constructor = function( target, executable) {
+ var args = [].slice.call( arguments, 2);
this.target = target;
this.executable = executable;
this.args = args;
@@ -23,4 +24,13 @@ PACKAGE( "utils.Closure").define_instance( function( _) {
return true;
};
};
+})
+.STATIC( function() {
+ this.wrap = function( target, executable) {
+ var args = [].slice.call( arguments, 2);
+ return function() {
+ var _args = [].slice.call( arguments);
+ executable.apply( target, args.concat( _args));
+ };
+ };
});
View
9 js/utils/PubSub.js
@@ -3,18 +3,19 @@
*
*****************************************************************************/
-PACKAGE( "utils.PubSub").define_instance( function( _) {
+CLASS( "utils.PubSub")
+.DEFINITION( function() {
var Closure = IMPORT( "utils.Closure");
var _topics = {};
- _.subscribe = function( target, executable, topic) {
+ this.subscribe = function( target, executable, topic) {
if( !_topics[ topic]) _topics[ topic] = [];
_topics[ topic].push( new Closure( target, executable));
};
- _.unsubscribe = function( target, executable, topic) {
+ this.unsubscribe = function( target, executable, topic) {
if( !_topics[ topic]) _topics[ topic] = [];
var subscriptions = _topics[ topic];
_topics[ topic] = [];
@@ -26,7 +27,7 @@ PACKAGE( "utils.PubSub").define_instance( function( _) {
}
};
- _.sendMessage = function( topic) {
+ this.sendMessage = function( topic) {
if( !_topics[ topic]) _topics[ topic] = [];
var _args = [].slice.call( arguments, 1);
for( var i = 0; i < _topics[ topic].length; i++) {
View
5 js/utils/configure.js
@@ -3,9 +3,10 @@
*
*****************************************************************************/
-PACKAGE_FUNCTION( "utils.configure", function( target, source, defaults) {
+FUNCTION( "utils.configure")
+.DEFINITION( function( target, source, defaults) {
- var configure = IMPORT_FUNCTION( "utils.configure");
+ var configure = IMPORT( "utils.configure");
//helper function for configure
function _copy_value( target, source, attribute) {
View
3  js/utils/deepPrint.js
@@ -3,7 +3,8 @@
*
*****************************************************************************/
-PACKAGE_FUNCTION( "utils.deepPrint", function( target, indent) {
+FUNCTION( "utils.deepPrint")
+.DEFINITION( function( target, indent) {
var deepPrint = IMPORT_FUNCTION( "utils.deepPrint");
Please sign in to comment.
Something went wrong with that request. Please try again.