Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

repo-projects.js 6.2KB

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