@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
155 lines (152 loc) • 4.21 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.1.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
import { Type } from '../tools/type.mjs';
import { MessageCommands } from './message/commands.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class PlacementManager {
static {
__name(this, "PlacementManager");
}
#messageManager;
#placement = "";
#options = {};
constructor(messageManager) {
this.#messageManager = messageManager;
}
/**
* Initializes the data received from the parent window message.
* @param data
*/
initData(data) {
this.#placement = data.PLACEMENT || "DEFAULT";
this.#options = Object.freeze(data.PLACEMENT_OPTIONS);
return this;
}
/**
* Symlink on `placement`
* For backward compatibility
*/
get title() {
return this.#placement;
}
get placement() {
return this.#placement;
}
get isDefault() {
return this.placement === "DEFAULT";
}
get options() {
return this.#options;
}
get isSliderMode() {
return this.options?.IFRAME === "Y";
}
/**
* Get Information About the JS Interface of the Current Embedding Location
*
* @return {Promise<any>}
*
* @link https://apidocs.bitrix24.com/api-reference/widgets/ui-interaction/bx24-placement-get-interface.html
*/
async getInterface() {
return this.#messageManager.send(
MessageCommands.getInterface,
{
isSafely: true
}
);
}
/**
* Set Up the Interface Event Handler
* @param {string} eventName
* @param {(...args: any[]) => void} callBack
* @return {Promise<any>}
*
* @link https://apidocs.bitrix24.com/api-reference/widgets/ui-interaction/bx24-placement-bind-event.html
*/
async bindEvent(eventName, callBack) {
return this.#messageManager.send(
MessageCommands.placementBindEvent,
{
event: eventName,
callBack,
isSafely: true
}
);
}
async call(command, parameters = {}) {
if (command === "setValue" && !Type.isString(parameters?.["value"])) {
throw new TypeError(
"placement.call('setValue', { value }) expects `value` to be a JSON-serialized string. Use placement.setValue(value) to serialize automatically, or call JSON.stringify yourself."
);
}
return this.#messageManager.send(
command,
{
...parameters,
isSafely: true,
isRawValue: ["setValue"].includes(command)
}
);
}
/**
* Set Value for the Current Embedding Location
*
* Convenience wrapper around `placement.call('setValue', ...)` that handles
* JSON serialization. Pass any value (string, number, boolean, object, array)
* — it will be serialized via `JSON.stringify` before being sent to the
* parent window, which performs `JSON.parse` on receipt.
*
* @param { unknown } value Any JSON-serializable value
* @return { Promise<any> }
*
* @link https://apidocs.bitrix24.com/api-reference/widgets/ui-interaction/bx24-placement-call.html
*
* @example
* await b24.placement.setValue('test')
* await b24.placement.setValue({ id: 1, title: 'demo' })
*/
async setValue(value) {
return this.#messageManager.send(
"setValue",
{
value: JSON.stringify(value),
isSafely: true,
isRawValue: true
}
);
}
/**
* Set Up the Interface Event Handler
* @param {string} command
* @param {null | string | Record<string, any>} parameters
* @param {(...args: any[]) => void} callBack
*
* @return {Promise<any>}
*/
async callCustomBind(command, parameters = null, callBack) {
let options = {};
if (Type.isString(parameters)) {
options["singleOption"] = parameters;
} else if (Type.isObjectLike(parameters)) {
options = { ...parameters };
}
return this.#messageManager.send(
command,
{
...options,
callBack,
isSafely: true
}
);
}
}
export { PlacementManager };
//# sourceMappingURL=placement.mjs.map