blob: a1b6245a96c56c8ba523936b46dc8dc43ed60c77 [file] [log] [blame]
ddkilzer@apple.com097da082009-07-03 02:14:25 +00001/* The contents of this file are subject to the Mozilla Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/MPL/
5 *
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
10 *
11 * The Original Code is the Bugzilla Bug Tracking System.
12 *
13 * The Initial Developer of the Original Code is Everything Solved, Inc.
14 * Portions created by Everything Solved are Copyright (C) 2007 Everything
15 * Solved, Inc. All Rights Reserved.
16 *
17 * Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
18 * Guy Pyrzak <guy.pyrzak@gmail.com>
19 */
20
21/* This library assumes that the needed YUI libraries have been loaded
22 already. */
23
24function createCalendar(name) {
25 var cal = new YAHOO.widget.Calendar('calendar_' + name,
26 'con_calendar_' + name);
27 YAHOO.bugzilla['calendar_' + name] = cal;
28 var field = document.getElementById(name);
29 cal.selectEvent.subscribe(setFieldFromCalendar, field, false);
30 updateCalendarFromField(field);
31 cal.render();
32}
33
34/* The onclick handlers for the button that shows the calendar. */
35function showCalendar(field_name) {
36 var calendar = YAHOO.bugzilla["calendar_" + field_name];
37 var field = document.getElementById(field_name);
38 var button = document.getElementById('button_calendar_' + field_name);
39
40 bz_overlayBelow(calendar.oDomContainer, field);
41 calendar.show();
42 button.onclick = function() { hideCalendar(field_name); };
43
44 // Because of the way removeListener works, this has to be a function
45 // attached directly to this calendar.
46 calendar.bz_myBodyCloser = function(event) {
47 var container = this.oDomContainer;
48 var target = YAHOO.util.Event.getTarget(event);
49 if (target != container && target != button
50 && !YAHOO.util.Dom.isAncestor(container, target))
51 {
52 hideCalendar(field_name);
53 }
54 };
55
56 // If somebody clicks outside the calendar, hide it.
57 YAHOO.util.Event.addListener(document.body, 'click',
58 calendar.bz_myBodyCloser, calendar, true);
59
60 // Make Esc close the calendar.
61 calendar.bz_escCal = function (event) {
62 var key = YAHOO.util.Event.getCharCode(event);
63 if (key == 27) {
64 hideCalendar(field_name);
65 }
66 };
67 YAHOO.util.Event.addListener(document.body, 'keydown', calendar.bz_escCal);
68}
69
70function hideCalendar(field_name) {
71 var cal = YAHOO.bugzilla["calendar_" + field_name];
72 cal.hide();
73 var button = document.getElementById('button_calendar_' + field_name);
74 button.onclick = function() { showCalendar(field_name); };
75 YAHOO.util.Event.removeListener(document.body, 'click',
76 cal.bz_myBodyCloser);
77 YAHOO.util.Event.removeListener(document.body, 'keydown', cal.bz_escCal);
78}
79
80/* This is the selectEvent for our Calendar objects on our custom
81 * DateTime fields.
82 */
83function setFieldFromCalendar(type, args, date_field) {
84 var dates = args[0];
85 var setDate = dates[0];
86
87 // We can't just write the date straight into the field, because there
88 // might already be a time there.
89 var timeRe = /\b(\d{1,2}):(\d\d)(?::(\d\d))?/;
90 var currentTime = timeRe.exec(date_field.value);
91 var d = new Date(setDate[0], setDate[1] - 1, setDate[2]);
92 if (currentTime) {
93 d.setHours(currentTime[1], currentTime[2]);
94 if (currentTime[3]) {
95 d.setSeconds(currentTime[3]);
96 }
97 }
98
99 var year = d.getFullYear();
100 // JavaScript's "Date" represents January as 0 and December as 11.
101 var month = d.getMonth() + 1;
102 if (month < 10) month = '0' + String(month);
103 var day = d.getDate();
104 if (day < 10) day = '0' + String(day);
105 var dateStr = year + '-' + month + '-' + day;
106
107 if (currentTime) {
108 var minutes = d.getMinutes();
109 if (minutes < 10) minutes = '0' + String(minutes);
110 var seconds = d.getSeconds();
111 if (seconds > 0 && seconds < 10) {
112 seconds = '0' + String(seconds);
113 }
114
115 dateStr = dateStr + ' ' + d.getHours() + ':' + minutes;
116 if (seconds) dateStr = dateStr + ':' + seconds;
117 }
118
119 date_field.value = dateStr;
120 hideCalendar(date_field.id);
121}
122
123/* Sets the calendar based on the current field value.
124 */
125function updateCalendarFromField(date_field) {
126 var dateRe = /(\d\d\d\d)-(\d\d?)-(\d\d?)/;
127 var pieces = dateRe.exec(date_field.value);
128 if (pieces) {
129 var cal = YAHOO.bugzilla["calendar_" + date_field.id];
130 cal.select(new Date(pieces[1], pieces[2] - 1, pieces[3]));
131 var selectedArray = cal.getSelectedDates();
132 var selected = selectedArray[0];
133 cal.cfg.setProperty("pagedate", (selected.getMonth() + 1) + '/'
134 + selected.getFullYear());
135 cal.render();
136 }
137}
138
139
140/* Hide input fields and show the text with (edit) next to it */
141function hideEditableField( container, input, action, field_id, original_value ) {
142 YAHOO.util.Dom.setStyle(container, 'display', 'inline');
143 YAHOO.util.Dom.setStyle(input, 'display', 'none');
144 YAHOO.util.Event.addListener(action, 'click', showEditableField,
145 new Array(container, input));
146 if(field_id != ""){
147 YAHOO.util.Event.addListener(window, 'load', checkForChangedFieldValues,
148 new Array(container, input, field_id, original_value));
149 }
150}
151
152/* showEditableField (e, ContainerInputArray)
153 * Function hides the (edit) link and the text and displays the input
154 *
155 * var e: the event
156 * var ContainerInputArray: An array containing the (edit) and text area and the input being displayed
157 * var ContainerInputArray[0]: the conainer that will be hidden usually shows the (edit) text
158 * var ContainerInputArray[1]: the input area and label that will be displayed
159 *
160 */
161function showEditableField (e, ContainerInputArray) {
162 var inputs = new Array();
163 var inputArea = YAHOO.util.Dom.get(ContainerInputArray[1]);
164 if ( ! inputArea ){
165 YAHOO.util.Event.preventDefault(e);
166 return;
167 }
168 YAHOO.util.Dom.setStyle(ContainerInputArray[0], 'display', 'none');
169 YAHOO.util.Dom.setStyle(inputArea, 'display', 'inline');
170 if ( inputArea.tagName.toLowerCase() == "input" ) {
171 inputs.push(inputArea);
172 } else {
173 inputs = inputArea.getElementsByTagName('input');
174 }
175 if ( inputs.length > 0 ) {
176 // focus on the first field, this makes it easier to edit
177 inputs[0].focus();
178 inputs[0].select();
179 }
180 YAHOO.util.Event.preventDefault(e);
181}
182
183
184/* checkForChangedFieldValues(e, array )
185 * Function checks if after the autocomplete by the browser if the values match the originals.
186 * If they don't match then hide the text and show the input so users don't get confused.
187 *
188 * var e: the event
189 * var ContainerInputArray: An array containing the (edit) and text area and the input being displayed
190 * var ContainerInputArray[0]: the conainer that will be hidden usually shows the (edit) text
191 * var ContainerInputArray[1]: the input area and label that will be displayed
192 * var ContainerInputArray[2]: the field that is on the page, might get changed by browser autocomplete
193 * var ContainerInputArray[3]: the original value from the page loading.
194 *
195 */
196function checkForChangedFieldValues(e, ContainerInputArray ) {
197 var el = document.getElementById(ContainerInputArray[2]);
198 var unhide = false;
199 if ( el ) {
200 if ( el.value != ContainerInputArray[3] ||
201 ( el.value == "" && el.id != "alias") ) {
202 unhide = true;
203 }
204 else {
205 var set_default = document.getElementById("set_default_" +
206 ContainerInputArray[2]);
207 if ( set_default ) {
208 if(set_default.checked){
209 unhide = true;
210 }
211 }
212 }
213 }
214 if(unhide){
215 YAHOO.util.Dom.setStyle(ContainerInputArray[0], 'display', 'none');
216 YAHOO.util.Dom.setStyle(ContainerInputArray[1], 'display', 'inline');
217 }
218
219}
220
221function hideAliasAndSummary(short_desc_value, alias_value) {
222 // check the short desc field
223 hideEditableField( 'summary_alias_container','summary_alias_input',
224 'editme_action','short_desc', short_desc_value);
225 // check that the alias hasn't changed
226 var bz_alias_check_array = new Array('summary_alias_container',
227 'summary_alias_input', 'alias', alias_value);
228 YAHOO.util.Event.addListener( window, 'load', checkForChangedFieldValues,
229 bz_alias_check_array);
230}
231
232function showPeopleOnChange( field_id_list ) {
233 for(var i = 0; i < field_id_list.length; i++) {
234 YAHOO.util.Event.addListener( field_id_list[i],'change', showEditableField,
235 new Array('bz_qa_contact_edit_container',
236 'bz_qa_contact_input'));
237 YAHOO.util.Event.addListener( field_id_list[i],'change',showEditableField,
238 new Array('bz_assignee_edit_container',
239 'bz_assignee_input'));
240 }
241}
242
243function assignToDefaultOnChange(field_id_list) {
244 showPeopleOnChange( field_id_list );
245 for(var i = 0; i < field_id_list.length; i++) {
246 YAHOO.util.Event.addListener( field_id_list[i],'change', setDefaultCheckbox,
247 'set_default_assignee');
248 YAHOO.util.Event.addListener( field_id_list[i],'change',setDefaultCheckbox,
249 'set_default_qa_contact');
250 }
251}
252
253function initDefaultCheckbox(field_id){
254 YAHOO.util.Event.addListener( 'set_default_' + field_id,'change', boldOnChange,
255 'set_default_' + field_id);
256 YAHOO.util.Event.addListener( window,'load', checkForChangedFieldValues,
257 new Array( 'bz_' + field_id + '_edit_container',
258 'bz_' + field_id + '_input',
259 'set_default_' + field_id ,'1'));
260
261 YAHOO.util.Event.addListener( window, 'load', boldOnChange,
262 'set_default_' + field_id );
263}
264
265function showHideStatusItems(e, dupArrayInfo) {
266 var el = document.getElementById('bug_status');
267 // finish doing stuff based on the selection.
268 if ( el ) {
269 showDuplicateItem(el);
270 YAHOO.util.Dom.setStyle('resolution_settings', 'display', 'none');
271 if (document.getElementById('resolution_settings_warning')) {
272 YAHOO.util.Dom.setStyle('resolution_settings_warning', 'display', 'none');
273 }
274 YAHOO.util.Dom.setStyle('duplicate_display', 'display', 'none');
275
276 if ( el.value == dupArrayInfo[1] && dupArrayInfo[0] == "is_duplicate" ) {
277 YAHOO.util.Dom.setStyle('resolution_settings', 'display', 'inline');
278 YAHOO.util.Dom.setStyle('resolution_settings_warning', 'display', 'block');
279 }
280 else if ( bz_isValueInArray(close_status_array, el.value) ) {
281 // hide duplicate and show resolution
282 YAHOO.util.Dom.setStyle('resolution_settings', 'display', 'inline');
283 YAHOO.util.Dom.setStyle('resolution_settings_warning', 'display', 'block');
284 }
285 }
286}
287
288function showDuplicateItem(e) {
289 var resolution = document.getElementById('resolution');
290 var bug_status = document.getElementById('bug_status');
291 var dup_id = document.getElementById('dup_id');
292 if (resolution) {
293 if (resolution.value == 'DUPLICATE' && bz_isValueInArray( close_status_array, bug_status.value) ) {
294 // hide resolution show duplicate
295 YAHOO.util.Dom.setStyle('duplicate_settings', 'display', 'inline');
296 YAHOO.util.Dom.setStyle('dup_id_discoverable', 'display', 'none');
297 dup_id.focus();
298 dup_id.select();
299 }
300 else {
301 YAHOO.util.Dom.setStyle('duplicate_settings', 'display', 'none');
302 YAHOO.util.Dom.setStyle('dup_id_discoverable', 'display', 'block');
303 dup_id.blur();
304 }
305 }
306 YAHOO.util.Event.preventDefault(e); //prevents the hyperlink from going to the url in the href.
307}
308
309function setResolutionToDuplicate(e, duplicate_or_move_bug_status) {
310 var status = document.getElementById('bug_status');
311 var resolution = document.getElementById('resolution');
312 YAHOO.util.Dom.setStyle('dup_id_discoverable', 'display', 'none');
313 status.value = duplicate_or_move_bug_status;
314 resolution.value = "DUPLICATE";
315 showHideStatusItems("", ["",""]);
316 YAHOO.util.Event.preventDefault(e);
317}
318
319function setDefaultCheckbox(e, field_id ) {
320 var el = document.getElementById(field_id);
321 var elLabel = document.getElementById(field_id + "_label");
322 if( el && elLabel ) {
323 el.checked = "true";
324 YAHOO.util.Dom.setStyle(elLabel, 'font-weight', 'bold');
325 }
326}
327
328function boldOnChange(e, field_id){
329 var el = document.getElementById(field_id);
330 var elLabel = document.getElementById(field_id + "_label");
331 if( el && elLabel ) {
332 if( el.checked ){
333 YAHOO.util.Dom.setStyle(elLabel, 'font-weight', 'bold');
334 }
335 else{
336 YAHOO.util.Dom.setStyle(elLabel, 'font-weight', 'normal');
337 }
338 }
339}