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-projects.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import $ from 'jquery';
  2. import {useLightTextOnBackground} from '../utils/color.js';
  3. import tinycolor from 'tinycolor2';
  4. import {createSortable} from '../modules/sortable.js';
  5. import {POST, DELETE, PUT} from '../modules/fetch.js';
  6. function updateIssueCount(cards) {
  7. const parent = cards.parentElement;
  8. const cnt = parent.getElementsByClassName('issue-card').length;
  9. parent.getElementsByClassName('project-column-issue-count')[0].textContent = cnt;
  10. }
  11. async function createNewColumn(url, columnTitle, projectColorInput) {
  12. try {
  13. await POST(url, {
  14. data: {
  15. title: columnTitle.val(),
  16. color: projectColorInput.val(),
  17. },
  18. });
  19. } catch (error) {
  20. console.error(error);
  21. } finally {
  22. columnTitle.closest('form').removeClass('dirty');
  23. window.location.reload();
  24. }
  25. }
  26. async function moveIssue({item, from, to, oldIndex}) {
  27. const columnCards = to.getElementsByClassName('issue-card');
  28. updateIssueCount(from);
  29. updateIssueCount(to);
  30. const columnSorting = {
  31. issues: Array.from(columnCards, (card, i) => ({
  32. issueID: parseInt(card.getAttribute('data-issue')),
  33. sorting: i,
  34. })),
  35. };
  36. try {
  37. await POST(`${to.getAttribute('data-url')}/move`, {
  38. data: columnSorting,
  39. });
  40. } catch (error) {
  41. console.error(error);
  42. from.insertBefore(item, from.children[oldIndex]);
  43. }
  44. }
  45. async function initRepoProjectSortable() {
  46. const els = document.querySelectorAll('#project-board > .board.sortable');
  47. if (!els.length) return;
  48. // the HTML layout is: #project-board > .board > .project-column .cards > .issue-card
  49. const mainBoard = els[0];
  50. let boardColumns = mainBoard.getElementsByClassName('project-column');
  51. createSortable(mainBoard, {
  52. group: 'project-column',
  53. draggable: '.project-column',
  54. handle: '.project-column-header',
  55. delayOnTouchOnly: true,
  56. delay: 500,
  57. onSort: async () => {
  58. boardColumns = mainBoard.getElementsByClassName('project-column');
  59. for (let i = 0; i < boardColumns.length; i++) {
  60. const column = boardColumns[i];
  61. if (parseInt($(column).data('sorting')) !== i) {
  62. try {
  63. await PUT($(column).data('url'), {
  64. data: {
  65. sorting: i,
  66. color: rgbToHex(window.getComputedStyle($(column)[0]).backgroundColor),
  67. },
  68. });
  69. } catch (error) {
  70. console.error(error);
  71. }
  72. }
  73. }
  74. },
  75. });
  76. for (const boardColumn of boardColumns) {
  77. const boardCardList = boardColumn.getElementsByClassName('cards')[0];
  78. createSortable(boardCardList, {
  79. group: 'shared',
  80. onAdd: moveIssue,
  81. onUpdate: moveIssue,
  82. delayOnTouchOnly: true,
  83. delay: 500,
  84. });
  85. }
  86. }
  87. export function initRepoProject() {
  88. if (!$('.repository.projects').length) {
  89. return;
  90. }
  91. const _promise = initRepoProjectSortable();
  92. $('.edit-project-column-modal').each(function () {
  93. const $projectHeader = $(this).closest('.project-column-header');
  94. const $projectTitleLabel = $projectHeader.find('.project-column-title');
  95. const $projectTitleInput = $(this).find('.project-column-title-input');
  96. const $projectColorInput = $(this).find('#new_project_column_color');
  97. const $boardColumn = $(this).closest('.project-column');
  98. const bgColor = $boardColumn[0].style.backgroundColor;
  99. if (bgColor) {
  100. setLabelColor($projectHeader, rgbToHex(bgColor));
  101. }
  102. $(this).find('.edit-project-column-button').on('click', async function (e) {
  103. e.preventDefault();
  104. try {
  105. await PUT($(this).data('url'), {
  106. data: {
  107. title: $projectTitleInput.val(),
  108. color: $projectColorInput.val(),
  109. },
  110. });
  111. } catch (error) {
  112. console.error(error);
  113. } finally {
  114. $projectTitleLabel.text($projectTitleInput.val());
  115. $projectTitleInput.closest('form').removeClass('dirty');
  116. if ($projectColorInput.val()) {
  117. setLabelColor($projectHeader, $projectColorInput.val());
  118. }
  119. $boardColumn[0].style = `background: ${$projectColorInput.val()} !important`;
  120. $('.ui.modal').modal('hide');
  121. }
  122. });
  123. });
  124. $('.default-project-column-modal').each(function () {
  125. const $boardColumn = $(this).closest('.project-column');
  126. const $showButton = $($boardColumn).find('.default-project-column-show');
  127. const $commitButton = $(this).find('.actions > .ok.button');
  128. $($commitButton).on('click', async (e) => {
  129. e.preventDefault();
  130. try {
  131. await POST($($showButton).data('url'));
  132. } catch (error) {
  133. console.error(error);
  134. } finally {
  135. window.location.reload();
  136. }
  137. });
  138. });
  139. $('.show-delete-project-column-modal').each(function () {
  140. const $deleteColumnModal = $(`${this.getAttribute('data-modal')}`);
  141. const $deleteColumnButton = $deleteColumnModal.find('.actions > .ok.button');
  142. const deleteUrl = this.getAttribute('data-url');
  143. $deleteColumnButton.on('click', async (e) => {
  144. e.preventDefault();
  145. try {
  146. await DELETE(deleteUrl);
  147. } catch (error) {
  148. console.error(error);
  149. } finally {
  150. window.location.reload();
  151. }
  152. });
  153. });
  154. $('#new_project_column_submit').on('click', (e) => {
  155. e.preventDefault();
  156. const $columnTitle = $('#new_project_column');
  157. const $projectColorInput = $('#new_project_column_color_picker');
  158. if (!$columnTitle.val()) {
  159. return;
  160. }
  161. const url = e.target.getAttribute('data-url');
  162. createNewColumn(url, $columnTitle, $projectColorInput);
  163. });
  164. }
  165. function setLabelColor(label, color) {
  166. const {r, g, b} = tinycolor(color).toRgb();
  167. if (useLightTextOnBackground(r, g, b)) {
  168. label.removeClass('dark-label').addClass('light-label');
  169. } else {
  170. label.removeClass('light-label').addClass('dark-label');
  171. }
  172. }
  173. function rgbToHex(rgb) {
  174. rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+).*\)$/);
  175. return `#${hex(rgb[1])}${hex(rgb[2])}${hex(rgb[3])}`;
  176. }
  177. function hex(x) {
  178. const hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
  179. return Number.isNaN(x) ? '00' : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
  180. }