labels.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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) { // only set css transform at every sub-popup, not at the main popup
  47. const content = currentItem.closest('.content')[0]
  48. const offsetLeft = content.offsetLeft;
  49. const offsetTop = $('.pop-over > .header').height() * -1;
  50. ret.css("transform", `translate(${offsetLeft}px, ${offsetTop}px)`);
  51. }
  52. return ret;
  53. },
  54. distance: 7,
  55. items: itemsSelector,
  56. placeholder: 'card-label-wrapper placeholder',
  57. start(evt, ui) {
  58. ui.helper.css('z-index', 1000);
  59. ui.placeholder.height(ui.helper.height());
  60. EscapeActions.clickExecute(evt.target, 'inlinedForm');
  61. },
  62. stop(evt, ui) {
  63. const newLabelOrderOnlyIds = ui.item.parent().children().toArray().map(_element => Blaze.getData(_element)._id)
  64. const card = Blaze.getData(this);
  65. card.board().setNewLabelOrder(newLabelOrderOnlyIds);
  66. },
  67. });
  68. // Disable drag-dropping if the current user is not a board member or is comment only
  69. this.autorun(() => {
  70. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  71. $labels.sortable({
  72. handle: '.label-handle',
  73. });
  74. }
  75. });
  76. },
  77. events() {
  78. return [
  79. {
  80. 'click .js-select-label'(event) {
  81. const card = this.data();
  82. const labelId = this.currentData()._id;
  83. card.toggleLabel(labelId);
  84. event.preventDefault();
  85. },
  86. 'click .js-edit-label': Popup.open('editLabel'),
  87. 'click .js-add-label': Popup.open('createLabel'),
  88. }
  89. ];
  90. }
  91. }).register('cardLabelsPopup');
  92. Template.cardLabelsPopup.events({
  93. });
  94. Template.formLabel.events({
  95. 'click .js-palette-color'(event) {
  96. const $this = $(event.currentTarget);
  97. // hide selected ll colors
  98. $('.js-palette-select').addClass('hide');
  99. // show select color
  100. $this.find('.js-palette-select').removeClass('hide');
  101. },
  102. });
  103. Template.createLabelPopup.events({
  104. // Create the new label
  105. 'submit .create-label'(event, templateInstance) {
  106. event.preventDefault();
  107. const board = Boards.findOne(Session.get('currentBoard'));
  108. const name = templateInstance
  109. .$('#labelName')
  110. .val()
  111. .trim();
  112. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  113. board.addLabel(name, color);
  114. Popup.back();
  115. },
  116. });
  117. Template.editLabelPopup.events({
  118. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function () {
  119. const board = Boards.findOne(Session.get('currentBoard'));
  120. board.removeLabel(this._id);
  121. Popup.back(2);
  122. }),
  123. 'submit .edit-label'(event, templateInstance) {
  124. event.preventDefault();
  125. const board = Boards.findOne(Session.get('currentBoard'));
  126. const name = templateInstance
  127. .$('#labelName')
  128. .val()
  129. .trim();
  130. const color = Blaze.getData(templateInstance.find('.fa-check')).color;
  131. board.editLabel(this._id, name, color);
  132. Popup.back();
  133. },
  134. });
  135. Template.cardLabelsPopup.helpers({
  136. isLabelSelected(cardId) {
  137. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  138. },
  139. });