SmsSender::getJs

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. SmsSender
  4. getJs
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integration/bizproc/fieldtype/smssender.php
  • Класс: Bitrix\Crm\Integration\BizProc\FieldType\SmsSender
  • Вызов: SmsSender::getJs
static function getJs(array $providers, string $controlId, string $labelId)
{
	$providersJs = Json::encode($providers);
	$controlIdJs = \CUtil::JSEscape($controlId);
	$labelIdJs = \CUtil::JSEscape($labelId);
	$marketJs = \CUtil::JSEscape(Loc::getMessage('CRM_BP_FIELDTYPE_SMS_SENDER_MARKETPLACE'));

	return <<
			BX.ready(function()
			{
				var labelNode = document.getElementById('{$labelIdJs}');
				var valueNode = document.getElementById('{$controlIdJs}');
				
				if (!labelNode || !valueNode)
				{
					return;
				}
				
				var manageUrlNode = labelNode.closest('form').querySelector('[data-role="provider-manage-url"]');
				var noticeNode = labelNode.closest('form').querySelector('[data-role="provider-notice"]');
		
				var menuId = 'BPCRMSSMSA_menu' + Math.random();
				var providers = {$providersJs};
		
				var getProviderInfo = function(id)
				{
					for (var i = 0; i < providers.length; ++i)
					{
						if (providers[i]['ID'] === id)
						{
							return providers[i];
						}
					}
					return null;
				};
		
				var setProviderLabel = function(providerInfo, fromId)
				{
					var providerLabel = providerInfo['NAME'];
					var fromLabel = null;
					if (fromId)
					{
						fromLabel = fromId;
						providerInfo['FROM_LIST'].forEach(function(item)
						{
							if (item.id === fromId)
							{
								fromLabel = item.name;
							}
						});
					}
		
					if (providerInfo['ID'] === 'rest')
					{
						labelNode.textContent = fromLabel;
					}
					else if (fromLabel)
					{
						labelNode.textContent = providerLabel + ' (' + fromLabel + ')';
					}
					else
					{
						labelNode.textContent = providerLabel;
					}
				};
		
				var getNoticeText = function(providerInfo)
				{
					if (providerInfo['IS_INTERNAL'])
					{
						if (providerInfo['IS_DEMO'])
						{
							return noticeNode.getAttribute('data-text-demo');
						}
						else if (!providerInfo['CAN_USE'])
						{
							return noticeNode.getAttribute('data-text-cantuse');
						}
					}
					return '';
				};
		
				var setProvider = function(providerId)
				{
					valueNode.value = providerId;
					
					var fromId = '';
					providerId = providerId.split('@');
					if (providerId.length > 1)
					{
						fromId = providerId.shift();
					}
					providerId = providerId.join('');

					var info = getProviderInfo(providerId);
					if (!info)
					{
						return;
					}

					setProviderLabel(info, fromId);
		
					manageUrlNode.href = info['MANAGE_URL'] ? info['MANAGE_URL'] : '#';
					BX.style(manageUrlNode, 'visibility', info['MANAGE_URL'] ? 'visible' : 'hidden');
		
					var noticeText = getNoticeText(info);
					noticeNode.textContent = noticeText? noticeText : '';
					BX.style(noticeNode, 'display', noticeText? '' : 'none');
				};
		
				var onItemClick = function(e, item)
				{
					(this.getRootMenuWindow() || this.popupWindow).close();
					setProvider(item.providerId);
				};
		
				BX.bind(labelNode, 'click', function(e)
				{
					var element = this;
					var menuItems = [];
		
					for (var i = 0; i < providers.length; ++i)
					{
						var proviver = providers[i];
		
						if (proviver['ID'] === 'rest')
						{
							proviver['FROM_LIST'].forEach(function(item)
							{
								menuItems.push({
									text: item.name,
									providerId: [item.id, 'rest'].join('@'),
									onclick: onItemClick
								});
							});
						}
						else
						{
							var menuItem = {
								text: proviver['NAME'],
								providerId: proviver['ID'],
								onclick: onItemClick
							};
		
							if (proviver['FROM_LIST'].length > 1)
							{
								var subItems = [];
		
								proviver['FROM_LIST'].forEach(function(item)
								{
									subItems.push({
										text: item.name,
										providerId: [item.id, proviver['ID']].join('@'),
										onclick: onItemClick
									});
								});
		
								menuItem['items'] = subItems;
							}
		
							menuItems.push(menuItem);
						}
					}
		
					menuItems.push({delimiter: true}, {
						text: '{$marketJs}',
						href: '/marketplace/category/crm_robot_sms/',
						target: '_blank'
					});
		
					BX.PopupMenu.show(
						menuId,
						element,
						menuItems,
						{
							overlay: { backgroundColor: 'transparent' },
							autoHide: true,
							offsetLeft: 40,
							angle: { position: 'top', offset: 0 },
							closeByEsc: true
						}
					);
				});
		
				setProvider(valueNode.value);
			});
		
HTML;
}

Добавить комментарий