share.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. var STOPPED = 0
  4. var RECORDING = 1
  5. var PLAYING = 2
  6. var RECORD_COUNTDOWN = 3
  7. var RECORD_LENGTH = 60 * 3
  8. JqueryClass('shareBox', {
  9. init: function (options) {
  10. var self = $(this)
  11. options = $.extend({
  12. recordStart: function (callback) {
  13. callback()
  14. },
  15. recordStop: function (callback) {
  16. callback()
  17. },
  18. playStart: function (startCallback, stopCallback) {
  19. startCallback();
  20. setTimeout(stopCallback, 3000)
  21. },
  22. playStop: function (callback) {
  23. callback()
  24. },
  25. recordDownload: function (callback) {
  26. callback({})
  27. },
  28. recordReset: function (callback) {
  29. callback()
  30. },
  31. share: function (data, callback) {
  32. callback({ok:true})
  33. },
  34. waitForScreenshot: function (generate, bundlepath, callback) {
  35. callback(true)
  36. },
  37. }, options)
  38. self.data(options)
  39. self.data('bundlepath', '')
  40. self.data('recordedData', null)
  41. self.data('recordTimeout', null)
  42. self.data('status', STOPPED)
  43. self.data('step', 0)
  44. self.data('screenshotDone', false)
  45. self.find('#record-rec').click(function () {
  46. if (! $(this).hasClass("disabled")) {
  47. self.shareBox('recordStartCountdown');
  48. }
  49. return false
  50. })
  51. self.find('#record-stop').click(function () {
  52. self.shareBox('recordStop');
  53. return false
  54. })
  55. self.find('#record-play').click(function () {
  56. self.shareBox('recordPlay');
  57. return false
  58. })
  59. self.find('#record-play-stop').click(function () {
  60. self.shareBox('recordStop');
  61. return false
  62. })
  63. self.find('#record-again').click(function () {
  64. self.shareBox('recordStartCountdown');
  65. return false
  66. })
  67. self.find('#record-delete').click(function () {
  68. self.shareBox('recordDelete');
  69. return false
  70. })
  71. self.find('#record-cancel').click(function () {
  72. self.shareBox('close');
  73. return false
  74. })
  75. self.find('.js-cancel').click(function () {
  76. self.shareBox('close');
  77. return false
  78. })
  79. self.find('#record-share').click(function () {
  80. self.shareBox('share');
  81. return false
  82. })
  83. self.find('#share-window-url-btn').click(function () {
  84. self.find('#share-window-url').select()
  85. var ok
  86. try {
  87. ok = document.execCommand('copy')
  88. } catch (err) {
  89. ok = false
  90. }
  91. if (ok) {
  92. self.find('#share-tooltip').css('opacity',1).find('.tooltip-inner').html('Copied to clipboard')
  93. } else {
  94. console.log('Unable to copy to clipboard.')
  95. self.find('#share-tooltip').css('opacity',1).find('.tooltip-inner').html('Press Ctrl/Cmd + C to copy')
  96. }
  97. setTimeout(function() {
  98. self.find('#share-tooltip').css('opacity',0)
  99. }, 2000)
  100. })
  101. $('body').keydown(function (e) {
  102. if (e.keyCode == 27)
  103. self.shareBox('close')
  104. })
  105. },
  106. showStep: function (step) {
  107. var self = $(this)
  108. self.data('step', step)
  109. for (var i = 1; i < 5; i++) {
  110. if (i == step)
  111. self.find('#record-step-' + i).show()
  112. else
  113. self.find('#record-step-' + i).hide()
  114. }
  115. var button = self.find('#record-share')
  116. if (step == 1) {
  117. button.text('Just share').attr('disabled', !self.data('screenshotDone'))
  118. } else {
  119. button.text('Share')
  120. if (step == 4)
  121. button.attr('disabled', !self.data('screenshotDone'))
  122. else
  123. button.attr('disabled', true)
  124. }
  125. },
  126. recordStartCountdown: function () {
  127. var self = $(this)
  128. var status = self.data('status')
  129. var start = function () {
  130. self.data('recordedData', null)
  131. self.shareBox('recordCountdown', RECORD_COUNTDOWN)
  132. }
  133. if (status == STOPPED) {
  134. start()
  135. } else if (status == PLAYING) {
  136. self.shareBox('recordStop', start)
  137. }
  138. },
  139. recordCountdown: function (secs) {
  140. var self = $(this)
  141. self.shareBox('showStep', 2)
  142. if (secs == 0) {
  143. self.data('status', RECORDING)
  144. self.data('recordStart')(function () {
  145. self.shareBox('recordStopCountdown', RECORD_LENGTH)
  146. })
  147. return
  148. }
  149. self.find('#record-countdown').text(secs)
  150. var recordTimeout = setTimeout(function () {
  151. self.shareBox('recordCountdown', secs - 1)
  152. }, 1000)
  153. self.data('recordTimeout', recordTimeout)
  154. },
  155. recordStopCountdown: function (secs) {
  156. var self = $(this)
  157. self.data('stopTimeout', null)
  158. if (secs == 0)
  159. return self.shareBox('recordStop')
  160. self.shareBox('showStep', 3)
  161. self.find('#record-stop').text(sprintf('%d:%02d', Math.floor(secs/60), secs%60))
  162. var timeout = setTimeout(function () {
  163. self.shareBox('recordStopCountdown', secs - 1)
  164. }, 1000)
  165. self.data('stopTimeout', timeout)
  166. },
  167. recordStop: function (callback) {
  168. var self = $(this)
  169. var status = self.data('status')
  170. var _callback = callback || function () {}
  171. if (status == STOPPED) {
  172. return _callback(false)
  173. } else if (status == RECORDING) {
  174. var timeout = self.data('stopTimeout')
  175. if (timeout)
  176. clearTimeout(timeout)
  177. self.data('recordStop')(function () {
  178. self.data('status', STOPPED)
  179. self.shareBox('showStep', 4)
  180. self.find('#record-play').show()
  181. self.find('#record-play-stop').hide()
  182. _callback(true)
  183. })
  184. } else { // PLAYING
  185. self.data('playStop')(function () {
  186. self.find('#record-play').removeClass('playing')
  187. self.data('status', STOPPED)
  188. _callback(true)
  189. })
  190. }
  191. },
  192. recordPlay: function () {
  193. var self = $(this)
  194. var play = function () {
  195. self.data('playStart')(function () {
  196. self.find('#record-play').hide()
  197. self.find('#record-play-stop').show()
  198. self.data('status', PLAYING)
  199. }, function () {
  200. self.find('#record-play').show()
  201. self.find('#record-play-stop').hide()
  202. self.data('status', STOPPED)
  203. })
  204. }
  205. var status = self.data('status')
  206. if (status == STOPPED)
  207. play()
  208. else
  209. self.shareBox('recordStop', play)
  210. },
  211. recordDelete: function () {
  212. var self = $(this)
  213. self.data('recordReset')(function () {
  214. self.shareBox('showStep', 1)
  215. })
  216. },
  217. share: function () {
  218. var self = $(this)
  219. var step = self.data('step')
  220. var data = {
  221. bundlepath : self.data('bundlepath'),
  222. name : self.find('#pedalboard-share-name').val(),
  223. email : self.find('#pedalboard-share-email').val(),
  224. description: self.find('#pedalboard-share-comment').val(),
  225. title : self.find('#pedalboard-share-title').val()
  226. }
  227. if (self.find('#pedalboard-share-hidden').prop("checked")) {
  228. data.hidden = true
  229. }
  230. self.find('#record-share').attr('disabled', true)
  231. var hasAudio = (step == 4)
  232. var shareNow = function (data) {
  233. self.find('#record-rec').addClass("disabled").attr('disabled', true)
  234. self.find('#record-stop').addClass("disabled").attr('disabled', true)
  235. self.find('#record-play').addClass("disabled").attr('disabled', true)
  236. self.find('#record-play-stop').addClass("disabled").attr('disabled', true)
  237. self.find('#record-again').addClass("disabled").attr('disabled', true)
  238. self.find('#record-delete').addClass("disabled").attr('disabled', true)
  239. self.find('#share-wait-message').show().text("Uploading. Please wait...")
  240. self.shareBox('recordStop', function () {
  241. self.data('share')(data, function (resp) {
  242. self.find('#record-rec').removeClass("disabled").attr('disabled', false)
  243. self.find('#record-stop').removeClass("disabled").attr('disabled', false)
  244. self.find('#record-play').removeClass("disabled").attr('disabled', false)
  245. self.find('#record-play-stop').removeClass("disabled").attr('disabled', false)
  246. self.find('#record-again').removeClass("disabled").attr('disabled', false)
  247. self.find('#record-delete').removeClass("disabled").attr('disabled', false)
  248. if (resp.ok) {
  249. self.find('#record-step-' + step).hide()
  250. self.find('#record-share').attr('disabled', resp.ok).hide()
  251. self.find('#share-wait-message').hide()
  252. var pb_url = PEDALBOARDS_URL + "/pedalboards/" + resp.id
  253. self.find('#share-window-url').attr('value', pb_url)
  254. self.find('#share-window-fb').attr('href', "https://www.facebook.com/sharer/sharer.php?u="+pb_url)
  255. self.find('#share-window-tw').attr('href', "https://twitter.com/intent/tweet?source="+pb_url)
  256. if (hasAudio) {
  257. self.data('recordReset')(function () {
  258. self.find('#share-window-form').hide()
  259. self.find('#share-window-links').show()
  260. })
  261. } else {
  262. self.find('#share-window-form').hide()
  263. self.find('#share-window-links').show()
  264. }
  265. } else {
  266. new Notification('error', "Couldn't share pedalboard: " + resp.error)
  267. self.find('#record-share').attr('disabled', false)
  268. self.find('#share-wait-message').show().text("Upload failed!")
  269. }
  270. })
  271. })
  272. }
  273. if (hasAudio) {
  274. // User has recorded some sound
  275. self.data('recordDownload')(function (audioData) {
  276. data.audio = audioData.audio
  277. shareNow(data)
  278. })
  279. } else {
  280. // Just share without audio
  281. shareNow(data)
  282. }
  283. },
  284. open: function (bundlepath, title, stable) {
  285. var self = $(this)
  286. self.find('#record-share').show()
  287. self.find('#share-window-form').show()
  288. self.find('#share-window-links').hide()
  289. self.shareBox('showStep', 1)
  290. self.data('bundlepath', bundlepath)
  291. self.data('screenshotDone', false)
  292. self.find('#pedalboard-share-title').val(title)
  293. self.find('#record-share').attr('disabled', true)
  294. self.find('#share-wait-message').show().text("Waiting for screenshot...")
  295. self.find('.js-share').addClass('disabled')
  296. self.find('textarea').val('').focus()
  297. if (stable) {
  298. self.find('.unstable-warning').hide();
  299. } else {
  300. self.find('.unstable-warning').show();
  301. }
  302. self.show()
  303. var done = function () {
  304. self.data('screenshotDone', true)
  305. self.find('#share-wait-message').hide()
  306. self.shareBox('showStep', self.data('step'))
  307. }
  308. self.data('waitForScreenshot')(false, bundlepath, function (ok) {
  309. if (ok) {
  310. done()
  311. return
  312. }
  313. // 2nd try
  314. self.find('#share-wait-message').show().text("Generating screenshot...")
  315. self.data('waitForScreenshot')(true, bundlepath, function (ok) {
  316. if (ok) {
  317. done()
  318. return
  319. }
  320. // 3rd and final try
  321. self.find('#share-wait-message').show().text("Generating for screenshot... (final attempt)")
  322. self.data('waitForScreenshot')(true, bundlepath, function (ok) {
  323. // shit! just upload without screenshot then.. :(
  324. self.find('#share-wait-message').show().text("Generating for screenshot... failed!")
  325. done()
  326. })
  327. })
  328. })
  329. },
  330. close: function () {
  331. var self = $(this)
  332. var recordTimeout = self.data('recordTimeout')
  333. if (recordTimeout) {
  334. clearTimeout(recordTimeout)
  335. self.data('recordTimeout', null)
  336. }
  337. self.shareBox('recordStop', function (resetNeeded) {
  338. if (! resetNeeded) {
  339. self.hide()
  340. return
  341. }
  342. self.data('recordReset')(function () {
  343. self.hide()
  344. })
  345. })
  346. }
  347. })