From ef84e3f661a26f995771c33f53c46388c0a786f2 Mon Sep 17 00:00:00 2001 From: Mavis Ou Date: Thu, 1 Aug 2013 12:33:02 -0700 Subject: [PATCH] [Issue #336] code cleanup and bug fixes --- data/collusion.js | 1 - data/list.js | 25 +++++++++------ data/ui.js | 79 +++++++++++++++++++++++++++++++---------------- 3 files changed, 68 insertions(+), 37 deletions(-) diff --git a/data/collusion.js b/data/collusion.js index 95f7d40..b4df28d 100644 --- a/data/collusion.js +++ b/data/collusion.js @@ -220,7 +220,6 @@ function stopSharing(callback){ '

By clicking OK you will no longer be uploading data.

' }, function(confirmed){ - console.log(confirmed); if ( confirmed ){ localStorage.userHasOptedIntoSharing = false; if (uploadTimer){ diff --git a/data/list.js b/data/list.js index 19441df..cb6c206 100644 --- a/data/list.js +++ b/data/list.js @@ -424,17 +424,22 @@ var listStageStackClickHandler = function(event){ } ); }else if (target.mozMatchesSelector('.hide-pref.active a')){ - dialog( { "name": "hideDialog", - "dnsPrompt": true, - "title": "Hide Sites", - "message": "

These sites will not be shown in Collusion visualizations, including List View, unless you specifically toggle them back on with the Show Hidden Sites button.

" + - "

You can use this to ignore trusted sites from the data.

" - },function(confirmed){ - if ( confirmed ){ - setPreferences('hide'); + var hideDialogName = "hideDialog"; + if ( doNotShowDialog(hideDialogName) ){ + setPreferences('hide'); + }else{ + dialog( { "name": hideDialogName, + "dnsPrompt": true, + "title": "Hide Sites", + "message": "

These sites will not be shown in Collusion visualizations, including List View, unless you specifically toggle them back on with the Show Hidden Sites button.

" + + "

You can use this to ignore trusted sites from the data.

" + },function(confirmed){ + if ( confirmed ){ + setPreferences('hide'); + } } - } - ); + ); + } }else if (target.mozMatchesSelector('.watch-pref.active a')){ setPreferences('watch'); }else if(target.mozMatchesSelector('.no-pref.active a')){ diff --git a/data/ui.js b/data/ui.js index 8266a24..b1801a9 100644 --- a/data/ui.js +++ b/data/ui.js @@ -387,51 +387,78 @@ function legendBtnClickHandler(legendElm){ // options: name, title, message, type, dnsPrompt(Do Not Show) function dialog(options,callback){ - var dnsPref = localStorage.dnsDialogs || "[]"; - dnsPref = JSON.parse(dnsPref); - if ( dnsPref.indexOf(options.name) > -1 ) return; // according to user pref, do not show this dialog - showDialog(options,dnsPref,callback); + if ( doNotShowDialog(options.name) ) return; // according to user pref, do not show this dialog + createDialog(options,callback); } -function showDialog(options,dnsPref,callback){ +function doNotShowDialog(dialogName){ + var dnsPref = localStorage.dnsDialogs || "[]"; + dnsPref = JSON.parse(dnsPref); + return ( dnsPref.indexOf(dialogName) > -1 ) ? true : false; +} + +function createDialog(options,callback){ var titleBar = "
" + (options.title || " ") + "
"; var messageBody = "
" + (options.message || " ") + "
"; - var controls = "
"+ - "" + - "
Cancel
" + - "
OK
" + - "
"; + var childElems = ""; + if ( options.dnsPrompt ){ // show Do Not Show Again prompt + childElems += "
Do not show this again.
"; + } + if ( navigator.appVersion.indexOf("Win") > -1 ){ // runs on Windows + childElems += "
Cancel
"; + childElems += "
OK
"; + }else{ + childElems += "
OK
"; + childElems += "
Cancel
"; + } + var controls = "
" + childElems + "
"; + // create dialog var modal = picoModal({ content: titleBar + messageBody + controls, closeButton: false, overlayClose: false, - // width: 400, overlayStyles: { backgroundColor: "#000", opacity: 0.75 } }); - if ( options.dnsPrompt ){ // show Do Not Show Again prompt - document.querySelector(".dialog-dns").classList.remove("hidden"); - } if ( options.type == "alert" ){ document.querySelector(".dialog-cancel").classList.add("hidden"); } - toArray(document.querySelectorAll(".pico-close")).forEach(function(btn){ - btn.addEventListener("click", function(event){ - if ( options.dnsPrompt && (event.target.innerHTML == "OK") ){ // Do Not Show - var checked = document.querySelector(".dialog-dns input").checked; - if ( checked ){ // user does not want this dialog to show again - dnsPref.push(options.name); - localStorage.dnsDialogs = JSON.stringify(dnsPref); - } - } - modal.close(); - callback( (event.target.innerHTML == "OK") ? true : false ); - }); + addDialogEventHandlers(modal,options,function(userResponse){ + callback(userResponse); }); } +function addDialogEventHandlers(modal,options,callback){ + // press Esc to close the dialog (functions the same as clicking Cancel) + var escapeDialogKeyHandler = function(e){ + if ( e.keyCode == "27" ){ // Esc key pressed + modal.close(); + callback(false); + } + } + document.addEventListener("keydown", escapeDialogKeyHandler); + modal.onClose(function(){ + document.removeEventListener("keydown", escapeDialogKeyHandler); + }); + // OK button click event handler + document.querySelector(".pico-close.dialog-ok").addEventListener("click",function(){ + if ( document.querySelector(".dialog-dns input") && document.querySelector(".dialog-dns input").checked ){ // Do Not Show + var dnsPref = localStorage.dnsDialogs || "[]"; + dnsPref = JSON.parse(dnsPref); + dnsPref.push(options.name); + localStorage.dnsDialogs = JSON.stringify(dnsPref); + } + modal.close(); + callback(true); + }); + // Cancel button click event handler + document.querySelector(".pico-close.dialog-cancel").addEventListener("click",function(){ + modal.close(); + callback(false); + }); +} \ No newline at end of file