labels.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. let labelColors;
  2. Meteor.startup(() => {
  3. labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  4. });
  5. BlazeComponent.extendComponent({
  6. template() {
  7. return 'formLabel';
  8. },
  9. onCreated() {
  10. this.currentColor = new ReactiveVar(this.data().color);
  11. },
  12. labels() {
  13. return _.map(labelColors, (color) => {
  14. return { color, name: '' };
  15. });
  16. },
  17. isSelected(color) {
  18. return this.currentColor.get() === color;
  19. },
  20. events() {
  21. return [{
  22. 'click .js-palette-color'() {
  23. this.currentColor.set(this.currentData().color);
  24. },
  25. }];
  26. },
  27. }).register('formLabel');
  28. Template.createLabelPopup.helpers({
  29. // This is the default color for a new label. We search the first color that
  30. // is not already used in the board (although it's not a problem if two
  31. // labels have the same color).
  32. defaultColor() {
  33. const labels = Boards.findOne(Session.get('currentBoard')).labels;
  34. const usedColors = _.pluck(labels, 'color');
  35. const availableColors = _.difference(labelColors, usedColors);
  36. return availableColors.length > 1 ? availableColors[0] : labelColors[0];
  37. },
  38. });
  39. Template.cardLabelsPopup.events({
  40. 'click .js-select-label'(evt) {
  41. const cardId = Template.parentData(2).data._id;
  42. const labelId = this._id;
  43. let operation;
  44. if (Cards.find({ _id: cardId, labelIds: labelId}).count() === 0)
  45. operation = '$addToSet';
  46. else
  47. operation = '$pull';
  48. Cards.update(cardId, {
  49. [operation]: {
  50. labelIds: labelId,
  51. },
  52. });
  53. evt.preventDefault();
  54. },
  55. 'click .js-edit-label': Popup.open('editLabel'),
  56. 'click .js-add-label': Popup.open('createLabel'),
  57. });
  58. Template.formLabel.events({
  59. 'click .js-palette-color'(evt) {
  60. const $this = $(evt.currentTarget);
  61. // hide selected ll colors
  62. $('.js-palette-select').addClass('hide');
  63. // show select color
  64. $this.find('.js-palette-select').removeClass('hide');
  65. },
  66. });
  67. Template.createLabelPopup.events({
  68. // Create the new label
  69. 'submit .create-label'(evt, tpl) {
  70. const name = tpl.$('#labelName').val().trim();
  71. const boardId = Session.get('currentBoard');
  72. const color = Blaze.getData(tpl.find('.fa-check')).color;
  73. Boards.update(boardId, {
  74. $push: {
  75. labels: {
  76. name,
  77. color,
  78. _id: Random.id(6),
  79. },
  80. },
  81. });
  82. Popup.back();
  83. evt.preventDefault();
  84. },
  85. });
  86. Template.editLabelPopup.events({
  87. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
  88. const boardId = Session.get('currentBoard');
  89. Boards.update(boardId, {
  90. $pull: {
  91. labels: {
  92. _id: this._id,
  93. },
  94. },
  95. });
  96. Popup.back(2);
  97. }),
  98. 'submit .edit-label'(evt, tpl) {
  99. evt.preventDefault();
  100. const name = tpl.$('#labelName').val().trim();
  101. const boardId = Session.get('currentBoard');
  102. const getLabel = Utils.getLabelIndex(boardId, this._id);
  103. const color = Blaze.getData(tpl.find('.fa-check')).color;
  104. Boards.update(boardId, {
  105. $set: {
  106. [getLabel.key('name')]: name,
  107. [getLabel.key('color')]: color,
  108. },
  109. });
  110. Popup.back();
  111. },
  112. });
  113. Template.cardLabelsPopup.helpers({
  114. isLabelSelected(cardId) {
  115. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  116. },
  117. });