PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/javascript/dev/common.js

https://github.com/fusenigk/mantisbt-1
JavaScript | 307 lines | 230 code | 39 blank | 38 comment | 45 complexity | 9fcfd49ed5d7063b5c667852375a3e4d MD5 | raw file
  1. /*
  2. # Mantis - a php based bugtracking system
  3. # Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  4. # Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  5. # Mantis is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Mantis is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Mantis. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --------------------------------------------------------
  19. * $Id$
  20. * --------------------------------------------------------
  21. */
  22. /*
  23. * Collapsible element functions
  24. */
  25. 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;
  29. if (a!= -1) {
  30. style_display = 'block';
  31. } else {
  32. style_display = 'table-row';
  33. }
  34. style_display = 'block';
  35. $(document).ready( function() {
  36. /* Global Tag change event added only if #tag_select exists */
  37. $('#tag_select').live('change', function() {
  38. var selected_tag = $('#tag_select option:selected').text();
  39. tag_string_append( selected_tag );
  40. });
  41. $('.collapse-open').show();
  42. $('.collapse-closed').hide();
  43. $('.collapse-link').click( function(event) {
  44. event.preventDefault();
  45. var id = $(this).attr('id');
  46. var t_pos = id.indexOf('_closed_link' );
  47. if( t_pos == -1 ) {
  48. t_pos = id.indexOf('_open_link' );
  49. }
  50. var t_div = id.substring(0, t_pos );
  51. ToggleDiv( t_div );
  52. });
  53. $('input[type=text].autocomplete').autocomplete({
  54. source: function(request, callback) {
  55. var fieldName = $(this).attr('element').attr('id');
  56. var postData = {};
  57. postData['entrypoint']= fieldName + '_get_with_prefix';
  58. postData[fieldName] = request.term;
  59. $.getJSON('xmlhttprequest.php', postData, function(data) {
  60. var results = [];
  61. $.each(data, function(i, value) {
  62. var item = {};
  63. item.label = $('<div/>').text(value).html();
  64. item.value = value;
  65. results.push(item);
  66. });
  67. callback(results);
  68. });
  69. }
  70. });
  71. $('input.autofocus:first, select.autofocus:first, textarea.autofocus:first').focus();
  72. $('input[type=checkbox].check_all').click(function() {
  73. var matchingName = $(this).attr('name').replace(/_all$/, '');
  74. $(this).closest('form').find('input[type=checkbox][name=' + matchingName + '\[\]]').attr('checked', this.checked);
  75. });
  76. var stopwatch = {
  77. timerID: null,
  78. elapsedTime: 0,
  79. tick: function() {
  80. this.elapsedTime += 1000;
  81. var seconds = Math.floor(this.elapsedTime / 1000) % 60;
  82. var minutes = Math.floor(this.elapsedTime / 60000) % 60;
  83. var hours = Math.floor(this.elapsedTime / 3600000) % 60;
  84. if (seconds < 10) {
  85. seconds = '0' + seconds;
  86. }
  87. if (minutes < 10) {
  88. minutes = '0' + minutes;
  89. }
  90. if (hours < 10) {
  91. hours = '0' + hours;
  92. }
  93. $('input[type=text].stopwatch_time').attr('value', hours + ':' + minutes + ':' + seconds);
  94. this.start();
  95. },
  96. reset: function() {
  97. this.stop();
  98. this.elapsedTime = 0;
  99. $('input[type=text].stopwatch_time').attr('value', '00:00:00');
  100. },
  101. start: function() {
  102. this.stop();
  103. var self = this;
  104. this.timerID = window.setTimeout(function() {
  105. self.tick();
  106. }, 1000);
  107. },
  108. stop: function() {
  109. if (typeof this.timerID == 'number') {
  110. window.clearTimeout(this.timerID);
  111. delete this.timerID;
  112. }
  113. }
  114. }
  115. $('input[type=button].stopwatch_toggle').click(function() {
  116. if (stopwatch.elapsedTime == 0) {
  117. stopwatch.stop();
  118. stopwatch.start();
  119. $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_stop']);
  120. } else if (typeof stopwatch.timerID == 'number') {
  121. stopwatch.stop();
  122. $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_start']);
  123. } else {
  124. stopwatch.start();
  125. $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_stop']);
  126. }
  127. });
  128. $('input[type=button].stopwatch_reset').click(function() {
  129. stopwatch.reset();
  130. $('input[type=button].stopwatch_toggle').attr('value', translations['time_tracking_stopwatch_start']);
  131. });
  132. $('input[type=text].datetime').each(function(index, element) {
  133. $(this).after('<input type="image" class="button datetime" id="' + element.id + '_datetime_button' + '" src="' + config['icon_path'] + 'calendar-img.gif" />');
  134. Calendar.setup({
  135. inputField: element.id,
  136. timeFormat: 24,
  137. showsTime: true,
  138. ifFormat: config['calendar_js_date_format'],
  139. button: element.id + '_datetime_button'
  140. });
  141. });
  142. $('.bug-jump').find('[name=bug_id]').focus( function() {
  143. var bug_label = $('.bug-jump-form').find('[name=bug_label]').val();
  144. if( $(this).val() == bug_label ) {
  145. $(this).val('');
  146. $(this).removeClass('field-default');
  147. }
  148. });
  149. $('.bug-jump').find('[name=bug_id]').blur( function() {
  150. var bug_label = $('.bug-jump-form').find('[name=bug_label]').val();
  151. if( $(this).val() == '' ) {
  152. $(this).val(bug_label);
  153. $(this).addClass('field-default');
  154. }
  155. });
  156. $('[name=source_query_id]').change( function() {
  157. $(this).parent().submit();
  158. });
  159. $('[name=form_set_project]').children('[name=project_id]').change( function() {
  160. $(this).parent().submit();
  161. });
  162. $('[name=form_set_project]').children('.button').hide();
  163. setBugLabel();
  164. });
  165. function setBugLabel() {
  166. var bug_label = $('.bug-jump-form').find('[name=bug_label]').val();
  167. var field = $('.bug-jump').find('[name=bug_id]');
  168. if( field.val() == '' ) {
  169. field.val(bug_label);
  170. field.addClass('field-default');
  171. }
  172. }
  173. /*
  174. * String manipulation
  175. */
  176. function Trim( p_string ) {
  177. if (typeof p_string != "string") {
  178. return p_string;
  179. }
  180. var t_string = p_string;
  181. var t_ch = '';
  182. // Trim beginning spaces
  183. t_ch = t_string.substring( 0, 1 );
  184. while ( t_ch == " " ) {
  185. t_string = t_string.substring( 1, t_string.length );
  186. t_ch = t_string.substring( 0, 1 );
  187. }
  188. // Trim trailing spaces
  189. t_ch = t_string.substring( t_string.length-1, t_string.length );
  190. while ( t_ch == " " ) {
  191. t_string = t_string.substring( 0, t_string.length-1 );
  192. t_ch = t_string.substring( t_string.length-1, t_string.length );
  193. }
  194. return t_string;
  195. }
  196. /*
  197. * Cookie functions
  198. */
  199. function GetCookie( p_cookie ) {
  200. var t_cookie_name = "MANTIS_" + p_cookie;
  201. var t_cookies = document.cookie;
  202. t_cookies = t_cookies.split( ";" );
  203. var i = 0;
  204. while( i < t_cookies.length ) {
  205. var t_cookie = t_cookies[ i ];
  206. t_cookie = t_cookie.split( "=" );
  207. if ( Trim( t_cookie[ 0 ] ) == t_cookie_name ) {
  208. return( t_cookie[ 1 ] );
  209. }
  210. i++;
  211. }
  212. return -1;
  213. }
  214. function SetCookie( p_cookie, p_value ) {
  215. var t_cookie_name = "MANTIS_" + p_cookie;
  216. var t_expires = new Date();
  217. t_expires.setTime( t_expires.getTime() + (365 * 24 * 60 * 60 * 1000));
  218. document.cookie = t_cookie_name + "=" + p_value + "; expires=" + t_expires.toUTCString() + ";";
  219. }
  220. function ToggleDiv( p_div ) {
  221. t_open_div = '#' + p_div + "_open";
  222. t_closed_div = '#' + p_div + "_closed";
  223. t_cookie = GetCookie( "collapse_settings" );
  224. if ( 1 == g_collapse_clear ) {
  225. t_cookie = "";
  226. g_collapse_clear = 0;
  227. }
  228. var t_open_display = $(t_open_div).css('display');
  229. $(t_open_div).toggle();
  230. if( $(t_closed_div).length ) {
  231. $(t_closed_div).toggle();
  232. }
  233. if ( t_open_display == "none" ) {
  234. t_cookie = t_cookie + "|" + p_div + ",1";
  235. } else {
  236. t_cookie = t_cookie + "|" + p_div + ",0";
  237. }
  238. SetCookie( "collapse_settings", t_cookie );
  239. }
  240. function setDisplay(idTag, state)
  241. {
  242. if(!document.getElementById(idTag)) alert('SetDisplay(): id '+idTag+' is empty');
  243. // change display visibility
  244. if ( state != 0 ) {
  245. document.getElementById(idTag).style.display = style_display;
  246. } else {
  247. document.getElementById(idTag).style.display = 'none';
  248. }
  249. }
  250. function toggleDisplay(idTag)
  251. {
  252. setDisplay( idTag, (document.getElementById(idTag).style.display == 'none')?1:0 );
  253. }
  254. /* Append a tag name to the tag input box, with repect for tag separators, etc */
  255. function tag_string_append( p_string ) {
  256. t_tag_separator = $('#tag_separator').val();
  257. t_tag_string = $('#tag_string');
  258. t_tag_select = $('#tag_select');
  259. if ( Trim( p_string ) == '' ) { return; }
  260. if ( t_tag_string.val() != '' ) {
  261. t_tag_string.val( t_tag_string.val() + t_tag_separator + p_string );
  262. } else {
  263. t_tag_string.val( t_tag_string.val() + p_string );
  264. }
  265. t_tag_select.val(0);
  266. }