moe-video.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /** moe-video enabler component **/
  2. (function() {
  3. function hasResponseError(_data) {
  4. let msg = 'Unknown error!';
  5. if (_data) {
  6. if (_data.success) return false;
  7. else if (_data.message) msg = _data.message;
  8. }
  9. toastr.error(msg);
  10. return true;
  11. }
  12. // state
  13. let moeOTSession = false;
  14. let moePublisher = false;
  15. let moeVideoElement = false;
  16. let moeArchiveID = false;
  17. let uploadOrRecord = 'record';
  18. // initialize elements, events
  19. function init(_container = false) {
  20. $('[moe-video-progress]').remove();
  21. $('<div/>')
  22. .attr('moe-video-progress', 1)
  23. .append('<img src="/img/loading.svg">')
  24. .appendTo('body');
  25. _container = $(_container || 'body');
  26. _container
  27. .find('[moe-video]:not([moe-initialized])')
  28. .each(function() {
  29. // assign a unique id
  30. let moeID = Math.ceil(Math.random() * 1000000);
  31. $(this).attr('moe-id', moeID);
  32. var buttonsDiv = $('<div class="d-flex flex-wrap" />').appendTo(this);
  33. // record button
  34. $('<button type="button" />')
  35. .addClass('btn btn-info ' + ($(this).attr('custom-handler') ? '' : 'btn-sm ml-2'))
  36. .attr('moe-show-popup', 1)
  37. .html($(this).attr('custom-handler') ? 'Upload or Record Video' : '<i class="fa fa-video"></i>')
  38. .appendTo(buttonsDiv);
  39. // mark as initialized
  40. $(this).attr('moe-initialized', 1);
  41. });
  42. // bind events
  43. $(document).off('click.moe-show-popup', '[moe-video] [moe-show-popup]')
  44. $(document).on('click.moe-show-popup', '[moe-video] [moe-show-popup]', function() {
  45. initVideoPopup();
  46. moeVideoElement = $(this).closest('[moe-video]');
  47. let title = moeVideoElement.find('p').first().text();
  48. if(!title) {
  49. title = moeVideoElement.attr('data-title');
  50. }
  51. $('[moe-video-backdrop]')
  52. .css('display', 'flex')
  53. .find('.card-title').first().text(title);
  54. });
  55. $(document).off('change.moe-video-file', '[moe-video-file]')
  56. $(document).on('change.moe-video-file', '[moe-video-file]', function() {
  57. // check if video
  58. let file = this.files[0];
  59. if(file.type !== 'video/mp4' && file.type !== 'video/mpeg' && file.type !== 'video/webm') {
  60. toastr.error('Invalid video file!');
  61. return false;
  62. }
  63. let blobURL = URL.createObjectURL(file);
  64. $('[moe-video-player]')
  65. .attr('src', blobURL)
  66. .removeClass('d-none')
  67. .addClass('d-block');
  68. $('.upload-or-record')
  69. .removeClass('d-block')
  70. .addClass('d-none');
  71. $('[moe-play-recording]').show();
  72. $('[moe-submit-recording]').show();
  73. $('[moe-close-popup]').show();
  74. uploadOrRecord = 'upload';
  75. return false;
  76. });
  77. $(document).off('click.moe-choose-record', '[moe-choose-record]')
  78. $(document).on('click.moe-choose-record', '[moe-choose-record]', function() {
  79. initCamera(moeVideoElement);
  80. uploadOrRecord = 'record';
  81. return false;
  82. });
  83. $(document).off('click.moe-start-recording', '[moe-video-backdrop] [moe-start-recording]')
  84. $(document).on('click.moe-start-recording', '[moe-video-backdrop] [moe-start-recording]', function() {
  85. startWait();
  86. $('[moe-video-player]')
  87. .attr('src', '')
  88. .removeClass('d-block')
  89. .addClass('d-none');
  90. $('[moe-video-canvas]')
  91. .removeClass('d-none')
  92. .addClass('d-block');
  93. $.post('/start/' + moeVideoElement.attr('moe-ot-session-id'));
  94. });
  95. $(document).off('click.moe-stop-recording', '[moe-video-backdrop] [moe-stop-recording]')
  96. $(document).on('click.moe-stop-recording', '[moe-video-backdrop] [moe-stop-recording]', function() {
  97. startWait();
  98. $.post('/stop/' + moeArchiveID).done(function () {
  99. $.ajax('/archive/'+ moeArchiveID, {
  100. success: function (data) { // success callback function
  101. stopWait();
  102. $('[moe-video-player]')
  103. .attr('src', data.video)
  104. .removeClass('d-none')
  105. .addClass('d-block');
  106. $('[moe-video-canvas]')
  107. .removeClass('d-block')
  108. .addClass('d-none');
  109. $('[moe-start-recording]').show();
  110. $('[moe-stop-recording]').hide();
  111. $('[moe-play-recording]').show();
  112. $('[moe-restart-recording]').hide();
  113. $('[moe-submit-recording]').show();
  114. $('[moe-close-popup]').show();
  115. }
  116. });
  117. });
  118. });
  119. $(document).off('click.moe-play-recording', '[moe-video-backdrop] [moe-play-recording]')
  120. $(document).on('click.moe-play-recording', '[moe-video-backdrop] [moe-play-recording]', function() {
  121. $('[moe-video-player]')[0].play();
  122. });
  123. $(document).off('click.moe-submit-recording', '[moe-video-backdrop] [moe-submit-recording]')
  124. $(document).on('click.moe-submit-recording', '[moe-video-backdrop] [moe-submit-recording]', function() {
  125. //TODO: remove closest('body')
  126. var toProUid = $('[moe-submit-recording]').closest('body').find('[to-pro-uid]').attr('to-pro-uid');
  127. var regardingCompanyUid = $('[moe-submit-recording]').closest('body').find('[regarding-company-uid]').attr('regarding-company-uid');
  128. var regardingClientUid = $('[moe-submit-recording]').closest('body').find('[regarding-client-uid]').attr('regarding-client-uid');
  129. var customHandler = $('[moe-submit-recording]').closest('body').find('[custom-handler]').attr('custom-handler');
  130. var proUid = $('[moe-submit-recording]').closest('body').find('[pro-uid]').attr('pro-uid');
  131. if(uploadOrRecord === 'upload') {
  132. startWait();
  133. let formData = new FormData();
  134. formData.set('regardingCompanyUid', regardingCompanyUid);
  135. formData.set('regardingClientUid', regardingClientUid);
  136. formData.set('contentText', '');
  137. let videoInput = $('[moe-submit-recording]').closest('body').find('[moe-video-file]').first()[0];
  138. formData.append('messageVideo', videoInput.files[0]);
  139. $.ajax('/api/internalMessage/create', {
  140. dataType: 'json',
  141. data: formData,
  142. processData: false,
  143. contentType: false,
  144. type: 'POST',
  145. }).done(function (_data) {
  146. if(!hasResponseError(_data)) {
  147. window.top.location.reload();
  148. }
  149. });
  150. return false;
  151. }
  152. else if(uploadOrRecord === 'record') {
  153. startWait();
  154. $.post('/save-archive', {
  155. archiveID: moeArchiveID,
  156. toProUid: toProUid,
  157. regardingCompanyUid: regardingCompanyUid,
  158. regardingClientUid: regardingClientUid,
  159. contentText: '',
  160. }, function(_data) {
  161. stopWait();
  162. if(!!moeOTSession) {
  163. moeOTSession.disconnect();
  164. moeOTSession = false;
  165. moePublisher = false;
  166. }
  167. // $('[moe-video-backdrop]').css('display', 'none');
  168. window.top.location.reload();
  169. }, 'json');
  170. }
  171. return false;
  172. });
  173. $(document).off('click.moe-close-popup', '[moe-video-backdrop] [moe-close-popup]')
  174. $(document).on('click.moe-close-popup', '[moe-video-backdrop] [moe-close-popup]', function() {
  175. if(!!moeOTSession) {
  176. moeOTSession.disconnect();
  177. moeOTSession = false;
  178. moePublisher = false;
  179. }
  180. $('[moe-video-backdrop]').css('display', 'none');
  181. });
  182. $(document).off('change.input-file', '[moe-video] .input-file')
  183. $(document).on('change.input-file', '[moe-video] .input-file', function() {
  184. $(this).closest('form').submit();
  185. });
  186. }
  187. function initVideoPopup() {
  188. $('[moe-video-backdrop]').remove();
  189. $('<div moe-video-backdrop>' +
  190. '<div class="card mcp-theme-1">' +
  191. '<div class="card-header py-2 d-flex align-items-baseline">' +
  192. '<div class="card-title text-left flex-grow-1"></div>' +
  193. '<a href="#" class="text-secondary ml-auto" moe-close-popup><i class="fa fa-times-circle"></i></a>' +
  194. '</div>' +
  195. '<div class="card-body">' +
  196. '<div class="upload-or-record">' +
  197. '<div class="d-flex align-items-center justify-content-center">' +
  198. '<button type="button" class="btn btn-primary font-weight-bold mr-3 position-relative overflow-hidden" moe-choose-upload>' +
  199. '<i class="fa fa-file-upload mr-2"></i>Upload' +
  200. '<input type="file" moe-video-file class="position-absolute opacity-0x" style="bottom: 0; right: 0">' +
  201. '</button>' +
  202. '<button type="button" class="btn btn-primary font-weight-bold" moe-choose-record>' +
  203. '<i class="fa fa-video mr-2"></i>Record' +
  204. '</button>' +
  205. '</div>' +
  206. '</div>' +
  207. '<div class="access-information d-none">' +
  208. '<p>Setting up camera and microphone access...</p>' +
  209. '<p class="click-allow d-none">Click "Allow" to allow hardware access and begin the recording</p>' +
  210. '<div class="denied d-none">' +
  211. '<p class="text-danger"><b>It appears we do not have access to your input hardware.</b></p>' +
  212. '<p class="font-weight-normal">Please allow access to your hardware by clicking the ' +
  213. '<img class="border border-dark" src="/img/denied.png" alt=""> icon ' +
  214. 'towards the far right of the browser address bar. <b>Once done, please click <a href="">here</a> to refresh the page and retry.</b></p>' +
  215. '</div>' +
  216. '</div>' +
  217. '<div moe-video-canvas class="d-none"></div>' +
  218. '<video moe-video-player class="d-none" controls><source src="" type="video/mp4"></video>' +
  219. '</div>' +
  220. '<div class="card-footer">' +
  221. '<div class="d-flex align-items-center justify-content-center">' +
  222. '<button type="button" class="btn btn-danger btn-sm mr-3" moe-start-recording style="display: none">' +
  223. '<i class="fa fa-video mr-2"></i>Record' +
  224. '</button>' +
  225. '<button type="button" class="btn btn-danger btn-sm mr-3" moe-stop-recording style="display: none">' +
  226. '<i class="fa fa-stop mr-2"></i>Stop' +
  227. '</button>' +
  228. '<button type="button" class="btn btn-info btn-sm mr-3" moe-play-recording style="display: none">' +
  229. '<i class="fa fa-play mr-2"></i>Play' +
  230. '</button>' +
  231. '<button type="button" class="btn btn-info btn-sm mr-3" moe-restart-recording style="display: none">' +
  232. '<i class="fa fa-play mr-2"></i>Play' +
  233. '</button>' +
  234. '<button type="button" class="btn btn-success btn-sm mr-3" moe-submit-recording style="display: none">' +
  235. '<i class="fa fa-check mr-2"></i>Submit' +
  236. '</button>' +
  237. '<button type="button" class="btn btn-default bg-light border border-default btn-sm mr-3" moe-close-popup style="display: none">' +
  238. '<i class="fa fa-times mr-2"></i>Close' +
  239. '</button>' +
  240. '<span moe-wait>Please wait...</span>' +
  241. '</div>' +
  242. '</div>' +
  243. '</div>' +
  244. '</div>').appendTo('body');
  245. }
  246. function startWait() {
  247. $('[moe-start-recording]').hide();
  248. $('[moe-stop-recording]').hide();
  249. $('[moe-play-recording]').hide();
  250. $('[moe-restart-recording]').hide();
  251. $('[moe-submit-recording]').hide();
  252. $('[moe-close-popup]').hide();
  253. $('[moe-wait]').show();
  254. $('[moe-video-progress]').removeClass('d-none').addClass('d-flex');
  255. }
  256. function stopWait() {
  257. $('[moe-wait]').hide();
  258. $('[moe-video-progress]').removeClass('d-flex').addClass('d-none');
  259. }
  260. // init ot, publisher and show cam feed
  261. function initCamera(_moeVideoElement) {
  262. startWait();
  263. $('.upload-or-record').removeClass('d-block').addClass('d-none');
  264. $('.access-information').removeClass('d-none').addClass('d-block');
  265. // cleanup
  266. if(!!moeOTSession) {
  267. moeOTSession.disconnect();
  268. moeOTSession = false;
  269. moePublisher = false;
  270. }
  271. // create session (using sessionID given by the server)
  272. moeOTSession = OT.initSession(
  273. _moeVideoElement.attr('moe-ot-api-key'),
  274. _moeVideoElement.attr('moe-ot-session-id')
  275. );
  276. // init publisher
  277. moePublisher = OT.initPublisher(
  278. $('[moe-video-canvas]').first()[0],
  279. {
  280. //videoSource: 'screen', // comment out in prod
  281. width: 540
  282. }
  283. );
  284. // publisher events
  285. moePublisher.on({
  286. accessAllowed: function (event) {
  287. // The user has granted access to the camera and mic.
  288. console.log('ALIX: accessAllowed');
  289. $('.access-information').removeClass('d-block').addClass('d-none');
  290. $('[moe-video-canvas]').removeClass('d-none').addClass('d-block');
  291. $('[moe-start-recording]').show();
  292. $('[moe-close-popup]').show();
  293. stopWait()
  294. },
  295. accessDenied: function accessDeniedHandler(event) {
  296. // The user has denied access to the camera and mic.
  297. console.log('ALIX: accessDenied');
  298. $('.click-allow').addClass('d-none');
  299. $('.denied').removeClass('d-none');
  300. stopWait()
  301. },
  302. accessDialogOpened: function (event) {
  303. // The Allow/Deny dialog box is opened.
  304. console.log('ALIX: accessDialogOpened');
  305. $('.click-allow').removeClass('d-none');
  306. stopWait()
  307. },
  308. accessDialogClosed: function (event) {
  309. // The Allow/Deny dialog box is closed.
  310. console.log('ALIX: accessDialogClosed');
  311. stopWait()
  312. }
  313. });
  314. // OT events
  315. moeOTSession.connect(_moeVideoElement.attr('moe-ot-client-token'), function(error) {
  316. if (error) {
  317. console.error('Failed to connect', error);
  318. } else {
  319. moeOTSession.publish(moePublisher, function(error) {
  320. if (error) {
  321. console.error('Failed to publish', error);
  322. }
  323. else {
  324. stopWait();
  325. $('[moe-start-recording]').show();
  326. $('[moe-stop-recording]').hide();
  327. $('[moe-play-recording]').hide();
  328. $('[moe-restart-recording]').hide();
  329. $('[moe-submit-recording]').hide();
  330. $('[moe-close-popup]').show();
  331. }
  332. });
  333. }
  334. });
  335. moeOTSession.on('archiveStarted', function(event) {
  336. stopWait();
  337. moeArchiveID = event.id;
  338. $('[moe-start-recording]').hide();
  339. $('[moe-stop-recording]').show();
  340. $('[moe-play-recording]').hide();
  341. $('[moe-restart-recording]').hide();
  342. $('[moe-submit-recording]').hide();
  343. $('[moe-close-popup]').hide();
  344. });
  345. moeOTSession.on('archiveStopped', function() {
  346. // moeArchiveID = false;
  347. // stopWait();
  348. // $('[moe-start-recording]').show();
  349. // $('[moe-stop-recording]').hide();
  350. // $('[moe-play-recording]').hide();
  351. // $('[moe-restart-recording]').hide();
  352. // $('[moe-submit-recording]').hide();
  353. // $('[moe-close-popup]').show();
  354. });
  355. }
  356. $(document).ready(function() {
  357. init();
  358. });
  359. })();