www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 9d58fac7e0ee12fe048994e061662e363e7bcafd
parent b4c8dbe7003e307a79c274254af4a92ffb24dc9e
Author: Dan Stillman <dstillman@zotero.org>
Date:   Fri,  4 Aug 2006 04:34:16 +0000

Abstracted the Scholar.*Types logic to a base function that can be extended and added singletons for the various types -- rock the JS prototype model


Diffstat:
Mchrome/chromeFiles/content/scholar/xpcom/data_access.js | 277+++++++++++++++++++++----------------------------------------------------------
1 file changed, 73 insertions(+), 204 deletions(-)

diff --git a/chrome/chromeFiles/content/scholar/xpcom/data_access.js b/chrome/chromeFiles/content/scholar/xpcom/data_access.js @@ -2828,27 +2828,54 @@ Scholar.Tags = new function(){ + /* - * Same structure as Scholar.ItemTypes and FileTypes -- - * make changes in both places if possible + * Base function for retrieving ids and names of static types stored in the DB + * (e.g. creatorType, fileType, charset, itemType) + * + * Extend using the following code within a child constructor: + * + * Scholar.CachedTypes.apply(this, arguments); + * this.constructor.prototype = new Scholar.CachedTypes(); + * + * And the following properties: + * + * this._typeDesc = 'c'; + * this._idCol = ''; + * this._nameCol = ''; + * this._table = ''; + * this._ignoreCase = true; + * */ -Scholar.CreatorTypes = new function(){ +Scholar.CachedTypes = function(){ var _types = new Array(); var _typesLoaded; var self = this; + // Override these variables in child classes + this._typeDesc = ''; + this._idCol = ''; + this._nameCol = ''; + this._table = ''; + this._ignoreCase = true; + this.getName = getName; this.getID = getID; this.getTypes = getTypes; - function getName(idOrName){ if (!_typesLoaded){ _load(); } + idOrName = idOrName + ''; + + if (this._ignoreCase){ + idOrName = idOrName.toLowerCase(); + } + if (!_types[idOrName]){ - Scholar.debug('Invalid creator type ' + idOrName, 1); + Scholar.debug('Invalid ' + this._typeDesc + ' ' + idOrName, 1); } return _types[idOrName]['name']; @@ -2860,8 +2887,12 @@ Scholar.CreatorTypes = new function(){ _load(); } + if (this._ignoreCase){ + idOrName = idOrName.toLowerCase(); + } + if (!_types[idOrName]){ - Scholar.debug('Invalid creator type ' + idOrName, 1); + Scholar.debug('Invalid ' + this._typeDesc + ' ' + idOrName, 1); } return _types[idOrName]['id']; @@ -2869,8 +2900,8 @@ Scholar.CreatorTypes = new function(){ function getTypes(){ - return Scholar.DB.query('SELECT creatorTypeID AS id, ' - + 'creatorType AS name FROM creatorTypes order BY creatorType'); + return Scholar.DB.query('SELECT ' + this._idCol + ' AS id, ' + + this._nameCol + ' AS name FROM ' + this._table + ' order BY ' + this._nameCol); } @@ -2881,7 +2912,7 @@ Scholar.CreatorTypes = new function(){ // Store as both id and name for access by either var typeData = { id: types[i]['id'], - name: types[i]['name'] + name: this._ignoreCase ? types[i]['name'].toLowerCase() : types[i]['name'] } _types[types[i]['id']] = typeData; _types[types[i]['name']] = _types[types[i]['id']]; @@ -2892,230 +2923,68 @@ Scholar.CreatorTypes = new function(){ } +Scholar.CreatorTypes = new function(){ + Scholar.CachedTypes.apply(this, arguments); + this.constructor.prototype = new Scholar.CachedTypes(); + + this._typeDesc = 'creator type'; + this._idCol = 'creatorTypeID'; + this._nameCol = 'creatorType'; + this._table = 'creatorTypes'; + this._ignoreCase = true; +} -/* - * Same structure as Scholar.CreatorTypes and FileTypes -- - * make changes in both places if possible - */ Scholar.ItemTypes = new function(){ - var _types = new Array(); - var _typesLoaded; - var self = this; - - this.getName = getName; - this.getID = getID; - this.getTypes = getTypes; - - - function getName(idOrName){ - if (!_typesLoaded){ - _load(); - } - - if (!_types[idOrName]){ - Scholar.debug('Invalid item type ' + idOrName, 1); - } - - return _types[idOrName]['name']; - } - - - function getID(idOrName){ - if (!_typesLoaded){ - _load(); - } - - if (!_types[idOrName]){ - Scholar.debug('Invalid item type ' + idOrName, 1); - } - - return _types[idOrName]['id']; - } - - - function getTypes(){ - return Scholar.DB.query('SELECT itemTypeID AS id, typeName AS name ' - + 'FROM itemTypes order BY typeName'); - } - + Scholar.CachedTypes.apply(this, arguments); + this.constructor.prototype = new Scholar.CachedTypes(); - function _load(){ - var types = self.getTypes(); - - for (i in types){ - // Store as both id and name for access by either - var typeData = { - id: types[i]['id'], - name: types[i]['name'] - } - _types[types[i]['id']] = typeData; - _types[types[i]['name']] = _types[types[i]['id']]; - } - - _typesLoaded = true; - } + this._typeDesc = 'item type'; + this._idCol = 'itemTypeID'; + this._nameCol = 'typeName'; + this._table = 'itemTypes'; + this._ignoreCase = true; } - - - -/* - * Same structure as Scholar.ItemTypes and CreatorTypes -- - * make changes in both places if possible - */ Scholar.FileTypes = new function(){ - var _types = new Array(); - var _typesLoaded; - var self = this; - - this.getName = getName; - this.getID = getID; - this.getIDFromMIMEType = getIDFromMIMEType; - this.getTypes = getTypes; + Scholar.CachedTypes.apply(this, arguments); + this.constructor.prototype = new Scholar.CachedTypes(); + this._typeDesc = 'file type'; + this._idCol = 'fileTypeID'; + this._nameCol = 'fileType'; + this._table = 'fileTypes'; + this._ignoreCase = true; - function getName(idOrName){ - if (!_typesLoaded){ - _load(); - } - - if (!_types[idOrName]){ - Scholar.debug('Invalid file type ' + idOrName, 1); - } - - return _types[idOrName]['name']; - } - - - function getID(idOrName){ - if (!_typesLoaded){ - _load(); - } - - if (!_types[idOrName]){ - Scholar.debug('Invalid file type ' + idOrName, 1); - } - - return _types[idOrName]['id']; - } - + this.getIDFromMIMEType = getIDFromMIMEType; function getIDFromMIMEType(mimeType){ // TODO } - - - function getTypes(){ - return Scholar.DB.query('SELECT fileTypeID AS id, ' - + 'fileType AS name FROM fileTypes order BY fileType'); - } - - - function _load(){ - var types = self.getTypes(); - - for (i in types){ - // Store as both id and name for access by either - var typeData = { - id: types[i]['id'], - name: types[i]['name'] - } - _types[types[i]['id']] = typeData; - _types[types[i]['name']] = _types[types[i]['id']]; - } - - _typesLoaded = true; - } } - -/* - * Same structure as Scholar.ItemTypes, CreatorTypes, etc. -- - * make changes in all versions if possible - */ Scholar.CharacterSets = new function(){ - var _types = new Array(); - var _typesLoaded; - var self = this; + Scholar.CachedTypes.apply(this, arguments); + this.constructor.prototype = new Scholar.CachedTypes(); - var _typeDesc = 'character set'; - var _idCol = 'charsetID'; - var _nameCol = 'charset'; - var _table = 'charsets'; - var _ignoreCase = true; + this._typeDesc = 'character sets'; + this._idCol = 'charsetID'; + this._nameCol = 'charset'; + this._table = 'charsets'; + this._ignoreCase = true; - this.getName = getName; - this.getID = getID; - this.getTypes = getTypes; this.getAll = getAll; - function getName(idOrName){ - if (!_typesLoaded){ - _load(); - } - - if (_ignoreCase){ - idOrName = idOrName.toLowerCase(); - } - - if (!_types[idOrName]){ - Scholar.debug('Invalid ' + _typeDesc + ' ' + idOrName, 1); - } - - return _types[idOrName]['name']; - } - - - function getID(idOrName){ - if (!_typesLoaded){ - _load(); - } - - if (_ignoreCase){ - idOrName = idOrName.toLowerCase(); - } - - if (!_types[idOrName]){ - Scholar.debug('Invalid ' + _typeDesc + ' ' + idOrName, 1); - } - - return _types[idOrName]['id']; - } - - - function getTypes(){ - return Scholar.DB.query('SELECT ' + _idCol + ' AS id, ' - + _nameCol + ' AS name FROM ' + _table + ' order BY ' + _nameCol); - } - - function getAll(){ return this.getTypes(); } - - - function _load(){ - var types = self.getTypes(); - - for (i in types){ - // Store as both id and name for access by either - var typeData = { - id: types[i]['id'], - name: _ignoreCase ? types[i]['name'].toLowerCase() : types[i]['name'] - } - _types[types[i]['id']] = typeData; - _types[types[i]['name']] = _types[types[i]['id']]; - } - - _typesLoaded = true; - } } + Scholar.ItemFields = new function(){ // Private members var _fields = new Array();