labels.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 labelColors.map((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 card = Cards.findOne(Session.get('currentCard'));
  42. const labelId = this._id;
  43. card.toggleLabel(labelId);
  44. evt.preventDefault();
  45. },
  46. 'click .js-edit-label': Popup.open('editLabel'),
  47. 'click .js-add-label': Popup.open('createLabel'),
  48. });
  49. Template.formLabel.events({
  50. 'click .js-palette-color'(evt) {
  51. const $this = $(evt.currentTarget);
  52. // hide selected ll colors
  53. $('.js-palette-select').addClass('hide');
  54. // show select color
  55. $this.find('.js-palette-select').removeClass('hide');
  56. },
  57. });
  58. Template.createLabelPopup.events({
  59. // Create the new label
  60. 'submit .create-label'(evt, tpl) {
  61. evt.preventDefault();
  62. const board = Boards.findOne(Session.get('currentBoard'));
  63. const name = tpl.$('#labelName').val().trim();
  64. const color = Blaze.getData(tpl.find('.fa-check')).color;
  65. board.addLabel(name, color);
  66. Popup.back();
  67. },
  68. });
  69. Template.editLabelPopup.events({
  70. 'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
  71. const board = Boards.findOne(Session.get('currentBoard'));
  72. board.removeLabel(this._id);
  73. Popup.back(2);
  74. }),
  75. 'submit .edit-label'(evt, tpl) {
  76. evt.preventDefault();
  77. const board = Boards.findOne(Session.get('currentBoard'));
  78. const name = tpl.$('#labelName').val().trim();
  79. const color = Blaze.getData(tpl.find('.fa-check')).color;
  80. board.editLabel(this._id, name, color);
  81. Popup.back();
  82. },
  83. });
  84. Template.cardLabelsPopup.helpers({
  85. isLabelSelected(cardId) {
  86. return _.contains(Cards.findOne(cardId).labelIds, this._id);
  87. },
  88. });