Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SharedVideo.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /* global $, APP, YT, onPlayerReady, onPlayerStateChange, onPlayerError */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import UIUtil from '../util/UIUtil';
  4. import UIEvents from '../../../service/UI/UIEvents';
  5. import VideoLayout from "../videolayout/VideoLayout";
  6. import LargeContainer from '../videolayout/LargeContainer';
  7. import Filmstrip from '../videolayout/Filmstrip';
  8. import { sendEvent } from '../../../react/features/analytics';
  9. import {
  10. participantJoined,
  11. participantLeft
  12. } from '../../../react/features/base/participants';
  13. import { dockToolbox, showToolbox } from '../../../react/features/toolbox';
  14. import SharedVideoThumb from './SharedVideoThumb';
  15. export const SHARED_VIDEO_CONTAINER_TYPE = "sharedvideo";
  16. /**
  17. * Example shared video link.
  18. * @type {string}
  19. */
  20. const defaultSharedVideoLink = "https://www.youtube.com/watch?v=xNXN7CZk8X0";
  21. const updateInterval = 5000; // milliseconds
  22. /**
  23. * The dialog for user input (video link).
  24. * @type {null}
  25. */
  26. let dialog = null;
  27. /**
  28. * Manager of shared video.
  29. */
  30. export default class SharedVideoManager {
  31. constructor (emitter) {
  32. this.emitter = emitter;
  33. this.isSharedVideoShown = false;
  34. this.isPlayerAPILoaded = false;
  35. this.mutedWithUserInteraction = false;
  36. }
  37. /**
  38. * Indicates if the player volume is currently on. This will return true if
  39. * we have an available player, which is currently in a PLAYING state,
  40. * which isn't muted and has it's volume greater than 0.
  41. *
  42. * @returns {boolean} indicating if the volume of the shared video is
  43. * currently on.
  44. */
  45. isSharedVideoVolumeOn() {
  46. return (this.player
  47. && this.player.getPlayerState() === YT.PlayerState.PLAYING
  48. && !this.player.isMuted()
  49. && this.player.getVolume() > 0);
  50. }
  51. /**
  52. * Indicates if the local user is the owner of the shared video.
  53. * @returns {*|boolean}
  54. */
  55. isSharedVideoOwner() {
  56. return this.from && APP.conference.isLocalId(this.from);
  57. }
  58. /**
  59. * Starts shared video by asking user for url, or if its already working
  60. * asks whether the user wants to stop sharing the video.
  61. */
  62. toggleSharedVideo () {
  63. if (dialog)
  64. return;
  65. if(!this.isSharedVideoShown) {
  66. requestVideoLink().then(
  67. url => {
  68. this.emitter.emit(
  69. UIEvents.UPDATE_SHARED_VIDEO, url, 'start');
  70. sendEvent('sharedvideo.started');
  71. },
  72. err => {
  73. logger.log('SHARED VIDEO CANCELED', err);
  74. sendEvent('sharedvideo.canceled');
  75. }
  76. );
  77. return;
  78. }
  79. if(APP.conference.isLocalId(this.from)) {
  80. showStopVideoPropmpt().then(
  81. () => {
  82. // make sure we stop updates for playing before we send stop
  83. // if we stop it after receiving self presence, we can end
  84. // up sending stop playing, and on the other end it will not
  85. // stop
  86. if (this.intervalId) {
  87. clearInterval(this.intervalId);
  88. this.intervalId = null;
  89. }
  90. this.emitter.emit(
  91. UIEvents.UPDATE_SHARED_VIDEO, this.url, 'stop');
  92. sendEvent('sharedvideo.stoped');
  93. },
  94. () => {});
  95. } else {
  96. dialog = APP.UI.messageHandler.openMessageDialog(
  97. "dialog.shareVideoTitle",
  98. "dialog.alreadySharedVideoMsg",
  99. null,
  100. function () {
  101. dialog = null;
  102. }
  103. );
  104. sendEvent('sharedvideo.alreadyshared');
  105. }
  106. }
  107. /**
  108. * Shows the player component and starts the process that will be sending
  109. * updates, if we are the one shared the video.
  110. *
  111. * @param id the id of the sender of the command
  112. * @param url the video url
  113. * @param attributes
  114. */
  115. onSharedVideoStart (id, url, attributes) {
  116. if (this.isSharedVideoShown)
  117. return;
  118. this.isSharedVideoShown = true;
  119. // the video url
  120. this.url = url;
  121. // the owner of the video
  122. this.from = id;
  123. this.mutedWithUserInteraction = APP.conference.isLocalAudioMuted();
  124. //listen for local audio mute events
  125. this.localAudioMutedListener = this.onLocalAudioMuted.bind(this);
  126. this.emitter.on(UIEvents.AUDIO_MUTED, this.localAudioMutedListener);
  127. // This code loads the IFrame Player API code asynchronously.
  128. var tag = document.createElement('script');
  129. tag.src = "https://www.youtube.com/iframe_api";
  130. var firstScriptTag = document.getElementsByTagName('script')[0];
  131. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  132. // sometimes we receive errors like player not defined
  133. // or player.pauseVideo is not a function
  134. // we need to operate with player after start playing
  135. // self.player will be defined once it start playing
  136. // and will process any initial attributes if any
  137. this.initialAttributes = attributes;
  138. var self = this;
  139. if(self.isPlayerAPILoaded)
  140. window.onYouTubeIframeAPIReady();
  141. else
  142. window.onYouTubeIframeAPIReady = function() {
  143. self.isPlayerAPILoaded = true;
  144. let showControls = APP.conference.isLocalId(self.from) ? 1 : 0;
  145. let p = new YT.Player('sharedVideoIFrame', {
  146. height: '100%',
  147. width: '100%',
  148. videoId: self.url,
  149. playerVars: {
  150. 'origin': location.origin,
  151. 'fs': '0',
  152. 'autoplay': 0,
  153. 'controls': showControls,
  154. 'rel' : 0
  155. },
  156. events: {
  157. 'onReady': onPlayerReady,
  158. 'onStateChange': onPlayerStateChange,
  159. 'onError': onPlayerError
  160. }
  161. });
  162. // add listener for volume changes
  163. p.addEventListener(
  164. "onVolumeChange", "onVolumeChange");
  165. if (APP.conference.isLocalId(self.from)){
  166. // adds progress listener that will be firing events
  167. // while we are paused and we change the progress of the
  168. // video (seeking forward or backward on the video)
  169. p.addEventListener(
  170. "onVideoProgress", "onVideoProgress");
  171. }
  172. };
  173. /**
  174. * Indicates that a change in state has occurred for the shared video.
  175. * @param event the event notifying us of the change
  176. */
  177. window.onPlayerStateChange = function(event) {
  178. if (event.data == YT.PlayerState.PLAYING) {
  179. self.player = event.target;
  180. if(self.initialAttributes)
  181. {
  182. // If a network update has occurred already now is the
  183. // time to process it.
  184. self.processVideoUpdate(
  185. self.player,
  186. self.initialAttributes);
  187. self.initialAttributes = null;
  188. }
  189. self.smartAudioMute();
  190. } else if (event.data == YT.PlayerState.PAUSED) {
  191. self.smartAudioUnmute();
  192. sendEvent('sharedvideo.paused');
  193. }
  194. self.fireSharedVideoEvent(event.data == YT.PlayerState.PAUSED);
  195. };
  196. /**
  197. * Track player progress while paused.
  198. * @param event
  199. */
  200. window.onVideoProgress = function (event) {
  201. let state = event.target.getPlayerState();
  202. if (state == YT.PlayerState.PAUSED) {
  203. self.fireSharedVideoEvent(true);
  204. }
  205. };
  206. /**
  207. * Gets notified for volume state changed.
  208. * @param event
  209. */
  210. window.onVolumeChange = function (event) {
  211. self.fireSharedVideoEvent();
  212. // let's check, if player is not muted lets mute locally
  213. if(event.data.volume > 0 && !event.data.muted) {
  214. self.smartAudioMute();
  215. }
  216. else if (event.data.volume <=0 || event.data.muted) {
  217. self.smartAudioUnmute();
  218. }
  219. sendEvent('sharedvideo.volumechanged');
  220. };
  221. window.onPlayerReady = function(event) {
  222. let player = event.target;
  223. // do not relay on autoplay as it is not sending all of the events
  224. // in onPlayerStateChange
  225. player.playVideo();
  226. let thumb = new SharedVideoThumb(
  227. self.url, SHARED_VIDEO_CONTAINER_TYPE, VideoLayout);
  228. thumb.setDisplayName(player.getVideoData().title);
  229. VideoLayout.addRemoteVideoContainer(self.url, thumb);
  230. let iframe = player.getIframe();
  231. self.sharedVideo = new SharedVideoContainer(
  232. {url, iframe, player});
  233. //prevents pausing participants not sharing the video
  234. // to pause the video
  235. if (!APP.conference.isLocalId(self.from)) {
  236. $("#sharedVideo").css("pointer-events","none");
  237. }
  238. VideoLayout.addLargeVideoContainer(
  239. SHARED_VIDEO_CONTAINER_TYPE, self.sharedVideo);
  240. APP.store.dispatch(participantJoined({
  241. id: self.url,
  242. isBot: true,
  243. name: player.getVideoData().title
  244. }));
  245. VideoLayout.handleVideoThumbClicked(self.url);
  246. // If we are sending the command and we are starting the player
  247. // we need to continuously send the player current time position
  248. if(APP.conference.isLocalId(self.from)) {
  249. self.intervalId = setInterval(
  250. self.fireSharedVideoEvent.bind(self),
  251. updateInterval);
  252. }
  253. };
  254. window.onPlayerError = function(event) {
  255. logger.error("Error in the player:", event.data);
  256. // store the error player, so we can remove it
  257. self.errorInPlayer = event.target;
  258. };
  259. }
  260. /**
  261. * Process attributes, whether player needs to be paused or seek.
  262. * @param player the player to operate over
  263. * @param attributes the attributes with the player state we want
  264. */
  265. processVideoUpdate (player, attributes)
  266. {
  267. if(!attributes)
  268. return;
  269. if (attributes.state == 'playing') {
  270. let isPlayerPaused
  271. = (this.player.getPlayerState() === YT.PlayerState.PAUSED);
  272. // If our player is currently paused force the seek.
  273. this.processTime(player, attributes, isPlayerPaused);
  274. // Process mute.
  275. let isAttrMuted = (attributes.muted === "true");
  276. if (player.isMuted() !== isAttrMuted) {
  277. this.smartPlayerMute(isAttrMuted, true);
  278. }
  279. // Process volume
  280. if (!isAttrMuted
  281. && attributes.volume !== undefined
  282. && player.getVolume() != attributes.volume) {
  283. player.setVolume(attributes.volume);
  284. logger.info("Player change of volume:" + attributes.volume);
  285. this.showSharedVideoMutedPopup(false);
  286. }
  287. if (isPlayerPaused)
  288. player.playVideo();
  289. } else if (attributes.state == 'pause') {
  290. // if its not paused, pause it
  291. player.pauseVideo();
  292. this.processTime(player, attributes, true);
  293. }
  294. }
  295. /**
  296. * Check for time in attributes and if needed seek in current player
  297. * @param player the player to operate over
  298. * @param attributes the attributes with the player state we want
  299. * @param forceSeek whether seek should be forced
  300. */
  301. processTime (player, attributes, forceSeek)
  302. {
  303. if(forceSeek) {
  304. logger.info("Player seekTo:", attributes.time);
  305. player.seekTo(attributes.time);
  306. return;
  307. }
  308. // check received time and current time
  309. let currentPosition = player.getCurrentTime();
  310. let diff = Math.abs(attributes.time - currentPosition);
  311. // if we drift more than the interval for checking
  312. // sync, the interval is in milliseconds
  313. if(diff > updateInterval/1000) {
  314. logger.info("Player seekTo:", attributes.time,
  315. " current time is:", currentPosition, " diff:", diff);
  316. player.seekTo(attributes.time);
  317. }
  318. }
  319. /**
  320. * Checks current state of the player and fire an event with the values.
  321. */
  322. fireSharedVideoEvent(sendPauseEvent)
  323. {
  324. // ignore update checks if we are not the owner of the video
  325. // or there is still no player defined or we are stopped
  326. // (in a process of stopping)
  327. if(!APP.conference.isLocalId(this.from) || !this.player
  328. || !this.isSharedVideoShown)
  329. return;
  330. let state = this.player.getPlayerState();
  331. // if its paused and haven't been pause - send paused
  332. if (state === YT.PlayerState.PAUSED && sendPauseEvent) {
  333. this.emitter.emit(UIEvents.UPDATE_SHARED_VIDEO,
  334. this.url, 'pause', this.player.getCurrentTime());
  335. }
  336. // if its playing and it was paused - send update with time
  337. // if its playing and was playing just send update with time
  338. else if (state === YT.PlayerState.PLAYING) {
  339. this.emitter.emit(UIEvents.UPDATE_SHARED_VIDEO,
  340. this.url, 'playing',
  341. this.player.getCurrentTime(),
  342. this.player.isMuted(),
  343. this.player.getVolume());
  344. }
  345. }
  346. /**
  347. * Updates video, if its not playing and needs starting or
  348. * if its playing and needs to be paysed
  349. * @param id the id of the sender of the command
  350. * @param url the video url
  351. * @param attributes
  352. */
  353. onSharedVideoUpdate (id, url, attributes) {
  354. // if we are sending the event ignore
  355. if(APP.conference.isLocalId(this.from)) {
  356. return;
  357. }
  358. if(!this.isSharedVideoShown) {
  359. this.onSharedVideoStart(id, url, attributes);
  360. return;
  361. }
  362. if(!this.player)
  363. this.initialAttributes = attributes;
  364. else {
  365. this.processVideoUpdate(this.player, attributes);
  366. }
  367. }
  368. /**
  369. * Stop shared video if it is currently showed. If the user started the
  370. * shared video is the one in the id (called when user
  371. * left and we want to remove video if the user sharing it left).
  372. * @param id the id of the sender of the command
  373. */
  374. onSharedVideoStop (id, attributes) {
  375. if (!this.isSharedVideoShown)
  376. return;
  377. if(this.from !== id)
  378. return;
  379. if(!this.player) {
  380. // if there is no error in the player till now,
  381. // store the initial attributes
  382. if (!this.errorInPlayer) {
  383. this.initialAttributes = attributes;
  384. return;
  385. }
  386. }
  387. this.emitter.removeListener(UIEvents.AUDIO_MUTED,
  388. this.localAudioMutedListener);
  389. this.localAudioMutedListener = null;
  390. VideoLayout.removeParticipantContainer(this.url);
  391. VideoLayout.showLargeVideoContainer(SHARED_VIDEO_CONTAINER_TYPE, false)
  392. .then(() => {
  393. VideoLayout.removeLargeVideoContainer(
  394. SHARED_VIDEO_CONTAINER_TYPE);
  395. if(this.player) {
  396. this.player.destroy();
  397. this.player = null;
  398. } // if there is an error in player, remove that instance
  399. else if (this.errorInPlayer) {
  400. this.errorInPlayer.destroy();
  401. this.errorInPlayer = null;
  402. }
  403. this.smartAudioUnmute();
  404. // revert to original behavior (prevents pausing
  405. // for participants not sharing the video to pause it)
  406. $("#sharedVideo").css("pointer-events","auto");
  407. this.emitter.emit(
  408. UIEvents.UPDATE_SHARED_VIDEO, null, 'removed');
  409. });
  410. APP.store.dispatch(participantLeft(this.url));
  411. this.url = null;
  412. this.isSharedVideoShown = false;
  413. this.initialAttributes = null;
  414. }
  415. /**
  416. * Receives events for local audio mute/unmute by local user.
  417. * @param muted boolena whether it is muted or not.
  418. * @param {boolean} indicates if this mute was a result of user interaction,
  419. * i.e. pressing the mute button or it was programatically triggerred
  420. */
  421. onLocalAudioMuted (muted, userInteraction) {
  422. if(!this.player)
  423. return;
  424. if (muted) {
  425. this.mutedWithUserInteraction = userInteraction;
  426. }
  427. else if (this.player.getPlayerState() !== YT.PlayerState.PAUSED) {
  428. this.smartPlayerMute(true, false);
  429. // Check if we need to update other participants
  430. this.fireSharedVideoEvent();
  431. }
  432. }
  433. /**
  434. * Mutes / unmutes the player.
  435. * @param mute true to mute the shared video, false - otherwise.
  436. * @param {boolean} Indicates if this mute is a consequence of a network
  437. * video update or is called locally.
  438. */
  439. smartPlayerMute(mute, isVideoUpdate) {
  440. if (!this.player.isMuted() && mute) {
  441. this.player.mute();
  442. if (isVideoUpdate)
  443. this.smartAudioUnmute();
  444. }
  445. else if (this.player.isMuted() && !mute) {
  446. this.player.unMute();
  447. if (isVideoUpdate)
  448. this.smartAudioMute();
  449. }
  450. this.showSharedVideoMutedPopup(mute);
  451. }
  452. /**
  453. * Smart mike unmute. If the mike is currently muted and it wasn't muted
  454. * by the user via the mike button and the volume of the shared video is on
  455. * we're unmuting the mike automatically.
  456. */
  457. smartAudioUnmute() {
  458. if (APP.conference.isLocalAudioMuted()
  459. && !this.mutedWithUserInteraction
  460. && !this.isSharedVideoVolumeOn()) {
  461. sendEvent("sharedvideo.audio.unmuted");
  462. logger.log('Shared video: audio unmuted');
  463. this.emitter.emit(UIEvents.AUDIO_MUTED, false, false);
  464. this.showMicMutedPopup(false);
  465. }
  466. }
  467. /**
  468. * Smart mike mute. If the mike isn't currently muted and the shared video
  469. * volume is on we mute the mike.
  470. */
  471. smartAudioMute() {
  472. if (!APP.conference.isLocalAudioMuted()
  473. && this.isSharedVideoVolumeOn()) {
  474. sendEvent("sharedvideo.audio.muted");
  475. logger.log('Shared video: audio muted');
  476. this.emitter.emit(UIEvents.AUDIO_MUTED, true, false);
  477. this.showMicMutedPopup(true);
  478. }
  479. }
  480. /**
  481. * Shows a popup under the microphone toolbar icon that notifies the user
  482. * of automatic mute after a shared video has started.
  483. * @param show boolean, show or hide the notification
  484. */
  485. showMicMutedPopup (show) {
  486. if(show)
  487. this.showSharedVideoMutedPopup(false);
  488. APP.UI.showCustomToolbarPopup(
  489. 'microphone', 'micMutedPopup', show, 5000);
  490. }
  491. /**
  492. * Shows a popup under the shared video toolbar icon that notifies the user
  493. * of automatic mute of the shared video after the user has unmuted their
  494. * mic.
  495. * @param show boolean, show or hide the notification
  496. */
  497. showSharedVideoMutedPopup (show) {
  498. if(show)
  499. this.showMicMutedPopup(false);
  500. APP.UI.showCustomToolbarPopup(
  501. 'sharedvideo', 'sharedVideoMutedPopup', show, 5000);
  502. }
  503. }
  504. /**
  505. * Container for shared video iframe.
  506. */
  507. class SharedVideoContainer extends LargeContainer {
  508. constructor ({url, iframe, player}) {
  509. super();
  510. this.$iframe = $(iframe);
  511. this.url = url;
  512. this.player = player;
  513. }
  514. show () {
  515. let self = this;
  516. return new Promise(resolve => {
  517. this.$iframe.fadeIn(300, () => {
  518. self.bodyBackground = document.body.style.background;
  519. document.body.style.background = 'black';
  520. this.$iframe.css({opacity: 1});
  521. APP.store.dispatch(dockToolbox(true));
  522. resolve();
  523. });
  524. });
  525. }
  526. hide () {
  527. let self = this;
  528. APP.store.dispatch(dockToolbox(false));
  529. return new Promise(resolve => {
  530. this.$iframe.fadeOut(300, () => {
  531. document.body.style.background = self.bodyBackground;
  532. this.$iframe.css({opacity: 0});
  533. resolve();
  534. });
  535. });
  536. }
  537. onHoverIn () {
  538. APP.store.dispatch(showToolbox());
  539. }
  540. get id () {
  541. return this.url;
  542. }
  543. resize (containerWidth, containerHeight) {
  544. let height = containerHeight - Filmstrip.getFilmstripHeight();
  545. let width = containerWidth;
  546. this.$iframe.width(width).height(height);
  547. }
  548. /**
  549. * @return {boolean} do not switch on dominant speaker event if on stage.
  550. */
  551. stayOnStage () {
  552. return false;
  553. }
  554. }
  555. /**
  556. * Checks if given string is youtube url.
  557. * @param {string} url string to check.
  558. * @returns {boolean}
  559. */
  560. function getYoutubeLink(url) {
  561. let p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;//jshint ignore:line
  562. return (url.match(p)) ? RegExp.$1 : false;
  563. }
  564. /**
  565. * Ask user if he want to close shared video.
  566. */
  567. function showStopVideoPropmpt() {
  568. return new Promise(function (resolve, reject) {
  569. let submitFunction = function(e,v) {
  570. if (v) {
  571. resolve();
  572. } else {
  573. reject();
  574. }
  575. };
  576. let closeFunction = function () {
  577. dialog = null;
  578. };
  579. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  580. titleKey: "dialog.removeSharedVideoTitle",
  581. msgKey: "dialog.removeSharedVideoMsg",
  582. leftButtonKey: "dialog.Remove",
  583. submitFunction,
  584. closeFunction
  585. });
  586. });
  587. }
  588. /**
  589. * Ask user for shared video url to share with others.
  590. * Dialog validates client input to allow only youtube urls.
  591. */
  592. function requestVideoLink() {
  593. let i18n = APP.translation;
  594. const cancelButton = i18n.generateTranslationHTML("dialog.Cancel");
  595. const shareButton = i18n.generateTranslationHTML("dialog.Share");
  596. const backButton = i18n.generateTranslationHTML("dialog.Back");
  597. const linkError
  598. = i18n.generateTranslationHTML("dialog.shareVideoLinkError");
  599. return new Promise(function (resolve, reject) {
  600. dialog = APP.UI.messageHandler.openDialogWithStates({
  601. state0: {
  602. titleKey: "dialog.shareVideoTitle",
  603. html: `
  604. <input name="sharedVideoUrl" type="text"
  605. class="input-control"
  606. data-i18n="[placeholder]defaultLink"
  607. autofocus>`,
  608. persistent: false,
  609. buttons: [
  610. {title: cancelButton, value: false},
  611. {title: shareButton, value: true}
  612. ],
  613. focus: ':input:first',
  614. defaultButton: 1,
  615. submit: function (e, v, m, f) {
  616. e.preventDefault();
  617. if (!v) {
  618. reject('cancelled');
  619. dialog.close();
  620. return;
  621. }
  622. let sharedVideoUrl = f.sharedVideoUrl;
  623. if (!sharedVideoUrl) {
  624. return;
  625. }
  626. let urlValue = encodeURI(UIUtil.escapeHtml(sharedVideoUrl));
  627. let yVideoId = getYoutubeLink(urlValue);
  628. if (!yVideoId) {
  629. dialog.goToState('state1');
  630. return false;
  631. }
  632. resolve(yVideoId);
  633. dialog.close();
  634. }
  635. },
  636. state1: {
  637. titleKey: "dialog.shareVideoTitle",
  638. html: linkError,
  639. persistent: false,
  640. buttons: [
  641. {title: cancelButton, value: false},
  642. {title: backButton, value: true}
  643. ],
  644. focus: ':input:first',
  645. defaultButton: 1,
  646. submit: function (e, v) {
  647. e.preventDefault();
  648. if (v === 0) {
  649. reject();
  650. dialog.close();
  651. } else {
  652. dialog.goToState('state0');
  653. }
  654. }
  655. }
  656. }, {
  657. close: function () {
  658. dialog = null;
  659. }
  660. }, {
  661. url: defaultSharedVideoLink
  662. });
  663. });
  664. }