{StackJS: Framework}
Javascript Object Oriented Programming Framework By Elad Yarkoni
Class("Human", {
firstname: null,
lastname: null,
toString: function() {
return "Hi, my name is " + this.firstname + " " + this.lastname;
}
});
Class("Doctor::Human", {
// C'tor
Doctor: function(fname, lname) {
this.firstname = fname;
this.lastname = lname;
},
checkPatient: function(human) {
return human.getFirstname() + ", you're ok!";
},
toString: function() {
return "Hi, my name is Dr " + this.firstname + " " + this.lastname;
}
});
// Create new objects
var newDoctor = new Doctor("Gregory", "House");
var newPatient = new Human();
// automatic setters and getters generator
newPatient.setFirstname("John");
newPatient.setLastname("Smith");
newDoctor.checkPatient(newPatient);
Class('TableView::View', {
elementClass: 'table-view',
elementType: 'ul',
list: null,
TableView: function(list) {
this.list = list;
},
render: function() {
for (var i = 0; i < this.list.length; i++) {
this.addViews("<li><span>Name</span><span>$$</span></li>", this.list[i].name);
}
this.addEvent('li > span', 'onclick', 'nameClickEvent');
},
handleEvents: function(evt, name) {
if (name === "nameClickEvent") {
alert('table row is clicked');
}
}
});
render: function() {
this.addViews('<div class="row" ><span>$$</span><span>$$</span><span>$$</span><<span>$$</span>/div>',
myModel.getId(),
myModel.getName(),
myModel.getLabel(),
new PictureView()
);
}
Class('MyView::View', {
nameTextBoxOutlet: null,
render: function() {
this.addViews('<form >$$</form>',
'<input type="text" outlet="nameTextBoxOutlet" > </input >'
);
},
getInputText: function() {
return this.nameTextBoxOutlet.value;
}
});
Class('Car', {
engineSize: null,
model: null,
manufacturer: null,
maxSpeed: null,
Car: function(manufacturer) {
this.manufacturer = manufacturer;
}
});
var car = new Car('Toyota');
car.setModel(2007);
console.log(car.getManufacturer());
Class("Engine",{
size: null,
Engine: function(size) {
this.size = size;
},
start: function() {
// engine did broken after start
this.callDelegate("didBrokenPart",[this]);
}
});
Class("Car",{
engine: null,
Car: function() {
this.engine = new Engine(1600);
// make this car the engine delegate
this.engine.setDelegate(this);
},
drive: function() {
console.log("start drive");
this.engine.start();
},
stop: function() {
console.log("car is stopped");
},
// broken engine stops the car
didBrokenPart: function(object) {
this.stop();
}
});
// Define new exception
Class("StillAliveException::Exception", {
human: null,
StillAliveException: function(human) {
this.human = human;
this.message = "Human is still alive";
}
});
Class("Human", {
firstname: null,
lastname: null,
godmode: false,
Human: function(fname,lname) {
this.firstname = fname;
this.lastname = lname;
},
kill: function(human) {
if (human.getGodmode()) {
Throw(new StillAliveException(human));
}
}
});
// Main
var killer = new Human("Tony", "Soprano");
var victim = new Human("John", "Smith");
victim.setGodmode(true);
// register exception events by exception type
Catch('StillAliveException', function(ex) {
/* Handle still alive exception */
});
killer.kill(victim);
Class("Shape",{
x: null,
y: null,
Shape: function(x,y) {
this.x = x;
this.y = y;
}
});
Class("Rectangle::Shape",{
edgeSize: null,
Rectangle: function(x,y,edgeSize){
this.x = x;
this.y = y;
this.edgeSize = edgeSize;
},
accept: function(visitor) {
visitor.visitRectange(this);
}
});
Class("Circle::Shape",{
radius: null,
Circle: function(x,y,radius) {
this.x = x;
this.y = y;
this.radius = radius;
},
accept: function(visitor) {
visitor.visitCircle(this);
}
});
Class("CanvasVisitor",{
visitCircle: function(circle) {
var mycanvas = document.createElement('canvas');
mycanvas.style.position = 'absolute';
mycanvas.style.left = circle.getX() - circle.getRadius() + 'px';
mycanvas.style.top = circle.getY() - circle.getRadius() + 'px';
mycanvas.style.width = (circle.getRadius() * 2) + 'px';
mycanvas.style.height = (circle.getRadius() * 2) + 'px';
var ctx = mycanvas.getContext('2d');
ctx.beginPath();
ctx.arc(circle.getRadius(), circle.getRadius(), circle.getRadius(), 0, 2 * Math.PI, false);
ctx.fillStyle = "#8ED6FF";
ctx.fill();
document.body.appendChild(mycanvas);
},
visitRectange: function(rectange) {
var mycanvas = document.createElement('canvas');
mycanvas.style.position = 'absolute';
mycanvas.style.left = rectange.getX() + 'px';
mycanvas.style.top = rectange.getY() + 'px';
mycanvas.style.width = rectange.getEdgeSize() + 'px';
mycanvas.style.height = rectange.getEdgeSize() + 'px';
var ctx = mycanvas.getContext('2d');
ctx.beginPath();
ctx.rect(0, 0, rectange.getX() + rectange.getEdgeSize(), rectange.getY() + rectange.getEdgeSize());
ctx.fillStyle = '#8ED6FF';
ctx.fill();
ctx.strokeStyle = 'black';
document.body.appendChild(mycanvas);
}
});
/*
*
* Main
*/
var shapes = [
new Circle(100,50,50),
new Rectangle(300,50,100)
];
for (var i = 0; i < shapes.length; i++) {
shapes[i].accept(new CanvasVisitor());
}
Class('Car', {
type: null,
Car: function(type) {
this.type = type;
}
});
Class('ToyotaFactory', {
prototypeObject: null,
ToyotaFactory: function() {
this.prototypeObject = new Car('Toyota');
},
createCars: function(size) {
var carsArray = [];
for (var i = 0; i < size; i++) {
carsArray.push(this.prototypeObject.clone());
}
return carsArray;
}
});
var toyotaFactory = new ToyotaFactory();
var toyotaCars = toyotaFactory.createCars(50);
Class('Application', {
applicationName: null,
start: function() {
console.log('application has started');
}
});
//
// Main Program
//
var myApplication = Application();
var anotherMyApplication = Application();
myApplication.setApplicationName('StackJS Application');
console.log(anotherMyApplication.getApplicationName()); // prints 'StackJS Application'
Class('PrivateCar', {
manufacturer: null,
PrivateCar: function(manufacturer) {
this.manufacturer = manufacturer;
}
});
Class('Truck', {
manufacturer: null,
Truck: function(manufacturer) {
this.manufacturer = manufacturer;
}
});
Class('Bus', {
manufacturer: null,
Bus: function(manufacturer) {
this.manufacturer = manufacturer;
}
});
Class('VehicleFactory', {
createPrivateCar: function(manufacturer) {
return new PrivateCar(manufacturer);
},
createTruck: function(manufacturer) {
return new Truck(manufacturer);
},
createBus: function(manufacturer) {
return new Bus(manufacturer);
}
});
// Notification Object
Class('NSNotification', {
object: null,
name: null,
NSNotification: function(name, object) {
this.name = name;
this.object = object;
}
});
// Notification Center
Class('NSNotificationCenter', {
_observers: [],
addObserver: function(observer, method, notificationName) {
this._observers.push({
name: notificationName,
observer: observer,
method: method
});
},
postNotificationName: function(name, object) {
for (var i = 0; i < this._observers.length; i++) {
if (this._observers[i].name === name) {
this._observers[i].observer[this._observers[i].method](new NSNotification(name, object));
}
}
}
});
Class('Human', {
name: null,
sick: function() {
NSNotificationCenter().postNotificationName('humanSick', this);
}
});
Class('Doctor', {
Doctor: function() {
NSNotificationCenter().addObserver(this, 'humanIsSick', 'humanSick');
},
humanIsSick: function(nsnotification) {
console.log("Hi " + nsnotification.getObject().getName() + ", I will take care of you");
}
});
Class('ShoppingList', {
shoppingList: [],
addItem: function(name, quantity) {
this.shoppingList.push({
name: name,
quantity: quantity
});
},
removeItemByName: function(name) {
for (var i = 0; i < this.shoppingList.length; i++) {
if (this.shoppingList[i].name === name) {
this.shoppingList.splice(i, 1);
}
}
},
getItem: function(index) {
return this.shoppingList[i];
},
size: function() {
return this.shoppingList.length;
}
});
Class('TableView::View', {
// View class rendering method
render: function() {
this.clearViews();
this.addClasses("table-view");
for (var i = 0; i < this.getModel().size(); i++) {
this.addViews('$$', new TableCellView(i));
}
}
});
Class('TableCellView::View', {
index: null,
TableCellView: function(index) {
this.index = index;
},
render: function: function() {
this.addClasses("table-cell", "no-border");
this.addViews('<span>$$</span><span>$$</span>',
this.getModel().getItem(this.index).name,
this.getModel().getItem(this.index).quantity
);
this.addEvent("span", "onclick", "tableCellClicked");
},
handleEvents: function(evt, eventName) {
if (eventName === "tableCellClicked") {
this.callDelegate("shoppingCartItemClicked", [this.index]);
}
}
});
Class('TableViewController', {
model: null,
view: null,
// C'tor
TableViewController: function() {
this.view = new TableView();
this.model = new ShoppingList();
this.view.setDelegate(this);
this.view.setModel(this.model);
Viewport().addViews('$$',this.view);
},
// delegation method
shoppingCartItemClicked: function(index) {
// do something...
}
});
StackJS Boilerplate Generator