1212 */
1313class Youtube extends \yii \base \Widget
1414{
15+ const ID_JS = 'YoutubeAPIReady ' ;
16+ const POS_JS = View::POS_HEAD ;
17+ const START_JS = "function onYouTubePlayerAPIReady() { " ;
1518 /**
1619 * @link https://developers.google.com/youtube/iframe_api_reference?hl=ru
1720 * шаблон скпирта
1821 */
1922 const JS_SCRIPT = <<<JS
20- var __player_id__;
21- function onYouTubeIframeAPIReady() {
22- __player_id__ = new YT.Player('__div_id__', {
23+ __player_id__ = new YT.Player('__div_id__', {
2324 height: '%s',
2425 width: '%s',
2526 videoId: '%s',
2627 playerVars:%s,
2728 /*events*/%s
2829 });
29- }
3030JS ;
3131
3232 /**
@@ -98,6 +98,10 @@ function onYouTubeIframeAPIReady() {
9898 * @var bool|string $_videoId youtube video id
9999 */
100100 protected $ _videoId = false ;
101+ /**
102+ * @var string
103+ */
104+ private $ _playerId ;
101105
102106 /**
103107 * получить id video (проверка являеться ли ссылкой или сразу уже айди)
@@ -115,6 +119,7 @@ public function init()
115119 {
116120 parent ::init ();
117121 $ this ->_videoId = $ this ->_getVideoId ();
122+ $ this ->_playerId = 'player_ ' .$ this ->id ;
118123 }
119124
120125 public function run ()
@@ -128,6 +133,23 @@ public function run()
128133 return $ this ->_runWidget ();
129134 }
130135
136+ /*
137+ * Merge script for many players
138+ */
139+ protected function addJs ($ js ) {
140+ $ script = $ this ->view ->js [self ::POS_JS ][self ::ID_JS ];
141+ $ new_script = str_replace (self ::START_JS , self ::START_JS . ' ' . $ js , $ script );
142+ return $ new_script ;
143+ }
144+ /*
145+ * check setting
146+ */
147+ protected function checkSettings () {
148+ if (!$ this ->video ){
149+ throw new InvalidParamException ('Empty video indentificator. ' );
150+ }
151+ }
152+
131153 /*
132154 * подгрузка настроек
133155 */
@@ -160,7 +182,7 @@ protected function _registerJs() {
160182 $ _eventsStr .= '} ' ;
161183 }
162184
163- $ _playerId = ' player_ ' . $ this ->id ;
185+ $ _playerId = $ this ->_playerId ;
164186
165187 $ _script = sprintf (
166188 self ::JS_SCRIPT ,
@@ -175,15 +197,27 @@ protected function _registerJs() {
175197 $ _script = str_replace ('__div_id__ ' , 'div_ ' .$ this ->id , $ _script );
176198
177199 $ view = $ this ->getView ();
178- $ view ->registerJs ($ _script , View::POS_HEAD );
200+
201+ $ script = '' ;
202+ if (!isset ($ this ->view ->js [self ::POS_JS ][self ::ID_JS ])) {
203+ $ script .= self ::START_JS . $ _script ;
204+ $ script .= "} " ;
205+ } else {
206+ $ script = $ this ->addJs ($ _script );
207+ }
208+ $ view ->registerJs ($ script , View::POS_HEAD , self ::ID_JS );
179209 }
180210 /**
181211 * Вывод виджета
182212 * @return string html widget
183213 */
184214 protected function _runWidget () {
185- $ this ->_registerJs ();
186215
216+ $ js = "var " . $ this ->_playerId ."; " ;
217+ $ this ->getView ()->registerJs ($ js , View::POS_HEAD );
218+
219+ $ this ->_registerJs ();
220+
187221 $ html =
188222 Html::tag ('div ' ,
189223 Html::tag ('div ' , '' , ArrayHelper::merge (
0 commit comments