SOAP API: do not try to unserialize an invalid filter
[mantis.git] / javascript / common.js
blobf99d0723310f7e796024ab3f0f5b94e9014264d6
1 /*
2 # Mantis - a php based bugtracking system
4 # Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
5 # Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
7 # Mantis is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
12 # Mantis is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Mantis. If not, see <http://www.gnu.org/licenses/>.
22 * Collapsible element functions
24 var g_collapse_clear = 1;
26 // global code to determine how to set visibility
27 var a = navigator.userAgent.indexOf("MSIE");
28 var style_display;
30 if (a!= -1) {
31 style_display = 'block';
32 } else {
33 style_display = 'table-row';
35 style_display = 'block';
37 $(document).ready( function() {
38 $('.collapse-open').show();
39 $('.collapse-closed').hide();
40 $('.collapse-link').click( function(event) {
41 event.preventDefault();
42 var id = $(this).attr('id');
43 var t_pos = id.indexOf('_closed_link' );
44 if( t_pos == -1 ) {
45 t_pos = id.indexOf('_open_link' );
47 var t_div = id.substring(0, t_pos );
48 ToggleDiv( t_div );
49 });
51 $('input[type=text].autocomplete').autocomplete({
52 source: function(request, callback) {
53 var fieldName = $(this).attr('element').attr('id');
54 var postData = {};
55 postData['entrypoint']= fieldName + '_get_with_prefix';
56 postData[fieldName] = request.term;
57 $.getJSON('xmlhttprequest.php', postData, function(data) {
58 var results = [];
59 $.each(data, function(i, value) {
60 var item = {};
61 item.label = $('<div/>').text(value).html();
62 item.value = value;
63 results.push(item);
64 });
65 callback(results);
66 });
68 });
70 $('a.dynamic-filter-expander').click(function(event) {
71 event.preventDefault();
72 var fieldID = $(this).attr('id');
73 var targetID = fieldID + '_target';
74 var viewType = $('#filters_form_open input[name=view_type]').val();
75 $('#' + targetID).html('<span class="dynamic-filter-loading">' + translations['loading'] + "</span>");
76 $.ajax({
77 url: 'return_dynamic_filters.php',
78 data: 'view_type=' + viewType + '&filter_target=' + fieldID,
79 cache: false,
80 context: $('#' + targetID),
81 success: function(html) {
82 $(this).html(html);
84 });
85 });
87 $('input.autofocus:first, select.autofocus:first, textarea.autofocus:first').focus();
90 * jQuery bug http://bugs.jquery.com/ticket/4283 prevents the check_all
91 * functionality from working when the Content-Type is set to
92 * application/xhtml+xml.
94 var checkAllSelectors = '';
95 $(':checkbox.check_all').each(function() {
96 var baseFieldName = $(this).attr('name').replace(/_all$/, '');
97 if (checkAllSelectors.length > 0) {
98 checkAllSelectors += ', ';
100 checkAllSelectors += ':checkbox[name="' + baseFieldName + '[]"]';
102 if (checkAllSelectors.length > 0) {
103 $(checkAllSelectors).click(function() {
104 var fieldName = $(this).attr('name').replace(/\[\]/g, '');
105 var checkedCount = $(this).closest('form').find(':checkbox[name="' + fieldName + '[]"]:checked').length;
106 var totalCount = $(this).closest('form').find(':checkbox[name="' + fieldName + '[]"]').length;
107 var allSelected = checkedCount == totalCount;
108 $(this).closest('form').find(':checkbox[name=' + fieldName + '_all]').attr('checked', allSelected);
110 $(':checkbox.check_all').click(function() {
111 var baseFieldName = $(this).attr('name').replace(/_all$/, '');
112 $(this).closest('form').find(':checkbox[name="' + baseFieldName + '[]"]').attr('checked', $(this).is(':checked'));
116 var stopwatch = {
117 timerID: null,
118 elapsedTime: 0,
119 tick: function() {
120 this.elapsedTime += 1000;
121 var seconds = Math.floor(this.elapsedTime / 1000) % 60;
122 var minutes = Math.floor(this.elapsedTime / 60000) % 60;
123 var hours = Math.floor(this.elapsedTime / 3600000) % 60;
124 if (seconds < 10) {
125 seconds = '0' + seconds;
127 if (minutes < 10) {
128 minutes = '0' + minutes;
130 if (hours < 10) {
131 hours = '0' + hours;
133 $('input[type=text].stopwatch_time').attr('value', hours + ':' + minutes + ':' + seconds);
134 this.start();
136 reset: function() {
137 this.stop();
138 this.elapsedTime = 0;
139 $('input[type=text].stopwatch_time').attr('value', '00:00:00');
141 start: function() {
142 this.stop();
143 var self = this;
144 this.timerID = window.setTimeout(function() {
145 self.tick();
146 }, 1000);
148 stop: function() {
149 if (typeof this.timerID == 'number') {
150 window.clearTimeout(this.timerID);
151 delete this.timerID;
155 $('input[type=button].stopwatch_toggle').click(function() {
156 if (stopwatch.elapsedTime == 0) {
157 stopwatch.stop();
158 stopwatch.start();
159 $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_stop']);
160 } else if (typeof stopwatch.timerID == 'number') {
161 stopwatch.stop();
162 $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_start']);
163 } else {
164 stopwatch.start();
165 $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_stop']);
168 $('input[type=button].stopwatch_reset').click(function() {
169 stopwatch.reset();
170 $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_start']);
173 $('input[type=text].datetime').each(function(index, element) {
174 $(this).after('<input type="image" class="button datetime" id="' + element.id + '_datetime_button' + '" src="' + config['icon_path'] + 'calendar-img.gif" />');
175 Calendar.setup({
176 inputField: element.id,
177 timeFormat: 24,
178 showsTime: true,
179 ifFormat: config['calendar_js_date_format'],
180 button: element.id + '_datetime_button'
185 $('.bug-jump').find('[name=bug_id]').focus( function() {
186 var bug_label = $('.bug-jump-form').find('[name=bug_label]').val();
187 if( $(this).val() == bug_label ) {
188 $(this).val('');
189 $(this).removeClass('field-default');
192 $('.bug-jump').find('[name=bug_id]').blur( function() {
193 var bug_label = $('.bug-jump-form').find('[name=bug_label]').val();
194 if( $(this).val() == '' ) {
195 $(this).val(bug_label);
196 $(this).addClass('field-default');
199 $('[name=source_query_id]').change( function() {
200 $(this).parent().submit();
202 $('#project-selector').children('[name=project_id]').change( function() {
203 $('#form-set-project').submit();
205 $('#project-selector').children('.button').hide();
206 setBugLabel();
208 $('input[type=checkbox]#use_date_filters').live('click', function() {
209 if (!$(this).is(':checked')) {
210 $('div.filter-box select[name=start_year]').attr('disabled', 'disabled');
211 $('div.filter-box select[name=start_month]').attr('disabled', 'disabled');
212 $('div.filter-box select[name=start_day]').attr('disabled', 'disabled');
213 $('div.filter-box select[name=end_year]').attr('disabled', 'disabled');
214 $('div.filter-box select[name=end_month]').attr('disabled', 'disabled');
215 $('div.filter-box select[name=end_day]').attr('disabled', 'disabled');
216 } else {
217 $('div.filter-box select[name=start_year]').removeAttr('disabled');
218 $('div.filter-box select[name=start_month]').removeAttr('disabled');
219 $('div.filter-box select[name=start_day]').removeAttr('disabled');
220 $('div.filter-box select[name=end_year]').removeAttr('disabled');
221 $('div.filter-box select[name=end_month]').removeAttr('disabled');
222 $('div.filter-box select[name=end_day]').removeAttr('disabled');
226 /* For Period.php bundled with the core MantisGraph plugin */
227 $('#dates > input[type=image].datetime').hide();
228 $('#period_menu > select#interval').change(function() {
229 if ($(this).val() == 10) {
230 $('#dates > input[type=text].datetime').removeAttr('disabled');
231 $('#dates > input[type=image].datetime').show();
232 } else {
233 $('#dates > input[type=text].datetime').attr('disabled', 'disabled');
234 $('#dates > input[type=image].datetime').hide();
238 $('#tag_select').live('change', function() {
239 var tagSeparator = $('#tag_separator').val();
240 var currentTagString = $('#tag_string').val();
241 var newTagOptionID = $(this).val();
242 var newTag = $('#tag_select option[value=' + newTagOptionID + ']').text();
243 if (currentTagString.indexOf(newTag) == -1) {
244 if (currentTagString.length > 0) {
245 $('#tag_string').val(currentTagString + tagSeparator + newTag);
246 } else {
247 $('#tag_string').val(newTag);
250 $(this).val(0);
254 function setBugLabel() {
255 var bug_label = $('.bug-jump-form').find('[name=bug_label]').val();
256 var field = $('.bug-jump').find('[name=bug_id]');
257 if( field.val() == '' ) {
258 field.val(bug_label);
259 field.addClass('field-default');
264 * String manipulation
266 function Trim( p_string ) {
267 if (typeof p_string != "string") {
268 return p_string;
271 var t_string = p_string;
272 var t_ch = '';
274 // Trim beginning spaces
276 t_ch = t_string.substring( 0, 1 );
277 while ( t_ch == " " ) {
278 t_string = t_string.substring( 1, t_string.length );
279 t_ch = t_string.substring( 0, 1 );
282 // Trim trailing spaces
284 t_ch = t_string.substring( t_string.length-1, t_string.length );
285 while ( t_ch == " " ) {
286 t_string = t_string.substring( 0, t_string.length-1 );
287 t_ch = t_string.substring( t_string.length-1, t_string.length );
290 return t_string;
294 * Cookie functions
296 function GetCookie( p_cookie ) {
297 var t_cookie_name = "MANTIS_" + p_cookie;
298 var t_cookies = document.cookie;
300 t_cookies = t_cookies.split( ";" );
302 var i = 0;
303 while( i < t_cookies.length ) {
304 var t_cookie = t_cookies[ i ];
306 t_cookie = t_cookie.split( "=" );
308 if ( Trim( t_cookie[ 0 ] ) == t_cookie_name ) {
309 return( t_cookie[ 1 ] );
311 i++;
314 return -1;
317 function SetCookie( p_cookie, p_value ) {
318 var t_cookie_name = "MANTIS_" + p_cookie;
319 var t_expires = new Date();
321 t_expires.setTime( t_expires.getTime() + (365 * 24 * 60 * 60 * 1000));
323 document.cookie = t_cookie_name + "=" + p_value + "; expires=" + t_expires.toUTCString() + ";";
326 function ToggleDiv( p_div ) {
327 t_open_div = '#' + p_div + "_open";
328 t_closed_div = '#' + p_div + "_closed";
330 t_cookie = GetCookie( "collapse_settings" );
331 if ( 1 == g_collapse_clear ) {
332 t_cookie = "";
333 g_collapse_clear = 0;
335 var t_open_display = $(t_open_div).css('display');
336 $(t_open_div).toggle();
338 if( $(t_closed_div).length ) {
339 $(t_closed_div).toggle();
342 if ( t_open_display == "none" ) {
343 t_cookie = t_cookie + "|" + p_div + ",1";
344 } else {
345 t_cookie = t_cookie + "|" + p_div + ",0";
348 SetCookie( "collapse_settings", t_cookie );
351 function setDisplay(idTag, state)
353 if(!document.getElementById(idTag)) alert('SetDisplay(): id '+idTag+' is empty');
354 // change display visibility
355 if ( state != 0 ) {
356 document.getElementById(idTag).style.display = style_display;
357 } else {
358 document.getElementById(idTag).style.display = 'none';
362 function toggleDisplay(idTag)
364 setDisplay( idTag, (document.getElementById(idTag).style.display == 'none')?1:0 );