labels.js 2.7 KB

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