109f67ee8013c7bb37f4d2999168a571f99e5f59
[timetracker.git] / WEB-INF / templates / time_script.tpl
1 <script>
2 // This script is shared by time.tpl, time_edit.tpl (both regular and mobile),
3 // and also by WEB-INF/templates/mobile/timer.tpl.
4 // This creates certain restrictions, such as the form name being "timeRecordForm",
5 // variables such as $client_list, $project_list and others to be assigned in php files
6 // for all pages. Things need to be tested together for all involved files.
7
8 // We need a few arrays to populate project and task dropdowns.
9 // When client selection changes, the project dropdown must be re-populated with only relevant projects.
10 // When project selection changes, the task dropdown must be repopulated similarly.
11 // Format:
12 // project_ids[143] = "325,370,390,400";  // Comma-separated list of project ids for client.
13 // project_names[325] = "Time Tracker";   // Project name.
14 // task_ids[325] = "100,101,302,303,304"; // Comma-separated list ot task ids for project.
15 // task_names[100] = "Coding";            // Task name.
16
17 // Prepare an array of project ids for clients.
18 var project_ids = new Array();
19 {foreach $client_list as $client}
20   project_ids[{$client.id}] = "{$client.projects}";
21 {/foreach}
22 // Prepare an array of project names.
23 var project_names = new Array();
24 {foreach $project_list as $project}
25   project_names[{$project.id}] = "{$project.name|escape:'javascript'}";
26 {/foreach}
27 // We'll use this array to populate project dropdown when client is not selected.
28 var idx = 0;
29 var projects = new Array();
30 {foreach $project_list as $project}
31   projects[idx] = new Array("{$project.id}", "{$project.name|escape:'javascript'}");
32   idx++;
33 {/foreach}
34
35 // Prepare an array of task ids for projects.
36 var task_ids = new Array();
37 {foreach $project_list as $project}
38   task_ids[{$project.id}] = "{$project.tasks}";
39 {/foreach}
40 // Prepare an array of task names.
41 var task_names = new Array();
42 {foreach $task_list as $task}
43   task_names[{$task.id}] = "{$task.name|escape:'javascript'}";
44 {/foreach}
45
46 // Mandatory top options for project and task dropdowns.
47 var empty_label_project = "{$i18n.dropdown.select|escape:'javascript'}";
48 var empty_label_task = "{$i18n.dropdown.select|escape:'javascript'}";
49
50 // The fillDropdowns function populates the "project" and "task" dropdown controls
51 // with relevant values.
52 function fillDropdowns() {
53   if(document.body.contains(document.timeRecordForm.client))
54     fillProjectDropdown(document.timeRecordForm.client.value);
55
56   fillTaskDropdown(document.timeRecordForm.project.value);
57 }
58
59 // The fillProjectDropdown function populates the project combo box with
60 // projects associated with a selected client (client id is passed here as id).
61 function fillProjectDropdown(id) {
62   var str_ids = project_ids[id];
63   var dropdown = document.getElementById("project");
64   // Determine previously selected item.
65   var selected_item = dropdown.options[dropdown.selectedIndex].value;
66
67   // Remove existing content.
68   dropdown.length = 0;
69   var project_reset = true;
70   // Add mandatory top option.
71   dropdown.options[0] = new Option(empty_label_project, '', true);
72
73   // Populate project dropdown.
74   if (!id) {
75     // If we are here, client is not selected.
76     var len = projects.length;
77     for (var i = 0; i < len; i++) {
78       dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
79       if (dropdown.options[i+1].value == selected_item)  {
80         dropdown.options[i+1].selected = true;
81         project_reset = false;
82       }
83     }
84   } else if (str_ids) {
85     var ids = new Array();
86     ids = str_ids.split(",");
87     var len = ids.length;
88
89     for (var i = 0; i < len; i++) {
90       var p_id = ids[i];
91       dropdown.options[i+1] = new Option(project_names[p_id], p_id);
92       if (dropdown.options[i+1].value == selected_item)  {
93         dropdown.options[i+1].selected = true;
94         project_reset = false;
95       }
96     }
97   }
98
99   // If project selection was reset - clear the tasks dropdown.
100   if (project_reset) {
101     dropdown = document.getElementById("task");
102     dropdown.length = 0;
103     dropdown.options[0] = new Option(empty_label_task, '', true);
104   }
105 }
106
107 // The fillTaskDropdown function populates the task combo box with
108 // tasks associated with a selected project (project id is passed here as id).
109 function fillTaskDropdown(id) {
110   var str_ids = task_ids[id];
111
112   var dropdown = document.getElementById("task");
113   if (dropdown == null) return; // Nothing to do.
114
115   // Determine previously selected item.
116   var selected_item = dropdown.options[dropdown.selectedIndex].value;
117
118   // Remove existing content.
119   dropdown.length = 0;
120   // Add mandatory top option.
121   dropdown.options[0] = new Option(empty_label_task, '', true);
122
123   // Populate the dropdown from the task_names array.
124   if (str_ids) {
125     var ids = new Array();
126     ids = str_ids.split(",");
127     var len = ids.length;
128
129     var idx = 1;
130     for (var i = 0; i < len; i++) {
131       var t_id = ids[i];
132       if (task_names[t_id]) {
133         dropdown.options[idx] = new Option(task_names[t_id], t_id);
134         idx++;
135       }
136     }
137
138     // If a previously selected item is still in dropdown - select it.
139     if (dropdown.options.length > 0) {
140       for (var i = 0; i < dropdown.options.length; i++) {
141         if (dropdown.options[i].value == selected_item) {
142           dropdown.options[i].selected = true;
143         }
144       }
145     }
146
147     // Select a task if user is required to do so and there is only one task available.
148     if ({$user->task_required} && dropdown.options.length == 2) { // 2 because of mandatory top option.
149       dropdown.options[1].selected = true;
150     }
151   }
152 }
153
154 // The formDisable function disables some fields depending on what we have in other fields.
155 function formDisable(formField) {
156   var formFieldValue = eval("document.timeRecordForm." + formField + ".value");
157   var formFieldName = eval("document.timeRecordForm." + formField + ".name");
158   var x;
159
160   if (((formFieldValue != "") && (formFieldName == "start")) || ((formFieldValue != "") && (formFieldName == "finish"))) {
161     x = eval("document.timeRecordForm.duration");
162     x.value = "";
163     x.disabled = true;
164     x.style.background = "#e9e9e9";
165   }
166
167   if (((formFieldValue == "") && (formFieldName == "start") && (document.timeRecordForm.finish.value == "")) || ((formFieldValue == "") && (formFieldName == "finish") && (document.timeRecordForm.start.value == ""))) {
168     x = eval("document.timeRecordForm.duration");
169     x.value = "";
170     x.disabled = false;
171     x.style.background = "white";
172   }
173
174   if ((formFieldValue != "") && (formFieldName == "duration")) {
175     x = eval("document.timeRecordForm.start");
176     x.value = "";
177     x.disabled = true;
178     x.style.background = "#e9e9e9";
179     x = eval("document.timeRecordForm.finish");
180     x.value = "";
181     x.disabled = true;
182     x.style.background = "#e9e9e9";
183   }
184
185   if ((formFieldValue == "") && (formFieldName == "duration")) {
186     x = eval("document.timeRecordForm.start");
187     x.disabled = false;
188     x.style.background = "white";
189     x = eval("document.timeRecordForm.finish");
190     x.disabled = false;
191     x.style.background = "white";
192   }
193 }
194
195 // The setNow function fills a given field with current time.
196 function setNow(formField) {
197   var x = eval("document.timeRecordForm.start");
198   x.disabled = false;
199   x.style.background = "white";
200   x = eval("document.timeRecordForm.finish");
201   x.disabled = false;
202   x.style.background = "white";
203   var today = new Date();
204   var time_format = '{$user->time_format}';
205   var obj = eval("document.timeRecordForm." + formField);
206   obj.value = today.strftime(time_format);
207   formDisable(formField);
208 }
209
210 function get_date() {
211   var date = new Date();
212   return date.strftime("%Y-%m-%d");
213 }
214
215 function get_time() {
216   var date = new Date();
217   return date.strftime("%H:%M");
218 }
219 </script>