moved to a modular onject constructor...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-11-18 20:20:35 +03:00
parent 150cce52f6
commit d281b6e06e
5 changed files with 83 additions and 81 deletions

View File

@ -110,8 +110,7 @@ if(typeof(sha1) != 'undefined'){
// Data class methods and API...
//
var DataClassPrototype =
module.DataClassPrototype = {
var DataClassPrototype = {
// NOTE: we consider the input list sorted...
fromArray: function(list){
var res = new Data()
@ -139,8 +138,7 @@ module.DataClassPrototype = {
// Data object methods and API...
//
var DataPrototype =
module.DataPrototype = {
var DataPrototype = {
/*****************************************************************/
//
@ -188,7 +186,21 @@ module.DataPrototype = {
// NOTE: ribbons are sparse...
// NOTE: ribbons can be compact when serialized...
//
//
/*****************************************************************/
// XXX is this a good name for this??? (see: object.js)
__init__: function(json){
// load initial state...
if(json != null){
this.loadJSON(json)
} else {
this._reset()
}
return this
},
/******************************************************* Utils ***/
// Make a sparse list of image gids...
@ -1806,32 +1818,12 @@ module.DataPrototype = {
/*********************************************************************/
// Main Data object...
//
/*
var Data =
module.Data =
function Data(json){
// in case this is called as a function (without new)...
if(this.constructor !== Data){
return new Data(json)
}
module.Data =
object.makeConstructor('Data',
DataClassPrototype,
DataPrototype)
// load initial state...
if(json != null){
this.loadJSON(json)
} else {
this._reset()
}
return this
}
Data.__proto__ = DataClassPrototype
Data.prototype = DataPrototype
Data.prototype.constructor = Data
*/
var Data =
module.Data = object.makeConstructor('Data', DataClassPrototype, DataPrototype)
/**********************************************************************

View File

@ -11,6 +11,9 @@ console.log('>>> images')
var sha1 = require('./ext-lib/sha1')
var object = require('object')
/*********************************************************************/
@ -260,6 +263,18 @@ module.ImagesClassPrototype = {
var ImagesPrototype =
module.ImagesPrototype = {
// XXX is this a good name for this??? (see: object.js)
__init__: function(json){
// load initial state...
if(json != null){
this.loadJSON(json)
} else {
this._reset()
}
return this
},
get length(){
return Object.keys(this).length
},
@ -546,27 +561,11 @@ module.ImagesPrototype = {
/*********************************************************************/
// Main Images object...
//
var Images =
module.Images =
function Images(json){
// in case this is called as a function (without new)...
if(this.constructor !== Images){
return new Images(json)
}
// load initial state...
if(json != null){
this.loadJSON(json)
} else {
this._reset()
}
return this
}
Images.__proto__ = ImagesClassPrototype
Images.prototype = ImagesPrototype
Images.prototype.constructor = Images
module.Images =
object.makeConstructor('Images',
ImagesClassPrototype,
ImagesPrototype)

View File

@ -20,23 +20,36 @@ function makeConstructor(name, a, b){
var proto = b == null ? a : b
var cls_proto = b == null ? b : a
var _constructor = function Constructor(json){
var _constructor = function Constructor(){
// in case this is called as a function (without new)...
if(this.constructor !== _constructor){
return new _constructor(json)
// NOTE: the folowing does the job of the 'new' operator but
// with one advantage, we can now pass arbitrarry args
// in...
// This is equivalent to:
// return new _constructor(json)
var obj = {}
obj.__proto__ = _constructor.prototype
obj.constructor = _constructor
} else {
var obj = this
}
// load initial state...
if(json != null){
this.loadJSON(json)
} else {
this._reset()
if(obj.__init__ != null){
obj.__init__.apply(obj, arguments)
}
return this
return obj
}
eval('_constructor = '+ _constructor.toString().replace(/Constructor/g, name))
// this is here to make Chrome output more user friendly...
if(_constructor.name == 'Constructor'){
eval('_constructor = '+ _constructor
.toString()
.replace(/Constructor/g, name))
}
_constructor.__proto__ = cls_proto
_constructor.prototype = proto

View File

@ -13,6 +13,8 @@ console.log('>>> ribbons')
// XXX is this correct...
require('ext-lib/jquery')
var object = require('object')
var data = require('data')
var images = require('images')
@ -64,8 +66,7 @@ var RIBBON = '.ribbon:not(.clone)'
//
/*********************************************************************/
var RibbonsClassPrototype =
module.RibbonsClassPrototype = {
var RibbonsClassPrototype = {
// Generic getters...
getElemGID: function(elem){
return JSON.parse('"' + elem.attr('gid') + '"')
@ -114,8 +115,7 @@ module.RibbonsClassPrototype = {
// NOTE: this is a low level interface, not a set of actions...
var RibbonsPrototype =
module.RibbonsPrototype = {
var RibbonsPrototype = {
//
// .viewer (jQuery object)
//
@ -123,6 +123,12 @@ module.RibbonsPrototype = {
//
// XXX to update images we need to know about images...
__init__: function(viewer, images){
this.viewer = $(viewer)
this.images = images
},
// Constructors...
createViewer: RibbonsClassPrototype.createViewer,
createRibbon: RibbonsClassPrototype.createRibbon,
@ -1757,30 +1763,17 @@ module.RibbonsPrototype = {
},
_setup: function(viewer, images){
this.viewer = $(viewer)
this.images = images
},
}
// Main Ribbons object...
//
var Ribbons =
module.Ribbons =
function Ribbons(viewer, images){
// in case this is called as a function (without new)...
if(this.constructor !== Ribbons){
return new Ribbons(viewer, images)
}
this._setup(viewer, images)
/*********************************************************************/
return this
}
Ribbons.__proto__ = RibbonsClassPrototype
Ribbons.prototype = RibbonsPrototype
Ribbons.prototype.constructor = Ribbons
var Ribbons =
module.Ribbons =
object.makeConstructor('Ribbons',
RibbonsClassPrototype,
RibbonsPrototype)

View File

@ -29,8 +29,13 @@ var TagsPrototype = {
}
/*********************************************************************/
var Tags =
module.Tags = object.makeConstructor('Tags', TagsClassPrototype, TagsPrototype)
module.Tags =
object.makeConstructor('Tags',
TagsClassPrototype,
TagsPrototype)