libzotero
Version:
javascript libZotero
72 lines (62 loc) • 2.23 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var log = require('./Log.js').Logger('libZotero:Preferences');
var Preferences = function Preferences(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 === 'undefined' ? 'undefined' : _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];
if (!storageObjectString) {
preferences.preferencesObject = {};
} else {
preferences.preferencesObject = JSON.parse(storageObjectString);
}
};
module.exports = Preferences;