2 // This script is shared by time.tpl and time_edit.tpl (both regular and mobile).
4 // We need a few arrays to populate project and task dropdowns.
5 // When client selection changes, the project dropdown must be re-populated with only relevant projects.
6 // When project selection changes, the task dropdown must be repopulated similarly.
8 // project_ids[143] = "325,370,390,400"; // Comma-separated list of project ids for client.
9 // project_names[325] = "Time Tracker"; // Project name.
10 // task_ids[325] = "100,101,302,303,304"; // Comma-separated list ot task ids for project.
11 // task_names[100] = "Coding"; // Task name.
13 // Prepare an array of project ids for clients.
14 var project_ids = new Array();
15 {foreach $client_list as $client}
16 project_ids[{$client.id}] = "{$client.projects}";
18 // Prepare an array of project names.
19 var project_names = new Array();
20 {foreach $project_list as $project}
21 project_names[{$project.id}] = "{$project.name|escape:'javascript'}";
23 // We'll use this array to populate project dropdown when client is not selected.
25 var projects = new Array();
26 {foreach $project_list as $project}
27 projects[idx] = new Array("{$project.id}", "{$project.name|escape:'javascript'}");
31 // Prepare an array of task ids for projects.
32 var task_ids = new Array();
33 {foreach $project_list as $project}
34 task_ids[{$project.id}] = "{$project.tasks}";
36 // Prepare an array of task names.
37 var task_names = new Array();
38 {foreach $task_list as $task}
39 task_names[{$task.id}] = "{$task.name|escape:'javascript'}";
42 // Mandatory top options for project and task dropdowns.
43 var empty_label_project = "{$i18n.dropdown.select|escape:'javascript'}";
44 var empty_label_task = "{$i18n.dropdown.select|escape:'javascript'}";
46 // The fillDropdowns function populates the "project" and "task" dropdown controls
47 // with relevant values.
48 function fillDropdowns() {
49 if(document.body.contains(document.timeRecordForm.client))
50 fillProjectDropdown(document.timeRecordForm.client.value);
52 fillTaskDropdown(document.timeRecordForm.project.value);
55 // The fillProjectDropdown function populates the project combo box with
56 // projects associated with a selected client (client id is passed here as id).
57 function fillProjectDropdown(id) {
58 var str_ids = project_ids[id];
59 var dropdown = document.getElementById("project");
60 // Determine previously selected item.
61 var selected_item = dropdown.options[dropdown.selectedIndex].value;
63 // Remove existing content.
65 var project_reset = true;
66 // Add mandatory top option.
67 dropdown.options[0] = new Option(empty_label_project, '', true);
69 // Populate project dropdown.
71 // If we are here, client is not selected.
72 var len = projects.length;
73 for (var i = 0; i < len; i++) {
74 dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
75 if (dropdown.options[i+1].value == selected_item) {
76 dropdown.options[i+1].selected = true;
77 project_reset = false;
81 var ids = new Array();
82 ids = str_ids.split(",");
85 for (var i = 0; i < len; i++) {
87 dropdown.options[i+1] = new Option(project_names[p_id], p_id);
88 if (dropdown.options[i+1].value == selected_item) {
89 dropdown.options[i+1].selected = true;
90 project_reset = false;
95 // If project selection was reset - clear the tasks dropdown.
97 dropdown = document.getElementById("task");
99 dropdown.options[0] = new Option(empty_label_task, '', true);
103 // The fillTaskDropdown function populates the task combo box with
104 // tasks associated with a selected project (project id is passed here as id).
105 function fillTaskDropdown(id) {
106 var str_ids = task_ids[id];
108 var dropdown = document.getElementById("task");
109 if (dropdown == null) return; // Nothing to do.
111 // Determine previously selected item.
112 var selected_item = dropdown.options[dropdown.selectedIndex].value;
114 // Remove existing content.
116 // Add mandatory top option.
117 dropdown.options[0] = new Option(empty_label_task, '', true);
119 // Populate the dropdown from the task_names array.
121 var ids = new Array();
122 ids = str_ids.split(",");
123 var len = ids.length;
126 for (var i = 0; i < len; i++) {
128 if (task_names[t_id]) {
129 dropdown.options[idx] = new Option(task_names[t_id], t_id);
134 // If a previously selected item is still in dropdown - select it.
135 if (dropdown.options.length > 0) {
136 for (var i = 0; i < dropdown.options.length; i++) {
137 if (dropdown.options[i].value == selected_item) {
138 dropdown.options[i].selected = true;
145 // The formDisable function disables some fields depending on what we have in other fields.
146 function formDisable(formField) {
147 var formFieldValue = eval("document.timeRecordForm." + formField + ".value");
148 var formFieldName = eval("document.timeRecordForm." + formField + ".name");
151 if (((formFieldValue != "") && (formFieldName == "start")) || ((formFieldValue != "") && (formFieldName == "finish"))) {
152 x = eval("document.timeRecordForm.duration");
155 x.style.background = "#e9e9e9";
158 if (((formFieldValue == "") && (formFieldName == "start") && (document.timeRecordForm.finish.value == "")) || ((formFieldValue == "") && (formFieldName == "finish") && (document.timeRecordForm.start.value == ""))) {
159 x = eval("document.timeRecordForm.duration");
162 x.style.background = "white";
165 if ((formFieldValue != "") && (formFieldName == "duration")) {
166 x = eval("document.timeRecordForm.start");
169 x.style.background = "#e9e9e9";
170 x = eval("document.timeRecordForm.finish");
173 x.style.background = "#e9e9e9";
176 if ((formFieldValue == "") && (formFieldName == "duration")) {
177 x = eval("document.timeRecordForm.start");
179 x.style.background = "white";
180 x = eval("document.timeRecordForm.finish");
182 x.style.background = "white";
186 // The setNow function fills a given field with current time.
187 function setNow(formField) {
188 var x = eval("document.timeRecordForm.start");
190 x.style.background = "white";
191 x = eval("document.timeRecordForm.finish");
193 x.style.background = "white";
194 var today = new Date();
195 var time_format = '{$user->time_format}';
196 var obj = eval("document.timeRecordForm." + formField);
197 obj.value = today.strftime(time_format);
198 formDisable(formField);
201 function get_date() {
202 var date = new Date();
203 return date.strftime("%Y-%m-%d");
206 function get_time() {
207 var date = new Date();
208 return date.strftime("%H:%M");