aboutsummaryrefslogtreecommitdiff
path: root/chrome/js/settings.js
blob: fbbc332b188677d0438cc19f1389ce4b4e9c2d08 (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
Components.utils.import("chrome://redirector/content/code/redirector.js");
Components.utils.import("chrome://redirector/content/code/redirectorprefs.js");
Components.utils.import("chrome://redirector/content/code/xpcom.js");

$(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));
			}				
			//this.addItemsToListBox(newlist);
		}
	}
	
	var template = $('#redirect-list').html().replace(/^\s*|\s$/g, '');
	function databind() {
		$('#redirect-list').html('');
		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.redirect = redirect;
		}
		
		$('#redirect-list li a').click(function() {
			alert(this.parentNode.parentNode.redirect);
		});
	}
	

	databind();
	$('#import').click(importRedirects);
	$('#export').click(exportRedirects);
	
	function bindConfig() {
		$('#config input[type="checkbox"]').each(function() {
			var pref = $(this).attr('data-pref');
			$(this).attr('checked', prefs[pref]);
		});
	}
	
	bindConfig();
	prefs.addListener({ changedPrefs:bindConfig});
	
	$('#config input[type="checkbox"]').bind('CheckboxStateChange', function() {
		var pref = $(this).attr('data-pref');
		prefs[pref] = !!$(this).attr('checked');
	});
});