You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SharedVideo.js 25KB

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