labels.js 3.3 KB

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