labels.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // Disable drag-dropping if the current user is not a board member or is comment only
  70. this.autorun(() => {
  71. if (Utils.isMiniScreenOrShowDesktopDragHandles()) {
  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 = Boards.findOne(Session.get('currentBoard'));
  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 = Boards.findOne(Session.get('currentBoard'));
  121. board.removeLabel(this._id);
  122. Popup.back(2);
  123. }),
  124. 'submit .edit-label'(event, templateInstance) {
  125. event.preventDefault();
  126. const board = Boards.findOne(Session.get('currentBoard'));
  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(Cards.findOne(cardId).labelIds, this._id);
  139. },
  140. });