wekan/client/lib/utils.js
Maxime Quandalle 45b662a1dd Centralize all mutations at the model level
This commit uses a new package that I need to document. It tries to
solve the long-standing debate in the Meteor community about
allow/deny rules versus methods (RPC).

This approach gives us both the centralized security rules of
allow/deny and the white-list of allowed mutations similarly to Meteor
methods. The idea to have static mutation descriptions is also
inspired by Facebook's Relay/GraphQL.

This will allow the development of a REST API using the high-level
methods instead of the MongoDB queries to do the mapping between the
HTTP requests and our collections.
2015-09-08 20:19:42 +02:00

56 lines
1.6 KiB
JavaScript

Utils = {
// XXX We should remove these two methods
goBoardId(_id) {
const board = Boards.findOne(_id);
return board && FlowRouter.go('board', {
id: board._id,
slug: board.slug,
});
},
goCardId(_id) {
const card = Cards.findOne(_id);
const board = Boards.findOne(card.boardId);
return board && FlowRouter.go('card', {
cardId: card._id,
boardId: board._id,
slug: board.slug,
});
},
capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
// Determine the new sort index
calculateIndex(prevCardDomElement, nextCardDomElement, nCards = 1) {
let base, increment;
// If we drop the card to an empty column
if (!prevCardDomElement && !nextCardDomElement) {
base = 0;
increment = 1;
// If we drop the card in the first position
} else if (!prevCardDomElement) {
base = Blaze.getData(nextCardDomElement).sort - 1;
increment = -1;
// If we drop the card in the last position
} else if (!nextCardDomElement) {
base = Blaze.getData(prevCardDomElement).sort + 1;
increment = 1;
}
// In the general case take the average of the previous and next element
// sort indexes.
else {
const prevSortIndex = Blaze.getData(prevCardDomElement).sort;
const nextSortIndex = Blaze.getData(nextCardDomElement).sort;
increment = (nextSortIndex - prevSortIndex) / (nCards + 1);
base = prevSortIndex + increment;
}
// XXX Return a generator that yield values instead of a base with a
// increment number.
return {
base,
increment,
};
},
};