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

SharedVideo.js 26KB

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