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.

repo-issue.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. import $ from 'jquery';
  2. import {htmlEscape} from 'escape-goat';
  3. import {attachTribute} from './tribute.js';
  4. import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/EasyMDE.js';
  5. import {initEasyMDEImagePaste} from './comp/ImagePaste.js';
  6. import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
  7. import {initTooltip, showTemporaryTooltip} from '../modules/tippy.js';
  8. import {hideElem, showElem, toggleElem} from '../utils/dom.js';
  9. const {appSubUrl, csrfToken} = window.config;
  10. export function initRepoIssueTimeTracking() {
  11. $(document).on('click', '.issue-add-time', () => {
  12. $('.issue-start-time-modal').modal({
  13. duration: 200,
  14. onApprove() {
  15. $('#add_time_manual_form').trigger('submit');
  16. },
  17. }).modal('show');
  18. $('.issue-start-time-modal input').on('keydown', (e) => {
  19. if ((e.keyCode || e.key) === 13) {
  20. $('#add_time_manual_form').trigger('submit');
  21. }
  22. });
  23. });
  24. $(document).on('click', '.issue-start-time, .issue-stop-time', () => {
  25. $('#toggle_stopwatch_form').trigger('submit');
  26. });
  27. $(document).on('click', '.issue-cancel-time', () => {
  28. $('#cancel_stopwatch_form').trigger('submit');
  29. });
  30. $(document).on('click', 'button.issue-delete-time', function () {
  31. const sel = `.issue-delete-time-modal[data-id="${$(this).data('id')}"]`;
  32. $(sel).modal({
  33. duration: 200,
  34. onApprove() {
  35. $(`${sel} form`).trigger('submit');
  36. },
  37. }).modal('show');
  38. });
  39. }
  40. function updateDeadline(deadlineString) {
  41. hideElem($('#deadline-err-invalid-date'));
  42. $('#deadline-loader').addClass('loading');
  43. let realDeadline = null;
  44. if (deadlineString !== '') {
  45. const newDate = Date.parse(deadlineString);
  46. if (Number.isNaN(newDate)) {
  47. $('#deadline-loader').removeClass('loading');
  48. showElem($('#deadline-err-invalid-date'));
  49. return false;
  50. }
  51. realDeadline = new Date(newDate);
  52. }
  53. $.ajax(`${$('#update-issue-deadline-form').attr('action')}`, {
  54. data: JSON.stringify({
  55. due_date: realDeadline,
  56. }),
  57. headers: {
  58. 'X-Csrf-Token': csrfToken,
  59. },
  60. contentType: 'application/json',
  61. type: 'POST',
  62. success() {
  63. window.location.reload();
  64. },
  65. error() {
  66. $('#deadline-loader').removeClass('loading');
  67. showElem($('#deadline-err-invalid-date'));
  68. },
  69. });
  70. }
  71. export function initRepoIssueDue() {
  72. $(document).on('click', '.issue-due-edit', () => {
  73. $('#deadlineForm').fadeToggle(150);
  74. });
  75. $(document).on('click', '.issue-due-remove', () => {
  76. updateDeadline('');
  77. });
  78. $(document).on('submit', '.issue-due-form', () => {
  79. updateDeadline($('#deadlineDate').val());
  80. return false;
  81. });
  82. }
  83. export function initRepoIssueList() {
  84. const repolink = $('#repolink').val();
  85. const repoId = $('#repoId').val();
  86. const crossRepoSearch = $('#crossRepoSearch').val();
  87. const tp = $('#type').val();
  88. let issueSearchUrl = `${appSubUrl}/${repolink}/issues/search?q={query}&type=${tp}`;
  89. if (crossRepoSearch === 'true') {
  90. issueSearchUrl = `${appSubUrl}/issues/search?q={query}&priority_repo_id=${repoId}&type=${tp}`;
  91. }
  92. $('#new-dependency-drop-list')
  93. .dropdown({
  94. apiSettings: {
  95. url: issueSearchUrl,
  96. onResponse(response) {
  97. const filteredResponse = {success: true, results: []};
  98. const currIssueId = $('#new-dependency-drop-list').data('issue-id');
  99. // Parse the response from the api to work with our dropdown
  100. $.each(response, (_i, issue) => {
  101. // Don't list current issue in the dependency list.
  102. if (issue.id === currIssueId) {
  103. return;
  104. }
  105. filteredResponse.results.push({
  106. name: `#${issue.number} ${htmlEscape(issue.title)
  107. }<div class="text small dont-break-out">${htmlEscape(issue.repository.full_name)}</div>`,
  108. value: issue.id,
  109. });
  110. });
  111. return filteredResponse;
  112. },
  113. cache: false,
  114. },
  115. fullTextSearch: true,
  116. });
  117. function excludeLabel(item) {
  118. const href = $(item).attr('href');
  119. const id = $(item).data('label-id');
  120. const regStr = `labels=((?:-?[0-9]+%2c)*)(${id})((?:%2c-?[0-9]+)*)&`;
  121. const newStr = 'labels=$1-$2$3&';
  122. window.location = href.replace(new RegExp(regStr), newStr);
  123. }
  124. $('.menu a.label-filter-item').each(function () {
  125. $(this).on('click', function (e) {
  126. if (e.altKey) {
  127. e.preventDefault();
  128. excludeLabel(this);
  129. }
  130. });
  131. });
  132. $('.menu .ui.dropdown.label-filter').on('keydown', (e) => {
  133. if (e.altKey && e.keyCode === 13) {
  134. const selectedItems = $('.menu .ui.dropdown.label-filter .menu .item.selected');
  135. if (selectedItems.length > 0) {
  136. excludeLabel($(selectedItems[0]));
  137. }
  138. }
  139. });
  140. }
  141. export function initRepoIssueCommentDelete() {
  142. // Delete comment
  143. $(document).on('click', '.delete-comment', function () {
  144. const $this = $(this);
  145. if (window.confirm($this.data('locale'))) {
  146. $.post($this.data('url'), {
  147. _csrf: csrfToken,
  148. }).done(() => {
  149. const $conversationHolder = $this.closest('.conversation-holder');
  150. // Check if this was a pending comment.
  151. if ($conversationHolder.find('.pending-label').length) {
  152. const $counter = $('#review-box .review-comments-counter');
  153. let num = parseInt($counter.attr('data-pending-comment-number')) - 1 || 0;
  154. num = Math.max(num, 0);
  155. $counter.attr('data-pending-comment-number', num);
  156. $counter.text(num);
  157. }
  158. $(`#${$this.data('comment-id')}`).remove();
  159. if ($conversationHolder.length && !$conversationHolder.find('.comment').length) {
  160. const path = $conversationHolder.data('path');
  161. const side = $conversationHolder.data('side');
  162. const idx = $conversationHolder.data('idx');
  163. const lineType = $conversationHolder.closest('tr').data('line-type');
  164. if (lineType === 'same') {
  165. $(`[data-path="${path}"] a.add-code-comment[data-idx="${idx}"]`).removeClass('invisible');
  166. } else {
  167. $(`[data-path="${path}"] a.add-code-comment[data-side="${side}"][data-idx="${idx}"]`).removeClass('invisible');
  168. }
  169. $conversationHolder.remove();
  170. }
  171. });
  172. }
  173. return false;
  174. });
  175. }
  176. export function initRepoIssueDependencyDelete() {
  177. // Delete Issue dependency
  178. $(document).on('click', '.delete-dependency-button', (e) => {
  179. const id = e.currentTarget.getAttribute('data-id');
  180. const type = e.currentTarget.getAttribute('data-type');
  181. $('.remove-dependency').modal({
  182. closable: false,
  183. duration: 200,
  184. onApprove: () => {
  185. $('#removeDependencyID').val(id);
  186. $('#dependencyType').val(type);
  187. $('#removeDependencyForm').trigger('submit');
  188. },
  189. }).modal('show');
  190. });
  191. }
  192. export function initRepoIssueCodeCommentCancel() {
  193. // Cancel inline code comment
  194. $(document).on('click', '.cancel-code-comment', (e) => {
  195. const form = $(e.currentTarget).closest('form');
  196. if (form.length > 0 && form.hasClass('comment-form')) {
  197. form.addClass('gt-hidden');
  198. showElem(form.closest('.comment-code-cloud').find('button.comment-form-reply'));
  199. } else {
  200. form.closest('.comment-code-cloud').remove();
  201. }
  202. });
  203. }
  204. export function initRepoIssueStatusButton() {
  205. // Change status
  206. const $statusButton = $('#status-button');
  207. $('#comment-form textarea').on('keyup', function () {
  208. const easyMDE = getAttachedEasyMDE(this);
  209. const value = easyMDE?.value() || $(this).val();
  210. $statusButton.text($statusButton.data(value.length === 0 ? 'status' : 'status-and-comment'));
  211. });
  212. $statusButton.on('click', () => {
  213. $('#status').val($statusButton.data('status-val'));
  214. $('#comment-form').trigger('submit');
  215. });
  216. }
  217. export function initRepoPullRequestUpdate() {
  218. // Pull Request update button
  219. const $pullUpdateButton = $('.update-button > button');
  220. $pullUpdateButton.on('click', function (e) {
  221. e.preventDefault();
  222. const $this = $(this);
  223. const redirect = $this.data('redirect');
  224. $this.addClass('loading');
  225. $.post($this.data('do'), {
  226. _csrf: csrfToken
  227. }).done((data) => {
  228. if (data.redirect) {
  229. window.location.href = data.redirect;
  230. } else if (redirect) {
  231. window.location.href = redirect;
  232. } else {
  233. window.location.reload();
  234. }
  235. });
  236. });
  237. $('.update-button > .dropdown').dropdown({
  238. onChange(_text, _value, $choice) {
  239. const $url = $choice.data('do');
  240. if ($url) {
  241. $pullUpdateButton.find('.button-text').text($choice.text());
  242. $pullUpdateButton.data('do', $url);
  243. }
  244. }
  245. });
  246. }
  247. export function initRepoPullRequestMergeInstruction() {
  248. $('.show-instruction').on('click', () => {
  249. toggleElem($('.instruct-content'));
  250. });
  251. }
  252. export function initRepoPullRequestAllowMaintainerEdit() {
  253. const $checkbox = $('#allow-edits-from-maintainers');
  254. if (!$checkbox.length) return;
  255. const promptTip = $checkbox.attr('data-prompt-tip');
  256. const promptError = $checkbox.attr('data-prompt-error');
  257. initTooltip($checkbox[0], {content: promptTip});
  258. $checkbox.checkbox({
  259. 'onChange': () => {
  260. const checked = $checkbox.checkbox('is checked');
  261. let url = $checkbox.attr('data-url');
  262. url += '/set_allow_maintainer_edit';
  263. $checkbox.checkbox('set disabled');
  264. $.ajax({url, type: 'POST',
  265. data: {_csrf: csrfToken, allow_maintainer_edit: checked},
  266. error: () => {
  267. showTemporaryTooltip($checkbox[0], promptError);
  268. },
  269. complete: () => {
  270. $checkbox.checkbox('set enabled');
  271. },
  272. });
  273. },
  274. });
  275. }
  276. export function initRepoIssueReferenceRepositorySearch() {
  277. $('.issue_reference_repository_search')
  278. .dropdown({
  279. apiSettings: {
  280. url: `${appSubUrl}/repo/search?q={query}&limit=20`,
  281. onResponse(response) {
  282. const filteredResponse = {success: true, results: []};
  283. $.each(response.data, (_r, repo) => {
  284. filteredResponse.results.push({
  285. name: htmlEscape(repo.full_name),
  286. value: repo.full_name
  287. });
  288. });
  289. return filteredResponse;
  290. },
  291. cache: false,
  292. },
  293. onChange(_value, _text, $choice) {
  294. const $form = $choice.closest('form');
  295. $form.attr('action', `${appSubUrl}/${_text}/issues/new`);
  296. },
  297. fullTextSearch: true
  298. });
  299. }
  300. export function initRepoIssueWipTitle() {
  301. $('.title_wip_desc > a').on('click', (e) => {
  302. e.preventDefault();
  303. const $issueTitle = $('#issue_title');
  304. $issueTitle.focus();
  305. const value = $issueTitle.val().trim().toUpperCase();
  306. const wipPrefixes = $('.title_wip_desc').data('wip-prefixes');
  307. for (const prefix of wipPrefixes) {
  308. if (value.startsWith(prefix.toUpperCase())) {
  309. return;
  310. }
  311. }
  312. $issueTitle.val(`${wipPrefixes[0]} ${$issueTitle.val()}`);
  313. });
  314. }
  315. export async function updateIssuesMeta(url, action, issueIds, elementId) {
  316. return $.ajax({
  317. type: 'POST',
  318. url,
  319. data: {
  320. _csrf: csrfToken,
  321. action,
  322. issue_ids: issueIds,
  323. id: elementId,
  324. },
  325. });
  326. }
  327. export function initRepoIssueComments() {
  328. if ($('.repository.view.issue .timeline').length === 0) return;
  329. $('.re-request-review').on('click', function (e) {
  330. e.preventDefault();
  331. const url = $(this).data('update-url');
  332. const issueId = $(this).data('issue-id');
  333. const id = $(this).data('id');
  334. const isChecked = $(this).hasClass('checked');
  335. updateIssuesMeta(
  336. url,
  337. isChecked ? 'detach' : 'attach',
  338. issueId,
  339. id,
  340. ).then(() => window.location.reload());
  341. });
  342. $('.dismiss-review-btn').on('click', function (e) {
  343. e.preventDefault();
  344. const $this = $(this);
  345. const $dismissReviewModal = $this.next();
  346. $dismissReviewModal.modal('show');
  347. });
  348. $(document).on('click', (event) => {
  349. const urlTarget = $(':target');
  350. if (urlTarget.length === 0) return;
  351. const urlTargetId = urlTarget.attr('id');
  352. if (!urlTargetId) return;
  353. if (!/^(issue|pull)(comment)?-\d+$/.test(urlTargetId)) return;
  354. const $target = $(event.target);
  355. if ($target.closest(`#${urlTargetId}`).length === 0) {
  356. const scrollPosition = $(window).scrollTop();
  357. window.location.hash = '';
  358. $(window).scrollTop(scrollPosition);
  359. window.history.pushState(null, null, ' ');
  360. }
  361. });
  362. }
  363. function assignMenuAttributes(menu) {
  364. const id = Math.floor(Math.random() * Math.floor(1000000));
  365. menu.attr('data-write', menu.attr('data-write') + id);
  366. menu.attr('data-preview', menu.attr('data-preview') + id);
  367. menu.find('.item').each(function () {
  368. const tab = $(this).attr('data-tab') + id;
  369. $(this).attr('data-tab', tab);
  370. });
  371. menu.parent().find("*[data-tab='write']").attr('data-tab', `write${id}`);
  372. menu.parent().find("*[data-tab='preview']").attr('data-tab', `preview${id}`);
  373. initCompMarkupContentPreviewTab(menu.parent('.form'));
  374. return id;
  375. }
  376. export function initRepoPullRequestReview() {
  377. if (window.location.hash && window.location.hash.startsWith('#issuecomment-')) {
  378. const commentDiv = $(window.location.hash);
  379. if (commentDiv) {
  380. // get the name of the parent id
  381. const groupID = commentDiv.closest('div[id^="code-comments-"]').attr('id');
  382. if (groupID && groupID.startsWith('code-comments-')) {
  383. const id = groupID.slice(14);
  384. $(`#show-outdated-${id}`).addClass('gt-hidden');
  385. $(`#code-comments-${id}`).removeClass('gt-hidden');
  386. $(`#code-preview-${id}`).removeClass('gt-hidden');
  387. $(`#hide-outdated-${id}`).removeClass('gt-hidden');
  388. commentDiv[0].scrollIntoView();
  389. }
  390. }
  391. }
  392. $(document).on('click', '.show-outdated', function (e) {
  393. e.preventDefault();
  394. const id = $(this).data('comment');
  395. $(this).addClass('gt-hidden');
  396. $(`#code-comments-${id}`).removeClass('gt-hidden');
  397. $(`#code-preview-${id}`).removeClass('gt-hidden');
  398. $(`#hide-outdated-${id}`).removeClass('gt-hidden');
  399. });
  400. $(document).on('click', '.hide-outdated', function (e) {
  401. e.preventDefault();
  402. const id = $(this).data('comment');
  403. $(this).addClass('gt-hidden');
  404. $(`#code-comments-${id}`).addClass('gt-hidden');
  405. $(`#code-preview-${id}`).addClass('gt-hidden');
  406. $(`#show-outdated-${id}`).removeClass('gt-hidden');
  407. });
  408. $(document).on('click', 'button.comment-form-reply', async function (e) {
  409. e.preventDefault();
  410. hideElem($(this));
  411. const form = $(this).closest('.comment-code-cloud').find('.comment-form');
  412. form.removeClass('gt-hidden');
  413. const $textarea = form.find('textarea');
  414. let easyMDE = getAttachedEasyMDE($textarea);
  415. if (!easyMDE) {
  416. await attachTribute($textarea.get(), {mentions: true, emoji: true});
  417. easyMDE = await createCommentEasyMDE($textarea);
  418. }
  419. $textarea.focus();
  420. easyMDE.codemirror.focus();
  421. assignMenuAttributes(form.find('.menu'));
  422. });
  423. const $reviewBox = $('.review-box');
  424. if ($reviewBox.length === 1) {
  425. (async () => {
  426. // the editor's height is too large in some cases, and the panel cannot be scrolled with page now because there is `.repository .diff-detail-box.sticky { position: sticky; }`
  427. // the temporary solution is to make the editor's height smaller (about 4 lines). GitHub also only show 4 lines for default. We can improve the UI (including Dropzone area) in future
  428. // EasyMDE's options can not handle minHeight & maxHeight together correctly, we have to set max-height for .CodeMirror-scroll in CSS.
  429. const $reviewTextarea = $reviewBox.find('textarea');
  430. const easyMDE = await createCommentEasyMDE($reviewTextarea, {minHeight: '80px'});
  431. initEasyMDEImagePaste(easyMDE, $reviewBox.find('.dropzone'));
  432. })();
  433. }
  434. // The following part is only for diff views
  435. if ($('.repository.pull.diff').length === 0) {
  436. return;
  437. }
  438. $('.btn-review').on('click', function (e) {
  439. e.preventDefault();
  440. $(this).closest('.dropdown').find('.menu').toggle('visible'); // eslint-disable-line
  441. }).closest('.dropdown').find('.close').on('click', function (e) {
  442. e.preventDefault();
  443. $(this).closest('.menu').toggle('visible'); // eslint-disable-line
  444. });
  445. $(document).on('click', 'a.add-code-comment', async function (e) {
  446. if ($(e.target).hasClass('btn-add-single')) return; // https://github.com/go-gitea/gitea/issues/4745
  447. e.preventDefault();
  448. const isSplit = $(this).closest('.code-diff').hasClass('code-diff-split');
  449. const side = $(this).data('side');
  450. const idx = $(this).data('idx');
  451. const path = $(this).closest('[data-path]').data('path');
  452. const tr = $(this).closest('tr');
  453. const lineType = tr.data('line-type');
  454. let ntr = tr.next();
  455. if (!ntr.hasClass('add-comment')) {
  456. ntr = $(`
  457. <tr class="add-comment" data-line-type="${lineType}">
  458. ${isSplit ? `
  459. <td class="lines-num"></td>
  460. <td class="lines-escape"></td>
  461. <td class="lines-type-marker"></td>
  462. <td class="add-comment-left" colspan="4"></td>
  463. <td class="lines-num"></td>
  464. <td class="lines-escape"></td>
  465. <td class="lines-type-marker"></td>
  466. <td class="add-comment-right" colspan="4"></td>
  467. ` : `
  468. <td class="lines-num"></td>
  469. <td class="lines-num"></td>
  470. <td class="lines-escape"></td>
  471. <td class="add-comment-left add-comment-right" colspan="5"></td>
  472. `}
  473. </tr>`);
  474. tr.after(ntr);
  475. }
  476. const td = ntr.find(`.add-comment-${side}`);
  477. let commentCloud = td.find('.comment-code-cloud');
  478. if (commentCloud.length === 0 && !ntr.find('button[name="is_review"]').length) {
  479. const data = await $.get($(this).closest('[data-new-comment-url]').data('new-comment-url'));
  480. td.html(data);
  481. commentCloud = td.find('.comment-code-cloud');
  482. assignMenuAttributes(commentCloud.find('.menu'));
  483. td.find("input[name='line']").val(idx);
  484. td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed');
  485. td.find("input[name='path']").val(path);
  486. const $textarea = commentCloud.find('textarea');
  487. await attachTribute($textarea.get(), {mentions: true, emoji: true});
  488. const easyMDE = await createCommentEasyMDE($textarea);
  489. $textarea.focus();
  490. easyMDE.codemirror.focus();
  491. }
  492. });
  493. }
  494. export function initRepoIssueReferenceIssue() {
  495. // Reference issue
  496. $(document).on('click', '.reference-issue', function (event) {
  497. const $this = $(this);
  498. $this.closest('.dropdown').find('.menu').toggle('visible'); // eslint-disable-line
  499. const content = $(`#${$this.data('target')}`).text();
  500. const poster = $this.data('poster-username');
  501. const reference = $this.data('reference');
  502. const $modal = $($this.data('modal'));
  503. $modal.find('textarea[name="content"]').val(`${content}\n\n_Originally posted by @${poster} in ${reference}_`);
  504. $modal.modal('show');
  505. event.preventDefault();
  506. });
  507. }
  508. export function initRepoIssueWipToggle() {
  509. // Toggle WIP
  510. $('.toggle-wip a, .toggle-wip button').on('click', async (e) => {
  511. e.preventDefault();
  512. const toggleWip = e.currentTarget.closest('.toggle-wip');
  513. const title = toggleWip.getAttribute('data-title');
  514. const wipPrefix = toggleWip.getAttribute('data-wip-prefix');
  515. const updateUrl = toggleWip.getAttribute('data-update-url');
  516. await $.post(updateUrl, {
  517. _csrf: csrfToken,
  518. title: title?.startsWith(wipPrefix) ? title.slice(wipPrefix.length).trim() : `${wipPrefix.trim()} ${title}`,
  519. });
  520. window.location.reload();
  521. });
  522. }
  523. export function initRepoIssueTitleEdit() {
  524. // Edit issue title
  525. const $issueTitle = $('#issue-title');
  526. const $editInput = $('#edit-title-input input');
  527. const editTitleToggle = function () {
  528. toggleElem($issueTitle);
  529. toggleElem($('.not-in-edit'));
  530. toggleElem($('#edit-title-input'));
  531. toggleElem($('#pull-desc'));
  532. toggleElem($('#pull-desc-edit'));
  533. toggleElem($('.in-edit'));
  534. $('#issue-title-wrapper').toggleClass('edit-active');
  535. $editInput.focus();
  536. return false;
  537. };
  538. $('#edit-title').on('click', editTitleToggle);
  539. $('#cancel-edit-title').on('click', editTitleToggle);
  540. $('#save-edit-title').on('click', editTitleToggle).on('click', function () {
  541. const pullrequest_targetbranch_change = function (update_url) {
  542. const targetBranch = $('#pull-target-branch').data('branch');
  543. const $branchTarget = $('#branch_target');
  544. if (targetBranch === $branchTarget.text()) {
  545. window.location.reload();
  546. return false;
  547. }
  548. $.post(update_url, {
  549. _csrf: csrfToken,
  550. target_branch: targetBranch
  551. }).done((data) => {
  552. $branchTarget.text(data.base_branch);
  553. }).always(() => {
  554. window.location.reload();
  555. });
  556. };
  557. const pullrequest_target_update_url = $(this).attr('data-target-update-url');
  558. if ($editInput.val().length === 0 || $editInput.val() === $issueTitle.text()) {
  559. $editInput.val($issueTitle.text());
  560. pullrequest_targetbranch_change(pullrequest_target_update_url);
  561. } else {
  562. $.post($(this).attr('data-update-url'), {
  563. _csrf: csrfToken,
  564. title: $editInput.val()
  565. }, (data) => {
  566. $editInput.val(data.title);
  567. $issueTitle.text(data.title);
  568. if (pullrequest_target_update_url) {
  569. pullrequest_targetbranch_change(pullrequest_target_update_url); // it will reload the window
  570. } else {
  571. window.location.reload();
  572. }
  573. });
  574. }
  575. return false;
  576. });
  577. }
  578. export function initRepoIssueBranchSelect() {
  579. const changeBranchSelect = function () {
  580. const selectionTextField = $('#pull-target-branch');
  581. const baseName = selectionTextField.data('basename');
  582. const branchNameNew = $(this).data('branch');
  583. const branchNameOld = selectionTextField.data('branch');
  584. // Replace branch name to keep translation from HTML template
  585. selectionTextField.html(selectionTextField.html().replace(
  586. `${baseName}:${branchNameOld}`,
  587. `${baseName}:${branchNameNew}`
  588. ));
  589. selectionTextField.data('branch', branchNameNew); // update branch name in setting
  590. };
  591. $('#branch-select > .item').on('click', changeBranchSelect);
  592. }