aboutsummaryrefslogtreecommitdiff
path: root/chrome/js/redirector-ui.js
blob: 643f3f61861af8f81234325acd9913a794d18cf1 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Components.utils.import("chrome://redirector/content/js/redirect.js");
Components.utils.import("chrome://redirector/content/js/redirector.js");
Components.utils.import("chrome://redirector/content/js/redirectorprefs.js");
Components.utils.import("chrome://redirector/content/js/xpcom.js");

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

$(document).ready(function() {
	var prefs = new RedirectorPrefs();
	var strings = StringBundleService.createBundle('chrome://redirector/locale/redirector.properties', LocaleService.getApplicationLocale());
	function tr(name) {
		return strings.GetStringFromName(name);
	}
	
	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');
		
		if (picker.show() == picker.returnCancel) {
			return null;
		}
		prefs.defaultDir = picker.displayDirectory.path;
		return picker.file;
	}
	
	function exportRedirects() {
		var file = getFile('exportCaption', FilePickerMode.save);
		if (file) {
			Redirector.exportRedirects(file);
		}
	}
	
	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');
		}

		var title = tr('importResult');
		PromptService.alert(null, title, msg);

		if (imported > 0) {
			var newlist = [];
			for (var i = Redirector.redirectCount-imported; i < Redirector.redirectCount; i++) {
				newlist.push(Redirector.getRedirectAt(i));
			}				
			databind();
		}
	}
	
	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);
		}
	}
	
	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);
	}

	$('#redirect-list li div a.delete').live('click', function(ev) {
		var redirect = $(this.parentNode.parentNode).data('redirect');
		if (PromptService.confirm(null, tr("deleteConfirmationTitle"), tr("deleteConfirmationText"))) {
			Redirector.deleteRedirect(redirect);
			$(this.parentNode.parentNode).fadeOut(function() { $(this).remove(); });
		}
		ev.preventDefault();
	});
	
	function showRedirect(redirect) {
		bindRedirect(redirect);
		$('#redirect-form').center().css('top', '-=40px').show();
	}

	$('#redirect-list li div a.edit').live('click', function(ev) {
		var redirect = $(this.parentNode.parentNode).data('redirect');
		showRedirect(redirect);
		ev.preventDefault();
	});

	databind();
	$('#import').click(importRedirects);
	$('#export').click(exportRedirects);
	$('#new-redirect').click(function() { showRedirect({patternType:Redirect.REGEX});});
	$('#configure').click(configure);
	$('#cancel').click(function() { $('#redirect-form').hide();});
	$('#configure').click(configure);
	
	function configure() {
		$('#config').show();
	}
	function bindConfig() {
		$('#config input[type="checkbox"]').each(function() {
			var pref = $(this).attr('data-pref');
			$(this).attr('checked', prefs[pref]);
		});
	}
	
	bindConfig();
	prefs.addListener({ changedPrefs:bindConfig});

	var movingElement = null;
	$('li').mousedown(function(ev) {
		if (ev.target && ev.target.tagName == 'A') {
			return;
		}
		$(this).css('background', '-moz-linear-gradient(top, #aac, #99b)');
		$('#redirect-list').css('cursor', 'move');
		movingElement = this;
	});


	$('li').mouseover(function() {
		if (movingElement && this !== movingElement) {
			if ($(movingElement).offset().top > $(this).offset().top) {
				$(movingElement).detach().insertBefore(this);
			} else {
				$(movingElement).detach().insertAfter(this);
			}
		}
	});
	
	$(document).mouseup(function() {
		if (movingElement) {
			$(movingElement).css('background', '');
			movingElement = null;
			$('#redirect-list').css('cursor', '');
			var newOrder = {};
			$('#redirect-list li').each(function(i) {
				newOrder[$(this).data('redirect')] = i;
			});

			Redirector.sortRedirects(function(a,b) {
				return newOrder[a] - newOrder[b];
			});
		}
	});

	$('#config input[type="checkbox"]').bind('CheckboxStateChange', function() {
		var pref = $(this).attr('data-pref');
		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;
				}
			}
		}
	
	
*/