aboutsummaryrefslogtreecommitdiff
path: root/chrome/content/code/editRedirect.xul.js
blob: 578f498ea2858f841882daa611ad72c02accb3ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//// $Id$

var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;

var EditRedirect = {
    txtExampleUrl : null,
    txtIncludePattern : null,
    txtRedirectUrl : null,
    txtExcludePattern : null,
    chkUnescapeMatches : null,
    rdoRegex : null,
    rdoWildcard : null, 
    
    onLoad : function() {
        var args = window.arguments[0];
		var redirect = args.redirect;
		this.txtExampleUrl = document.getElementById('txtExampleUrl');
		this.txtIncludePattern = document.getElementById('txtIncludePattern');
		this.txtRedirectUrl= document.getElementById('txtRedirectUrl');
		this.txtExcludePattern= document.getElementById('txtExcludePattern');
		this.chkUnescapeMatches= document.getElementById('chkUnescapeMatches');
		this.rdoWildcard= document.getElementById('rdoWildcard');
		this.rdoRegex = document.getElementById('rdoRegex');
	
		this.txtExampleUrl.value = redirect.exampleUrl;
		this.txtIncludePattern.value = redirect.includePattern;
		this.txtExcludePattern.value = redirect.excludePattern;
		this.txtRedirectUrl.value = redirect.redirectUrl;
		this.chkUnescapeMatches.setAttribute('checked', redirect.unescapeMatches);
        this.rdoRegex.setAttribute('selected', redirect.isRegex());
        this.rdoWildcard.setAttribute('selected', redirect.isWildcard());

        this.txtIncludePattern.focus();
        this.strings = document.getElementById("redirector-strings");
    },

    onAccept : function() {
		var args = window.arguments[0];
		args.saved = true;
		this.saveValues(args.redirect);
		
		var oldDisabled = args.redirect.disabled;
		args.redirect.disabled = false;
		if (!/^\s*$/.test(args.redirect.exampleUrl)) {
			var result = args.redirect.getMatch(args.redirect.exampleUrl);
			if (!result.isMatch) {
				//TODO: warn about match	
			} else {
				var resultUrl = '';
				var secondResult = args.redirect.getMatch(resultUrl);
				if (secondResult.isMatch) {
					//TODO: Warn about recursive match...	
				}
			}
		}
        return true;
    },

    msgBox : function(title, text) {
        Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
            .getService(Components.interfaces.nsIPromptService)
                .alert(window, title, text);
    },
    
    saveValues : function(redirect) {
		redirect.exampleUrl = this.txtExampleUrl.value;
		redirect.includePattern = this.txtIncludePattern.value;
		redirect.excludePattern = this.txtExcludePattern.value;
		redirect.redirectUrl = this.txtRedirectUrl.value;
		redirect.patternType = this.rdoRegex.getAttribute('selected') == 'true' ? Redirect.REGEX : Redirect.WILDCARD;
		var val = this.chkUnescapeMatches.getAttribute('checked');
		redirect.unescapeMatches = val === 'true' || val === true;
		//Disabled cannot be set here
    },
    
    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);}
    }
};