From b0c6f7a2bff3d57f1c8d05a561dbd0bbca0228ca Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Sat, 2 Jun 2007 00:24:35 +0000 Subject: Redirector: All main functionality except Regex working. git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@57 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/overlay.js | 56 ++++++++++++++++++++++++--- chrome/content/overlay.xul | 9 +++++ chrome/content/redirect.js | 39 ++++++++++++------- chrome/content/redirect.xul | 8 +++- chrome/content/redirectList.js | 62 +++++++++++++++++++++++++++++- chrome/content/redirectList.xul | 28 +++++++++----- chrome/content/redirector.js | 59 +++++++++++++++++++++------- chrome/content/redirlib.js | 4 ++ chrome/content/statusactive.PNG | Bin 0 -> 360 bytes chrome/content/statusinactive.PNG | Bin 0 -> 396 bytes chrome/locale/en-US/redirect.dtd | 4 ++ chrome/locale/en-US/redirectList.dtd | 9 +++-- chrome/locale/en-US/redirector.dtd | 2 +- chrome/locale/en-US/redirector.properties | 6 ++- defaults/preferences/redirector.js | 1 + 15 files changed, 236 insertions(+), 51 deletions(-) create mode 100644 chrome/content/statusactive.PNG create mode 100644 chrome/content/statusinactive.PNG diff --git a/chrome/content/overlay.js b/chrome/content/overlay.js index 9092938..a230d23 100644 --- a/chrome/content/overlay.js +++ b/chrome/content/overlay.js @@ -32,6 +32,9 @@ var RedirectorOverlay = { } this.strings = document.getElementById("redirector-strings"); + this.prefObserver.register(); + this.setStatusBarImg(); + RedirLib.debug("Finished initialization"); this.initialized = true; @@ -74,17 +77,17 @@ var RedirectorOverlay = { onContextMenuCommand: function(event) { - params = { inn : { url : window.content.location.href}, out : {} }; + var item = { exampleUrl : window.content.location.href, pattern: window.content.location.href}; if (gContextMenu.onLink) { - params.inn.redirect = gContextMenu.link.toString(); + item.redirectUrl = gContextMenu.link.toString(); } window.openDialog("chrome://redirector/content/redirect.xul", "redirect", - "chrome,dialog,modal,centerscreen", params); + "chrome,dialog,modal,centerscreen", item); - if (params.out.pattern) { - Redirector.addRedirect(params.out); + if (item.saved) { + Redirector.addRedirect(item); } }, @@ -96,6 +99,47 @@ var RedirectorOverlay = { }, + toggleEnabled : function(event) { + RedirLib.setBoolPref('enabled', !RedirLib.getBoolPref('enabled')); + }, + + setStatusBarImg : function() { + var statusImg = $('redirector-statusbar-img'); + + if (RedirLib.getBoolPref('enabled')) { + statusImg.src = 'chrome://redirector/content/statusactive.png' + statusImg.setAttribute('tooltiptext', this.strings.getString('enabledTooltip')); + } else { + statusImg.src = 'chrome://redirector/content/statusinactive.png' + statusImg.setAttribute('tooltiptext', this.strings.getString('disabledTooltip')); + } + }, + + prefObserver : { + + getService : function() { + return Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranchInternal); + }, + + register: function() { + this.getService().addObserver('extensions.redirector', this, false); + }, + + unregister: function() { + this.getService().removeObserver('extensions.redirector', this); + }, + + observe : function(subject, topic, data) { + if (topic == 'nsPref:changed' && data == 'extensions.redirector.enabled') { + RedirectorOverlay.setStatusBarImg(); + } + } + + } + + }; window.addEventListener("load", function(event) { RedirectorOverlay.onLoad(event); }, false); -window.addEventListener("unload", function(event) { RedirectorOverlay.onUnload(event); }, false); \ No newline at end of file +window.addEventListener("unload", function(event) { RedirectorOverlay.onUnload(event); }, false); + + diff --git a/chrome/content/overlay.xul b/chrome/content/overlay.xul index c65df70..d180d1f 100644 --- a/chrome/content/overlay.xul +++ b/chrome/content/overlay.xul @@ -24,4 +24,13 @@ insertafter="context-stop" oncommand="RedirectorOverlay.onContextMenuCommand(event)"/> + + + + + + \ No newline at end of file diff --git a/chrome/content/redirect.js b/chrome/content/redirect.js index cfd2a2e..5be85f7 100644 --- a/chrome/content/redirect.js +++ b/chrome/content/redirect.js @@ -3,29 +3,40 @@ var Redirect = { onLoad : function() { - var params = window.arguments[0]; - $('txtExampleUrl').value = params.inn.url; - $('txtPattern').value = params.inn.url; - $('txtRedirectUrl').value = params.inn.redirect || ''; - + var item = window.arguments[0]; + item.saved = false; + $('txtExampleUrl').value = item.exampleUrl; + $('txtPattern').value = item.pattern; + $('txtRedirectUrl').value = item.redirectUrl || ''; + $('chkOnlyIfLinkExists').checked = item.onlyIfLinkExists || false; + + if (item.patternType == kRedirectorRegex) { + $('rdoRegex').setAttribute('selected', true); + $('rdoWildcard').setAttribute('selected', false); + } }, onAccept : function() { - var params = window.arguments[0]; - - params.out.pattern = $('txtPattern').value; - params.out.patternType = kRedirectorWildcard; - params.out.exampleUrl =$('txtExampleUrl').value; - params.out.redirectUrl = $('txtRedirectUrl').value; - params.out.onlyIfLinkExists = $('chkOnlyIfLinkExists').checked; + var item = window.arguments[0]; + + item.pattern = $('txtPattern').value; + if ($('rdoRegex').selected) { + item.patternType = kRedirectorRegex; + } else { + item.patternType = kRedirectorWildcard; + } + item.exampleUrl =$('txtExampleUrl').value; + item.redirectUrl = $('txtRedirectUrl').value; + item.onlyIfLinkExists = $('chkOnlyIfLinkExists').checked; + item.saved = true; return true; }, testPattern : function() { - try { + var match; + alert(Redirector.wildcardMatch($('txtPattern').value, $('txtExampleUrl').value)); - } catch(e) {alert(e);} } }; \ No newline at end of file diff --git a/chrome/content/redirect.xul b/chrome/content/redirect.xul index 5d6ff2c..8db1891 100644 --- a/chrome/content/redirect.xul +++ b/chrome/content/redirect.xul @@ -32,8 +32,14 @@ + + + + + + + - diff --git a/chrome/content/redirectList.js b/chrome/content/redirectList.js index 7a273cd..081359f 100644 --- a/chrome/content/redirectList.js +++ b/chrome/content/redirectList.js @@ -2,6 +2,9 @@ var RedirectList = { id : "redirector@einaregilsson.com", name : "Redirector", + lstRedirects: null, + btnDelete : null, + btnEdit : null, addItemsToListBox : function(items) { @@ -11,10 +14,13 @@ var RedirectList = { for each (item in items) { row = document.createElement('listitem'); - this.createCell(row, item.exampleUrl); this.createCell(row, item.pattern); + this.createCell(row, item.exampleUrl); this.createCell(row, item.redirectUrl); this.createCell(row, item.onlyIfLinkExists); + this.createCell(row, item.patternType); + + row.item = item; list.appendChild(row); } @@ -32,10 +38,64 @@ var RedirectList = { try { RedirLib.initialize(this); Redirector.init(); + this.lstRedirects = $('lstRedirects'); + this.btnDelete = $('btnDelete'); + this.btnEdit = $('btnEdit'); this.addItemsToListBox(Redirector.list); } catch(e) { alert(e); } + }, + + close : function() { + window.close(); + }, + + editRedirect : function() { + + var listItem = this.lstRedirects.selectedItem; + + if (!listItem) { + return; + } + + var item = listItem.item; + + window.openDialog("chrome://redirector/content/redirect.xul", + "redirect", + "chrome,dialog,modal,centerscreen", item); + + if (item.saved) { + var map = [item.pattern, item.exampleUrl, item.redirectUrl, item.onlyIfLinkExists, item.patternType]; + + for (var i in map) { + listItem.childNodes[i].setAttribute('value', map[i]); + listItem.childNodes[i].setAttribute('label', map[i]); + } + + Redirector.save(); + } + + }, + + deleteRedirect : function() { + var index = this.lstRedirects.selectedIndex; + + if (index == -1) { + return; + } + + try { + this.lstRedirects.removeItemAt(index); + Redirector.deleteAt(index); + } catch(e) {alert(e);} + }, + + selectionChange : function() { + var index = this.lstRedirects.selectedIndex; + + this.btnEdit.disabled = (index == -1); + this.btnDelete.disabled = (index == -1); } }; diff --git a/chrome/content/redirectList.xul b/chrome/content/redirectList.xul index 3c0b687..688457b 100644 --- a/chrome/content/redirectList.xul +++ b/chrome/content/redirectList.xul @@ -1,12 +1,13 @@ - @@ -14,24 +15,31 @@