From b02a0f0e51b63699e670e45e9c9af75cbe70cf18 Mon Sep 17 00:00:00 2001 From: Dan Hromada Date: Mon, 12 May 2014 16:47:50 +0200 Subject: [PATCH] Fixed host switch (source and controls view) --- lib/popup.js | 89 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/lib/popup.js b/lib/popup.js index 32292d0..3607452 100644 --- a/lib/popup.js +++ b/lib/popup.js @@ -383,26 +383,6 @@ popup.applyTitles(); - /** - * Change host by select - */ - - popup.el.hostSelect.on('change', function(e) { - var host = $(this).val(); - if( host !== popup.protocol + '//' + popup.host ) { - popup.el.hostGoToLink.removeClass('is-hidden'); - popup.el.saveBtn.addClass('pure-button-disabled'); - } - else { - popup.el.hostGoToLink.addClass('is-hidden'); - popup.el.saveBtn.removeClass('pure-button-disabled'); - } - - var hostData = JSON.parse(localStorage.getItem(popup.key + '-' + host), true); - popup.applyData(hostData.data, true); - }); - - /** * Click to goTo host link */ @@ -454,21 +434,66 @@ * Auto save draft */ - setInterval(function() { - var draft = popup.getCurrentData(), - source = draft.source; - - if( (source || !popup.data.source) && source !== popup.data.source ) { - - popup.storage.setMode(popup.storage.MODE.private); - popup.storage.set('draft', draft); + var draftAutoSave = function() { + var draft = popup.getCurrentData(), + source = draft.source; - // Auto switch 'enable checkbox' on source edit - if( !popup.el.enableCheck.hasClass('not-auto-change') ) { - popup.el.enableCheck.prop('checked', true); + if( (source || !popup.data.source) && source !== popup.data.source ) { + + popup.storage.setMode(popup.storage.MODE.private); + popup.storage.set('draft', draft); + + // Auto switch 'enable checkbox' on source edit + if( !popup.el.enableCheck.hasClass('not-auto-change') ) { + popup.el.enableCheck.prop('checked', true); + } } + }, + draftAutoSaveInterval = setInterval(draftAutoSave, 2000); + + + /** + * Change host by select + */ + + popup.el.hostSelect.on('change', function(e) { + var host = $(this).val(), + hostData = JSON.parse(localStorage.getItem(popup.key + '-' + host), true);; + + if( host !== popup.protocol + '//' + popup.host ) { + // Stop making drafts + clearInterval(draftAutoSaveInterval); + + // Show goto link + popup.el.hostGoToLink.removeClass('is-hidden'); + + // Hide controls + popup.el.saveBtn.addClass('pure-button-disabled'); + popup.el.resetBtn.addClass('pure-button-disabled'); + popup.el.draftRemoveLink.addClass('is-hidden'); + + // Apply other host data + popup.applyData(hostData.data, true); } - }, 2000); + else { + // Start making drafts + draftAutoSaveInterval = setInterval(draftAutoSave, 2000); + + // Hide goto link + popup.el.hostGoToLink.addClass('is-hidden'); + + // Show controls + popup.el.saveBtn.removeClass('pure-button-disabled'); + popup.el.resetBtn.removeClass('pure-button-disabled'); + if( popup.storage.get('draft') ) { + popup.el.draftRemoveLink.removeClass('is-hidden'); + } + + // Apply current host data + popup.applyData(hostData.draft || hostData.data, hostData.draft ? false : true); + } + }); + /** * Protect 'enable checkbox' if was manually modified