summaryrefslogtreecommitdiff
path: root/static/mailremind.js
blob: 0e3725afdcb32a4cd1dc7f0f7579b7f8c5ae0dcf (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
$(function() {
	var mkRow = function() {
		var row = $("<tr/>");

		row.append($("<td><input type=\"text\" name=\"Start\" /></td>"));

		var yn = mkYNCheckbox("RepetitionEnabled", false);
		var cell = $("<td/>");
		cell.append(yn.hidden);
		cell.append(yn.checkbox);
		row.append(cell);

		cell = $("<td/>");
		cell.append($("<input type=\"text\" class=\"quant\" name=\"Count\" /><span> </span>"));
		var unitsel = $("<select name=\"Unit\" size=\"0\" />");
		var units = ["Minute", "Hour", "Day", "Week", "Month", "Year"];
		for(i in units) {
			unitsel.append($("<option value=\""+units[i]+"\">"+units[i]+"(s)</option>"));
		}
		cell.append(unitsel);
		row.append(cell);

		yn = mkYNCheckbox("EndEnabled", false);
		cell = $("<td/>");
		cell.append(yn.hidden);
		cell.append(yn.checkbox);
		row.append(cell);

		row.append($("<td><input type=\"text\" name=\"End\" /></td>"));

		attachFocusHandler($("input, select", row));
		return row;
	};

	var mkYNCheckbox = function(name, b) {
		var hidden = $("<input type=\"hidden\" />");
		hidden.prop("value", b ? "yes" : "no");
		hidden.prop("name", name);

		var checkbox = $("<input type=\"checkbox\" />");
		checkbox.prop("checked", b);
		checkbox.change(function() {
			hidden.prop("value", checkbox.prop("checked") ? "yes" : "no");
		});

		return {"hidden": hidden, "checkbox": checkbox};
	};

	$("select.enabler").each(function(i) {
		var self = $(this);
		var yn = mkYNCheckbox(self.prop("name"), self.val() == "yes");

		self.before(yn.hidden);
		self.before(yn.checkbox);
		self.remove();
	});

	var maxSchedules = $("table.schedules tbody tr").length;
	var checkInsRow = function() {
		if($("table.schedules tbody tr").length >= maxSchedules) {
			return;
		}

		$("table.schedules tbody").append(mkRow());
	};

	var attachFocusHandler = function(q) {
		q.focus(function() {
			var myrow = $(this).parent().parent();
			$("table.schedules tbody tr").not(myrow).each(function(i) {
				checkRemoveRow($(this));
			});

			checkInsRow();
		});
	};

	var checkRemoveRow = function(row) {
		if($("input[name=\"Start\"]", row).val() == "") {
			row.remove();
		}
	};

	var updateSchedulesTab = function() {
		$("table.schedules tbody tr").each(function(i) {
			checkRemoveRow($(this));
		});

		checkInsRow();
	};

	attachFocusHandler($("table.schedules input, table.schedules select"));

	updateSchedulesTab();
});