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... // Data class methods and API...
// //
var DataClassPrototype = var DataClassPrototype = {
module.DataClassPrototype = {
// NOTE: we consider the input list sorted... // NOTE: we consider the input list sorted...
fromArray: function(list){ fromArray: function(list){
var res = new Data() var res = new Data()
@ -139,8 +138,7 @@ module.DataClassPrototype = {
// Data object methods and API... // Data object methods and API...
// //
var DataPrototype = var DataPrototype = {
module.DataPrototype = {
/*****************************************************************/ /*****************************************************************/
// //
@ -188,7 +186,21 @@ module.DataPrototype = {
// NOTE: ribbons are sparse... // NOTE: ribbons are sparse...
// NOTE: ribbons can be compact when serialized... // 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 ***/ /******************************************************* Utils ***/
// Make a sparse list of image gids... // Make a sparse list of image gids...
@ -1806,32 +1818,12 @@ module.DataPrototype = {
/*********************************************************************/ /*********************************************************************/
// Main Data object... // Main Data object...
//
/*
var Data = var Data =
module.Data = module.Data =
function Data(json){ object.makeConstructor('Data',
// in case this is called as a function (without new)... DataClassPrototype,
if(this.constructor !== Data){ DataPrototype)
return new Data(json)
}
// 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 sha1 = require('./ext-lib/sha1')
var object = require('object')
/*********************************************************************/ /*********************************************************************/
@ -260,6 +263,18 @@ module.ImagesClassPrototype = {
var ImagesPrototype = var ImagesPrototype =
module.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(){ get length(){
return Object.keys(this).length return Object.keys(this).length
}, },
@ -546,27 +561,11 @@ module.ImagesPrototype = {
/*********************************************************************/ /*********************************************************************/
// Main Images object... // Main Images object...
//
var Images = var Images =
module.Images = module.Images =
function Images(json){ object.makeConstructor('Images',
// in case this is called as a function (without new)... ImagesClassPrototype,
if(this.constructor !== Images){ ImagesPrototype)
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

View File

@ -20,23 +20,36 @@ function makeConstructor(name, a, b){
var proto = b == null ? a : b var proto = b == null ? a : b
var cls_proto = b == null ? b : a 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)... // in case this is called as a function (without new)...
if(this.constructor !== _constructor){ 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... // load initial state...
if(json != null){ if(obj.__init__ != null){
this.loadJSON(json) obj.__init__.apply(obj, arguments)
} else {
this._reset()
} }
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.__proto__ = cls_proto
_constructor.prototype = proto _constructor.prototype = proto

View File

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

View File

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