labels.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. let labelColors;
  2. Meteor.startup(() => {
  3. labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  4. });
  5. BlazeComponent.extendComponent({
  6. onCreated() {
  7. this.currentColor = new ReactiveVar(this.data().color);
  8. },
  9. labels() {
  10. return labelColors.map(color => ({ color, name: '' }));
  11. },
  12. isSelected(color) {
  13. return this.currentColor.get() === color;
  14. },
  15. events() {
  16. return [
  17. {
  18. 'click .js-palette-color'() {
  19. this.currentColor.set(this.currentData().color);
  20. },
  21. },
  22. ];
  23. },
  24. }).register('formLabel');
  25. Template.createLabelPopup.helpers({
  26. // This is the default color for a new label. We search the first color that
  27. // is not already used in the board (although it's not a problem if two
  28. // labels have the same color).
  29. defaultColor() {
  30. const labels = Boards.findOne(Session.get('currentBoard')).labels;
  31. const usedColors = _.pluck(labels, 'color');
  32. const availableColors = _.difference(labelColors, usedColors);
  33. return availableColors.length > 1 ? availableColors[0] : labelColors[0];
  34. },
  35. });
  36. BlazeComponent.extendComponent({
  37. onRendered() {
  38. const itemsSelector = 'li.js-card-label-item:not(.placeholder)';
  39. const $labels = this.$('.edit-labels-pop-over');
  40. $labels.sortable({
  41. connectWith: '.edit-labels-pop-over',
  42. tolerance: 'pointer',
  43. appendTo: '.edit-labels-pop-over',
  44. helper(element, currentItem) {
  45. let ret = currentItem.clone();
  46. if (currentItem.closest('.popup-container-depth-0').size() == 0)
  47. { // 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. Utils.enableClickOnTouch(itemsSelector);
  70. // Disable drag-dropping if the current user is not a board member or is comment only
  71. this.autorun(() => {
  72. if (Utils.isMiniScreenOrShowDesktopDragHandles()) {
  73. $labels.sortable({
  74. handle: '.label-handle',
  75. });
  76. }
  77. });
  78. },
  79. events() {
  80. return [
  81. {
  82. 'click .js-select-label'(event) {
  83. const card = this.data();
  84. const labelId = this.currentData()._id;
  85. card.toggleLabel(labelId);
  86. event.preventDefault();
  87. },
  88. 'click .js-edit-label': Popup.open('editLabel'),
  89. 'click .js-add-label': Popup.open('createLabel'),
  90. }
  91. ];
  92. }
  93. }).register('cardLabelsPopup');
  94. Template.cardLabelsPopup.events({
  95. });
  96. Template.formLabel.events({
  97. 'click .js-palette-color'(event) {
  98. const $this = $(event.currentTarget);
  99. // hide selected ll colors
  100. $('.js-palette-select').addClass('hide');
  101. // show select color
  102. $this.find('.js-palette-select').removeClass('hide');
  103. },
  104. });
  105. Template.createLabelPopup.events({
  106. // Create the new label
  107. 'submit .create-label'(event, templateInstance) {
  108. event.preventDefault();
  109. const board = Boards.findOne(Session.get('currentBoard'));
  110. const name = templateInstance
  111. .$('#labelName')
  112. .val()
  113. .trim();
  114. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  115. board.addLabel(name, color);
  116. Popup.back();
  117. },
  118. });
  119. Template.editLabelPopup.events({
  120. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
  121. const board = Boards.findOne(Session.get('currentBoard'));
  122. board.removeLabel(this._id);
  123. Popup.back(2);
  124. }),
  125. 'submit .edit-label'(event, templateInstance) {
  126. event.preventDefault();
  127. const board = Boards.findOne(Session.get('currentBoard'));
  128. const name = templateInstance
  129. .$('#labelName')
  130. .val()
  131. .trim();
  132. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  133. board.editLabel(this._id, name, color);
  134. Popup.back();
  135. },
  136. });
  137. Template.cardLabelsPopup.helpers({
  138. isLabelSelected(cardId) {
  139. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  140. },
  141. });