aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/redirectorpage.js
blob: d18883c721de2e2eece8eca7e0ab4ff41f3eba00 (plain) (blame)
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
// This is the main controller of the page. It is responsible for showing messages,
// modal windows and loading and saving the list of redirects, that all of the
// controllers work with.
redirectorApp.controller('RedirectorPageCtrl', ['$scope', '$timeout', function($s, $timeout) {
	
	$s.deleting = null;  //Variable for redirect being edited, of the form { index:<nr>, redirect:<redirect>};
	$s.showEditForm = $s.showDeleteForm = false; // Variables, child controllers can set them to show their forms

	var storage = chrome.storage.local; //TODO: Change to sync when Firefox supports it...

	function normalize(r) {
		return new Redirect(r).toObject(); //Cleans out any extra props, and adds default values for missing ones.
	}

	// Saves the entire list of redirects to storage.
	$s.saveChanges = function() {

		// Clean them up so angular $$hash things and stuff don't get serialized.
		var arr = $s.redirects.map(normalize);

		chrome.runtime.sendMessage({type:"saveredirects", redirects:arr}, function(response) {
			console.log('Saved ' + arr.length + ' redirects at ' + new Date() + '. Message from background page:' + response.message);
		});
	}
 
 	$s.redirects = [];
	
	//Need to proxy this through the background page, because Firefox gives us dead objects
	//nonsense when accessing chrome.storage directly.
	chrome.runtime.sendMessage({type: "getredirects"}, function(response) {
		console.log('Received redirects message, count=' + response.redirects.length);
 		for (var i=0; i < response.redirects.length; i++) {
			$s.redirects.push(normalize(response.redirects[i]));
		}
		$s.$apply();
	}); 	

 	// Shows a message bar above the list of redirects.
 	$s.showMessage = function(message, success) {
 		$s.message = message;
 		$s.messageType = success ? 'success' : 'error';

 		//Remove the message in 20 seconds if it hasn't been changed...
 		$timeout(function() {
 			if ($s.message == message) {
 				$s.message = null;
 			}
 		}, 20 * 1000);
 	}
}]);