all files / src/ Preferences.js

48.72% Statements 19/39
12.5% Branches 1/8
28.57% Functions 2/7
48.72% Lines 19/39
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                                                                                                             
'use strict';
 
var log = require('./Log.js').Logger('libZotero:Preferences');
 
var Preferences = function(store, idString) {
	this.store = store;
	this.idString = idString;
	this.preferencesObject = {};
	this.defaults = {
		debug_level: 3, //lower level is higher priority
		debug_log: true,
		debug_mock: false,
		listDisplayedFields: ['title', 'creator', 'dateModified'],
		showAutomaticTags: false,//tagType:1 is automatic, tagType:0 was added by user
		itemsPerPage: 25,
		order: 'title',
		title: 'asc'
	};
	this.load();
};
 
Preferences.prototype.setPref = function(key, value) {
	var preferences = this;
	preferences.preferencesObject[key] = value;
	preferences.persist();
};
 
Preferences.prototype.setPrefs = function(newPrefs) {
	var preferences = this;
	if(typeof(newPrefs) != 'object') {
		throw new Error('Preferences must be an object');
	}
	preferences.preferencesObject = newPrefs;
	preferences.persist();
};
 
Preferences.prototype.getPref = function(key){
	var preferences = this;
	if(preferences.preferencesObject[key]){
		return preferences.preferencesObject[key];
	}
	else if(preferences.defaults[key]){
		return preferences.defaults[key];
	}
	else {
		return null;
	}
};
 
Preferences.prototype.getPrefs = function(){
	var preferences = this;
	return preferences.preferencesObject;
};
 
Preferences.prototype.persist = function(){
	var preferences = this;
	var storageString = 'preferences_' + preferences.idString;
	preferences.store[storageString] = JSON.stringify(preferences.preferencesObject);
};
 
Preferences.prototype.load = function(){
	var preferences = this;
	var storageString = 'preferences_' + preferences.idString;
	var storageObjectString = preferences.store[storageString];
	Eif(!storageObjectString){
		preferences.preferencesObject = {};
	}
	else {
		preferences.preferencesObject = JSON.parse(storageObjectString);
	}
};
 
module.exports = Preferences;