labels.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. let labelColors;
  3. Meteor.startup(() => {
  4. labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  5. });
  6. BlazeComponent.extendComponent({
  7. onCreated() {
  8. this.currentColor = new ReactiveVar(this.data().color);
  9. },
  10. labels() {
  11. return labelColors.map(color => ({ color, name: '' }));
  12. },
  13. isSelected(color) {
  14. return this.currentColor.get() === color;
  15. },
  16. events() {
  17. return [
  18. {
  19. 'click .js-palette-color'() {
  20. this.currentColor.set(this.currentData().color);
  21. },
  22. },
  23. ];
  24. },
  25. }).register('formLabel');
  26. Template.createLabelPopup.helpers({
  27. // This is the default color for a new label. We search the first color that
  28. // is not already used in the board (although it's not a problem if two
  29. // labels have the same color).
  30. defaultColor() {
  31. const labels = Utils.getCurrentBoard().labels;
  32. const usedColors = _.pluck(labels, 'color');
  33. const availableColors = _.difference(labelColors, usedColors);
  34. return availableColors.length > 1 ? availableColors[0] : labelColors[0];
  35. },
  36. });
  37. BlazeComponent.extendComponent({
  38. onRendered() {
  39. const itemsSelector = 'li.js-card-label-item:not(.placeholder)';
  40. const $labels = this.$('.edit-labels-pop-over');
  41. $labels.sortable({
  42. connectWith: '.edit-labels-pop-over',
  43. tolerance: 'pointer',
  44. appendTo: '.edit-labels-pop-over',
  45. helper(element, currentItem) {
  46. let ret = currentItem.clone();
  47. if (currentItem.closest('.popup-container-depth-0').size() == 0) { // only set css transform at every sub-popup, not at the main popup
  48. const content = currentItem.closest('.content')[0]
  49. const offsetLeft = content.offsetLeft;
  50. const offsetTop = $('.pop-over > .header').height() * -1;
  51. ret.css("transform", `translate(${offsetLeft}px, ${offsetTop}px)`);
  52. }
  53. return ret;
  54. },
  55. distance: 7,
  56. items: itemsSelector,
  57. placeholder: 'card-label-wrapper placeholder',
  58. start(evt, ui) {
  59. ui.helper.css('z-index', 1000);
  60. ui.placeholder.height(ui.helper.height());
  61. EscapeActions.clickExecute(evt.target, 'inlinedForm');
  62. },
  63. stop(evt, ui) {
  64. const newLabelOrderOnlyIds = ui.item.parent().children().toArray().map(_element => Blaze.getData(_element)._id)
  65. const card = Blaze.getData(this);
  66. card.board().setNewLabelOrder(newLabelOrderOnlyIds);
  67. },
  68. });
  69. // Disable drag-dropping if the current user is not a board member or is comment only
  70. this.autorun(() => {
  71. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  72. $labels.sortable({
  73. handle: '.label-handle',
  74. });
  75. }
  76. });
  77. },
  78. events() {
  79. return [
  80. {
  81. 'click .js-select-label'(event) {
  82. const card = this.data();
  83. const labelId = this.currentData()._id;
  84. card.toggleLabel(labelId);
  85. event.preventDefault();
  86. },
  87. 'click .js-edit-label': Popup.open('editLabel'),
  88. 'click .js-add-label': Popup.open('createLabel'),
  89. }
  90. ];
  91. }
  92. }).register('cardLabelsPopup');
  93. Template.cardLabelsPopup.events({
  94. });
  95. Template.formLabel.events({
  96. 'click .js-palette-color'(event) {
  97. const $this = $(event.currentTarget);
  98. // hide selected ll colors
  99. $('.js-palette-select').addClass('hide');
  100. // show select color
  101. $this.find('.js-palette-select').removeClass('hide');
  102. },
  103. });
  104. Template.createLabelPopup.events({
  105. // Create the new label
  106. 'submit .create-label'(event, templateInstance) {
  107. event.preventDefault();
  108. const board = Utils.getCurrentBoard();
  109. const name = templateInstance
  110. .$('#labelName')
  111. .val()
  112. .trim();
  113. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  114. board.addLabel(name, color);
  115. Popup.back();
  116. },
  117. });
  118. Template.editLabelPopup.events({
  119. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function () {
  120. const board = Utils.getCurrentBoard();
  121. board.removeLabel(this._id);
  122. Popup.back(2);
  123. }),
  124. 'submit .edit-label'(event, templateInstance) {
  125. event.preventDefault();
  126. const board = Utils.getCurrentBoard();
  127. const name = templateInstance
  128. .$('#labelName')
  129. .val()
  130. .trim();
  131. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  132. board.editLabel(this._id, name, color);
  133. Popup.back();
  134. },
  135. });
  136. Template.cardLabelsPopup.helpers({
  137. isLabelSelected(cardId) {
  138. return _.contains(ReactiveCache.getCard(cardId).labelIds, this._id);
  139. },
  140. });