Skip to content

Commit

Permalink
Remove options save cancel buttons (switch to autosave #381)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanoemcke committed Aug 22, 2017
1 parent 0c543b1 commit c50941a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
10 changes: 10 additions & 0 deletions src/js/gsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ var gsUtils = {
var whitelistString = whitelistItems.join('\n');
this.setOption(this.WHITELIST, whitelistString);
this.syncSettings({ [this.WHITELIST]: whitelistString });
this.updateOptionsView();
},

testForMatch: function (whitelistItem, word) {
Expand Down Expand Up @@ -257,6 +258,7 @@ var gsUtils = {
whitelist = this.cleanupWhitelist(whitelist);
this.setOption(this.WHITELIST, whitelist);
this.syncSettings({ [this.WHITELIST]: whitelist });
this.updateOptionsView();
},

cleanupWhitelist: function (whitelist) {
Expand All @@ -277,6 +279,14 @@ var gsUtils = {
}
},

updateOptionsView: function () {
chrome.tabs.query({ url: chrome.extension.getURL('options.html') }, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.sendMessage(tabs[i].id, { action: 'reloadOptions' });
}
})
},

fetchLastVersion: function () {
var version = localStorage.getItem(this.APP_VERSION);
if (version !== null) {
Expand Down
54 changes: 22 additions & 32 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,17 @@
setSyncNoteVisibility(false);
}
}

var valueChanged = saveChange(element);
if (valueChanged) {
gsUtils.syncSettings();
var prefKey = elementPrefMap[element.id];
gsUtils.performPostSaveUpdates([prefKey]);
}
};
}

function saveChange(element, updatedPreferences) {
function saveChange(element) {

var pref = elementPrefMap[element.id],
oldValue = gsUtils.getOption(pref),
Expand All @@ -155,51 +162,25 @@
//save option
gsUtils.setOption(elementPrefMap[element.id], newValue);

if (oldValue !== newValue) {
updatedPreferences.push(pref);
}
}

function closeSettings() {
//only close the window if we were opened in a new tab.
//else, go back to the page we were on.
//this is to fix closing tabs if they were opened from the context menu.
if (window.history.length > 1) {
window.history.back();
} else {
window.close();
}
return (oldValue !== newValue);
}

gsUtils.documentReadyAsPromsied(document).then(function () {

initSettings();

var optionEls = document.getElementsByClassName('option'),
saveEl = document.getElementById('saveBtn'),
cancelEl = document.getElementById('cancelBtn'),
element,
i;

//add change listeners for all 'option' elements
for (i = 0; i < optionEls.length; i++) {
element = optionEls[i];
element.onchange = handleChange(element);
}
saveEl.onclick = function (e) {
var updatedPreferences = [];
for (i = 0; i < optionEls.length; i++) {
saveChange(optionEls[i], updatedPreferences);
if (element.tagName === 'TEXTAREA') {
element.addEventListener('input', handleChange(element), false);
} else {
element.onchange = handleChange(element);
}

// Push out all our saved settings to sync storage.
gsUtils.syncSettings();
gsUtils.performPostSaveUpdates(updatedPreferences);

closeSettings();
};
cancelEl.onclick = function (e) {
closeSettings();
};

//hide incompatible sidebar items if in incognito mode
Expand All @@ -209,6 +190,15 @@
});
window.alert('You are in incognito mode. Any changes to settings from within an incognito session will be reset when this session is closed.');
}
});

//listen for background events
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
switch (request.action) {

case 'reloadOptions':
initSettings();
return false;
}
});
}());
5 changes: 0 additions & 5 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ <h2>Other</h2>
Turning this on will overwrite settings on all synced systems
</div>
</div>
<hr />

<a href="#" id="cancelBtn" class="fl btn btnNeg">Cancel</a>
<a href="#" id="saveBtn" class="fr btn">Save settings</a>

</div>

</div>
Expand Down

0 comments on commit c50941a

Please sign in to comment.