all files / src/ Tags.js

46.84% Statements 37/79
50% Branches 3/6
29.41% Functions 5/17
46.84% Lines 37/79
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                        46× 46× 46× 46×                                                                                                                                     46× 46× 46×      
'use strict';
 
var log = require('./Log.js').Logger('libZotero:Tags');
 
module.exports = function(jsonBody){
	this.instance = 'Zotero.Tags';
	//represent collections as array for ordering purposes
	this.tagsVersion = 0;
	this.syncState = {
		earliestVersion: null,
		latestVersion: null
	};
	this.displayTagsArray = [];
	this.displayTagsUrl = '';
	this.tagObjects = {};
	this.tagsArray = [];
	this.loaded = false;
	Iif(jsonBody){
		this.addTagsFromJson(jsonBody);
	}
};
 
module.exports.prototype = new Zotero.Container();
 
module.exports.prototype.addTag = function(tag){
	var tags = this;
	tags.tagObjects[tag.apiObj.tag] = tag;
	tags.tagsArray.push(tag);
	Iif(tags.owningLibrary){
		tag.associateWithLibrary(tags.owningLibrary);
	}
};
 
module.exports.prototype.getTag = function(tagname){
	var tags = this;
	Eif(tags.tagObjects.hasOwnProperty(tagname)){
		return this.tagObjects[tagname];
	}
	return null;
};
 
module.exports.prototype.removeTag = function(tagname){
	var tags = this;
	delete tags.tagObjects[tagname];
	tags.updateSecondaryData();
};
 
module.exports.prototype.removeTags = function(tagnames){
	var tags = this;
	tagnames.forEach(function(tagname){
		delete tags.tagObjects[tagname];
	});
	tags.updateSecondaryData();
};
 
module.exports.prototype.plainTagsList = function(tagsArray){
	log.debug('Zotero.Tags.plainTagsList', 3);
	var plainList = [];
	tagsArray.forEach(function(tag){
		plainList.push(tag.apiObj.tag);
	});
	return plainList;
};
 
module.exports.prototype.clear = function(){
	log.debug('Zotero.Tags.clear', 3);
	this.tagsVersion = 0;
	this.syncState.earliestVersion = null;
	this.syncState.latestVersion = null;
	this.displayTagsArray = [];
	this.displayTagsUrl = '';
	this.tagObjects = {};
	this.tagsArray = [];
};
 
module.exports.prototype.updateSecondaryData = function(){
	log.debug('Zotero.Tags.updateSecondaryData', 3);
	var tags = this;
	tags.tagsArray = [];
	Object.keys(tags.tagObjects).forEach(function(key){
		var val = tags.tagObjects[key];
		tags.tagsArray.push(val);
	});
	tags.tagsArray.sort(Zotero.Tag.prototype.tagComparer());
	var plainList = tags.plainTagsList(tags.tagsArray);
	plainList.sort(Zotero.Library.prototype.comparer());
	tags.plainList = plainList;
};
 
module.exports.prototype.updateTagsVersion = function(tagsVersion) {
	var tags = this;
	Object.keys(tags.tagObjects).forEach(function(key){
		var tag = tags.tagObjects[key];
		tag.set('version', tagsVersion);
	});
};
 
module.exports.prototype.rebuildTagsArray = function() {
	var tags = this;
	tags.tagsArray = [];
	Object.keys(tags.tagObjects).forEach(function(key){
		var tag = tags.tagObjects[key];
		tags.tagsArray.push(tag);
	});
};
 
module.exports.prototype.addTagsFromJson = function(jsonBody){
	log.debug('Zotero.Tags.addTagsFromJson', 3);
	var tags = this;
	var tagsAdded = [];
	jsonBody.forEach(function(tagObj){
		var tag = new Zotero.Tag(tagObj);
		tags.addTag(tag);
		tagsAdded.push(tag);
	});
	return tagsAdded;
};