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 26KB

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