ez-shp-storefront
Version:
A helper function collection for Shopify storefront.
37 lines (36 loc) • 1.02 kB
JavaScript
export var Positions;
(function (Positions) {
Positions["TOP"] = "top";
Positions["BOTTOM"] = "bottom";
Positions["BEFORE"] = "before";
Positions["AFTER"] = "after";
})(Positions || (Positions = {}));
export function moveByElements(fromEl, toEl, type) {
switch (type) {
case Positions.TOP:
toEl.prepend(fromEl);
break;
case Positions.BOTTOM:
toEl.append(fromEl);
break;
case Positions.BEFORE:
toEl.parentNode.insertBefore(fromEl, toEl);
break;
case Positions.AFTER:
toEl.parentNode.insertBefore(fromEl, toEl.nextSibling);
break;
}
}
export function moveByElementQueries(from, to, type) {
let fromEl = document.querySelector(from);
let toEl;
try {
toEl = to
? document.querySelector(to) || document.body
: document.body;
}
catch (e) {
toEl = document.body;
}
return moveByElements(fromEl, toEl, type);
}