Javascript OO Template

Javascript OO Template

index.html

style.css

[css]
div.dialog {
width: 600px;
margin: 2px;
padding: 0 10px 0 10px;
border: 1px solid #ccc;
overflow:auto;
}
div#input {
height: 200px;
}
div#output {
height: 400px;
}
[/css]

RoomController.js

[javascript]
/**
* RoomController is the main interface to the application
*/
var testVariable = "helloVar";
function testFunction() {
return "helloFun";
}

function testIncludes() {
var output = "testing Item object: ";
var item1 = new Item("title1", "type", null, null, null, null, null);
var item2 = new Item("title2", "type", null, null, null, null, null);
output += " Item1 version=" + item1.getVersion() + " id=" + item1.getID() + " title=" + item1.getTitle() + " type=" + item1.getType() + " ";
output += " Item2 version=" + item2.getVersion() + " id=" + item2.getID() + " title=" + item2.getTitle() + " type=" + item2.getType() + " ";

var room1 = new Room("title3", null, null, null, null, null);
var room2 = new Room("title4", null, null, null, null, null);
output += " Room1 version=" + room1.getVersion() + " id=" + room1.getID() + " title=" + room1.getTitle() + " type=" + room1.getType() + " ";
output += " Room2 version=" + room2.getVersion() + " id=" + room2.getID() + " title=" + room2.getTitle() + " type=" + room2.getType() + " ";

var door1 = new Door("title5", null, null, null, room1, room2, false); //title, position, dimensions, colour, roomA, roomB, isOpen
output += " Door1=" + door1.toString() + " ";

return output;
}

//base RoomController object
var RoomController = function() {
this.VERSION = ‘0.1a’;
}
RoomController.prototype.getVersion = function() {return this.VERSION; }
RoomController.prototype.processCommand = function(key, param) {
var output = "";
if (key!=null && (typeof key=="string") && key.length>0) {
switch (key) {
case "?" :
case "help" :
output = "commands: [?|help] view help [q|quit] quit this application";
break;
case "q" :
case "quit" :
output = "exiting application…";
break;
default :
output = "!unrecognised command. Enter help to see list of commands.";
break;
}
}
return output;
}

//main initialisation
function roomInit() {
var controller = new RoomController();
return controller;
}

[/javascript]

Item.js

[javascript]

/**
* class Item represents an individual item or collection of items in the world
*/
/**
* The semicolon at the beginning takes care of any improperly terminated statements that might preface this class.
* Wrap everything in a function to preserve scope.
*/
//;(function(window) {
function Item(title, type, container, position, dimensions, colour, children) {
/**********************************************************
* Static Properties – shared between all instances
**********************************************************/
//constants
Item.VERSION = "Item 0.1";
Item.TYPE = "Item";

//static fields
if ( typeof Item.IDTOP == ‘undefined’ ) {
// It has not been used yet, so perform the initialisation
Item.IDTOP = 0;
}

/**********************************************************
* Privileged Properties – unique to each instance
**********************************************************/
this.id = Item.IDTOP++; //int
this.title = title; //String
this.type = type; //String
this.container = container; //Item object
this.position = position; //Position object
this.dimensions = dimensions; //Dimension object
this.colour = colour; //Colour object
this.children = children; //Array

/**********************************************************
* Private Properties – scope only in this inner function
**********************************************************/
var self = this; //!!!always required to sort out scope issues with window, inner functions etc.
//var _privateVar = ‘Private Variable’;

/**********************************************************
* Privileged Methods
* can access public, privileged and private methods and properties
**********************************************************/
//this.getVersion = function() {
//return Item.VERSION;
//};

/**********************************************************
* Private Methods
* can also access any of the types
**********************************************************/
//function privateMethod() {
//
//}

/**********************************************************
* Getters / Setters – of Private Properties via Privileged Methods
**********************************************************/
//self.getPrivateVar = function() {
//return _privateVar;
//};

//self.setPrivateVar = function(val) {
//_privateVar = val;
//};

/**********************************************************
* Constructor
**********************************************************/
self.__contruct = function() {
//already handled by Privileged Properties
}
}

/**********************************************************
* Public Properties – shared between all instances
**********************************************************/
//Item.prototype.publicProp = ‘Public Variable’;

/**********************************************************
* Public Methods – shared between all instances
**********************************************************/
Item.prototype.getVersion = function() { return Item.VERSION; }
Item.prototype.getTypeOf = function() { return Item.TYPE; }
Item.prototype.getIDTop = function() { return Item.IDTOP; }

Item.prototype.getID = function() { return this.id; }
Item.prototype.getTitle = function() { return this.title; }
Item.prototype.getType = function() { return this.type; }
Item.prototype.getContainer = function() { return this.container; }
Item.prototype.getPosition = function() { return this.position; }
Item.prototype.getDimensions = function() { return this.dimensions; }
Item.prototype.getColour = function() { return this.colour; }
Item.prototype.getChildren = function() { return this.children; }

Item.prototype.setID = function(id) { this.id = id; }
Item.prototype.setTitle = function(title) { this.title = title; }
Item.prototype.setType = function(type) { this.type = type; }
Item.prototype.setContainer = function(container) { this.container = container; }
Item.prototype.setPosition = function(position) { this.position = position; }
Item.prototype.setDimensions = function(dimensions) { this.dimensions = dimensions; }
Item.prototype.setColour = function(colour) { this.colour = colour; }
Item.prototype.setChildren = function(children) { this.children = children; }

Item.prototype.getChildByID = function(id, remove) {
var output = null; //returns Item
if (id>=0) {
if (this.id==id) {
output = this;
if (remove) {

//to do

}
} else if (this.children!=null && this.children.length>0) {
for (var i=0; i<this.children.length; i++) {
if (this.children[i]!=null) {
output = this.children[i].getChildByID(id, remove);
//removal handled by if case above
}
}
} else {
//no match
}
}
return output;
}

Item.prototype.addNewChild = function(title, type, container, position, dimensions, colour, children) {
var output = null; //return Item
output = new Item(title, type, container, position, dimensions, colour, children);
if (this.children==null) {
this.children = new Array();
}
this.children.push(output);
return output;
}

Item.prototype.addExistingChild = function(child, overwrite) {
var output = false; //Boolean
if (child!=null &amp;&amp; child instanceof Item) {
if (this.children==null) {
this.children = new Array();
}
this.children.push(child);
output = true;
}
return output;
}

Item.prototype.toString = function() {
var output = "id=" + this.id;
output += ", title=" + this.title;
output += ", type=" + this.type;
output += ", container=" + (this.container==null) ? this.container.toString() : "";
output += ", position=" + (this.position==null) ? this.position.toString() : "";
output += ", dimensions=" + (this.dimensions==null) ? this.dimensions.toString() : "";
output += ", colour=" + (this.colour==null) ? this.colour.toString() : "";
output += ", children="; //to do
return output;
}

Item.prototype.compareTo = function(other) {
var output = 0; //int
if (other!=null &amp;&amp; other instanceof Item) {
if (this.id<other.id) {
output = 1;
} else if (this.id>other.id) {
output = -1;
} else {
output = 0;
}
} else {
//invalid argument
output = null;
}
return output;
}

//make Item function visible to global scope
// window.Item = Item;
//}(window));

[/javascript]

Room.js

[javascript]

/**
* class Room represents a collection of boundary and furniture objects.
* extends Item
*/
//;(function(window) {
/**
* Create a new constructor function, whose prototype is the parent object’s prototype.
* Set the child’s prototype to the newly created constructor function.
* if argument exists use this mthod rather than: Room.prototype = new Item();
**/
if ( typeof extendObj == ‘undefined’ ) {
var extendObj = function(childObj, parentObj) {
var tmpObj = function () {}
tmpObj.prototype = parentObj.prototype;
childObj.prototype = new tmpObj();
childObj.prototype.constructor = childObj;
};
}

//setup inheritance of object – must be called BEFORE class defined otherwise
// child prototypes get overridden by parent
extendObj(Room, Item);

function Room(title, level, position, dimensions, colour, children) {
/**********************************************************
* call parent constructor
**********************************************************/
if ( typeof Item == ‘undefined’ ) {
//parent object not defined yet
alert("Item object undefined to Room!");
} else {
Item.call(this, title, "room", level, position, dimensions, colour, children);
}

/**********************************************************
* Static Properties – shared between all instances
**********************************************************/
//constants
Room.VERSION = "Room 0.1";
Room.TYPE = "Room";

//static fields
//if ( typeof Room.IDTOP == ‘undefined’ ) {
// It has not been used yet, so perform the initialisation
//Room.IDTOP = 0;
//}

/**********************************************************
* Privileged Properties – unique to each instance
**********************************************************/
this.doorList = []; //Array of Door objects

/**********************************************************
* Private Properties – scope only in this inner function
**********************************************************/
var self = this; //!!!always required to sort out scope issues with window, inner functions etc.
//var _privateVar = ‘Private Variable’;

/**********************************************************
* Privileged Methods
* can access public, privileged and private methods and properties
**********************************************************/
//this.getVersion = function() {
//return Room.VERSION;
//};

/**********************************************************
* Private Methods
* can also access any of the types
**********************************************************/
//function privateMethod() {
//
// }

/**********************************************************
* Getters / Setters – of Private Properties via Privileged Methods
**********************************************************/
//self.getPrivateVar = function() {
//return _privateVar;
//};

//self.setPrivateVar = function(val) {
//_privateVar = val;
//};

/**********************************************************
* Constructor
**********************************************************/
self.__contruct = function() {
//already handled by Privileged Properties
}

}

//setup inheritance of object
//extendObj(Room, Item);

/**********************************************************
* Public Properties – shared between all instances
**********************************************************/
//Room.prototype.publicProp = ‘Public Variable’;

/**********************************************************
* Public Methods – shared between all instances
**********************************************************/
Room.prototype.getVersion = function() { return Room.VERSION; }
Room.prototype.getTypeOf = function() { return Room.TYPE; }

Room.prototype.getItem = function(title, remove) {
var output = null; //Item object
if (title!=null &amp;&amp; title.length>0 &amp;&amp; this.itemList!=null) {
var matchedIndex = -1;
for (var i=0; i<this.itemList.length; i++) {
if (this.itemList[i]!=null &amp;&amp; this.itemList[i] instanceof Item &amp;&amp; this.itemList[i].getTitle()==title) {
output = this.itemList[i];
matchedIndex = i;
}
}
if (output!=null &amp;&amp; remove &amp;&amp; matchedIndex>=0) {
this.itemList.splice(i, 1); //at position i, remove 1 item
}
}
return output;
}

Room.prototype.addItem = function(title, type, parent, position, dimensions, colour, children, overwrite) {
var output = null; //Item object
if (title!=null &amp;&amp; title.length>0) {
output = this.getItem(title, false);
if (output==null) {
output = new Item(title, type, parent, position, dimensions, colour, children);
} else if (overwrite) {
output.setType(type);
output.setParent(parent);
output.setPosition(position);
output.setDimensions(dimensions);
output.setColour(colour);
output.setChildren(children);
} else {
//do not overwrite existing item
}
}
return output;
}

Room.prototype.getDoor = function(title, remove) {
var output = null; //Door

return output;
}

Room.prototype.addDoor = function(door, overwrite) {
var output = false;

return output;
}

//make Room function visible to global scope
// window.Room = Room;
//}(window));

[/javascript]

Door.js

[javascript]

/**
* class Door represents a boundary between two rooms
* extends Item
*/
//;(function(window) {
/**
* Create a new constructor function, whose prototype is the parent object’s prototype.
* Set the child’s prototype to the newly created constructor function.
* if argument exists use this mthod rather than: Room.prototype = new Item();
**/
if ( typeof extendObj == ‘undefined’ ) {
var extendObj = function(childObj, parentObj) {
var tmpObj = function () {}
tmpObj.prototype = parentObj.prototype;
childObj.prototype = new tmpObj();
childObj.prototype.constructor = childObj;
};
}

//setup inheritance of object – must be called BEFORE class defined otherwise
// child prototypes get overridden by parent
extendObj(Door, Item);

function Door(title, position, dimensions, colour, roomA, roomB, isOpen) {
/**********************************************************
* call parent constructor
**********************************************************/
if ( typeof Item == ‘undefined’ ) {
//parent object not defined yet
alert("Item object undefined to Room!");
} else {
Item.call(this, title, "door", null, position, dimensions, colour, null);
}

/**********************************************************
* Static Properties – shared between all instances
**********************************************************/
//constants
Door.VERSION = "Door 0.1";
Room.TYPE = "Door";

//static fields
//if ( typeof Room.IDTOP == ‘undefined’ ) {
// It has not been used yet, so perform the initialisation
//Room.IDTOP = 0;
//}

/**********************************************************
* Privileged Properties – unique to each instance
**********************************************************/
this.roomA = roomA; //Room object
this.roomB = roomB; //Room object
this.isOpen = isOpen; //Boolean

/**********************************************************
* Private Properties – scope only in this inner function
**********************************************************/
var self = this; //!!!always required to sort out scope issues with window, inner functions etc.
//var _privateVar = ‘Private Variable’;

/**********************************************************
* Privileged Methods
* can access public, privileged and private methods and properties
**********************************************************/
//this.getVersion = function() {
//return Room.VERSION;
//};

/**********************************************************
* Private Methods
* can also access any of the types
**********************************************************/
//function privateMethod() {
//
// }

/**********************************************************
* Getters / Setters – of Private Properties via Privileged Methods
**********************************************************/
//self.getPrivateVar = function() {
//return _privateVar;
//};

//self.setPrivateVar = function(val) {
//_privateVar = val;
//};

/**********************************************************
* Constructor
**********************************************************/
self.__contruct = function() {
//already handled by Privileged Properties
}

}

//setup inheritance of object
//extendObj(Room, Item);

/**********************************************************
* Public Properties – shared between all instances
**********************************************************/
//Room.prototype.publicProp = ‘Public Variable’;

/**********************************************************
* Public Methods – shared between all instances
**********************************************************/
Door.prototype.getVersion = function() { return Door.VERSION; }
Door.prototype.getTypeOf = function() { return Door.TYPE; }

Door.prototype.getRoomA = function() { return this.roomA; }
Door.prototype.getRoomB = function() { return this.roomB; }
Door.prototype.getIsOpen = function() { return this.isOpen; }

Door.prototype.setRoomA = function(roomA) { this.roomA = roomA; }
Door.prototype.setRoomB = function(roomB) { this.roomB = roomB; }
Door.prototype.setIsOpen = function(isOpen) { this.isOpen = isOpen; }

Door.prototype.toString = function() {
var output = "";
output += this.getVersion() + ",title=" + this.getTitle() + ",roomA=" + this.roomA.getTitle() + ",roomB=" + this.roomB.getTitle() + ",isOpen=" + this.getIsOpen();
return output;
}

//make Room function visible to global scope
// window.Room = Room;
//}(window));

[/javascript]