您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SharedVideo.js 26KB

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