You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application.js 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. /**
  2. * Redmine - project management software
  3. * Copyright (C) 2006- Jean-Philippe Lang
  4. * This code is released under the GNU General Public License.
  5. */
  6. function sanitizeHTML(string) {
  7. var temp = document.createElement('span');
  8. temp.textContent = string;
  9. return temp.innerHTML;
  10. }
  11. function checkAll(id, checked) {
  12. $('#'+id).find('input[type=checkbox]:enabled').prop('checked', checked);
  13. }
  14. function toggleCheckboxesBySelector(selector) {
  15. var all_checked = true;
  16. $(selector).each(function(index) {
  17. if (!$(this).is(':checked')) { all_checked = false; }
  18. });
  19. $(selector).prop('checked', !all_checked).trigger('change');
  20. }
  21. function showAndScrollTo(id, focus) {
  22. $('#'+id).show();
  23. if (focus !== null) {
  24. $('#'+focus).focus();
  25. }
  26. $('html, body').animate({scrollTop: $('#'+id).offset().top}, 100);
  27. }
  28. function toggleRowGroup(el) {
  29. var tr = $(el).parents('tr').first();
  30. var n = tr.next();
  31. tr.toggleClass('open');
  32. $(el).toggleClass('icon-expanded icon-collapsed');
  33. while (n.length && !n.hasClass('group')) {
  34. n.toggle();
  35. n = n.next('tr');
  36. }
  37. }
  38. function collapseAllRowGroups(el) {
  39. var tbody = $(el).parents('tbody').first();
  40. tbody.children('tr').each(function(index) {
  41. if ($(this).hasClass('group')) {
  42. $(this).removeClass('open');
  43. $(this).find('.expander').switchClass('icon-expanded', 'icon-collapsed');
  44. } else {
  45. $(this).hide();
  46. }
  47. });
  48. }
  49. function expandAllRowGroups(el) {
  50. var tbody = $(el).parents('tbody').first();
  51. tbody.children('tr').each(function(index) {
  52. if ($(this).hasClass('group')) {
  53. $(this).addClass('open');
  54. $(this).find('.expander').switchClass('icon-collapsed', 'icon-expanded');
  55. } else {
  56. $(this).show();
  57. }
  58. });
  59. }
  60. function toggleAllRowGroups(el) {
  61. var tr = $(el).parents('tr').first();
  62. if (tr.hasClass('open')) {
  63. collapseAllRowGroups(el);
  64. } else {
  65. expandAllRowGroups(el);
  66. }
  67. }
  68. function toggleFieldset(el) {
  69. var fieldset = $(el).parents('fieldset').first();
  70. fieldset.toggleClass('collapsed');
  71. fieldset.children('legend').toggleClass('icon-expanded icon-collapsed');
  72. fieldset.children('div').toggle();
  73. }
  74. function hideFieldset(el) {
  75. var fieldset = $(el).parents('fieldset').first();
  76. fieldset.toggleClass('collapsed');
  77. fieldset.children('div').hide();
  78. }
  79. // columns selection
  80. function moveOptions(theSelFrom, theSelTo) {
  81. $(theSelFrom).find('option:selected').detach().prop("selected", false).appendTo($(theSelTo));
  82. }
  83. function moveOptionUp(theSel) {
  84. $(theSel).find('option:selected').each(function(){
  85. $(this).prev(':not(:selected)').detach().insertAfter($(this));
  86. });
  87. }
  88. function moveOptionTop(theSel) {
  89. $(theSel).find('option:selected').detach().prependTo($(theSel));
  90. }
  91. function moveOptionDown(theSel) {
  92. $($(theSel).find('option:selected').get().reverse()).each(function(){
  93. $(this).next(':not(:selected)').detach().insertBefore($(this));
  94. });
  95. }
  96. function moveOptionBottom(theSel) {
  97. $(theSel).find('option:selected').detach().appendTo($(theSel));
  98. }
  99. function initFilters() {
  100. $('#add_filter_select').change(function() {
  101. addFilter($(this).val(), '', []);
  102. });
  103. $('#filters-table .field input[type=checkbox]').each(function() {
  104. toggleFilter($(this).val());
  105. });
  106. $('#filters-table').on('click', '.field input[type=checkbox]', function() {
  107. toggleFilter($(this).val());
  108. });
  109. $('#filters-table').on('keypress', 'input[type=text]', function(e) {
  110. if (e.keyCode == 13) $(this).closest('form').submit();
  111. });
  112. }
  113. function addFilter(field, operator, values) {
  114. var fieldId = field.replace('.', '_');
  115. var tr = $('#tr_'+fieldId);
  116. var filterOptions = availableFilters[field];
  117. if (!filterOptions) return;
  118. if (filterOptions['remote'] && filterOptions['values'] == null) {
  119. $.getJSON(filtersUrl, {'name': field}).done(function(data) {
  120. filterOptions['values'] = data;
  121. addFilter(field, operator, values) ;
  122. });
  123. return;
  124. }
  125. if (tr.length > 0) {
  126. tr.show();
  127. } else {
  128. buildFilterRow(field, operator, values);
  129. }
  130. $('#cb_'+fieldId).prop('checked', true);
  131. toggleFilter(field);
  132. toggleMultiSelectIconInit();
  133. $('#add_filter_select').val('').find('option').each(function() {
  134. if ($(this).attr('value') == field) {
  135. $(this).attr('disabled', true);
  136. }
  137. });
  138. }
  139. function buildFilterRow(field, operator, values) {
  140. var fieldId = field.replace('.', '_');
  141. var filterTable = $("#filters-table");
  142. var filterOptions = availableFilters[field];
  143. if (!filterOptions) return;
  144. var operators = operatorByType[filterOptions['type']];
  145. var filterValues = filterOptions['values'];
  146. var i, select;
  147. var tr = $('<div class="filter">').attr('id', 'tr_'+fieldId).html(
  148. '<div class="field"><input checked="checked" id="cb_'+fieldId+'" name="f[]" value="'+field+'" type="checkbox"><label for="cb_'+fieldId+'"> '+filterOptions['name']+'</label></div>' +
  149. '<div class="operator"><select id="operators_'+fieldId+'" name="op['+field+']"></select></div>' +
  150. '<div class="values"></div>'
  151. );
  152. filterTable.append(tr);
  153. select = tr.find('.operator select');
  154. for (i = 0; i < operators.length; i++) {
  155. var option = $('<option>').val(operators[i]).text(operatorLabels[operators[i]]);
  156. if (operators[i] == operator) { option.prop('selected', true); }
  157. select.append(option);
  158. }
  159. select.change(function(){ toggleOperator(field); });
  160. switch (filterOptions['type']) {
  161. case "list":
  162. case "list_with_history":
  163. case "list_optional":
  164. case "list_optional_with_history":
  165. case "list_status":
  166. case "list_subprojects":
  167. tr.find('.values').append(
  168. '<span style="display:none;"><select class="value" id="values_'+fieldId+'_1" name="v['+field+'][]"></select>' +
  169. ' <span class="toggle-multiselect icon-only '+(values.length > 1 ? 'icon-toggle-minus' : 'icon-toggle-plus')+'">&nbsp;</span></span>'
  170. );
  171. select = tr.find('.values select');
  172. if (values.length > 1) { select.attr('multiple', true); }
  173. for (i = 0; i < filterValues.length; i++) {
  174. var filterValue = filterValues[i];
  175. var option = $('<option>');
  176. if ($.isArray(filterValue)) {
  177. option.val(filterValue[1]).text(filterValue[0]);
  178. if ($.inArray(filterValue[1], values) > -1) {option.prop('selected', true);}
  179. if (filterValue.length == 3) {
  180. var optgroup = select.find('optgroup').filter(function(){return $(this).attr('label') == filterValue[2]});
  181. if (!optgroup.length) {optgroup = $('<optgroup>').attr('label', filterValue[2]);}
  182. option = optgroup.append(option);
  183. }
  184. } else {
  185. option.val(filterValue).text(filterValue);
  186. if ($.inArray(filterValue, values) > -1) {option.prop('selected', true);}
  187. }
  188. select.append(option);
  189. }
  190. break;
  191. case "date":
  192. case "date_past":
  193. tr.find('.values').append(
  194. '<span style="display:none;"><input type="date" name="v['+field+'][]" id="values_'+fieldId+'_1" size="10" class="value date_value" /></span>' +
  195. ' <span style="display:none;"><input type="date" name="v['+field+'][]" id="values_'+fieldId+'_2" size="10" class="value date_value" /></span>' +
  196. ' <span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'" size="3" class="value" /> '+labelDayPlural+'</span>'
  197. );
  198. $('#values_'+fieldId+'_1').val(values[0]).datepickerFallback(datepickerOptions);
  199. $('#values_'+fieldId+'_2').val(values[1]).datepickerFallback(datepickerOptions);
  200. $('#values_'+fieldId).val(values[0]);
  201. break;
  202. case "string":
  203. case "text":
  204. case "search":
  205. tr.find('.values').append(
  206. '<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'" size="30" class="value" /></span>'
  207. );
  208. $('#values_'+fieldId).val(values[0]);
  209. break;
  210. case "relation":
  211. tr.find('.values').append(
  212. '<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'" size="6" class="value" /></span>' +
  213. '<span style="display:none;"><select class="value" name="v['+field+'][]" id="values_'+fieldId+'_1"></select></span>'
  214. );
  215. $('#values_'+fieldId).val(values[0]);
  216. select = tr.find('.values select');
  217. for (i = 0; i < filterValues.length; i++) {
  218. var filterValue = filterValues[i];
  219. var option = $('<option>');
  220. option.val(filterValue[1]).text(filterValue[0]);
  221. if (values[0] == filterValue[1]) { option.prop('selected', true); }
  222. select.append(option);
  223. }
  224. break;
  225. case "integer":
  226. case "float":
  227. case "tree":
  228. tr.find('.values').append(
  229. '<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_1" size="14" class="value" /></span>' +
  230. ' <span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_2" size="14" class="value" /></span>'
  231. );
  232. $('#values_'+fieldId+'_1').val(values[0]);
  233. $('#values_'+fieldId+'_2').val(values[1]);
  234. break;
  235. }
  236. }
  237. function toggleFilter(field) {
  238. var fieldId = field.replace('.', '_');
  239. if ($('#cb_' + fieldId).is(':checked')) {
  240. $("#operators_" + fieldId).show().removeAttr('disabled');
  241. toggleOperator(field);
  242. } else {
  243. $("#operators_" + fieldId).hide().attr('disabled', true);
  244. enableValues(field, []);
  245. }
  246. }
  247. function enableValues(field, indexes) {
  248. var fieldId = field.replace('.', '_');
  249. $('#tr_'+fieldId+' .values .value').each(function(index) {
  250. if ($.inArray(index, indexes) >= 0) {
  251. $(this).removeAttr('disabled');
  252. $(this).parents('span').first().show();
  253. } else {
  254. $(this).val('');
  255. $(this).attr('disabled', true);
  256. $(this).parents('span').first().hide();
  257. }
  258. if ($(this).hasClass('group')) {
  259. $(this).addClass('open');
  260. } else {
  261. $(this).show();
  262. }
  263. });
  264. }
  265. function toggleOperator(field) {
  266. var fieldId = field.replace('.', '_');
  267. var operator = $("#operators_" + fieldId);
  268. switch (operator.val()) {
  269. case "!*":
  270. case "*":
  271. case "nd":
  272. case "t":
  273. case "ld":
  274. case "nw":
  275. case "w":
  276. case "lw":
  277. case "l2w":
  278. case "nm":
  279. case "m":
  280. case "lm":
  281. case "y":
  282. case "o":
  283. case "c":
  284. case "*o":
  285. case "!o":
  286. enableValues(field, []);
  287. break;
  288. case "><":
  289. enableValues(field, [0,1]);
  290. break;
  291. case "<t+":
  292. case ">t+":
  293. case "><t+":
  294. case "t+":
  295. case ">t-":
  296. case "<t-":
  297. case "><t-":
  298. case "t-":
  299. enableValues(field, [2]);
  300. break;
  301. case "=p":
  302. case "=!p":
  303. case "!p":
  304. enableValues(field, [1]);
  305. break;
  306. default:
  307. enableValues(field, [0]);
  308. break;
  309. }
  310. }
  311. function toggleMultiSelect(el) {
  312. var isWorkflow = el.closest('.controller-workflows');
  313. if (el.attr('multiple')) {
  314. el.removeAttr('multiple');
  315. if (isWorkflow) { el.find("option[value=all]").show(); }
  316. el.attr('size', 1);
  317. } else {
  318. el.attr('multiple', true);
  319. if (isWorkflow) { el.find("option[value=all]").attr("selected", false).hide(); }
  320. if (el.children().length > 10)
  321. el.attr('size', 10);
  322. else
  323. el.attr('size', 4);
  324. }
  325. }
  326. function showTab(name, url) {
  327. $('#tab-content-' + name).parent().find('.tab-content').hide();
  328. $('#tab-content-' + name).show();
  329. $('#tab-' + name).closest('.tabs').find('a').removeClass('selected');
  330. $('#tab-' + name).addClass('selected');
  331. replaceInHistory(url)
  332. return false;
  333. }
  334. function showIssueHistory(journal, url) {
  335. tab_content = $('#tab-content-history');
  336. tab_content.parent().find('.tab-content').hide();
  337. tab_content.show();
  338. tab_content.parent().children('div.tabs').find('a').removeClass('selected');
  339. $('#tab-' + journal).addClass('selected');
  340. replaceInHistory(url)
  341. switch(journal) {
  342. case 'notes':
  343. tab_content.find('.journal').show();
  344. tab_content.find('.journal:not(.has-notes)').hide();
  345. tab_content.find('.journal .wiki').show();
  346. tab_content.find('.journal .contextual .journal-actions').show();
  347. // always show thumbnails in notes tab
  348. var thumbnails = tab_content.find('.journal .thumbnails');
  349. thumbnails.show();
  350. // show journals without notes, but with thumbnails
  351. thumbnails.parents('.journal').show();
  352. break;
  353. case 'properties':
  354. tab_content.find('.journal').show();
  355. tab_content.find('.journal:not(.has-details)').hide();
  356. tab_content.find('.journal .wiki').hide();
  357. tab_content.find('.journal .thumbnails').hide();
  358. tab_content.find('.journal .contextual .journal-actions').hide();
  359. break;
  360. default:
  361. tab_content.find('.journal').show();
  362. tab_content.find('.journal .wiki').show();
  363. tab_content.find('.journal .thumbnails').show();
  364. tab_content.find('.journal .contextual .journal-actions').show();
  365. }
  366. return false;
  367. }
  368. function getRemoteTab(name, remote_url, url, load_always) {
  369. load_always = load_always || false;
  370. var tab_content = $('#tab-content-' + name);
  371. tab_content.parent().find('.tab-content').hide();
  372. tab_content.parent().children('div.tabs').find('a').removeClass('selected');
  373. $('#tab-' + name).addClass('selected');
  374. replaceInHistory(url);
  375. if (tab_content.children().length == 0 && load_always == false) {
  376. $.ajax({
  377. url: remote_url,
  378. type: 'get',
  379. success: function(data){
  380. tab_content.html(data)
  381. }
  382. });
  383. }
  384. tab_content.show();
  385. return false;
  386. }
  387. //replaces current URL with the "href" attribute of the current link
  388. //(only triggered if supported by browser)
  389. function replaceInHistory(url) {
  390. if ("replaceState" in window.history && url !== undefined) {
  391. window.history.replaceState(null, document.title, url);
  392. }
  393. }
  394. function moveTabRight(el) {
  395. var lis = $(el).parents('div.tabs').first().find('ul').children();
  396. var bw = $(el).parents('div.tabs-buttons').outerWidth(true);
  397. var tabsWidth = 0;
  398. var i = 0;
  399. lis.each(function() {
  400. if ($(this).is(':visible')) {
  401. tabsWidth += $(this).outerWidth(true);
  402. }
  403. });
  404. if (tabsWidth < $(el).parents('div.tabs').first().width() - bw) { return; }
  405. $(el).siblings('.tab-left').removeClass('disabled');
  406. while (i<lis.length && !lis.eq(i).is(':visible')) { i++; }
  407. var w = lis.eq(i).width();
  408. lis.eq(i).hide();
  409. if (tabsWidth - w < $(el).parents('div.tabs').first().width() - bw) {
  410. $(el).addClass('disabled');
  411. }
  412. }
  413. function moveTabLeft(el) {
  414. var lis = $(el).parents('div.tabs').first().find('ul').children();
  415. var i = 0;
  416. while (i < lis.length && !lis.eq(i).is(':visible')) { i++; }
  417. if (i > 0) {
  418. lis.eq(i-1).show();
  419. $(el).siblings('.tab-right').removeClass('disabled');
  420. }
  421. if (i <= 1) {
  422. $(el).addClass('disabled');
  423. }
  424. }
  425. function displayTabsButtons() {
  426. var lis;
  427. var tabsWidth;
  428. var el;
  429. var numHidden;
  430. $('div.tabs').each(function() {
  431. el = $(this);
  432. lis = el.find('ul').children();
  433. tabsWidth = 0;
  434. numHidden = 0;
  435. lis.each(function(){
  436. if ($(this).is(':visible')) {
  437. tabsWidth += $(this).outerWidth(true);
  438. } else {
  439. numHidden++;
  440. }
  441. });
  442. var bw = $(el).find('div.tabs-buttons').outerWidth(true);
  443. if ((tabsWidth < el.width() - bw) && (lis.length === 0 || lis.first().is(':visible'))) {
  444. el.find('div.tabs-buttons').hide();
  445. } else {
  446. el.find('div.tabs-buttons').show().children('button.tab-left').toggleClass('disabled', numHidden == 0);
  447. }
  448. });
  449. }
  450. function setPredecessorFieldsVisibility() {
  451. var relationType = $('#relation_relation_type');
  452. if (relationType.val() == "precedes" || relationType.val() == "follows") {
  453. $('#predecessor_fields').show();
  454. } else {
  455. $('#predecessor_fields').hide();
  456. }
  457. }
  458. function showModal(id, width, title) {
  459. var el = $('#'+id).first();
  460. if (el.length === 0 || el.is(':visible')) {return;}
  461. if (!title) title = el.find('h3.title').text();
  462. // moves existing modals behind the transparent background
  463. $(".modal").css('zIndex',99);
  464. el.dialog({
  465. width: width,
  466. modal: true,
  467. resizable: false,
  468. dialogClass: 'modal',
  469. title: title
  470. }).on('dialogclose', function(){
  471. $(".modal").css('zIndex',101);
  472. });
  473. el.find("input[type=text], input[type=submit]").first().focus();
  474. }
  475. function hideModal(el) {
  476. var modal;
  477. if (el) {
  478. modal = $(el).parents('.ui-dialog-content');
  479. } else {
  480. modal = $('#ajax-modal');
  481. }
  482. modal.dialog("close");
  483. }
  484. function collapseScmEntry(id) {
  485. $('.'+id).each(function() {
  486. if ($(this).hasClass('open')) {
  487. collapseScmEntry($(this).attr('id'));
  488. }
  489. $(this).hide();
  490. });
  491. $('#'+id).removeClass('open');
  492. }
  493. function expandScmEntry(id) {
  494. $('.'+id).each(function() {
  495. $(this).show();
  496. if ($(this).hasClass('loaded') && !$(this).hasClass('collapsed')) {
  497. expandScmEntry($(this).attr('id'));
  498. }
  499. });
  500. $('#'+id).addClass('open');
  501. }
  502. function scmEntryClick(id, url) {
  503. var el = $('#'+id);
  504. if (el.hasClass('open')) {
  505. collapseScmEntry(id);
  506. el.find('.expander').switchClass('icon-expanded', 'icon-collapsed');
  507. el.addClass('collapsed');
  508. return false;
  509. } else if (el.hasClass('loaded')) {
  510. expandScmEntry(id);
  511. el.find('.expander').switchClass('icon-collapsed', 'icon-expanded');
  512. el.removeClass('collapsed');
  513. return false;
  514. }
  515. if (el.hasClass('loading')) {
  516. return false;
  517. }
  518. el.addClass('loading');
  519. $.ajax({
  520. url: url,
  521. success: function(data) {
  522. el.after(data);
  523. el.addClass('open').addClass('loaded').removeClass('loading');
  524. el.find('.expander').switchClass('icon-collapsed', 'icon-expanded');
  525. }
  526. });
  527. return true;
  528. }
  529. function randomKey(size) {
  530. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  531. var key = '';
  532. for (var i = 0; i < size; i++) {
  533. key += chars.charAt(Math.floor(Math.random() * chars.length));
  534. }
  535. return key;
  536. }
  537. function copyTextToClipboard(target) {
  538. if (target) {
  539. var temp = document.createElement('textarea');
  540. temp.value = target.getAttribute('data-clipboard-text');
  541. document.body.appendChild(temp);
  542. temp.select();
  543. document.execCommand('copy');
  544. if (temp.parentNode) {
  545. temp.parentNode.removeChild(temp);
  546. }
  547. if ($(target).closest('.drdn.expanded').length) {
  548. $(target).closest('.drdn.expanded').removeClass("expanded");
  549. }
  550. }
  551. return false;
  552. }
  553. function updateIssueFrom(url, el) {
  554. $('#all_attributes input, #all_attributes textarea, #all_attributes select').each(function(){
  555. $(this).data('valuebeforeupdate', $(this).val());
  556. });
  557. if (el) {
  558. $("#form_update_triggered_by").val($(el).attr('id'));
  559. }
  560. return $.ajax({
  561. url: url,
  562. type: 'post',
  563. data: $('#issue-form').serialize()
  564. });
  565. }
  566. function replaceIssueFormWith(html){
  567. var replacement = $(html);
  568. $('#all_attributes input, #all_attributes textarea, #all_attributes select').each(function(){
  569. var object_id = $(this).attr('id');
  570. if (object_id && $(this).data('valuebeforeupdate')!=$(this).val()) {
  571. replacement.find('#'+object_id).val($(this).val());
  572. }
  573. });
  574. $('#all_attributes').empty();
  575. $('#all_attributes').prepend(replacement);
  576. }
  577. function updateBulkEditFrom(url) {
  578. $.ajax({
  579. url: url,
  580. type: 'post',
  581. data: $('#bulk_edit_form').serialize()
  582. });
  583. }
  584. function observeAutocompleteField(fieldId, url, options) {
  585. $(document).ready(function() {
  586. $('#'+fieldId).autocomplete($.extend({
  587. source: url,
  588. minLength: 2,
  589. position: {collision: "flipfit"},
  590. search: function(){$('#'+fieldId).addClass('ajax-loading');},
  591. response: function(){$('#'+fieldId).removeClass('ajax-loading');}
  592. }, options));
  593. $('#'+fieldId).addClass('autocomplete');
  594. });
  595. }
  596. function multipleAutocompleteField(fieldId, url, options) {
  597. function split(val) {
  598. return val.split(/,\s*/);
  599. }
  600. function extractLast(term) {
  601. return split(term).pop();
  602. }
  603. $(document).ready(function () {
  604. $('#' + fieldId).autocomplete($.extend({
  605. source: function (request, response) {
  606. $.getJSON(url, {
  607. term: extractLast(request.term)
  608. }, response);
  609. },
  610. minLength: 2,
  611. position: {collision: "flipfit"},
  612. search: function () {
  613. $('#' + fieldId).addClass('ajax-loading');
  614. },
  615. response: function () {
  616. $('#' + fieldId).removeClass('ajax-loading');
  617. },
  618. select: function (event, ui) {
  619. var terms = split(this.value);
  620. // remove the current input
  621. terms.pop();
  622. // add the selected item
  623. terms.push(ui.item.value);
  624. // add placeholder to get the comma-and-space at the end
  625. terms.push("");
  626. this.value = terms.join(", ");
  627. return false;
  628. }
  629. }, options));
  630. $('#' + fieldId).addClass('autocomplete');
  631. });
  632. }
  633. function observeSearchfield(fieldId, targetId, url) {
  634. $('#'+fieldId).each(function() {
  635. var $this = $(this);
  636. $this.addClass('autocomplete');
  637. $this.attr('data-value-was', $this.val());
  638. var check = function() {
  639. var val = $this.val();
  640. if ($this.attr('data-value-was') != val){
  641. $this.attr('data-value-was', val);
  642. $.ajax({
  643. url: url,
  644. type: 'get',
  645. data: {q: $this.val()},
  646. success: function(data){ if(targetId) $('#'+targetId).html(data); },
  647. beforeSend: function(){ $this.addClass('ajax-loading'); },
  648. complete: function(){ $this.removeClass('ajax-loading'); }
  649. });
  650. }
  651. };
  652. var reset = function() {
  653. if (timer) {
  654. clearInterval(timer);
  655. timer = setInterval(check, 300);
  656. }
  657. };
  658. var timer = setInterval(check, 300);
  659. $this.bind('keyup click mousemove', reset);
  660. });
  661. }
  662. $(document).ready(function(){
  663. $(".drdn .autocomplete").val('');
  664. // This variable is used to focus selected project
  665. var selected;
  666. $(document).on('click', '.drdn-trigger', function(e){
  667. var drdn = $(this).closest(".drdn");
  668. if (drdn.hasClass("expanded")) {
  669. drdn.removeClass("expanded");
  670. } else {
  671. $(".drdn").removeClass("expanded");
  672. drdn.addClass("expanded");
  673. selected = $('.drdn-items a.selected'); // Store selected project
  674. selected.focus(); // Calling focus to scroll to selected project
  675. if (!isMobile()) {
  676. drdn.find(".autocomplete").focus();
  677. }
  678. e.stopPropagation();
  679. }
  680. });
  681. $(document).click(function(e){
  682. if ($(e.target).closest(".drdn").length < 1) {
  683. $(".drdn.expanded").removeClass("expanded");
  684. }
  685. });
  686. observeSearchfield('projects-quick-search', null, $('#projects-quick-search').data('automcomplete-url'));
  687. $(".drdn-content").keydown(function(event){
  688. var items = $(this).find(".drdn-items");
  689. // If a project is selected set focused to selected only once
  690. if (selected && selected.length > 0) {
  691. var focused = selected;
  692. selected = undefined;
  693. }
  694. else {
  695. var focused = items.find("a:focus");
  696. }
  697. switch (event.which) {
  698. case 40: //down
  699. if (focused.length > 0) {
  700. focused.nextAll("a").first().focus();;
  701. } else {
  702. items.find("a").first().focus();;
  703. }
  704. event.preventDefault();
  705. break;
  706. case 38: //up
  707. if (focused.length > 0) {
  708. var prev = focused.prevAll("a");
  709. if (prev.length > 0) {
  710. prev.first().focus();
  711. } else {
  712. $(this).find(".autocomplete").focus();
  713. }
  714. event.preventDefault();
  715. }
  716. break;
  717. case 35: //end
  718. if (focused.length > 0) {
  719. focused.nextAll("a").last().focus();
  720. event.preventDefault();
  721. }
  722. break;
  723. case 36: //home
  724. if (focused.length > 0) {
  725. focused.prevAll("a").last().focus();
  726. event.preventDefault();
  727. }
  728. break;
  729. }
  730. });
  731. });
  732. function beforeShowDatePicker(input, inst) {
  733. var default_date = null;
  734. switch ($(input).attr("id")) {
  735. case "issue_start_date" :
  736. if ($("#issue_due_date").length > 0) {
  737. default_date = $("#issue_due_date").val();
  738. }
  739. break;
  740. case "issue_due_date" :
  741. if ($("#issue_start_date").length > 0) {
  742. var start_date = $("#issue_start_date").val();
  743. if (start_date != "") {
  744. start_date = new Date(Date.parse(start_date));
  745. if (start_date > new Date()) {
  746. default_date = $("#issue_start_date").val();
  747. }
  748. }
  749. }
  750. break;
  751. }
  752. $(input).datepickerFallback("option", "defaultDate", default_date);
  753. }
  754. (function($){
  755. $.fn.positionedItems = function(sortableOptions, options){
  756. var settings = $.extend({
  757. firstPosition: 1
  758. }, options );
  759. return this.sortable($.extend({
  760. axis: 'y',
  761. handle: ".sort-handle",
  762. helper: function(event, ui){
  763. ui.children('td').each(function(){
  764. $(this).width($(this).width());
  765. });
  766. return ui;
  767. },
  768. update: function(event, ui) {
  769. var sortable = $(this);
  770. var handle = ui.item.find(".sort-handle").addClass("ajax-loading");
  771. var url = handle.data("reorder-url");
  772. var param = handle.data("reorder-param");
  773. var data = {};
  774. data[param] = {position: ui.item.index() + settings['firstPosition']};
  775. $.ajax({
  776. url: url,
  777. type: 'put',
  778. dataType: 'script',
  779. data: data,
  780. error: function(jqXHR, textStatus, errorThrown){
  781. alert(jqXHR.status);
  782. sortable.sortable("cancel");
  783. },
  784. complete: function(jqXHR, textStatus, errorThrown){
  785. handle.removeClass("ajax-loading");
  786. }
  787. });
  788. },
  789. }, sortableOptions));
  790. }
  791. }( jQuery ));
  792. var warnLeavingUnsavedMessage;
  793. function warnLeavingUnsaved(message) {
  794. warnLeavingUnsavedMessage = message;
  795. $(document).on('submit', 'form', function(){
  796. $('textarea').removeData('changed');
  797. });
  798. $(document).on('change', 'textarea', function(){
  799. $(this).data('changed', 'changed');
  800. });
  801. window.onbeforeunload = function(){
  802. var warn = false;
  803. $('textarea').blur().each(function(){
  804. if ($(this).data('changed')) {
  805. warn = true;
  806. }
  807. });
  808. if (warn) {return warnLeavingUnsavedMessage;}
  809. };
  810. }
  811. function setupAjaxIndicator() {
  812. $(document).bind('ajaxSend', function(event, xhr, settings) {
  813. if ($('.ajax-loading').length === 0 && settings.contentType != 'application/octet-stream') {
  814. $('#ajax-indicator').show();
  815. }
  816. });
  817. $(document).bind('ajaxStop', function() {
  818. $('#ajax-indicator').hide();
  819. });
  820. }
  821. function setupTabs() {
  822. if($('.tabs').length > 0) {
  823. displayTabsButtons();
  824. $(window).resize(displayTabsButtons);
  825. }
  826. }
  827. function setupFilePreviewNavigation() {
  828. // only bind arrow keys when preview navigation is present
  829. const element = $('.pagination.filepreview').first();
  830. if (element) {
  831. const handleArrowKey = function(selector, e){
  832. const href = $(element).find(selector).attr('href');
  833. if (href) {
  834. window.location = href;
  835. e.preventDefault();
  836. }
  837. };
  838. $(document).keydown(function(e) {
  839. if(e.shiftKey || e.metaKey || e.ctrlKey || e.altKey) return;
  840. switch(e.key) {
  841. case 'ArrowLeft':
  842. handleArrowKey('.previous a', e);
  843. break;
  844. case 'ArrowRight':
  845. handleArrowKey('.next a', e);
  846. break;
  847. }
  848. });
  849. }
  850. }
  851. $(document).on('keydown', 'form textarea', function(e) {
  852. // Submit the form with Ctrl + Enter or Command + Return
  853. var targetForm = $(e.target).closest('form');
  854. if(e.keyCode == 13 && ((e.ctrlKey && !e.metaKey) || (!e.ctrlKey && e.metaKey) && targetForm.length)) {
  855. // For ajax, use click() instead of submit() to prevent "Invalid form authenticity token" error
  856. if (targetForm.attr('data-remote') == 'true') {
  857. if (targetForm.find('input[type=submit]').length === 0) { return false; }
  858. targetForm.find('textarea').blur().removeData('changed');
  859. targetForm.find('input[type=submit]').first().click();
  860. } else {
  861. targetForm.find('textarea').blur().removeData('changed');
  862. targetForm.submit();
  863. }
  864. }
  865. });
  866. function hideOnLoad() {
  867. $('.hol').hide();
  868. }
  869. function addFormObserversForDoubleSubmit() {
  870. $('form[method=post]').each(function() {
  871. if (!$(this).hasClass('multiple-submit')) {
  872. $(this).submit(function(form_submission) {
  873. if ($(form_submission.target).attr('data-submitted')) {
  874. form_submission.preventDefault();
  875. } else {
  876. $(form_submission.target).attr('data-submitted', true);
  877. }
  878. });
  879. }
  880. });
  881. }
  882. function defaultFocus(){
  883. if (($('#content :focus').length == 0) && (window.location.hash == '')) {
  884. $('#content input[type=text]:visible, #content textarea:visible').first().focus();
  885. }
  886. }
  887. function blockEventPropagation(event) {
  888. event.stopPropagation();
  889. event.preventDefault();
  890. }
  891. function toggleDisabledOnChange() {
  892. var checked = $(this).is(':checked');
  893. $($(this).data('disables')).attr('disabled', checked);
  894. $($(this).data('enables')).attr('disabled', !checked);
  895. $($(this).data('shows')).toggle(checked);
  896. }
  897. function toggleDisabledInit() {
  898. $('input[data-disables], input[data-enables], input[data-shows]').each(toggleDisabledOnChange);
  899. }
  900. function toggleMultiSelectIconInit() {
  901. $('.toggle-multiselect:not(.icon-toggle-minus), .toggle-multiselect:not(.icon-toggle-plus)').each(function(){
  902. if ($(this).siblings('select').find('option:selected').length > 1){
  903. $(this).addClass('icon-toggle-minus');
  904. } else {
  905. $(this).addClass('icon-toggle-plus');
  906. }
  907. });
  908. }
  909. function toggleNewObjectDropdown() {
  910. var dropdown = $('#new-object + ul.menu-children');
  911. if(dropdown.hasClass('visible')){
  912. dropdown.removeClass('visible');
  913. }else{
  914. dropdown.addClass('visible');
  915. }
  916. }
  917. (function ( $ ) {
  918. // detect if native date input is supported
  919. var nativeDateInputSupported = true;
  920. var input = document.createElement('input');
  921. input.setAttribute('type','date');
  922. if (input.type === 'text') {
  923. nativeDateInputSupported = false;
  924. }
  925. var notADateValue = 'not-a-date';
  926. input.setAttribute('value', notADateValue);
  927. if (input.value === notADateValue) {
  928. nativeDateInputSupported = false;
  929. }
  930. $.fn.datepickerFallback = function( options ) {
  931. if (nativeDateInputSupported) {
  932. return this;
  933. } else {
  934. return this.datepicker( options );
  935. }
  936. };
  937. }( jQuery ));
  938. $(document).ready(function(){
  939. $('#content').on('change', 'input[data-disables], input[data-enables], input[data-shows]', toggleDisabledOnChange);
  940. toggleDisabledInit();
  941. $('#content').on('click', '.toggle-multiselect', function() {
  942. toggleMultiSelect($(this).siblings('select'));
  943. $(this).toggleClass('icon-toggle-plus icon-toggle-minus');
  944. });
  945. toggleMultiSelectIconInit();
  946. $('#history .tabs').on('click', 'a', function(e){
  947. var tab = $(e.target).attr('id').replace('tab-','');
  948. document.cookie = 'history_last_tab=' + tab + '; SameSite=Lax'
  949. });
  950. });
  951. $(document).ready(function(){
  952. $('#content').on('click', 'div.jstTabs a.tab-preview', function(event){
  953. var tab = $(event.target);
  954. var url = tab.data('url');
  955. var form = tab.parents('form');
  956. var jstBlock = tab.parents('.jstBlock');
  957. var element = encodeURIComponent(jstBlock.find('.wiki-edit').val());
  958. var attachments = form.find('.attachments_fields input').serialize();
  959. $.ajax({
  960. url: url,
  961. type: 'post',
  962. data: "text=" + element + '&' + attachments,
  963. success: function(data){
  964. jstBlock.find('.wiki-preview').html(data);
  965. setupWikiTableSortableHeader();
  966. }
  967. });
  968. });
  969. });
  970. function keepAnchorOnSignIn(form){
  971. var hash = decodeURIComponent(self.document.location.hash);
  972. if (hash) {
  973. if (hash.indexOf("#") === -1) {
  974. hash = "#" + hash;
  975. }
  976. form.action = form.action + hash;
  977. }
  978. return true;
  979. }
  980. $(function ($) {
  981. $('#auth_source_ldap_mode').change(function () {
  982. $('.ldaps_warning').toggle($(this).val() != 'ldaps_verify_peer');
  983. }).change();
  984. });
  985. function setFilecontentContainerHeight() {
  986. var $filecontainer = $('.filecontent-container');
  987. var fileTypeSelectors = ['.image', 'video'];
  988. if($filecontainer.length > 0 && $filecontainer.find(fileTypeSelectors.join(',')).length === 1) {
  989. var containerOffsetTop = $filecontainer.offset().top;
  990. var containerMarginBottom = parseInt($filecontainer.css('marginBottom'));
  991. var paginationHeight = $filecontainer.next('.pagination').height();
  992. var diff = containerOffsetTop + containerMarginBottom + paginationHeight;
  993. $filecontainer.css('height', 'calc(100vh - ' + diff + 'px)')
  994. }
  995. }
  996. function setupAttachmentDetail() {
  997. setFilecontentContainerHeight();
  998. $(window).resize(setFilecontentContainerHeight);
  999. }
  1000. function setupWikiTableSortableHeader() {
  1001. $('div.wiki table').each(function(i, table){
  1002. if (table.rows.length < 3) return true;
  1003. var tr = $(table.rows).first();
  1004. if (tr.find("TH").length > 0) {
  1005. tr.attr('data-sort-method', 'none');
  1006. tr.find("TD").attr('data-sort-method', 'none');
  1007. new Tablesort(table);
  1008. }
  1009. });
  1010. }
  1011. $(function () {
  1012. $("[title]:not(.no-tooltip)").tooltip({
  1013. show: {
  1014. delay: 400
  1015. },
  1016. position: {
  1017. my: "center bottom-5",
  1018. at: "center top"
  1019. }
  1020. });
  1021. });
  1022. function inlineAutoComplete(element) {
  1023. 'use strict';
  1024. // do not attach if Tribute is already initialized
  1025. if (element.dataset.tribute === 'true') {return};
  1026. const getDataSource = function(entity) {
  1027. const dataSources = rm.AutoComplete.dataSources;
  1028. if (dataSources[entity]) {
  1029. return dataSources[entity];
  1030. } else {
  1031. return false;
  1032. }
  1033. }
  1034. const remoteSearch = function(url, cb) {
  1035. const xhr = new XMLHttpRequest();
  1036. xhr.onreadystatechange = function ()
  1037. {
  1038. if (xhr.readyState === 4) {
  1039. if (xhr.status === 200) {
  1040. var data = JSON.parse(xhr.responseText);
  1041. cb(data);
  1042. } else if (xhr.status === 403) {
  1043. cb([]);
  1044. }
  1045. }
  1046. };
  1047. xhr.open("GET", url, true);
  1048. xhr.send();
  1049. };
  1050. const tribute = new Tribute({
  1051. collection: [
  1052. {
  1053. trigger: '#',
  1054. values: function (text, cb) {
  1055. if (event.target.type === 'text' && $(element).attr('autocomplete') != 'off') {
  1056. $(element).attr('autocomplete', 'off');
  1057. }
  1058. remoteSearch(getDataSource('issues') + encodeURIComponent(text), function (issues) {
  1059. return cb(issues);
  1060. });
  1061. },
  1062. lookup: 'label',
  1063. fillAttr: 'label',
  1064. requireLeadingSpace: true,
  1065. selectTemplate: function (issue) {
  1066. let leadingHash = "#"
  1067. // keep ## syntax which is a valid issue syntax to show issue with title.
  1068. if (this.currentMentionTextSnapshot.charAt(0) === "#") {
  1069. leadingHash = "##"
  1070. }
  1071. return leadingHash + issue.original.id;
  1072. },
  1073. menuItemTemplate: function (issue) {
  1074. return sanitizeHTML(issue.original.label);
  1075. }
  1076. },
  1077. {
  1078. trigger: '[[',
  1079. values: function (text, cb) {
  1080. remoteSearch(getDataSource('wiki_pages') + encodeURIComponent(text), function (wikiPages) {
  1081. return cb(wikiPages);
  1082. });
  1083. },
  1084. lookup: 'label',
  1085. fillAttr: 'label',
  1086. requireLeadingSpace: true,
  1087. selectTemplate: function (wikiPage) {
  1088. return '[[' + wikiPage.original.value + ']]';
  1089. },
  1090. menuItemTemplate: function (wikiPage) {
  1091. return sanitizeHTML(wikiPage.original.label);
  1092. }
  1093. },
  1094. {
  1095. trigger: '@',
  1096. lookup: function (user, mentionText) {
  1097. return user.name + user.firstname + user.lastname + user.login;
  1098. },
  1099. values: function (text, cb) {
  1100. const url = getDataSource('users');
  1101. if (url) {
  1102. remoteSearch(url + encodeURIComponent(text), function (users) {
  1103. return cb(users);
  1104. });
  1105. }
  1106. },
  1107. menuItemTemplate: function (user) {
  1108. return user.original.name;
  1109. },
  1110. selectTemplate: function (user) {
  1111. return '@' + user.original.login;
  1112. }
  1113. }
  1114. ],
  1115. noMatchTemplate: ""
  1116. });
  1117. tribute.attach(element);
  1118. }
  1119. $(document).ready(setupAjaxIndicator);
  1120. $(document).ready(hideOnLoad);
  1121. $(document).ready(addFormObserversForDoubleSubmit);
  1122. $(document).ready(defaultFocus);
  1123. $(document).ready(setupAttachmentDetail);
  1124. $(document).ready(setupTabs);
  1125. $(document).ready(setupFilePreviewNavigation);
  1126. $(document).ready(setupWikiTableSortableHeader);
  1127. $(document).on('focus', '[data-auto-complete=true]', function(event) {
  1128. inlineAutoComplete(event.target);
  1129. });