Skip to content

Commit 4d7d6a5

Browse files
Sparkomaticgajus
authored andcommitted
feat: add (optional) prepend argument (#107)
* 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
1 parent 9e9010e commit 4d7d6a5

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const card = stack.createCard(HTMLElement);
148148

149149
| Name | Description |
150150
| --- | --- |
151-
| `stack.createCard(element)` | Creates an instance of Card and associates it with the element. |
151+
| `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]. |
152152
| `stack.getCard(element)` | Returns card associated with an element. |
153153
| `stack.on(event, listener)` | Attaches an [event listener](#events). |
154154
| `card.on(event, listener)` | Attaches an [event listener](#events). |

src/Card.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ const computeDirection = (fromX, fromY, allowedDirections) => {
3434
/**
3535
* @param {Stack} stack
3636
* @param {HTMLElement} targetElement
37+
* @param {boolean} prepend
3738
* @returns {Object} An instance of Card.
3839
*/
39-
const Card = (stack, targetElement) => {
40+
const Card = (stack, targetElement, prepend) => {
4041
let card;
4142
let config;
4243
let currentX;
@@ -97,7 +98,11 @@ const Card = (stack, targetElement) => {
9798
]
9899
});
99100

100-
Card.appendToParent(targetElement);
101+
if (prepend) {
102+
Card.prependToParent(targetElement);
103+
} else {
104+
Card.appendToParent(targetElement);
105+
}
101106

102107
eventEmitter.on('panstart', () => {
103108
Card.appendToParent(targetElement);
@@ -296,7 +301,7 @@ const Card = (stack, targetElement) => {
296301
lastTranslate.coordinateX = coordinateX || 0;
297302
lastTranslate.coordinateY = coordinateY || 0;
298303

299-
Card.transform(targetElement, coordinateX, coordinateY, rotation);
304+
config.transform(targetElement, coordinateX, coordinateY, rotation);
300305
};
301306

302307
/**
@@ -452,6 +457,23 @@ Card.appendToParent = (element) => {
452457
}
453458
};
454459

460+
/**
461+
* Prepend element to the parentNode.
462+
*
463+
* This makes the element last among the siblings.
464+
*
465+
* Invoked when card is added to the stack (when prepend is true).
466+
*
467+
* @param {HTMLElement} element The target element.
468+
* @return {undefined}
469+
*/
470+
Card.prependToParent = (element) => {
471+
const parentNode = element.parentNode;
472+
473+
parentNode.removeChild(element);
474+
parentNode.insertBefore(element, parentNode.firstChild);
475+
};
476+
455477
/**
456478
* Returns a value between 0 and 1 indicating the completeness of the throw out condition.
457479
*

src/Stack.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ const Stack = (config) => {
5555
* Creates an instance of Card and associates it with an element.
5656
*
5757
* @param {HTMLElement} element
58+
* @param {boolean} prepend
5859
* @returns {Card}
5960
*/
60-
stack.createCard = (element) => {
61-
const card = Card(stack, element);
61+
stack.createCard = (element, prepend) => {
62+
const card = Card(stack, element, prepend);
6263
const events = [
6364
'throwout',
6465
'throwoutend',

0 commit comments

Comments
 (0)