7 changed files with 168 additions and 35 deletions
@ -0,0 +1,72 @@
|
||||
// -----------------------------------------------
|
||||
// Common functions used in Options handling
|
||||
// -----------------------------------------------
|
||||
//
|
||||
// Get default values of preferences
|
||||
//
|
||||
function getPrefDefaults() { |
||||
return { |
||||
prefs: { |
||||
overrideDeleteConfirm: false, |
||||
selectTrashDelete: false, |
||||
selectJunkDelete: false, |
||||
consoleDebug: false, |
||||
disableDoneNotification: false, |
||||
alsoCompact: false |
||||
} |
||||
}; |
||||
} |
||||
|
||||
//
|
||||
// Get preferences from storage and return the structure asynchronously
|
||||
//
|
||||
async function getPrefs() { |
||||
let getting = await browser.storage.sync.get("prefs"); |
||||
|
||||
let fr = ""; |
||||
if ($.isEmptyObject(getting)) { |
||||
prefs = getPrefDefaults(); |
||||
fr = "defaults"; |
||||
} else { |
||||
prefs = getting; |
||||
fr = "storage"; |
||||
}; |
||||
console.log("Loaded prefs from " + fr); |
||||
|
||||
return getting; |
||||
} |
||||
|
||||
//
|
||||
// Handle the event of an option change in options UI. Event is passed in as
|
||||
// the 'ch' argument. This function records the changed option in to the
|
||||
// options storage and returns the updated structure asynchronously
|
||||
//
|
||||
async function changeOption(ch) { |
||||
var i = ch[0].id; |
||||
var c = ch[0].checked; |
||||
console.log("Pref: " + i + " -> " + c); |
||||
if (i == "overrideDeleteConfirm") { prefs.prefs.overrideDeleteConfirm = c; } |
||||
else if (i == "selectTrashDelete") { prefs.prefs.selectTrashDelete = c; } |
||||
else if (i == "selectJunkDelete") { prefs.prefs.selectJunkDelete = c; } |
||||
else if (i == "consoleDebug") { prefs.prefs.consoleDebug = c; } |
||||
else if (i == "disableDoneNotification") { prefs.prefs.disableDoneNotification = c; } |
||||
else if (i == "alsoCompact") { prefs.prefs.alsoCompact = c; } |
||||
await browser.storage.sync.set(prefs); |
||||
|
||||
prefs = await getPrefs(); |
||||
|
||||
return prefs; |
||||
} |
||||
|
||||
//
|
||||
// Given the preferences structure, update the options IO with appropriate values
|
||||
//
|
||||
function updateOptionsForm(prefs) { |
||||
$("#overrideDeleteConfirm").prop('checked', prefs.prefs.overrideDeleteConfirm); |
||||
$("#selectTrashDelete").prop('checked', prefs.prefs.selectTrashDelete); |
||||
$("#selectJunkDelete").prop('checked', prefs.prefs.selectJunkDelete); |
||||
$("#selectJunkDelete").prop('checked', prefs.prefs.selectJunkDelete); |
||||
$("#consoleDebug").prop('checked', prefs.prefs.consoleDebug); |
||||
$("#disableDoneNotification").prop('checked', prefs.prefs.disableDoneNotification); |
||||
$("#alsoCompact").prop('checked', prefs.prefs.alsoCompact); |
||||
} |
@ -1,3 +1,19 @@
|
||||
// -----------------------------------------------
|
||||
// Options UI and handling
|
||||
// -----------------------------------------------
|
||||
//
|
||||
// Load preferences from options storage. When done, update the options UI with
|
||||
// values. Then, set up the listener for option changes.
|
||||
//
|
||||
async function loadOptions() { |
||||
let prefs = await getPrefs(); |
||||
updateOptionsForm(prefs); |
||||
|
||||
$("input[type=checkbox]").change(function() { |
||||
changeOption($(this)); |
||||
}); |
||||
} |
||||
|
||||
$(function() { |
||||
loadOptions(); |
||||
}); |
||||
|
Loading…
Reference in new issue