Skip to content

Commit

Permalink
feat: add (optional) prepend argument (#107)
Browse files Browse the repository at this point in the history
* Bugfix [onSpringUpdate used Card.transform instead of config.transform]

* Optionally prepend cards instead of appending. Possible fix for #4

* Syntax consistency + fixed comments

* updating param for stack.createCard function

* adding @param for prepend to card constructor
  • Loading branch information
Sparkomatic authored and gajus committed Apr 18, 2017
1 parent 9e9010e commit 4d7d6a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const card = stack.createCard(HTMLElement);

| Name | Description |
| --- | --- |
| `stack.createCard(element)` | Creates an instance of Card and associates it with the element. |
| `stack.createCard(element, prepend)` | Creates an instance of Card and associates it with the element. If prepend is true, the card is prepended to the stack, instead of appended [default: false]. |
| `stack.getCard(element)` | Returns card associated with an element. |
| `stack.on(event, listener)` | Attaches an [event listener](#events). |
| `card.on(event, listener)` | Attaches an [event listener](#events). |
Expand Down
28 changes: 25 additions & 3 deletions src/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const computeDirection = (fromX, fromY, allowedDirections) => {
/**
* @param {Stack} stack
* @param {HTMLElement} targetElement
* @param {boolean} prepend
* @returns {Object} An instance of Card.
*/
const Card = (stack, targetElement) => {
const Card = (stack, targetElement, prepend) => {
let card;
let config;
let currentX;
Expand Down Expand Up @@ -97,7 +98,11 @@ const Card = (stack, targetElement) => {
]
});

Card.appendToParent(targetElement);
if (prepend) {
Card.prependToParent(targetElement);
} else {
Card.appendToParent(targetElement);
}

eventEmitter.on('panstart', () => {
Card.appendToParent(targetElement);
Expand Down Expand Up @@ -296,7 +301,7 @@ const Card = (stack, targetElement) => {
lastTranslate.coordinateX = coordinateX || 0;
lastTranslate.coordinateY = coordinateY || 0;

Card.transform(targetElement, coordinateX, coordinateY, rotation);
config.transform(targetElement, coordinateX, coordinateY, rotation);
};

/**
Expand Down Expand Up @@ -452,6 +457,23 @@ Card.appendToParent = (element) => {
}
};

/**
* Prepend element to the parentNode.
*
* This makes the element last among the siblings.
*
* Invoked when card is added to the stack (when prepend is true).
*
* @param {HTMLElement} element The target element.
* @return {undefined}
*/
Card.prependToParent = (element) => {
const parentNode = element.parentNode;

parentNode.removeChild(element);
parentNode.insertBefore(element, parentNode.firstChild);
};

/**
* Returns a value between 0 and 1 indicating the completeness of the throw out condition.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ const Stack = (config) => {
* Creates an instance of Card and associates it with an element.
*
* @param {HTMLElement} element
* @param {boolean} prepend
* @returns {Card}
*/
stack.createCard = (element) => {
const card = Card(stack, element);
stack.createCard = (element, prepend) => {
const card = Card(stack, element, prepend);
const events = [
'throwout',
'throwoutend',
Expand Down

0 comments on commit 4d7d6a5

Please sign in to comment.