@dflex/core-instance
Advanced tools
Comparing version 3.3.0 to 3.3.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var utils_1 = require("@dflex/utils"); | ||
var AbstractInstance = /** @class */ (function () { | ||
function AbstractInstance(_a, opts) { | ||
var ref = _a.ref, id = _a.id; | ||
const utils_1 = require("@dflex/utils"); | ||
class AbstractInstance { | ||
ref; | ||
id; | ||
translate; | ||
isInitialized; | ||
isPaused; | ||
hasAttribute; | ||
constructor({ ref, id }, opts) { | ||
this.id = id; | ||
@@ -25,27 +30,27 @@ if (opts.isInitialized) { | ||
*/ | ||
AbstractInstance.prototype.attach = function (incomingRef) { | ||
attach(incomingRef) { | ||
// Cleanup. | ||
this.ref = null; | ||
if (!incomingRef) { | ||
var ref = document.getElementById(this.id); | ||
const ref = document.getElementById(this.id); | ||
if (!ref) { | ||
throw new Error("DFlex: Element with ID: ".concat(this.id, " is not found.")); | ||
throw new Error(`DFlex: Element with ID: ${this.id} is not found.`); | ||
} | ||
} | ||
else if (incomingRef.nodeType !== Node.ELEMENT_NODE) { | ||
throw new Error("DFlex: Invalid HTMLElement: ".concat(incomingRef, " is passed to registry.")); | ||
throw new Error(`DFlex: Invalid HTMLElement: ${incomingRef} is passed to registry.`); | ||
} | ||
this.ref = incomingRef; | ||
this.isInitialized = true; | ||
}; | ||
} | ||
/** | ||
* Detach element DOM node from the instance. | ||
*/ | ||
AbstractInstance.prototype.detach = function () { | ||
detach() { | ||
this.isInitialized = false; | ||
this.ref = null; | ||
}; | ||
AbstractInstance.prototype.transform = function (x, y) { | ||
this.ref.style.transform = "translate3d(".concat(x, "px,").concat(y, "px, 0)"); | ||
}; | ||
} | ||
transform(x, y) { | ||
this.ref.style.transform = `translate3d(${x}px,${y}px, 0)`; | ||
} | ||
/** | ||
@@ -55,3 +60,3 @@ * Initialize the translate AxesCoordinates as part of abstract instance and | ||
*/ | ||
AbstractInstance.prototype.initTranslate = function () { | ||
initTranslate() { | ||
if (!this.translate) { | ||
@@ -63,6 +68,6 @@ this.translate = new utils_1.AxesCoordinates(0, 0); | ||
this.isPaused = false; | ||
}; | ||
AbstractInstance.prototype.setDataset = function (key, value) { | ||
} | ||
setDataset(key, value) { | ||
if (key === "index") { | ||
this.ref.dataset[key] = "".concat(value); | ||
this.ref.dataset[key] = `${value}`; | ||
return; | ||
@@ -72,6 +77,6 @@ } | ||
return; | ||
this.ref.dataset[key] = "".concat(value); | ||
this.ref.dataset[key] = `${value}`; | ||
this.hasAttribute[key] = true; | ||
}; | ||
AbstractInstance.prototype.rmDateset = function (key) { | ||
} | ||
rmDateset(key) { | ||
delete this.ref.dataset[key]; | ||
@@ -81,4 +86,4 @@ if (this.hasAttribute[key]) { | ||
} | ||
}; | ||
AbstractInstance.prototype.setAttribute = function (key, value) { | ||
} | ||
setAttribute(key, value) { | ||
if (this.hasAttribute[key]) | ||
@@ -88,4 +93,4 @@ return; | ||
this.hasAttribute[key] = true; | ||
}; | ||
AbstractInstance.prototype.removeAttribute = function (key) { | ||
} | ||
removeAttribute(key) { | ||
if (!this.hasAttribute[key]) | ||
@@ -95,16 +100,14 @@ return; | ||
this.hasAttribute[key] = false; | ||
}; | ||
AbstractInstance.prototype.clearAttributes = function () { | ||
var _this = this; | ||
Object.keys(this.hasAttribute).forEach(function (key) { | ||
} | ||
clearAttributes() { | ||
Object.keys(this.hasAttribute).forEach((key) => { | ||
if (key === "dragged") | ||
_this.removeAttribute(key); | ||
this.removeAttribute(key); | ||
else | ||
_this.rmDateset(key); | ||
this.rmDateset(key); | ||
}); | ||
// @ts-expect-error. | ||
this.hasAttribute = {}; | ||
}; | ||
return AbstractInstance; | ||
}()); | ||
} | ||
} | ||
exports.default = AbstractInstance; |
import { AxesCoordinates } from "@dflex/utils"; | ||
import type { Rect, EffectedElemDirection, Axes, AxesCoordinatesInterface } from "@dflex/utils"; | ||
import type { RectDimensions, EffectedElemDirection, Axes, AxesCoordinatesInterface } from "@dflex/utils"; | ||
import AbstractInstance from "./AbstractInstance"; | ||
import type { Keys, Order, TransitionHistory, CoreInput, AbstractOpts, CoreInstanceInterface } from "./types"; | ||
declare class CoreInstance extends AbstractInstance implements CoreInstanceInterface { | ||
/** Initial read-only element offset */ | ||
readonly offset: Rect; | ||
/** Store history of Y-transition according to unique ID. */ | ||
offset: RectDimensions; | ||
translateHistory?: AxesCoordinatesInterface<TransitionHistory>; | ||
/** Current element offset (x-left, y-top) */ | ||
currentPosition: AxesCoordinatesInterface; | ||
grid: AxesCoordinatesInterface; | ||
order: Order; | ||
@@ -13,0 +11,0 @@ keys: Keys; |
"use strict"; | ||
/* eslint-disable no-param-reassign */ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -33,22 +7,29 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var utils_1 = require("@dflex/utils"); | ||
var AbstractInstance_1 = __importDefault(require("./AbstractInstance")); | ||
var CoreInstance = /** @class */ (function (_super) { | ||
__extends(CoreInstance, _super); | ||
function CoreInstance(elementWithPointer, opts) { | ||
var _this = this; | ||
var order = elementWithPointer.order, keys = elementWithPointer.keys, depth = elementWithPointer.depth, scrollX = elementWithPointer.scrollX, scrollY = elementWithPointer.scrollY, element = __rest(elementWithPointer, ["order", "keys", "depth", "scrollX", "scrollY"]); | ||
_this = _super.call(this, element, opts) || this; | ||
_this.order = order; | ||
_this.keys = keys; | ||
_this.depth = depth; | ||
_this.isVisible = _this.isInitialized && !_this.isPaused; | ||
if (_this.isInitialized) { | ||
_this.setDataset("index", _this.order.self); | ||
const utils_1 = require("@dflex/utils"); | ||
const AbstractInstance_1 = __importDefault(require("./AbstractInstance")); | ||
class CoreInstance extends AbstractInstance_1.default { | ||
offset; | ||
translateHistory; | ||
currentPosition; | ||
grid; | ||
order; | ||
keys; | ||
depth; | ||
isVisible; | ||
hasToTransform; | ||
animatedFrame; | ||
constructor(elementWithPointer, opts) { | ||
const { order, keys, depth, scrollX, scrollY, ...element } = elementWithPointer; | ||
super(element, opts); | ||
this.order = order; | ||
this.keys = keys; | ||
this.depth = depth; | ||
this.isVisible = this.isInitialized && !this.isPaused; | ||
if (this.isInitialized) { | ||
this.setDataset("index", this.order.self); | ||
} | ||
if (!_this.isPaused) { | ||
_this.initIndicators(scrollX, scrollY); | ||
if (!this.isPaused) { | ||
this.initIndicators(scrollX, scrollY); | ||
} | ||
_this.animatedFrame = null; | ||
return _this; | ||
this.animatedFrame = null; | ||
} | ||
@@ -63,4 +44,4 @@ /** | ||
*/ | ||
CoreInstance.prototype.initIndicators = function (scrollX, scrollY) { | ||
var _a = this.ref.getBoundingClientRect(), height = _a.height, width = _a.width, left = _a.left, top = _a.top; | ||
initIndicators(scrollX, scrollY) { | ||
const { height, width, left, top } = this.ref.getBoundingClientRect(); | ||
/** | ||
@@ -71,6 +52,5 @@ * Element offset stored once without being triggered to re-calculate. | ||
*/ | ||
// @ts-expect-error | ||
this.offset = { | ||
height: height, | ||
width: width, | ||
height, | ||
width, | ||
left: left + scrollX, | ||
@@ -80,5 +60,6 @@ top: top + scrollY, | ||
this.currentPosition = new utils_1.AxesCoordinates(this.offset.left, this.offset.top); | ||
this.grid = new utils_1.AxesCoordinates(0, 0); | ||
this.hasToTransform = false; | ||
}; | ||
CoreInstance.prototype.resume = function (scrollX, scrollY) { | ||
} | ||
resume(scrollX, scrollY) { | ||
if (!this.isInitialized) | ||
@@ -88,4 +69,4 @@ this.attach(null); | ||
this.initIndicators(scrollX, scrollY); | ||
}; | ||
CoreInstance.prototype.changeVisibility = function (isVisible) { | ||
} | ||
changeVisibility(isVisible) { | ||
if (isVisible === this.isVisible) | ||
@@ -98,6 +79,6 @@ return; | ||
} | ||
}; | ||
CoreInstance.prototype.updateCurrentIndicators = function (leftSpace, topSpace) { | ||
} | ||
updateCurrentIndicators(leftSpace, topSpace) { | ||
this.translate.setAxes(this.translate.x + leftSpace, this.translate.y + topSpace); | ||
var _a = this.offset, left = _a.left, top = _a.top; | ||
const { left, top } = this.offset; | ||
/** | ||
@@ -110,19 +91,18 @@ * This offset related directly to translate Y and Y. It's isolated from | ||
this.hasToTransform = true; | ||
}; | ||
CoreInstance.prototype.isPositionedUnder = function (elmY) { | ||
} | ||
isPositionedUnder(elmY) { | ||
return elmY < this.currentPosition.y; | ||
}; | ||
CoreInstance.prototype.isPositionedLeft = function (elmX) { | ||
} | ||
isPositionedLeft(elmX) { | ||
return elmX < this.currentPosition.x; | ||
}; | ||
CoreInstance.prototype.transformElm = function () { | ||
var _this = this; | ||
} | ||
transformElm() { | ||
if (this.animatedFrame !== null) { | ||
window.cancelAnimationFrame(this.animatedFrame); | ||
} | ||
this.animatedFrame = window.requestAnimationFrame(function () { | ||
_this.transform(_this.translate.x, _this.translate.y); | ||
_this.animatedFrame = null; | ||
this.animatedFrame = window.requestAnimationFrame(() => { | ||
this.transform(this.translate.x, this.translate.y); | ||
this.animatedFrame = null; | ||
}); | ||
}; | ||
} | ||
/** | ||
@@ -133,15 +113,13 @@ * Update element index in siblings branch | ||
*/ | ||
CoreInstance.prototype.updateOrderIndexing = function (i) { | ||
var oldIndex = this.order.self; | ||
var newIndex = oldIndex + i; | ||
updateOrderIndexing(i) { | ||
const { self: oldIndex } = this.order; | ||
const newIndex = oldIndex + i; | ||
this.order.self = newIndex; | ||
return { oldIndex: oldIndex, newIndex: newIndex }; | ||
}; | ||
CoreInstance.prototype.assignNewPosition = function (branchIDsOrder, newIndex, oldIndex, siblingsEmptyElmIndex) { | ||
if (oldIndex === void 0) { oldIndex = -1; } | ||
if (siblingsEmptyElmIndex === void 0) { siblingsEmptyElmIndex = -1; } | ||
return { oldIndex, newIndex }; | ||
} | ||
assignNewPosition(branchIDsOrder, newIndex, oldIndex = -1, siblingsEmptyElmIndex = -1) { | ||
if (newIndex < 0 || newIndex > branchIDsOrder.length - 1) { | ||
if (process.env.NODE_ENV !== "production") { | ||
// eslint-disable-next-line no-console | ||
console.error("Illegal Attempt: Received an index:".concat(newIndex, " on siblings list:").concat(branchIDsOrder.length - 1)); | ||
console.error(`Illegal Attempt: Received an index:${newIndex} on siblings list:${branchIDsOrder.length - 1}`); | ||
} | ||
@@ -170,3 +148,3 @@ return siblingsEmptyElmIndex; | ||
return oldIndex; | ||
}; | ||
} | ||
/** | ||
@@ -178,6 +156,5 @@ * Set a new translate position and store the old one. | ||
*/ | ||
CoreInstance.prototype.seTranslate = function (elmSpace, axes, operationID, isForceTransform) { | ||
if (isForceTransform === void 0) { isForceTransform = false; } | ||
seTranslate(elmSpace, axes, operationID, isForceTransform = false) { | ||
if (operationID) { | ||
var elmAxesHistory = { | ||
const elmAxesHistory = { | ||
ID: operationID, | ||
@@ -208,3 +185,3 @@ pre: this.translate[axes], | ||
this.hasToTransform = false; | ||
}; | ||
} | ||
/** | ||
@@ -219,5 +196,3 @@ * | ||
*/ | ||
CoreInstance.prototype.setPosition = function (iDsInOrder, effectedElemDirection, elmSpace, operationID, siblingsEmptyElmIndex, axes, numberOfPassedElm, isShuffle) { | ||
if (numberOfPassedElm === void 0) { numberOfPassedElm = 1; } | ||
if (isShuffle === void 0) { isShuffle = true; } | ||
setPosition(iDsInOrder, effectedElemDirection, elmSpace, operationID, siblingsEmptyElmIndex, axes, numberOfPassedElm = 1, isShuffle = true) { | ||
/** | ||
@@ -229,6 +204,6 @@ * effectedElemDirection decides the direction of the element, negative or positive. | ||
this.seTranslate(elmSpace[axes], axes, operationID); | ||
var _a = this.updateOrderIndexing(effectedElemDirection[axes] * numberOfPassedElm), oldIndex = _a.oldIndex, newIndex = _a.newIndex; | ||
var newStatusSiblingsHasEmptyElm = this.assignNewPosition(iDsInOrder, newIndex, isShuffle ? oldIndex : undefined, siblingsEmptyElmIndex[axes]); | ||
const { oldIndex, newIndex } = this.updateOrderIndexing(effectedElemDirection[axes] * numberOfPassedElm); | ||
const newStatusSiblingsHasEmptyElm = this.assignNewPosition(iDsInOrder, newIndex, isShuffle ? oldIndex : undefined, siblingsEmptyElmIndex[axes]); | ||
return newStatusSiblingsHasEmptyElm; | ||
}; | ||
} | ||
/** | ||
@@ -241,3 +216,3 @@ * Roll back element position. | ||
*/ | ||
CoreInstance.prototype.rollBack = function (operationID, isForceTransform, axes) { | ||
rollBack(operationID, isForceTransform, axes) { | ||
if (!this.translateHistory || | ||
@@ -250,16 +225,15 @@ !this.translateHistory[axes] || | ||
} | ||
var lastMovement = this.translateHistory[axes].pop(); | ||
const lastMovement = this.translateHistory[axes].pop(); | ||
if (!lastMovement) | ||
return; | ||
var pre = lastMovement.pre; | ||
var elmSpace = pre - this.translate[axes]; | ||
var increment = elmSpace > 0 ? 1 : -1; | ||
const { pre } = lastMovement; | ||
const elmSpace = pre - this.translate[axes]; | ||
const increment = elmSpace > 0 ? 1 : -1; | ||
// Don't update UI if it's zero and wasn't transformed. | ||
this.seTranslate(elmSpace, axes, undefined, isForceTransform); | ||
var newIndex = this.updateOrderIndexing(increment).newIndex; | ||
const { newIndex } = this.updateOrderIndexing(increment); | ||
this.setDataset("index", newIndex); | ||
this.rollBack(operationID, isForceTransform, axes); | ||
}; | ||
return CoreInstance; | ||
}(AbstractInstance_1.default)); | ||
} | ||
} | ||
exports.default = CoreInstance; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var CoreInstance_1 = __importDefault(require("./CoreInstance")); | ||
const CoreInstance_1 = __importDefault(require("./CoreInstance")); | ||
exports.default = CoreInstance_1.default; |
@@ -1,3 +0,2 @@ | ||
import { AxesCoordinates } from "@dflex/utils"; | ||
import type { Rect, Axes, EffectedElemDirection } from "@dflex/utils"; | ||
import type { AxesCoordinatesInterface, RectDimensions, Axes, EffectedElemDirection } from "@dflex/utils"; | ||
export interface AbstractOpts { | ||
@@ -19,3 +18,3 @@ isInitialized: boolean; | ||
id: string; | ||
translate: AxesCoordinates; | ||
translate: AxesCoordinatesInterface; | ||
attach(ref: HTMLElement | null): void; | ||
@@ -65,10 +64,17 @@ detach(): void; | ||
export interface CoreInstanceInterface extends AbstractInterface { | ||
/** Initial read-only element offset */ | ||
readonly offset: RectDimensions; | ||
/** Store history of Y-transition according to unique ID. */ | ||
translateHistory?: AxesCoordinatesInterface<TransitionHistory>; | ||
/** Current element offset (x-left, y-top) */ | ||
currentPosition: AxesCoordinatesInterface; | ||
/** Element position in the grid container. */ | ||
grid: AxesCoordinatesInterface; | ||
/** Element visibility in the scroll container. */ | ||
isVisible: boolean; | ||
readonly offset: Rect; | ||
translateHistory?: AxesCoordinates<TransitionHistory>; | ||
currentPosition: AxesCoordinates; | ||
/** Animated frame if the element is transforming */ | ||
animatedFrame: number | null; | ||
order: Order; | ||
keys: Keys; | ||
depth: number; | ||
animatedFrame: number | null; | ||
isPositionedUnder(elmY: number): boolean; | ||
@@ -78,3 +84,3 @@ isPositionedLeft(elmX: number): boolean; | ||
changeVisibility(isVisible: boolean): void; | ||
setPosition(iDsInOrder: string[], effectedElemDirection: EffectedElemDirection, elmSpace: AxesCoordinates, operationID: string, siblingsEmptyElmIndex: AxesCoordinates, axes: Axes, vIncrement?: number, isShuffle?: boolean): number; | ||
setPosition(iDsInOrder: string[], effectedElemDirection: EffectedElemDirection, elmSpace: AxesCoordinatesInterface, operationID: string, siblingsEmptyElmIndex: AxesCoordinatesInterface, axes: Axes, vIncrement?: number, isShuffle?: boolean): number; | ||
transformElm(): void; | ||
@@ -81,0 +87,0 @@ assignNewPosition(branchIDsOrder: string[], newIndex: number, oldIndex?: number, siblingsHasEmptyElm?: number): number; |
{ | ||
"name": "@dflex/core-instance", | ||
"version": "3.3.0", | ||
"version": "3.3.1", | ||
"main": "dist/index.js", | ||
@@ -19,3 +19,3 @@ "types": "dist/index.d.ts", | ||
"dependencies": { | ||
"@dflex/utils": "^3.3.0" | ||
"@dflex/utils": "^3.3.1" | ||
}, | ||
@@ -42,3 +42,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "39cb2e4b182c56925f13915c8ecac2ff84e97718" | ||
"gitHead": "f1f6f51e5b66462e307cb368c77be9038894ddb9" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22568
526
Updated@dflex/utils@^3.3.1