Skip to content

Instantly share code, notes, and snippets.

@restlessmedia
Last active August 29, 2015 14:03
Show Gist options
  • Save restlessmedia/351340bc3af843dd18f5 to your computer and use it in GitHub Desktop.
Save restlessmedia/351340bc3af843dd18f5 to your computer and use it in GitHub Desktop.
Javascript prototype inheritance
var app = {};
Function.prototype.inherits = function (base) {
var isFunction = base.constructor == Function;
this.prototype = isFunction ? new base : base;
this.prototype.constructor = this;
return this;
};
(function (app) {
var forEach = function (collection, fn) {
var count = collection.length;
if (count) {
var i = 0;
for (; i < count; i++) {
if (fn.call(collection[i], collection[i]) === false) {
break;
}
}
}
};
var forEachWith = function (collection, context, fn) {
forEach(collection, function () {
fn.call(context, this);
});
};
app.utils = {
forEach: forEach,
forEachWith: forEachWith
}
}(app));
(function (app) {
var Modal = function (title) {
this.title = title;
};
Modal.prototype.show = function () {
this.el.style.display = 'block';
};
Modal.prototype.hide = function () {
this.el.style.display = 'none';
};
Modal.prototype.init = function () {
this.el = document.createElement('div');
this.hide();
var footer = document.createElement('div');
footer.className = 'footer';
this.el.appendChild(footer);
document.body.appendChild(this.el);
return this;
};
Modal.prototype.remove = function () {
document.removeChild(this.el);
};
Modal.prototype.footer = function () {
var footer;
app.utils.forEach(this.el.childNodes, function () {
if (this.className === 'footer') {
footer = this;
return false;
}
});
return footer;
};
app.modal = {
ctor: Modal,
create: function (title) {
return new Modal(title).init();
}
}
}(app));
(function (app) {
var Dialog = function (title, actions) {
if (actions) {
this.actions = actions;
}
};
Dialog.inherits(app.modal.ctor);
Dialog.prototype.addAction = function (action) {
var button = document.createElement('button');
button.value = button.innerText = action.title;
button.onclick = action.fn;
this.footer().appendChild(button);
};
Dialog.prototype.init = function () {
app.modal.ctor.prototype.init.apply(this);
app.utils.forEachWith(this.actions, this, this.addAction);
return this;
};
Dialog.prototype.actions = [];
app.dialog = {
ctor: Dialog,
create: function (title) {
return new Dialog(title).init();
}
}
}(app));
(function (app) {
var Confirm = function (title, ok, cancel) {
this.title = title;
this.actions = [
{
title: 'Ok',
fn: function () {
// ok()
console.log('action.ok')
}
},
{
title: 'Cancel',
fn: function () {
// cancel()
console.log('action.Cancel')
}
}
];
};
Confirm.inherits(app.dialog.ctor);
app.confirm = {
ctor: Confirm,
create: function (title, ok, cancel) {
return new Confirm(title, ok, cancel).init();
}
}
}(app));
var visible = false;
var c = app.confirm.create('Do you want to remove this?');
document.getElementById('toggle').onclick = function(){
if(!visible) {
c.show();
}else{
c.hide();
}
visible = !visible;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment