Skip to content

Commit

Permalink
move a bunch of RESutils options-related functions into settings.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jewel-andraia committed Feb 6, 2015
1 parent f3693d2 commit 0827c99
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 188 deletions.
194 changes: 193 additions & 1 deletion lib/core/settings.js
@@ -1,3 +1,126 @@
var RESUtils = RESUtils || {};

RESUtils.options = {};

RESUtils.options.listTypes = {};
RESUtils.options.listTypes['subreddits'] = {
source: '/api/search_reddit_names.json?app=res',
hintText: 'type a subreddit name',
onResult: function(response) {
var names = response.names,
results = names.map(function(name) {
return {
id: name,
name: name
};
});
return results;
},
onCachedResult: function(response) {
var names = response.names,
results = names.map(function(name) {
return {
id: name,
name: name
};
});
return results;
}
};


(function() {
RESUtils.options.table = {};

RESUtils.options.table.getMatchingValue = function (moduleID, optionKey, valueIdentifiers) {
var option = modules[moduleID].options[optionKey];
var values = option.value;
var matchingValue;
if (!(option.type === 'table' && values && values.length)) return;

for (var vi = 0, vlength = values.length; vi < vlength; vi++) {
var value = values[vi];
var match = false;
for (var fi = 0, flength = option.fields.length; fi < flength; fi++) {
var field = option.fields[fi];
var fieldValue = value[fi];
var matchValue = RESUtils.firstValid(valueIdentifiers[fi], valueIdentifiers[field.name]);

if (matchValue === void 0) {
continue;
} else if (matchValue === fieldValue) {
match = true;
continue;
} else {
match = false;
break;
}
}

if (match) {
matchingValue = value;
break;
}
}

return matchingValue;
};

RESUtils.options.table.addValue = function (moduleID, optionKey, value) {
var option = modules[moduleID].options[optionKey];
if (option.type !== 'table') {
console.error('Tried to save table value to non-table option: modules[\'' + moduleID + '\'].options.' + optionKey);
return;
}

if (!option.value) {
option.value = [];
}
var values = option.value;


var optionValue = [];
for (var i = 0, length = option.fields.length; i < length; i++) {
var field = option.fields[i];

var fieldValue = RESUtils.firstValid(value[i], value[field.name], field.value);
optionValue.push(fieldValue);
}

values.push(optionValue);
RESUtils.setOption(moduleID, optionKey, values);

return optionValue;
};

RESUtils.options.table.getMatchingValueOrAdd = function (moduleID, optionKey, valueIdentifier, hydrateValue) {
var matchingValue = RESUtils.options.table.getMatchingValue(moduleID, optionKey, valueIdentifier);
if (!matchingValue) {
var value = valueIdentifier;
if (hydrateValue) {
value = hydrateValue(valueIdentifier);
}

matchingValue = RESUtils.options.table.addValue(moduleID, optionKey, value);
}

return matchingValue;
};

RESUtils.options.table.mapValueToObject = function (moduleID, optionKey, value) {
var option = modules[moduleID].options[optionKey];

var object = {};
for (var i = 0, length = option.fields.length; i < length; i++) {
var field = option.fields[i];

object[field.name] = value[i];
}

return object;
};
})();

var RESSettings = {
resetModulePrefs: function() {
this.setModulePrefs({});
Expand Down Expand Up @@ -86,5 +209,74 @@ var RESSettings = {
if (typeof modules[moduleID].onToggle === 'function') {
modules[moduleID].onToggle(onOrOff);
}
}
},
setOption: function(moduleID, optionName, optionValue) {
if (/_[\d]+$/.test(optionName)) {
optionName = optionName.replace(/_[\d]+$/, '');
}
var thisOptions = this.getOptions(moduleID);
var saveOptionValue;
if (optionValue === '') {
saveOptionValue = '';
} else if ((isNaN(optionValue)) || (typeof optionValue === 'boolean') || (typeof optionValue === 'object')) {
saveOptionValue = optionValue;
} else if (optionValue.indexOf('.') !== -1) {
saveOptionValue = parseFloat(optionValue);
} else {
saveOptionValue = parseInt(optionValue, 10);
}
thisOptions[optionName].value = saveOptionValue;
// save it to the object and to RESStorage
RESSettings.saveModuleOptions(moduleID, thisOptions);
return true;
},
saveModuleOptions: function(moduleID, newOptions) {
function minify(obj) {
var min = {};
if (obj) {
for (var key in obj) {
if ('value' in obj[key]) {
min[key] = {value: obj[key].value};
}
}
}
return min;
}
if (newOptions) {
modules[moduleID].options = newOptions;
}
RESStorage.setItem('RESoptions.' + moduleID, JSON.stringify(minify(modules[moduleID].options)));
},
getOptionsFirstRun: [],
getOptions: function(moduleID) {
if (this.getOptionsFirstRun[moduleID]) {
// we've already grabbed these out of localstorage, so modifications should be done in memory. just return that object.
return modules[moduleID].options;
}
var thisOptions = RESStorage.getItem('RESoptions.' + moduleID);
if ((thisOptions) && (thisOptions !== 'undefined') && (thisOptions !== null)) {
// merge options (in case new ones were added via code) and if anything has changed, update to localStorage
var storedOptions = safeJSON.parse(thisOptions, 'RESoptions.' + moduleID);
var codeOptions = modules[moduleID].options;
var newOption = false;
for (var attrname in codeOptions) {
codeOptions[attrname].default = codeOptions[attrname].value;
if (typeof storedOptions[attrname] === 'undefined') {
newOption = true;
} else {
codeOptions[attrname].value = storedOptions[attrname].value;
}
}
modules[moduleID].options = codeOptions;
if (newOption) {
RESUtils.saveModuleOptions(moduleID);
}
} else {
// nothing in localStorage, let's set the defaults...
RESUtils.saveModuleOptions(moduleID);
}
this.getOptionsFirstRun[moduleID] = true;
return modules[moduleID].options;
};
};

0 comments on commit 0827c99

Please sign in to comment.