From c2c8baf9da4e4e564a166c122895ab8ea5029dc3 Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Fri, 6 Jan 2012 20:57:47 +0100 Subject: More CRUD fixes --- chrome/js/redirector-ui.js | 356 ++++++++++++++++++++++++--------------------- chrome/redirector.html | 1 + 2 files changed, 190 insertions(+), 167 deletions(-) diff --git a/chrome/js/redirector-ui.js b/chrome/js/redirector-ui.js index 79e7fea..3152718 100644 --- a/chrome/js/redirector-ui.js +++ b/chrome/js/redirector-ui.js @@ -10,104 +10,209 @@ jQuery.fn.center = function () { return this; } -$(document).ready(function() { - $('link').attr('href',$('link').attr('href')+new Date()); - var prefs = new RedirectorPrefs(); - var strings = StringBundleService.createBundle('chrome://redirector/locale/redirector.properties', LocaleService.getApplicationLocale()); - function tr(name) { +function alert(title, msg) { + PromptService.alert(null, title, msg); +} + +function tr(name, args) { + if (args) { + return strings.formatStringFromName(name, args, args.length); + } else { return strings.GetStringFromName(name); } +} + +function validateRedirect(redirect) { + if (!/^\s*$/.test(redirect.exampleUrl)) { + var result = redirect.getMatch(redirect.exampleUrl); + if (!result.isMatch) { + title = tr('warningExampleUrlDoesntMatchPatternTitle'); + msg = tr('warningExampleUrlDoesntMatchPattern'); + var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService); + var rv = PromptService.confirmEx(window, title, msg, PromptService.STD_YES_NO_BUTTONS, PromptService.BUTTON_TITLE_YES, PromptService.BUTTON_TITLE_NO, null, null, {}); + return rv == 0; + } else { + var resultUrl = result.redirectTo; + if (!resultUrl.match(/https?:/)) { + var uri = IOService.newURI(redirect.exampleUrl, null, null); + resultUrl = uri.resolve(resultUrl); + } - function trPlural(name, count) { - name += count == 1 ? 'Singular' : ''; - return strings.formatStringFromName(name, [count],1); + var secondResult = args.redirect.getMatch(resultUrl); + if (secondResult.isMatch) { + title = this.strings.getString('errorExampleUrlMatchesRecursiveTitle'); + msg = this.strings.getFormattedString('errorExampleUrlMatchesRecursive', [args.redirect.exampleUrl, resultUrl]); + alert(title, msg); + return false; + } + } } + return true; +} - function getFile(captionKey, mode) { - var picker = new FilePicker(window, tr(captionKey), mode); - picker.defaultExtension = ".rjson"; - var dir = prefs.defaultDir; - if (dir) { - picker.displayDirectory = new LocalFile(dir); - } - picker.appendFilter(tr('redirectorFiles'), '*.rjson'); - - if (picker.show() == picker.returnCancel) { - return null; - } - prefs.defaultDir = picker.displayDirectory.path; - return picker.file; +function trPlural(name, count) { + name += count == 1 ? 'Singular' : ''; + return strings.formatStringFromName(name, [count],1); +} + +function getFile(captionKey, mode) { + var picker = new FilePicker(window, tr(captionKey), mode); + picker.defaultExtension = ".rjson"; + var dir = prefs.defaultDir; + if (dir) { + picker.displayDirectory = new LocalFile(dir); } + picker.appendFilter(tr('redirectorFiles'), '*.rjson'); - function exportRedirects() { - var file = getFile('exportCaption', FilePickerMode.save); - if (file) { - Redirector.exportRedirects(file); - } + if (picker.show() == picker.returnCancel) { + return null; } - - function importRedirects() { - var file = getFile('importCaption', FilePickerMode.open); - var result; - if (!file) { - return; - } - result = Redirector.importRedirects(file); - var msg, imported, existed; - imported = result & 0xFFFF; - existed = result >> 16; - - if (imported > 0) { - msg = trPlural('importedMessage', imported); - if (existed > 0) { - msg += ', ' + trPlural('existedMessage',existed); - } else { - msg += '.'; - } - } else if (imported == 0 && existed > 0) { - msg = trPlural('allExistedMessage', existed); - } else { //Both 0 - msg = tr('importedNone'); - } + prefs.defaultDir = picker.displayDirectory.path; + return picker.file; +} - var title = tr('importResult'); - PromptService.alert(null, title, msg); +function exportRedirects() { + var file = getFile('exportCaption', FilePickerMode.save); + if (file) { + Redirector.exportRedirects(file); + } +} - if (imported > 0) { - var newlist = []; - for (var i = Redirector.redirectCount-imported; i < Redirector.redirectCount; i++) { - newlist.push(Redirector.getRedirectAt(i)); - } - databind(); - } +function importRedirects() { + var file = getFile('importCaption', FilePickerMode.open); + var result; + if (!file) { + return; } + result = Redirector.importRedirects(file); + var msg, imported, existed; + imported = result & 0xFFFF; + existed = result >> 16; - var template = $('#redirect-list').html().replace(/^\s*|\s$/g, ''); - function databind() { - $('#redirect-list').empty(); - for (var i = 0; i < Redirector.redirectCount; i++) { - var redirect = Redirector.getRedirectAt(i); - var node = $(template); - node.find('.pattern').html(redirect.includePattern); - node.find('.redirectTo').html(redirect.redirectUrl); - node.find('.exampleUrl').html(redirect.exampleUrl); - node.find('.redirectResult').html(redirect.getMatch(redirect.exampleUrl).redirectTo); - node.appendTo('#redirect-list'); - node.data('redirect', redirect); + if (imported > 0) { + msg = trPlural('importedMessage', imported); + if (existed > 0) { + msg += ', ' + trPlural('existedMessage',existed); + } else { + msg += '.'; } + } else if (imported == 0 && existed > 0) { + msg = trPlural('allExistedMessage', existed); + } else { //Both 0 + msg = tr('importedNone'); } - - function bindRedirect(redirect) { - $('#description').val(redirect.description); - $('#example-url').val(redirect.exampleUrl); - $('#include-pattern').val(redirect.includePattern); - $('#exclude-pattern').val(redirect.excludePattern); - $('#redirect-to').val(redirect.redirectUrl); - $('#redirect-enabled').attr('checked', !redirect.disabled); - $('#unescape-matches').attr('checked', redirect.unescapeMatches); - $('#regex-pattern').attr('checked', redirect.patternType == Redirect.REGEX); - $('#wildcard-pattern').attr('checked', redirect.patternType == Redirect.WILDCARD); + + var title = tr('importResult'); + alert(title, msg); + + if (imported > 0) { + var newlist = []; + for (var i = Redirector.redirectCount-imported; i < Redirector.redirectCount; i++) { + newlist.push(Redirector.getRedirectAt(i)); + } + databind(); + } +} + +function databind() { + $('#redirect-list').empty(); + for (var i = 0; i < Redirector.redirectCount; i++) { + var redirect = Redirector.getRedirectAt(i); + var node = $(template); + node.find('.pattern').html(redirect.includePattern); + node.find('.redirectTo').html(redirect.redirectUrl); + node.find('.exampleUrl').html(redirect.exampleUrl); + node.find('.redirectResult').html(redirect.getMatch(redirect.exampleUrl).redirectTo); + node.appendTo('#redirect-list'); + node.data('redirect', redirect); + } +} + +function bindRedirect(redirect) { + $('#description').val(redirect.description); + $('#example-url').val(redirect.exampleUrl); + $('#include-pattern').val(redirect.includePattern); + $('#exclude-pattern').val(redirect.excludePattern); + $('#redirect-to').val(redirect.redirectUrl); + $('#redirect-enabled').attr('checked', !redirect.disabled); + $('#unescape-matches').attr('checked', redirect.unescapeMatches); + $('#regex-pattern').attr('checked', redirect.patternType == Redirect.REGEX); + $('#wildcard-pattern').attr('checked', redirect.patternType == Redirect.WILDCARD); +} + +function showRedirect(redirect) { + bindRedirect(redirect); + $('#redirect-form').center().css('top', '-=40px').show(); +} + +function controlsToRedirect(redirect) { + redirect.description = $('#description').val(); + redirect.exampleUrl = $('#example-url').val(); + redirect.includePattern = $('#include-pattern').val(); + redirect.excludePattern = $('#exclude-pattern').val(); + redirect.redirectUrl = $('#redirect-to').val(); + redirect.disabled = !$('#redirect-enabled').attr('checked'); + redirect.unescapeMatches = $('#unescape-matches').attr('checked'); + if ($('#regex-pattern').attr('checked')) { + redirect.patternType = Redirect.REGEX; + } else { + redirect.patternType = Redirect.WILDCARD; + } +} + +function saveRedirect() { + //First validate: + var tmpRedirect = new Redirect(); + controlsToRedirect(tmpRedirect); + if (!validateRedirect(tmpRedirect)) { + return; + } + var isNew = !window.editRedirect; + var redirect = isNew ? new Redirect() : window.editRedirect; + controlsToRedirect(redirect); + if (isNew) { + Redirector.addRedirect(redirect); } + Redirector.save(); + $('#redirect-form').hide(); + databind(); +} + +function configure() { + $('#config').show(); +} +function bindConfig() { + $('#config input[type="checkbox"]').each(function() { + var pref = $(this).attr('data-pref'); + $(this).attr('checked', prefs[pref]); + }); +} + +function testPattern() { + try { + var redirect = new Redirect(); + controlsToRedirect(redirect); + var extName = tr('extensionName'); + var result = redirect.test(); + if (result.isMatch) { + alert(extName, tr('testPatternSuccess', [redirect.includePattern, redirect.exampleUrl, result.redirectTo])); + } else if (result.isExcludeMatch) { + alert(extName, tr('testPatternExclude', [redirect.exampleUrl, redirect.excludePattern])); + } else { + alert(extName, tr('testPatternFailure', [redirect.includePattern, redirect.exampleUrl])); + } + } catch(e) { + alert('Error', e.toString()); + } +} + + +$(document).ready(function() { + window.template = $('#redirect-list').html().replace(/^\s*|\s$/g, ''); + window.strings = StringBundleService.createBundle('chrome://redirector/locale/redirector.properties', LocaleService.getApplicationLocale()); + window.prefs = new RedirectorPrefs(); + + $('link').attr('href',$('link').attr('href')+new Date()); $('#redirect-list li div a.delete').live('click', function(ev) { var redirect = $(this.parentNode.parentNode).data('redirect'); @@ -118,34 +223,6 @@ $(document).ready(function() { ev.preventDefault(); }); - function showRedirect(redirect) { - bindRedirect(redirect); - $('#redirect-form').center().css('top', '-=40px').show(); - } - - function saveRedirect() { - var isNew = !window.editRedirect; - var redirect = isNew ? new Redirect() : window.editRedirect; - redirect.description = $('#description').val(); - redirect.exampleUrl = $('#example-url').val(); - redirect.includePattern = $('#include-pattern').val(); - redirect.excludePattern = $('#exclude-pattern').val(); - redirect.redirectUrl = $('#redirect-to').val(); - redirect.disabled = !$('#redirect-enabled').attr('checked'); - redirect.unescapeMatches = $('#unescape-matches').attr('checked'); - if ($('#regex-pattern').attr('checked')) { - redirect.patternType = Redirect.REGEX; - } else { - redirect.patternType = Redirect.WILDCARD; - } - if (isNew) { - Redirector.addRedirect(redirect); - } - Redirector.save(); - $('#redirect-form').hide(); - databind(); - } - $('#redirect-list li div a.edit').live('click', function(ev) { var redirect = $(this.parentNode.parentNode).data('redirect'); window.editRedirect = redirect; @@ -161,16 +238,7 @@ $(document).ready(function() { $('#cancel').click(function() { $('#redirect-form').hide();}); $('#configure').click(configure); $('#save').click(saveRedirect); - - function configure() { - $('#config').show(); - } - function bindConfig() { - $('#config input[type="checkbox"]').each(function() { - var pref = $(this).attr('data-pref'); - $(this).attr('checked', prefs[pref]); - }); - } + $('#test-pattern').click(testPattern); bindConfig(); prefs.addListener({ changedPrefs:bindConfig}); @@ -217,49 +285,3 @@ $(document).ready(function() { prefs[pref] = !!$(this).attr('checked'); }); }); - -/* - testPattern : function() { - try { - var redirect = new Redirect(); - this.saveValues(redirect); - var extName = this.strings.getString('extensionName'); - var result = redirect.test(); - if (result.isMatch) { - this.msgBox(extName, this.strings.getFormattedString('testPatternSuccess', [redirect.includePattern, redirect.exampleUrl, result.redirectTo])); - } else if (result.isExcludeMatch) { - this.msgBox(extName, this.strings.getFormattedString('testPatternExclude', [redirect.exampleUrl, redirect.excludePattern])); - } else { - this.msgBox(extName, this.strings.getFormattedString('testPatternFailure', [redirect.includePattern, redirect.exampleUrl])); - } - } catch(e) {alert(e);} - } - - if (!/^\s*$/.test(args.redirect.exampleUrl)) { - var result = args.redirect.getMatch(args.redirect.exampleUrl); - if (!result.isMatch) { - title = this.strings.getString('warningExampleUrlDoesntMatchPatternTitle'); - msg = this.strings.getString('warningExampleUrlDoesntMatchPattern'); - var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService); - var rv = ps.confirmEx(window, title, msg, ps.STD_YES_NO_BUTTONS, ps.BUTTON_TITLE_YES, ps.BUTTON_TITLE_NO, null, null, {}); - return rv == 0; - } else { - var resultUrl = result.redirectTo; - if (!resultUrl.match(/https?:/)) { - var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); - var uri = ioService.newURI(args.redirect.exampleUrl, null, null); - resultUrl = uri.resolve(resultUrl); - } - - var secondResult = args.redirect.getMatch(resultUrl); - if (secondResult.isMatch) { - title = this.strings.getString('errorExampleUrlMatchesRecursiveTitle'); - msg = this.strings.getFormattedString('errorExampleUrlMatchesRecursive', [args.redirect.exampleUrl, resultUrl]); - this.msgBox(title, msg); - return false; - } - } - } - - -*/ \ No newline at end of file diff --git a/chrome/redirector.html b/chrome/redirector.html index 6a9de05..54676f1 100644 --- a/chrome/redirector.html +++ b/chrome/redirector.html @@ -69,6 +69,7 @@