aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/editredirect.js
blob: f2f613c689b6ba4f8a3af3ff598e61b67ebf419b (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
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
redirectorApp.controller('EditRedirectCtrl', ['$scope', function($s) {
	

	$s.requestTypes = Redirect.requestTypes;

	// Ok, this is pretty ugly. But I want to make this controller to control
	// everything about the editing process, so I make this available on
	// the parent scope, so the RedirectListCtrl can access it.
	$s.$parent.editRedirect = function(index) {
		$s.redirect = new Redirect($s.redirects[index]); 
		$s.editIndex = index;
		$s.redirect.updateExampleResult();
		if ($s.redirect.escapeMatches || $s.redirect.unescapeMatches || $s.redirect.excludePattern
			|| !($s.redirect.appliesTo.length == 1 && $s.redirect.appliesTo[0] == "main_frame")) {
			$s.showAdvanced = true; //Auto show advanced if redirect uses advanced options
		}
		$s.$parent.showEditForm = true;
	};

	// Same, this is for the Create New button, which is starting
	// the edit form, so I want to control it from here.
	$s.$parent.createNewRedirect = function() {
		$s.redirect = new Redirect({});
		$s.$parent.showEditForm = true;
	};

	$s.saveRedirect = function() {
		if ($s.redirect.error) {
			return; //Button is already disabled, but we still get the click
		}

		//Just make sure it's freshly updated when saved
		$s.redirect.updateExampleResult();

		if ($s.editIndex >= 0) {
			$s.redirects[$s.editIndex] = $s.redirect;
		} else {
			$s.redirects.push($s.redirect);
		}
		closeEditForm();
		$s.saveChanges();
	};

	$s.cancelEdit = function() {
		closeEditForm();
	}

	// To bind a list of strings to a list of checkboxes
	$s.appliesTo = function(key) {
		if (!$s.redirect) {
			return;
		}
		return $s.redirect.appliesTo.indexOf(key) != -1;
	};

	// Add or remove string from array based on whether checkbox is checked
	$s.toggleApplies = function(key) {
		if (!$s.redirect) {
			return;
		}
		var arr = $s.redirect.appliesTo;

		var index = arr.indexOf(key);
		if (index == -1) {
			arr.push(key);
		} else {
			arr.splice(index, 1);
		}

		var order = 'main_frame,sub_frame,stylesheet,script,image,object,xmlhttprequest,other';

		arr.sort(function(a,b) {
			return order.indexOf(a) - order.indexOf(b);
		});

		$s.redirect.updateExampleResult();
	};
	
	function closeEditForm() {
		$s.editIndex = -1;
		$s.redirect = null;
		$s.showAdvanced = false;
		$s.$parent.showEditForm = false;
	}
}]);