Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
embed.php
1<?php
2namespace Bitrix\Landing\Node;
3
6
8{
9 protected const RATIO_CLASSES = [
10 'embed-responsive-16by9',
11 'embed-responsive-9by16',
12 'embed-responsive-4by3',
13 'embed-responsive-3by4',
14 'embed-responsive-21by9',
15 'embed-responsive-9by21',
16 'embed-responsive-1by1',
17 ];
18
19 protected const CONTAINER_CLASS = 'embed-responsive';
20
25 public static function getHandlerJS()
26 {
27 return 'BX.Landing.Node.Embed';
28 }
29
37 public static function saveNode(\Bitrix\Landing\Block $block, $selector, array $data)
38 {
39 $doc = $block->getDom();
40 $resultList = $doc->querySelectorAll($selector);
41 $valueBefore = Embed::getNode($block, $selector);
42
43 foreach ($data as $pos => $value)
44 {
45 if (isset($resultList[$pos]))
46 {
47 if (isset($value['src']) && $value['src'])
48 {
49 if ($resultList[$pos]->getTagName() === 'IFRAME')
50 {
51 $resultList[$pos]->setAttribute('src', $value['src']);
52 }
53 else
54 {
55 $resultList[$pos]->setAttribute('data-src', $value['src']);
56 }
57 }
58
59 if (isset($value['source']) && $value['source'])
60 {
61 $resultList[$pos]->setAttribute('data-source', $value['source']);
62 }
63
64 // set ratio
65 if (
66 isset($value['ratio'], $value['src'])
67 && $value['ratio'] && $value['src']
68 && $valueBefore['src'] !== $value['src']
69 && in_array($value['ratio'], self::RATIO_CLASSES)
70 )
71 {
72 $containerNode = $resultList[$pos]->getParentNode();
73 if ($containerNode && in_array(self::CONTAINER_CLASS, $containerNode->getClassList()))
74 {
75 $classes = $containerNode->getClassList();
76 $classes = array_diff($classes, self::RATIO_CLASSES);
77 $classes[] = $value['ratio'];
78 $containerNode->setClassList($classes);
79 }
80 }
81
82 // set preview image
83 $styles = [];
84 foreach (StyleInliner::getStyle($resultList[$pos]) as $key => $stylesValue)
85 {
86 if ($key !== 'background' && $key !== 'background-image')
87 {
88 $styles[] = "{$key}: {$stylesValue};";
89 }
90 }
91 if (isset($value['preview']) && $value['preview'])
92 {
93 $styles[] = "background-image: url('{$value['preview']}');";
94 $resultList[$pos]->setAttribute('data-preview', $value['preview']);
95 }
96 else
97 {
98 $resultList[$pos]->removeAttribute('data-preview');
99 }
100 $styles = implode(' ', $styles);
101 $resultList[$pos]->setAttribute('style', $styles);
102 }
103
104 if (History::isActive())
105 {
106 $history = new History($block->getLandingId(), History::ENTITY_TYPE_LANDING);
107 $history->push('EDIT_EMBED', [
108 'block' => $block,
109 'selector' => $selector,
110 'position' => (int)$pos,
111 'valueBefore' => $valueBefore[$pos],
112 'valueAfter' => $value,
113 ]);
114 }
115 }
116 }
117
124 public static function getNode(\Bitrix\Landing\Block $block, $selector)
125 {
126 $data = array();
127 $doc = $block->getDom();
128 $resultList = $doc->querySelectorAll($selector);
129
130 foreach ($resultList as $pos => $res)
131 {
132 $ratio = '';
133 $containerNode = $res->getParentNode();
134 if ($containerNode && in_array(self::CONTAINER_CLASS, $containerNode->getClassList()))
135 {
136 $ratio = array_intersect($containerNode->getCLassList(), self::RATIO_CLASSES);
137 $ratio = empty($ratio) ? '' : array_shift($ratio);
138 }
139
140 $data[$pos] = array(
141 'src' => $res->getAttribute('data-src') ?: $res->getAttribute('src'),
142 'source' => $res->getAttribute('data-source'),
143 'preview' => $res->getAttribute('data-preview'),
144 'ratio' => $ratio,
145 );
146 }
147
148 return $data;
149 }
150}
static getNode(\Bitrix\Landing\Block $block, $selector)
Definition embed.php:124
static saveNode(\Bitrix\Landing\Block $block, $selector, array $data)
Definition embed.php:37