A bit of cleanup in JavaScript, removed repeated var declarations.
[timetracker.git] / WEB-INF / templates / time_edit.tpl
1 <script>
2 // We need a few arrays to populate project and task dropdowns.
3 // When client selection changes, the project dropdown must be re-populated with only relevant projects.
4 // When project selection changes, the task dropdown must be repopulated similarly.
5 // Format:
6 // project_ids[143] = "325,370,390,400";  // Comma-separated list of project ids for client.
7 // project_names[325] = "Time Tracker";   // Project name.
8 // task_ids[325] = "100,101,302,303,304"; // Comma-separated list ot task ids for project.
9 // task_names[100] = "Coding";            // Task name.
10
11 //Prepare an array of projects ids for clients.
12 project_ids = new Array();
13 {foreach $client_list as $client}
14   project_ids[{$client.id}] = "{$client.projects}";
15 {/foreach}
16 // Prepare an array of project names.
17 project_names = new Array();
18 {foreach $project_list as $project}
19   project_names[{$project.id}] = "{$project.name|escape:'javascript'}";
20 {/foreach}
21 // We'll use this array to populate project dropdown when client is not selected.
22 var idx = 0;
23 projects = new Array();
24 {foreach $project_list as $project}
25   projects[idx] = new Array("{$project.id}", "{$project.name|escape:'javascript'}");
26   idx++;
27 {/foreach}
28
29 // Prepare an array of task ids for projects.
30 task_ids = new Array();
31 {foreach $project_list as $project}
32   task_ids[{$project.id}] = "{$project.tasks}";
33 {/foreach}
34 // Prepare an array of task names.
35 task_names = new Array();
36 {foreach $task_list as $task}
37   task_names[{$task.id}] = "{$task.name|escape:'javascript'}";
38 {/foreach}
39
40 // Mandatory top options for project and task dropdowns.
41 empty_label_project = '{$i18n.dropdown.select|escape:'javascript'}';
42 empty_label_task = '{$i18n.dropdown.select|escape:'javascript'}';
43
44 // The fillDropdowns function populates the "project" and "task" dropdown controls
45 // with relevant values.
46 function fillDropdowns() {
47   if(document.body.contains(document.timeRecordForm.client))
48     fillProjectDropdown(document.timeRecordForm.client.value);
49
50   fillTaskDropdown(document.timeRecordForm.project.value);
51 }
52
53 // The fillProjectDropdown function populates the project combo box with
54 // projects associated with a selected clientt (client id is passed here as id).
55 function fillProjectDropdown(id) {
56   var str_ids = project_ids[id];
57
58   var dropdown = document.getElementById("project");
59   // Determine previously selected item.
60   var selected_item = dropdown.options[dropdown.selectedIndex].value;
61
62   // Remove existing content.
63   dropdown.length = 0;
64   var project_reset = true;
65   // Add mandatory top option.
66   dropdown.options[0] = new Option(empty_label_project, '', true);
67
68   // Populate project dropdown.
69   if (!id) {
70     // If we are here, client is not selected.
71     var len = projects.length;
72     for (var i = 0; i < len; i++) {
73       dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
74       if (dropdown.options[i+1].value == selected_item)  {
75         dropdown.options[i+1].selected = true;
76         project_reset = false;
77       }
78     }
79   } else if (str_ids) {
80     var ids = new Array();
81     ids = str_ids.split(",");
82     var len = ids.length;
83
84     for (var i = 0; i < len; i++) {
85       var p_id = ids[i];
86       dropdown.options[i+1] = new Option(project_names[p_id], p_id);
87       if (dropdown.options[i+1].value == selected_item)  {
88         dropdown.options[i+1].selected = true;
89         project_reset = false;
90       }
91     }
92   }
93
94   // If project selection was reset - clear the tasks dropdown.
95   if (project_reset) {
96     dropdown = document.getElementById("task");
97     dropdown.length = 0;
98     dropdown.options[0] = new Option(empty_label_task, '', true);
99   }
100 }
101
102 // The fillTaskDropdown function populates the task combo box with
103 // tasks associated with a selected project (project id is passed here as id).
104 function fillTaskDropdown(id) {
105   var str_ids = task_ids[id];
106
107   var dropdown = document.getElementById("task");
108   if (dropdown == null) return; // Nothing to do.
109
110   // Determine previously selected item.
111   var selected_item = dropdown.options[dropdown.selectedIndex].value;
112
113   // Remove existing content.
114   dropdown.length = 0;
115   // Add mandatory top option.
116   dropdown.options[0] = new Option(empty_label_task, '', true);
117
118   // Populate the dropdown from the task_names array.
119   if (str_ids) {
120     var ids = new Array();
121     ids = str_ids.split(",");
122     var len = ids.length;
123
124     var idx = 1;
125     for (var i = 0; i < len; i++) {
126       var t_id = ids[i];
127       if (task_names[t_id]) {
128         dropdown.options[idx] = new Option(task_names[t_id], t_id);
129         idx++;
130       }
131     }
132
133     // If a previously selected item is still in dropdown - select it.
134     if (dropdown.options.length > 0) {
135       for (var i = 0; i < dropdown.options.length; i++) {
136         if (dropdown.options[i].value == selected_item)  {
137           dropdown.options[i].selected = true;
138         }
139       }
140     }
141   }
142 }
143
144 // The formDisable function disables some fields depending on what we have in other fields.
145 function formDisable(formField) {
146   var formFieldValue = eval("document.timeRecordForm." + formField + ".value");
147   var formFieldName = eval("document.timeRecordForm." + formField + ".name");
148   var x;
149
150   if (((formFieldValue != "") && (formFieldName == "start")) || ((formFieldValue != "") && (formFieldName == "finish"))) {
151     x = eval("document.timeRecordForm.duration");
152     x.value = "";
153     x.disabled = true;
154     x.style.background = "#e9e9e9";
155   }
156
157   if (((formFieldValue == "") && (formFieldName == "start") && (document.timeRecordForm.finish.value == "")) || ((formFieldValue == "") && (formFieldName == "finish") && (document.timeRecordForm.start.value == ""))) {
158     x = eval("document.timeRecordForm.duration");
159     x.value = "";
160     x.disabled = false;
161     x.style.background = "white";
162   }
163
164   if ((formFieldValue != "") && (formFieldName == "duration")) {
165     x = eval("document.timeRecordForm.start");
166     x.value = "";
167     x.disabled = true;
168     x.style.background = "#e9e9e9";
169     x = eval("document.timeRecordForm.finish");
170     x.value = "";
171     x.disabled = true;
172     x.style.background = "#e9e9e9";
173   }
174
175   if ((formFieldValue == "") && (formFieldName == "duration")) {
176     x = eval("document.timeRecordForm.start");
177     x.disabled = false;
178     x.style.background = "white";
179     x = eval("document.timeRecordForm.finish");
180     x.disabled = false;
181     x.style.background = "white";
182   }
183 }
184
185 // The setNow function fills a given field with current time.
186 function setNow(formField) {
187   var x = eval("document.timeRecordForm.start");
188   x.disabled = false;
189   x.style.background = "white";
190   x = eval("document.timeRecordForm.finish");
191   x.disabled = false;
192   x.style.background = "white";
193   var today = new Date();
194   var time_format = '{$user->time_format}';
195   var obj = eval("document.timeRecordForm." + formField);
196   obj.value = today.strftime(time_format);
197   formDisable(formField);
198 }
199 </script>
200
201 {$forms.timeRecordForm.open}
202 <table cellspacing="4" cellpadding="7" border="0">
203 <tr>
204   <td>
205   <table width = "100%">
206   <tr>
207     <td valign="top">
208     <table border="0">
209 {if $user->isPluginEnabled('cl')}
210     <tr>
211       <td align="right">{$i18n.label.client}{if $user->isPluginEnabled('cm')} (*){/if}:</td>
212       <td>{$forms.timeRecordForm.client.control}</td>
213     </tr>
214 {/if}
215 {if $user->isPluginEnabled('iv')}
216     <tr>
217       <td align="right">&nbsp;</td>
218       <td><label>{$forms.timeRecordForm.billable.control}{$i18n.form.time.billable}</label></td>
219     </tr>
220 {/if}
221 {if ($custom_fields && $custom_fields->fields[0])} 
222     <tr>
223       <td align="right">{$custom_fields->fields[0]['label']|escape}{if $custom_fields->fields[0]['required']} (*){/if}:</td><td>{$forms.timeRecordForm.cf_1.control}</td>
224     </tr>
225 {/if}
226 {if ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
227     <tr>
228       <td align="right">{$i18n.label.project} (*):</td>
229       <td>{$forms.timeRecordForm.project.control}</td>
230     </tr>
231 {/if}
232 {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
233     <tr>
234       <td align="right">{$i18n.label.task}:</td>
235       <td>{$forms.timeRecordForm.task.control}</td>
236     </tr>
237 {/if}
238 {if (($smarty.const.TYPE_START_FINISH == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}
239     <tr>
240       <td align="right">{$i18n.label.start}:</td>
241       <td>{$forms.timeRecordForm.start.control}&nbsp;<input onclick="setNow('start');" type="button" tabindex="-1" value="{$i18n.button.now}"></td>
242     </tr>
243     <tr>
244       <td align="right">{$i18n.label.finish}:</td>
245       <td>{$forms.timeRecordForm.finish.control}&nbsp;<input onclick="setNow('finish');" type="button" tabindex="-1" value="{$i18n.button.now}"></td>
246     </tr>
247 {/if}
248 {if (($smarty.const.TYPE_DURATION == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}
249     <tr>
250       <td align="right">{$i18n.label.duration}:</td>
251       <td>{$forms.timeRecordForm.duration.control}&nbsp;{$i18n.form.time.duration_format}</td>
252     </tr>
253 {/if}
254     <tr>
255       <td align="right">{$i18n.label.date}:</td>
256       <td>{$forms.timeRecordForm.date.control}</td>
257     </tr>
258     <tr>
259       <td align="right">{$i18n.label.note}:</td>
260       <td>{$forms.timeRecordForm.note.control}</td>
261     </tr>
262     <tr>
263       <td colspan="2">&nbsp;</td>
264     </tr>
265     <tr>
266       <td></td>
267       <td align="left">{$forms.timeRecordForm.btn_save.control} {$forms.timeRecordForm.btn_copy.control} {$forms.timeRecordForm.btn_delete.control}</td>
268     </tr>
269     </table>
270     </td>
271     </tr>
272   </table>
273   </td>
274   </tr>
275 </table>
276 {$forms.timeRecordForm.close}