Skip to content

Commit

Permalink
added partial support for iFrames' windows
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianDavideConte committed Jan 4, 2024
1 parent f668778 commit 6c4b081
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 169 deletions.
8 changes: 5 additions & 3 deletions src/extensions/uss-dev-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
CREATE_LOG_OPTIONS,
IS_FUNCTION,
IS_OBJECT,
IS_WINDOW,
NO_VAL,
TOP_WINDOW,
DEFAULT_ERROR_PRIMARY_MSG_1,
DEFAULT_ERROR_PRIMARY_MSG_3,
DEFAULT_ERROR_PRIMARY_MSG_4,
Expand Down Expand Up @@ -154,7 +156,7 @@ export async function isValidStepLengthCalculator(
}

//Check if the passed container is valid.
if (options.container !== window && !CHECK_INSTANCEOF(options.container)) {
if (!IS_WINDOW(options.container) && !CHECK_INSTANCEOF(options.container)) {
_errorLogger(CREATE_LOG_OPTIONS(options, _functionName, { secondaryMsg: options.container, idx: 2 }, DEFAULT_LOG_OPTIONS));
return false;
}
Expand Down Expand Up @@ -204,12 +206,12 @@ export async function isValidStepLengthCalculator(
return;
}

window.requestAnimationFrame(() => _tester(resolve, reject));
TOP_WINDOW.requestAnimationFrame(() => _tester(resolve, reject));
}

try {
await new Promise((resolve, reject) => {
window.requestAnimationFrame(() => _tester(resolve, reject));
TOP_WINDOW.requestAnimationFrame(() => _tester(resolve, reject));
});
} catch(result) {
_errorLogger(CREATE_LOG_OPTIONS(options, _functionName, { secondaryMsg: result, idx: 5 }, DEFAULT_LOG_OPTIONS));
Expand Down
6 changes: 4 additions & 2 deletions src/extensions/uss-easings.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,8 @@ export const EASE_ELASTIC_X = (forwardEasing, backwardEasing, elasticPointCalcul
const _originalCallback = typeof _oldData[10] === "function" ? _oldData[10] : () => {};
_oldData[10] = () => {
_debounceTimeout = setTimeout(() => {
const _currentPos = container === window ? window.scrollX : container.scrollLeft;
//TODO: use IS_WINDOW()
const _currentPos = container === window ? container.scrollX : container.scrollLeft;
const _oldDirection = _oldData[4];
const _elasticAmount = elasticPointCalculator(originalTimestamp, timestamp, _currentPos, _oldDirection, container);
Expand Down Expand Up @@ -1022,7 +1023,8 @@ export const EASE_ELASTIC_Y = (forwardEasing, backwardEasing, elasticPointCalcul
const _originalCallback = typeof _oldData[11] === "function" ? _oldData[11] : () => {};
_oldData[11] = () => {
_debounceTimeout = setTimeout(() => {
const _currentPos = container === window ? window.scrollY : container.scrollTop;
//TODO: use IS_WINDOW()
const _currentPos = container === window ? container.scrollY : container.scrollTop;
const _oldDirection = _oldData[5];
const _elasticAmount = elasticPointCalculator(originalTimestamp, timestamp, _currentPos, _oldDirection, container);
Expand Down
46 changes: 42 additions & 4 deletions src/main/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,20 @@ export const NO_SP = null;
*/
export const NO_VAL = undefined;

/**
* The topmost `window` in the `window hierarchy`.
*/
export const TOP_WINDOW = window.top;



/**
* The initial inner `width` of the `window`.
* The initial inner `width` of the `window` in which this script has been initialized.
*/
export const INITIAL_WINDOW_WIDTH = window.innerWidth;

/**
* The initial inner `height` of the `window`.
* The initial inner `height` of the `window` in which this script has been initialized.
*/
export const INITIAL_WINDOW_HEIGHT = window.innerHeight;

Expand All @@ -218,6 +223,7 @@ export const HIGHEST_SAFE_SCROLL_POS = 1073741824;




/**
* The regex used by the `scrollIntoView` and `scrollIntoViewIfNeeded` functions
* to test if the passed alignments are set to `nearest` mode.
Expand Down Expand Up @@ -264,7 +270,7 @@ export const REGEX_OVERFLOW_WITH_VISIBLE = /(auto|scroll|visible)/;
* A string containing part of an error message.
* It can be used to build the `options` object for the error logger.
*/
export const DEFAULT_ERROR_PRIMARY_MSG_1 = " to be an instance of Element or window";
export const DEFAULT_ERROR_PRIMARY_MSG_1 = " to be an instance of Element or a window";

/**
* A string containing part of an error message.
Expand Down Expand Up @@ -421,6 +427,17 @@ export const CREATE_LOG_OPTIONS = (staticOptions, functionName, runtimeOptions,
return MERGE_OBJECTS(staticOptions, defaultOptions);
}

/**
* Returns the `window` associated with the passed `container`.
*
* `Note:` no checks are done on `container`.
* @param {*} container An instance of `Element` or a `window`.
* @returns Returns the `window` associated with `container`.
*/
export const GET_WINDOW_OF = (container) => {
return container.ownerDocument.defaultView;
}

/**
* Checks whether `value` is a function.
* @param {*} value The value to check.
Expand All @@ -441,6 +458,27 @@ export const IS_OBJECT = (value) => {
!Array.isArray(value);
}

/**
* Checks whether `value` is a window object.
* Works with iFrames' windows too.
* @param {*} value The value to check.
* @returns `true` if `value` is a window object, `false` otherwise.
*/
export const IS_WINDOW = (value) => {
if (value === window) return true;

/**
* Inside iFrames the pointer to the window object may be different
* from the one used in this module, but a window still exists and
* it can be retrieved by asking for the value.window.
*/
try {
return value === value.window;
} catch (UnsupportedOperation) {
return false;
}
}

/**
* Merges two objects into one.
* @param {Object} obj1 In case of conflicts, this object's properties will have the priority.
Expand All @@ -460,7 +498,7 @@ export const TO_STRING = (value) => {
const _type = typeof value;

if (
value === window ||
IS_WINDOW(value) ||
value === null ||
value === undefined ||
_type === "boolean" ||
Expand Down
Loading

0 comments on commit 6c4b081

Please sign in to comment.