'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;
|