aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/redirectorpage.js
blob: 9f0fba60a5f8ed1158f5ad4527e4432c95517b91 (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
// 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);

		storage.set({redirects:arr}, function() {
			console.log('Saved ' + arr.length + ' redirects at ' + new Date());
		});
	}
 
 	$s.redirects = [];
 	storage.get({redirects:[]}, function(results) {

 		for (var i=0; i < results.redirects.length; i++) {
			$s.redirects.push(normalize(results.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);
 	}
}]);