@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
307 lines (303 loc) • 11.2 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 2.2.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
;
const scrollSize = require('../tools/scroll-size.cjs');
const commands = require('./message/commands.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class ParentManager {
static {
__name(this, "ParentManager");
}
#messageManager;
constructor(messageManager) {
this.#messageManager = messageManager;
}
get message() {
return this.#messageManager;
}
/**
* The method closes the open modal window with the application
*
* @return {Promise<void>}
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-close-application.html
*/
async closeApplication() {
return this.#messageManager.send(commands.MessageCommands.closeApplication, {
/**
* @memo There is no point - everything will be closed, and timeout will not be able to do anything
*/
isSafely: false
});
}
/**
* Sets the size of the frame containing the application to the size of the frame's content.
*
* @return {Promise<void>}
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-fit-window.html
*
* @memo in certain situations it may not be executed (placement of the main window after installing the application), in this case isSafely mode will work
*/
async fitWindow() {
const width = "100%";
const height = this.getScrollSize().scrollHeight;
return this.#messageManager.send(commands.MessageCommands.resizeWindow, {
width,
height,
isSafely: true
});
}
/**
* Sets the size of the frame containing the application to the size of the frame's content.
*
* @param {number} width
* @param {number} height
*
* @return {Promise<void>}
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-resize-window.html
*
* @memo in certain situations it may not be executed, in this case isSafely mode will be triggered
*/
async resizeWindow(width, height) {
if (width > 0 && height > 0) {
return this.#messageManager.send(commands.MessageCommands.resizeWindow, {
width,
height,
isSafely: true
});
}
return Promise.reject(
new Error(`Wrong width:number = ${width} or height:number = ${height}`)
);
}
/**
* Automatically resize `document.body` of frame with application according to frame content dimensions
* If you pass appNode, the height will be calculated relative to it
*
* @param {HTMLElement|null} appNode
* @param {number} minHeight
* @param {number} minWidth
*
* @return {Promise<void>}
*/
async resizeWindowAuto(appNode = null, minHeight = 0, minWidth = 0) {
const body = document.body;
let width = Math.max(
body.scrollWidth,
body.offsetWidth
// html.clientWidth,
// html.scrollWidth,
// html.offsetWidth
);
if (minWidth > 0) {
width = Math.max(minWidth, width);
}
let height = Math.max(
body.scrollHeight,
body.offsetHeight
// html.clientHeight,
// html.scrollHeight,
// html.offsetHeight
);
if (appNode) {
height = Math.max(appNode.scrollHeight, appNode.offsetHeight);
}
if (minHeight > 0) {
height = Math.max(minHeight, height);
}
return this.resizeWindow(width, height);
}
/**
* This function returns the inner dimensions of the application frame
*
* @return {Promise<{scrollWidth: number; scrollHeight: number}>}
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-get-scroll-size.html
*/
getScrollSize() {
return scrollSize.default();
}
/**
* Scrolls the parent window
*
* @param {number} scroll should specify the vertical scrollbar position (0 - scroll to the very top)
* @return {Promise<void>}
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-scroll-parent-window.html
*/
async scrollParentWindow(scroll) {
if (!Number.isInteger(scroll)) {
return Promise.reject(new Error("Wrong scroll number"));
}
if (scroll < 0) {
scroll = 0;
}
return this.#messageManager.send(commands.MessageCommands.setScroll, {
scroll,
isSafely: true
});
}
/**
* Reload the page with the application (the whole page, not just the frame).
*
* @return {Promise<void>}
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-reload-window.html
*/
async reloadWindow() {
return this.#messageManager.send(commands.MessageCommands.reloadWindow, {
isSafely: true
});
}
/**
* Sets the in-layout page title (the `#pagetitle` element the portal renders around the app).
*
* Does NOT change the browser tab title (`document.title`): the portal applies this command to
* `#pagetitle`, never to the tab. To set the browser tab title, open the view as a slider via
* `SliderManager.openSliderAppPage` with a `bx24_title` option.
*
* @param {string} title
*
* @return {Promise<void>}
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-set-title.html
*/
async setTitle(title) {
return this.#messageManager.send(commands.MessageCommands.setTitle, {
title: title.toString(),
isSafely: true
});
}
/**
* Initiates a call via internal communication.
*
* **Fire-and-forget.** The portal's bridge handler is declared as
* `function(params)` — it does not accept the callback argument the message
* layer offers, so it never reports back. The returned promise means "the
* command was posted", not "the call started"; it resolves on the SDK's own
* `isSafely` timer, and the accompanying `stop by timeout` log line is the
* normal outcome rather than a fault. See {@link ParentManager} — the same
* holds for every `im*` method here. (#331)
*
* The portal reaches the current API underneath: `BXIM.callTo` →
* `Messenger.Public.startVideoCall`. The deprecation warning in the portal
* console is emitted by the portal's own compatibility layer, not by this
* call, and an application cannot avoid it — the newer names are not part of
* the placement's command vocabulary.
*
* @param {number} userId The identifier of the account user
* @param {boolean} isVideo true - video call, false - audio call. Optional parameter.
*
* @return {Promise<void>} resolves once the command has been posted.
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-im-call-to.html
*/
async imCallTo(userId, isVideo = true) {
return this.#messageManager.send(commands.MessageCommands.imCallTo, {
userId,
video: isVideo,
isSafely: true
});
}
/**
* Makes a call to the phone number.
*
* **Fire-and-forget** — see {@link imCallTo} for what the returned promise
* does and does not mean.
*
* `params` is forwarded for the phone manager, matching the second argument of
* the portal's `Messenger.startPhoneCall(number, params)`. The portal's bridge
* handler currently enumerates fields by hand and reads only `phone`, so this
* is dropped on the way today; sending it costs nothing (an unknown field is
* ignored) and starts working without an application change once the portal
* forwards it. (#331)
*
* @param {string} phone Phone number. The number can be in the format: `+44 20 1234 5678` or `x (xxx) xxx-xx-xx`
* @param {Record<string, unknown>} [params] Extra call parameters for the phone manager.
*
* @return {Promise<void>} resolves once the command has been posted.
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-im-phone-to.html
*/
async imPhoneTo(phone, params) {
return this.#messageManager.send(commands.MessageCommands.imPhoneTo, {
phone,
...params === void 0 ? {} : { params },
isSafely: true
});
}
/**
* Opens the messenger window
* userId or chatXXX - chat, where XXX is the chat identifier, which can simply be a number.
* sgXXX - group chat, where XXX is the social network group number (the chat must be enabled in this group).
*
* XXXX** - open line, where XXX is the code obtained via the Rest method imopenlines.network.join.
*
* If nothing is passed, the chat interface will open with the last opened dialog.
*
* **Fire-and-forget** — see {@link imCallTo} for what the returned promise
* does and does not mean.
*
* `messageId` matches the second argument of the portal's
* `Messenger.openChat(dialogId, messageId)`, which focuses a specific message.
* The portal's bridge handler reads only `dialogId` today, so it is dropped on
* the way; sending it is free and starts working without an application change
* once the portal forwards it. (#331)
*
* @param {number|`chat${number}`|`sg${number}`|`imol|${number}`|undefined} dialogId
* @param {number} [messageId] Message to focus once the chat opens.
*
* @return {Promise<void>} resolves once the command has been posted.
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-im-open-messenger.html
* @link https://dev.1c-bitrix.ru/learning/course/index.php?COURSE_ID=93&LESSON_ID=20152&LESSON_PATH=7657.7883.8025.20150.20152
*
*/
async imOpenMessenger(dialogId, messageId) {
return this.#messageManager.send(commands.MessageCommands.imOpenMessenger, {
dialogId,
...messageId === void 0 ? {} : { messageId },
isSafely: true
});
}
/**
* Opens the history window
* Identifier of the dialog:
*
* userId or chatXXX - chat, where XXX is the chat identifier, which can simply be a number.
* imol|XXXX - open line, where XXX is the session number of the open line.
*
* **Fire-and-forget** — see {@link imCallTo} for what the returned promise
* does and does not mean.
*
* Note the portal routes this differently from the other three: its
* compatibility layer calls the opener directly, bypassing
* `Messenger.Public`. For an ordinary `dialogId` it lands in `openChat`, which
* is what the deprecation notice recommends; for an open-line id
* (`imol|…`) it takes a separate branch whose public equivalent is
* `openLinesHistory`, not `openChat`. (#331)
*
* @param {number|`chat${number}`|`imol|${number}`} dialogId
*
* @return {Promise<void>} resolves once the command has been posted.
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-im-open-history.html
*/
async imOpenHistory(dialogId) {
return this.#messageManager.send(commands.MessageCommands.imOpenHistory, {
dialogId,
isSafely: true
});
}
}
exports.ParentManager = ParentManager;
//# sourceMappingURL=parent.cjs.map