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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. animation: 150,
  55. ghostClass: 'card-ghost',
  56. delayOnTouchOnly: true,
  57. delay: 500,
  58. onSort: async () => {
  59. boardColumns = mainBoard.getElementsByClassName('project-column');
  60. for (let i = 0; i < boardColumns.length; i++) {
  61. const column = boardColumns[i];
  62. if (parseInt($(column).data('sorting')) !== i) {
  63. try {
  64. await PUT($(column).data('url'), {
  65. data: {
  66. sorting: i,
  67. color: rgbToHex(window.getComputedStyle($(column)[0]).backgroundColor),
  68. },
  69. });
  70. } catch (error) {
  71. console.error(error);
  72. }
  73. }
  74. }
  75. },
  76. });
  77. for (const boardColumn of boardColumns) {
  78. const boardCardList = boardColumn.getElementsByClassName('cards')[0];
  79. createSortable(boardCardList, {
  80. group: 'shared',
  81. animation: 150,
  82. ghostClass: 'card-ghost',
  83. onAdd: moveIssue,
  84. onUpdate: moveIssue,
  85. delayOnTouchOnly: true,
  86. delay: 500,
  87. });
  88. }
  89. }
  90. export function initRepoProject() {
  91. if (!$('.repository.projects').length) {
  92. return;
  93. }
  94. const _promise = initRepoProjectSortable();
  95. $('.edit-project-column-modal').each(function () {
  96. const $projectHeader = $(this).closest('.project-column-header');
  97. const $projectTitleLabel = $projectHeader.find('.project-column-title');
  98. const $projectTitleInput = $(this).find('.project-column-title-input');
  99. const $projectColorInput = $(this).find('#new_project_column_color');
  100. const $boardColumn = $(this).closest('.project-column');
  101. const bgColor = $boardColumn[0].style.backgroundColor;
  102. if (bgColor) {
  103. setLabelColor($projectHeader, rgbToHex(bgColor));
  104. }
  105. $(this).find('.edit-project-column-button').on('click', async function (e) {
  106. e.preventDefault();
  107. try {
  108. await PUT($(this).data('url'), {
  109. data: {
  110. title: $projectTitleInput.val(),
  111. color: $projectColorInput.val(),
  112. },
  113. });
  114. } catch (error) {
  115. console.error(error);
  116. } finally {
  117. $projectTitleLabel.text($projectTitleInput.val());
  118. $projectTitleInput.closest('form').removeClass('dirty');
  119. if ($projectColorInput.val()) {
  120. setLabelColor($projectHeader, $projectColorInput.val());
  121. }
  122. $boardColumn[0].style = `background: ${$projectColorInput.val()} !important`;
  123. $('.ui.modal').modal('hide');
  124. }
  125. });
  126. });
  127. $('.default-project-column-modal').each(function () {
  128. const $boardColumn = $(this).closest('.project-column');
  129. const $showButton = $($boardColumn).find('.default-project-column-show');
  130. const $commitButton = $(this).find('.actions > .ok.button');
  131. $($commitButton).on('click', async (e) => {
  132. e.preventDefault();
  133. try {
  134. await POST($($showButton).data('url'));
  135. } catch (error) {
  136. console.error(error);
  137. } finally {
  138. window.location.reload();
  139. }
  140. });
  141. });
  142. $('.show-delete-project-column-modal').each(function () {
  143. const $deleteColumnModal = $(`${this.getAttribute('data-modal')}`);
  144. const $deleteColumnButton = $deleteColumnModal.find('.actions > .ok.button');
  145. const deleteUrl = this.getAttribute('data-url');
  146. $deleteColumnButton.on('click', async (e) => {
  147. e.preventDefault();
  148. try {
  149. await DELETE(deleteUrl);
  150. } catch (error) {
  151. console.error(error);
  152. } finally {
  153. window.location.reload();
  154. }
  155. });
  156. });
  157. $('#new_project_column_submit').on('click', (e) => {
  158. e.preventDefault();
  159. const $columnTitle = $('#new_project_column');
  160. const $projectColorInput = $('#new_project_column_color_picker');
  161. if (!$columnTitle.val()) {
  162. return;
  163. }
  164. const url = e.target.getAttribute('data-url');
  165. createNewColumn(url, $columnTitle, $projectColorInput);
  166. });
  167. }
  168. function setLabelColor(label, color) {
  169. const {r, g, b} = tinycolor(color).toRgb();
  170. if (useLightTextOnBackground(r, g, b)) {
  171. label.removeClass('dark-label').addClass('light-label');
  172. } else {
  173. label.removeClass('light-label').addClass('dark-label');
  174. }
  175. }
  176. function rgbToHex(rgb) {
  177. rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+).*\)$/);
  178. return `#${hex(rgb[1])}${hex(rgb[2])}${hex(rgb[3])}`;
  179. }
  180. function hex(x) {
  181. const hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
  182. return Number.isNaN(x) ? '00' : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
  183. }