2017-09-12 10:38:03 +02:00
|
|
|
function initSorting(items) {
|
|
|
|
items.sortable({
|
|
|
|
tolerance: 'pointer',
|
|
|
|
helper: 'clone',
|
|
|
|
items: '.js-checklist-item:not(.placeholder)',
|
|
|
|
axis: 'y',
|
|
|
|
distance: 7,
|
|
|
|
placeholder: 'placeholder',
|
|
|
|
scroll: false,
|
|
|
|
start(evt, ui) {
|
|
|
|
ui.placeholder.height(ui.helper.height());
|
|
|
|
EscapeActions.executeUpTo('popup-close');
|
|
|
|
},
|
|
|
|
stop(evt, ui) {
|
|
|
|
const parent = ui.item.parents('.js-checklist-items');
|
|
|
|
const orderedItems = [];
|
|
|
|
parent.find('.js-checklist-item').each(function(i, item) {
|
|
|
|
const checklistItem = Blaze.getData(item).item;
|
|
|
|
orderedItems.push(checklistItem._id);
|
|
|
|
});
|
|
|
|
items.sortable('cancel');
|
|
|
|
const formerParent = ui.item.parents('.js-checklist-items');
|
2017-09-24 02:15:33 +01:00
|
|
|
const checklist = Blaze.getData(parent.get(0)).checklist;
|
2017-09-12 10:38:03 +02:00
|
|
|
const oldChecklist = Blaze.getData(formerParent.get(0)).checklist;
|
|
|
|
if (oldChecklist._id !== checklist._id) {
|
|
|
|
const currentItem = Blaze.getData(ui.item.get(0)).item;
|
|
|
|
for (let i = 0; i < orderedItems.length; i++) {
|
2017-09-24 02:15:33 +01:00
|
|
|
const itemId = orderedItems[i];
|
2017-09-12 10:38:03 +02:00
|
|
|
if (itemId !== currentItem._id) continue;
|
2017-09-24 02:15:33 +01:00
|
|
|
const newItem = {
|
|
|
|
_id: checklist.getNewItemId(),
|
|
|
|
title: currentItem.title,
|
|
|
|
sort: i,
|
|
|
|
isFinished: currentItem.isFinished,
|
|
|
|
};
|
|
|
|
checklist.addFullItem(newItem);
|
|
|
|
orderedItems[i] = currentItem._id;
|
|
|
|
oldChecklist.removeItem(itemId);
|
2017-09-12 10:38:03 +02:00
|
|
|
}
|
2017-09-24 02:15:33 +01:00
|
|
|
} else {
|
|
|
|
checklist.sortItems(orderedItems);
|
2017-09-12 10:38:03 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Template.checklists.onRendered(function () {
|
|
|
|
const self = BlazeComponent.getComponentForElement(this.firstNode);
|
|
|
|
self.itemsDom = this.$('.card-checklist-items');
|
|
|
|
initSorting(self.itemsDom);
|
|
|
|
self.itemsDom.mousedown(function(evt) {
|
|
|
|
evt.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
function userIsMember() {
|
|
|
|
return Meteor.user() && Meteor.user().isBoardMember();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable sorting if the current user is not a board member
|
|
|
|
self.autorun(() => {
|
|
|
|
const $itemsDom = $(self.itemsDom);
|
|
|
|
if ($itemsDom.data('sortable')) {
|
|
|
|
$(self.itemsDom).sortable('option', 'disabled', !userIsMember());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-20 21:05:48 +08:00
|
|
|
BlazeComponent.extendComponent({
|
2017-09-27 14:29:52 +02:00
|
|
|
|
2017-01-20 21:05:48 +08:00
|
|
|
addChecklist(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const textarea = this.find('textarea.js-add-checklist-item');
|
|
|
|
const title = textarea.value.trim();
|
|
|
|
const cardId = this.currentData().cardId;
|
2017-09-12 10:38:03 +02:00
|
|
|
const card = Cards.findOne(cardId);
|
2017-07-01 23:03:54 +09:00
|
|
|
|
|
|
|
if (title) {
|
|
|
|
Checklists.insert({
|
|
|
|
cardId,
|
|
|
|
title,
|
2017-09-12 10:38:03 +02:00
|
|
|
sort: card.checklists().count(),
|
2017-07-01 23:03:54 +09:00
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
this.$('.add-checklist-item').last().click();
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
textarea.value = '';
|
|
|
|
textarea.focus();
|
2017-01-20 21:05:48 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addChecklistItem(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const textarea = this.find('textarea.js-add-checklist-item');
|
|
|
|
const title = textarea.value.trim();
|
|
|
|
const checklist = this.currentData().checklist;
|
2017-06-30 10:10:42 +09:00
|
|
|
|
2017-07-01 23:03:54 +09:00
|
|
|
if (title) {
|
|
|
|
checklist.addItem(title);
|
|
|
|
}
|
2017-06-30 10:10:42 +09:00
|
|
|
// We keep the form opened, empty it.
|
|
|
|
textarea.value = '';
|
|
|
|
textarea.focus();
|
2017-01-20 21:05:48 +08:00
|
|
|
},
|
|
|
|
|
2017-09-27 14:29:52 +02:00
|
|
|
canModifyCard() {
|
|
|
|
return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteChecklist() {
|
|
|
|
const checklist = this.currentData().checklist;
|
|
|
|
if (checklist && checklist._id) {
|
|
|
|
Checklists.remove(checklist._id);
|
|
|
|
this.toggleDeleteDialog.set(false);
|
|
|
|
}
|
|
|
|
},
|
2017-09-29 14:23:54 +02:00
|
|
|
|
2017-09-27 14:29:52 +02:00
|
|
|
deleteItem() {
|
|
|
|
const checklist = this.currentData().checklist;
|
|
|
|
const item = this.currentData().item;
|
|
|
|
if (checklist && item && item._id) {
|
|
|
|
checklist.removeItem(item._id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-01-20 21:05:48 +08:00
|
|
|
editChecklist(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const textarea = this.find('textarea.js-edit-checklist-item');
|
|
|
|
const title = textarea.value.trim();
|
|
|
|
const checklist = this.currentData().checklist;
|
|
|
|
checklist.setTitle(title);
|
|
|
|
},
|
|
|
|
|
|
|
|
editChecklistItem(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const textarea = this.find('textarea.js-edit-checklist-item');
|
|
|
|
const title = textarea.value.trim();
|
|
|
|
const itemId = this.currentData().item._id;
|
|
|
|
const checklist = this.currentData().checklist;
|
|
|
|
checklist.editItem(itemId, title);
|
|
|
|
},
|
|
|
|
|
2017-09-27 14:29:52 +02:00
|
|
|
onCreated() {
|
|
|
|
this.toggleDeleteDialog = new ReactiveVar(false);
|
|
|
|
this.checklistToDelete = null; //Store data context to pass to checklistDeleteDialog template
|
2017-01-20 21:05:48 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
pressKey(event) {
|
|
|
|
//If user press enter key inside a form, submit it, so user doesn't have to leave keyboard to submit a form.
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
const $form = $(event.currentTarget).closest('form');
|
|
|
|
$form.find('button[type=submit]').click();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
events() {
|
2017-09-27 14:29:52 +02:00
|
|
|
const events = {
|
|
|
|
'click .toggle-delete-checklist-dialog'(event) {
|
|
|
|
if($(event.target).hasClass('js-delete-checklist')){
|
|
|
|
this.checklistToDelete = this.currentData().checklist; //Store data context
|
|
|
|
}
|
|
|
|
this.toggleDeleteDialog.set(!this.toggleDeleteDialog.get());
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-01-20 21:05:48 +08:00
|
|
|
return [{
|
2017-09-27 14:29:52 +02:00
|
|
|
...events,
|
2017-01-20 21:05:48 +08:00
|
|
|
'submit .js-add-checklist': this.addChecklist,
|
|
|
|
'submit .js-edit-checklist-title': this.editChecklist,
|
|
|
|
'submit .js-add-checklist-item': this.addChecklistItem,
|
|
|
|
'submit .js-edit-checklist-item': this.editChecklistItem,
|
|
|
|
'click .js-delete-checklist-item': this.deleteItem,
|
2017-09-27 14:29:52 +02:00
|
|
|
'click .confirm-checklist-delete': this.deleteChecklist,
|
2017-01-20 21:05:48 +08:00
|
|
|
keydown: this.pressKey,
|
|
|
|
}];
|
|
|
|
},
|
|
|
|
}).register('checklists');
|
2017-02-03 01:49:58 +02:00
|
|
|
|
2017-09-27 14:29:52 +02:00
|
|
|
Template.checklistDeleteDialog.onCreated(() => {
|
|
|
|
const $cardDetails = this.$('.card-details');
|
|
|
|
this.scrollState = { position: $cardDetails.scrollTop(), //save current scroll position
|
2017-09-27 15:42:48 +02:00
|
|
|
top: false, //required for smooth scroll animation
|
2017-09-27 14:29:52 +02:00
|
|
|
};
|
|
|
|
//Callback's purpose is to only prevent scrolling after animation is complete
|
|
|
|
$cardDetails.animate({ scrollTop: 0 }, 500, () => { this.scrollState.top = true; });
|
|
|
|
|
|
|
|
//Prevent scrolling while dialog is open
|
|
|
|
$cardDetails.on('scroll', () => {
|
|
|
|
if(this.scrollState.top) { //If it's already in position, keep it there. Otherwise let animation scroll
|
|
|
|
$cardDetails.scrollTop(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Template.checklistDeleteDialog.onDestroyed(() => {
|
|
|
|
const $cardDetails = this.$('.card-details');
|
|
|
|
$cardDetails.off('scroll'); //Reactivate scrolling
|
|
|
|
$cardDetails.animate( { scrollTop: this.scrollState.position });
|
|
|
|
});
|
|
|
|
|
2017-03-18 18:49:39 -04:00
|
|
|
Template.itemDetail.helpers({
|
|
|
|
canModifyCard() {
|
|
|
|
return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-02-03 01:49:58 +02:00
|
|
|
BlazeComponent.extendComponent({
|
|
|
|
toggleItem() {
|
|
|
|
const checklist = this.currentData().checklist;
|
|
|
|
const item = this.currentData().item;
|
|
|
|
if (checklist && item && item._id) {
|
|
|
|
checklist.toggleItem(item._id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
events() {
|
|
|
|
return [{
|
|
|
|
'click .item .check-box': this.toggleItem,
|
|
|
|
}];
|
|
|
|
},
|
|
|
|
}).register('itemDetail');
|