all files / src/ Tag.js

44.9% Statements 22/49
18.18% Branches 4/22
37.5% Functions 3/8
44.9% Lines 22/49
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97      46× 46× 46× 46× 46×                 46× 46× 46× 46×                                                                                                                                
'use strict';
 
var log = require('./Log.js').Logger('libZotero:Tag');
 
module.exports = function (tagObj) {
	this.instance = 'Zotero.Tag';
	this.color = null;
	this.version = 0;
	Eif( typeof tagObj == 'object'){
		this.parseJsonTag(tagObj);
	} else if(typeof tagObj == 'string') {
		this.parseJsonTag(this.templateApiObj(tagObj));
	} else {
		this.parseJsonTag(this.tamplateApiObj(''));
	}
};
 
module.exports.prototype = new Zotero.ApiObject();
 
module.exports.prototype.parseJsonTag = function(tagObj) {
	var tag = this;
	tag.apiObj = Z.extend({}, tagObj);
	tag.urlencodedtag = encodeURIComponent(tag.apiObj.tag);
	tag.version = tag.apiObj.version;
};
 
module.exports.prototype.templateApiObj = function(tagString) {
	return {
		tag: tagString,
		links: {},
		meta: {
			type:0,
			numItems:1
		}
	};
};
 
module.exports.prototype.tagComparer = function(){
	if(Intl){
		var collator = new Intl.Collator();
		return function(a, b){
			return collator.compare(a.apiObj.tag, b.apiObj.tag);
		};
	} else {
		return function(a, b) {
			if(a.apiObj.tag.toLocaleLowerCase() == b.apiObj.tag.toLocaleLowerCase()){
				return 0;
			}
			if(a.apiObj.tag.toLocaleLowerCase() < b.apiObj.tag.toLocaleLowerCase()){
				return -1;
			}
			return 1;
		};
	}
};
 
module.exports.prototype.set = function(key, val){
	var tag = this;
	
	if(key in tag.apiObj){
		tag.apiObj[key] = val;
	}
	if(key in tag.apiObj.meta){
		tag.apiObj.meta[key] = val;
	}
	
	switch (key) {
		case 'tagVersion':
		case 'version':
			tag.version = val;
			tag.apiObj.version = val;
			break;
	}
	
	return tag;
};
 
module.exports.prototype.get = function(key){
	var tag = this;
 
	if(key in tag.apiObj){
		return tag.apiObj[key];
	}
	Eif(key in tag.apiObj.meta){
		return tag.apiObj.meta[key];
	}
 
	switch (key) {
		case 'tagVersion':
		case 'version':
			return tag.version;
	}
 
	return null;
};