Skip to content

Commit

Permalink
feat: traversal next prev (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Osvaldas Valutis authored Nov 22, 2021
1 parent e7f2992 commit ea000b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 25 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,31 @@ const getInsertionNodeElem = function(insertionNode, insertionTraversal, btn) {
return insertionNode(btn);
}

if(insertionTraversal) {
// TODO:
// https://github.com/nathanvda/cocoon/blob/master/app/assets/javascripts/cocoon.js#L32
// data-association-insertion-traversal: the jquery traversal method to
// allow node selection relative to the link. closest, next, children, etc.
// return $this[insertionTraversal](insertionNode);
return null;
}
else {
return document.querySelector(insertionNode);
if(typeof insertionNode == 'string') {
if (insertionTraversal) {
// @TODO
// - prevUntil
// - nextUntil

const prevNext = {
prev: 'previousElementSibling',
next: 'nextElementSibling',
}[insertionTraversal]

if (prevNext) {
const el = btn[prevNext].closest(insertionNode)
if (el === btn[prevNext]) return el
}
else if (insertionTraversal == 'closest') {
return btn.closest(insertionNode)
}
else {
console.warn('The provided association-insertion-traversal is not supported');
}
}
else {
return insertionNode == 'this' ? btn : document.querySelector(insertionNode);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oddcamp/cocoon-vanilla-js",
"version": "1.1.2",
"version": "1.1.3",
"description": "A vanilla JS replacement for (Rails) Cocoon's jQuery script",
"main": "index.js",
"repository": "git+https://github.com/oddcamp/cocoon-vanilla-js.git",
Expand Down

0 comments on commit ea000b6

Please sign in to comment.