vevet
Version:
Vevet is a JavaScript library for creative development that simplifies crafting rich interactions like split text animations, carousels, marquees, preloading, and more.
308 lines • 10.9 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SnapSlide = void 0;
var utils_1 = require("../../../../utils");
var SlideParallax_1 = require("../SlideParallax");
var constants_1 = require("../SlideParallax/constants");
var SnapSlide = /** @class */ (function () {
function SnapSlide(_element, initProps) {
if (initProps === void 0) { initProps = {}; }
this._element = _element;
/** Current coordinate */
this._coord = 0;
/** If the slide is appended */
this._isAppended = false;
/** If the slide is visible */
this._isVisible = false;
/** Static coordinate (as if the slide was never moved) */
this._staticCoord = 0;
/** Current progress of slide movement: from -1 to 1 */
this._progress = 0;
this._index = 0;
this._parallax = [];
var defaultProps = {
virtual: false,
};
this._props = __assign(__assign({}, defaultProps), initProps);
if (this.props.virtual && (!initProps.size || initProps.size === 'auto')) {
throw new Error('Virtual slide must have a defined size');
}
}
Object.defineProperty(SnapSlide.prototype, "snap", {
/** Snap component */
get: function () {
return this._snap;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "props", {
/** Slide properties */
get: function () {
return this._props;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "element", {
/** Slide element */
get: function () {
return this._element;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "index", {
/** Slide index */
get: function () {
return this._index;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "coord", {
/** Current coordinate */
get: function () {
return this._coord;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "staticCoord", {
/** Static coordinate (as if the slide was never moved) */
get: function () {
return this._staticCoord;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "progress", {
/** Current progress of slide movement: from -1 to 1 */
get: function () {
return this._progress;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "sizeProp", {
/** Size property */
get: function () {
var _a, _b, _c;
return (_c = (_a = this.props.size) !== null && _a !== void 0 ? _a : (_b = this.snap) === null || _b === void 0 ? void 0 : _b.props.slideSize) !== null && _c !== void 0 ? _c : 'auto';
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "size", {
/** Slide size in pixels */
get: function () {
var _a;
var _b = this, snap = _b.snap, sizeProp = _b.sizeProp;
if (!snap) {
return 0;
}
if (sizeProp === 'stretch') {
return snap.containerSize;
}
if (sizeProp === 'auto') {
return (_a = this._domSize) !== null && _a !== void 0 ? _a : snap.containerSize;
}
return (0, utils_1.toPixels)(sizeProp);
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnapSlide.prototype, "isVisible", {
/** Check if the slide is visible */
get: function () {
return this._isVisible;
},
enumerable: false,
configurable: true
});
/** Resize the slide & trigger snap reflow */
SnapSlide.prototype.resize = function (isManual) {
if (isManual === void 0) { isManual = true; }
var _a = this, element = _a.element, snap = _a.snap;
if (!snap) {
return;
}
// Update DOM size
if (element) {
this._domSize =
snap.axis === 'x' ? element.offsetWidth : element.offsetHeight;
}
// Re-flow
snap.resize(isManual);
};
/**
* Attach the slide to the Snap class.
* For internal use only
*/
SnapSlide.prototype.$_attach = function (snap, index) {
var _this = this;
this.$_detach();
this._snap = snap;
this._index = index;
this._parallax = this._getParallaxNodes().map(function (node) { return new SlideParallax_1.SnapSlideParallax(snap, _this, node); });
if (this.element && this.sizeProp === 'auto') {
this._resizer = (0, utils_1.onResize)({
element: this.element,
viewportTarget: 'width',
callback: function () { return _this.resize(false); },
name: 'Snap Slide',
});
}
};
/**
* Detach the slide from the Snap class.
* For internal use only
*/
SnapSlide.prototype.$_detach = function () {
var _a, _b;
this._snap = undefined;
(_a = this._resizer) === null || _a === void 0 ? void 0 : _a.remove();
(_b = this._parallax) === null || _b === void 0 ? void 0 : _b.forEach(function (parallax) { return parallax.destroy(); });
};
/**
* Static coordinate (as if the slide was never moved).
* For internal use only
*/
SnapSlide.prototype.$_setStaticCoord = function (value) {
this._staticCoord = value;
};
/**
* Render the slide.
* For internal use only
*/
SnapSlide.prototype.$_render = function () {
var _a;
this._toggleAppend();
(_a = this._parallax) === null || _a === void 0 ? void 0 : _a.forEach(function (parallax) { return parallax.render(); });
};
/** Get list of parallax nodes */
SnapSlide.prototype._getParallaxNodes = function () {
var element = this.element;
if (!element) {
return [];
}
var selector = constants_1.parallaxAttributes.map(function (attr) { return "[".concat(attr, "]"); }).join(',');
var nodeList = element.querySelectorAll(selector);
return Array.from(nodeList);
};
/** Toggle slide append/remove */
SnapSlide.prototype._toggleAppend = function () {
if (!this.props.virtual || !this.element || !this.snap) {
return;
}
var element = this.element;
var container = this.snap.container;
if (this.isVisible && !this._isAppended) {
this._isAppended = true;
container.appendChild(element);
}
else if (!this.isVisible && this._isAppended) {
this._isAppended = false;
container.removeChild(element);
}
};
Object.defineProperty(SnapSlide.prototype, "magnets", {
/** Get magnets with static coordinates but dynamic alignment */
get: function () {
if (!this.snap) {
return [];
}
var _a = this, snap = _a.snap, staticCoord = _a.staticCoord, size = _a.size, index = _a.index;
var containerSize = snap.containerSize;
var points = [];
if (index === 0 && snap.props.loop) {
points.push(snap.max);
}
if (snap.props.centered) {
var point = staticCoord + size / 2 - snap.firstSlideSize / 2;
if (size > containerSize) {
points.push(point);
points.push(point + (containerSize - size) / 2);
points.push(point - (containerSize - size) / 2);
}
else {
points.push(point);
}
}
else {
points.push(staticCoord);
if (size > containerSize) {
points.push(staticCoord + (size - containerSize));
}
}
if (!snap.canLoop && !snap.props.centered) {
points = points.map(function (point) { return (0, utils_1.clamp)(point, 0, snap.max); });
}
return points;
},
enumerable: false,
configurable: true
});
/**
* Update slide progress.
* For internal use only
*/
SnapSlide.prototype.$_updateProgress = function () {
var snap = this.snap;
if (!snap) {
return;
}
var _a = this, coord = _a.coord, size = _a.size;
var props = snap.props, containerSize = snap.containerSize;
if (props.centered) {
var center = containerSize / 2 - size / 2;
this._progress = (0, utils_1.scoped)(coord, center, center - size);
return;
}
this._progress = (0, utils_1.scoped)(coord, 0, -size);
};
/**
* Update slide values.
* For internal use only
*/
SnapSlide.prototype.$_updateCoords = function (offset) {
var snap = this.snap;
if (!snap) {
return;
}
var _a = this, staticCoord = _a.staticCoord, size = _a.size;
var isCentered = snap.props.centered;
if (!snap.canLoop) {
this._setCoord(staticCoord + offset - snap.current);
return;
}
if (isCentered) {
this._setCoord((0, utils_1.loop)(staticCoord + offset - snap.current, -snap.max / 2 + offset, snap.max / 2 + offset));
return;
}
this._setCoord((0, utils_1.loop)(staticCoord - snap.current, -size, snap.max - size));
};
/** Set slide coordinate */
SnapSlide.prototype._setCoord = function (value) {
var _a, _b;
this._coord = value;
this._isVisible =
this.size > 0 &&
this.coord > -this.size &&
this.coord < ((_b = (_a = this.snap) === null || _a === void 0 ? void 0 : _a.containerSize) !== null && _b !== void 0 ? _b : 0);
};
return SnapSlide;
}());
exports.SnapSlide = SnapSlide;
//# sourceMappingURL=index.js.map