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.
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.
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.
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}";
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'}";
27 // We'll use this array to populate project dropdown when client is not selected.
29 var projects = new Array();
30 {foreach $project_list as $project}
31 projects[idx] = new Array("{$project.id}", "{$project.name|escape:'javascript'}");
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}";
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'}";
46 {if $template_dropdown}
47 var templates = new Array();
48 {foreach $templates as $template}
49 templates[{$template.id}] = "{$template.content|escape:'javascript'}";
52 // The fillNote function populates the Note field with a selected template body.
53 function fillNote(id) {
54 if (!id) return; // Do nothing.
55 var template_body = templates[id];
56 var note = document.getElementById("note");
57 note.value = template_body;
61 // Mandatory top options for project and task dropdowns.
62 var empty_label_project = "{$i18n.dropdown.select|escape:'javascript'}";
63 var empty_label_task = "{$i18n.dropdown.select|escape:'javascript'}";
65 // The fillDropdowns function populates the "project" and "task" dropdown controls
66 // with relevant values.
67 function fillDropdowns() {
68 if(document.body.contains(document.timeRecordForm.client))
69 fillProjectDropdown(document.timeRecordForm.client.value);
71 fillTaskDropdown(document.timeRecordForm.project.value);
74 // The fillProjectDropdown function populates the project combo box with
75 // projects associated with a selected client (client id is passed here as id).
76 function fillProjectDropdown(id) {
77 var str_ids = project_ids[id];
78 var dropdown = document.getElementById("project");
79 // Determine previously selected item.
80 var selected_item = dropdown.options[dropdown.selectedIndex].value;
82 // Remove existing content.
84 var project_reset = true;
85 // Add mandatory top option.
86 dropdown.options[0] = new Option(empty_label_project, '', true);
88 // Populate project dropdown.
90 // If we are here, client is not selected.
91 var len = projects.length;
92 for (var i = 0; i < len; i++) {
93 dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
94 if (dropdown.options[i+1].value == selected_item) {
95 dropdown.options[i+1].selected = true;
96 project_reset = false;
100 var ids = new Array();
101 ids = str_ids.split(",");
102 var len = ids.length;
104 for (var i = 0; i < len; i++) {
106 dropdown.options[i+1] = new Option(project_names[p_id], p_id);
107 if (dropdown.options[i+1].value == selected_item) {
108 dropdown.options[i+1].selected = true;
109 project_reset = false;
114 // If project selection was reset - clear the tasks dropdown.
116 dropdown = document.getElementById("task");
118 dropdown.options[0] = new Option(empty_label_task, '', true);
122 // The fillTaskDropdown function populates the task combo box with
123 // tasks associated with a selected project (project id is passed here as id).
124 function fillTaskDropdown(id) {
125 var str_ids = task_ids[id];
127 var dropdown = document.getElementById("task");
128 if (dropdown == null) return; // Nothing to do.
130 // Determine previously selected item.
131 var selected_item = dropdown.options[dropdown.selectedIndex].value;
133 // Remove existing content.
135 // Add mandatory top option.
136 dropdown.options[0] = new Option(empty_label_task, '', true);
138 // Populate the dropdown from the task_names array.
140 var ids = new Array();
141 ids = str_ids.split(",");
142 var len = ids.length;
145 for (var i = 0; i < len; i++) {
147 if (task_names[t_id]) {
148 dropdown.options[idx] = new Option(task_names[t_id], t_id);
153 // If a previously selected item is still in dropdown - select it.
154 if (dropdown.options.length > 0) {
155 for (var i = 0; i < dropdown.options.length; i++) {
156 if (dropdown.options[i].value == selected_item) {
157 dropdown.options[i].selected = true;
162 // Select a task if user is required to do so and there is only one task available.
163 if ({$user->task_required} && dropdown.options.length == 2) { // 2 because of mandatory top option.
164 dropdown.options[1].selected = true;
169 // The formDisable function disables some fields depending on what we have in other fields.
170 function formDisable(formField) {
171 var formFieldValue = eval("document.timeRecordForm." + formField + ".value");
172 var formFieldName = eval("document.timeRecordForm." + formField + ".name");
175 if (((formFieldValue != "") && (formFieldName == "start")) || ((formFieldValue != "") && (formFieldName == "finish"))) {
176 x = eval("document.timeRecordForm.duration");
179 x.style.background = "#e9e9e9";
182 if (((formFieldValue == "") && (formFieldName == "start") && (document.timeRecordForm.finish.value == "")) || ((formFieldValue == "") && (formFieldName == "finish") && (document.timeRecordForm.start.value == ""))) {
183 x = eval("document.timeRecordForm.duration");
186 x.style.background = "white";
189 if ((formFieldValue != "") && (formFieldName == "duration")) {
190 x = eval("document.timeRecordForm.start");
193 x.style.background = "#e9e9e9";
194 x = eval("document.timeRecordForm.finish");
197 x.style.background = "#e9e9e9";
200 if ((formFieldValue == "") && (formFieldName == "duration")) {
201 x = eval("document.timeRecordForm.start");
203 x.style.background = "white";
204 x = eval("document.timeRecordForm.finish");
206 x.style.background = "white";
210 // The setNow function fills a given field with current time.
211 function setNow(formField) {
212 var x = eval("document.timeRecordForm.start");
214 x.style.background = "white";
215 x = eval("document.timeRecordForm.finish");
217 x.style.background = "white";
218 var today = new Date();
219 var time_format = '{$user->time_format}';
220 var obj = eval("document.timeRecordForm." + formField);
221 obj.value = today.strftime(time_format);
222 formDisable(formField);
225 function get_date() {
226 var date = new Date();
227 return date.strftime("%Y-%m-%d");
230 function get_time() {
231 var date = new Date();
232 return date.strftime("%H:%M");