| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
- // SPDX-License-Identifier: AGPL-3.0-or-later
- var STOPPED = 0
- var RECORDING = 1
- var PLAYING = 2
- var RECORD_COUNTDOWN = 3
- var RECORD_LENGTH = 60 * 3
- JqueryClass('shareBox', {
- init: function (options) {
- var self = $(this)
- options = $.extend({
- recordStart: function (callback) {
- callback()
- },
- recordStop: function (callback) {
- callback()
- },
- playStart: function (startCallback, stopCallback) {
- startCallback();
- setTimeout(stopCallback, 3000)
- },
- playStop: function (callback) {
- callback()
- },
- recordDownload: function (callback) {
- callback({})
- },
- recordReset: function (callback) {
- callback()
- },
- share: function (data, callback) {
- callback({ok:true})
- },
- waitForScreenshot: function (generate, bundlepath, callback) {
- callback(true)
- },
- }, options)
- self.data(options)
- self.data('bundlepath', '')
- self.data('recordedData', null)
- self.data('recordTimeout', null)
- self.data('status', STOPPED)
- self.data('step', 0)
- self.data('screenshotDone', false)
- self.find('#record-rec').click(function () {
- if (! $(this).hasClass("disabled")) {
- self.shareBox('recordStartCountdown');
- }
- return false
- })
- self.find('#record-stop').click(function () {
- self.shareBox('recordStop');
- return false
- })
- self.find('#record-play').click(function () {
- self.shareBox('recordPlay');
- return false
- })
- self.find('#record-play-stop').click(function () {
- self.shareBox('recordStop');
- return false
- })
- self.find('#record-again').click(function () {
- self.shareBox('recordStartCountdown');
- return false
- })
- self.find('#record-delete').click(function () {
- self.shareBox('recordDelete');
- return false
- })
- self.find('#record-cancel').click(function () {
- self.shareBox('close');
- return false
- })
- self.find('.js-cancel').click(function () {
- self.shareBox('close');
- return false
- })
- self.find('#record-share').click(function () {
- self.shareBox('share');
- return false
- })
- self.find('#share-window-url-btn').click(function () {
- self.find('#share-window-url').select()
- var ok
- try {
- ok = document.execCommand('copy')
- } catch (err) {
- ok = false
- }
- if (ok) {
- self.find('#share-tooltip').css('opacity',1).find('.tooltip-inner').html('Copied to clipboard')
- } else {
- console.log('Unable to copy to clipboard.')
- self.find('#share-tooltip').css('opacity',1).find('.tooltip-inner').html('Press Ctrl/Cmd + C to copy')
- }
- setTimeout(function() {
- self.find('#share-tooltip').css('opacity',0)
- }, 2000)
- })
- $('body').keydown(function (e) {
- if (e.keyCode == 27)
- self.shareBox('close')
- })
- },
- showStep: function (step) {
- var self = $(this)
- self.data('step', step)
- for (var i = 1; i < 5; i++) {
- if (i == step)
- self.find('#record-step-' + i).show()
- else
- self.find('#record-step-' + i).hide()
- }
- var button = self.find('#record-share')
- if (step == 1) {
- button.text('Just share').attr('disabled', !self.data('screenshotDone'))
- } else {
- button.text('Share')
- if (step == 4)
- button.attr('disabled', !self.data('screenshotDone'))
- else
- button.attr('disabled', true)
- }
- },
- recordStartCountdown: function () {
- var self = $(this)
- var status = self.data('status')
- var start = function () {
- self.data('recordedData', null)
- self.shareBox('recordCountdown', RECORD_COUNTDOWN)
- }
- if (status == STOPPED) {
- start()
- } else if (status == PLAYING) {
- self.shareBox('recordStop', start)
- }
- },
- recordCountdown: function (secs) {
- var self = $(this)
- self.shareBox('showStep', 2)
- if (secs == 0) {
- self.data('status', RECORDING)
- self.data('recordStart')(function () {
- self.shareBox('recordStopCountdown', RECORD_LENGTH)
- })
- return
- }
- self.find('#record-countdown').text(secs)
- var recordTimeout = setTimeout(function () {
- self.shareBox('recordCountdown', secs - 1)
- }, 1000)
- self.data('recordTimeout', recordTimeout)
- },
- recordStopCountdown: function (secs) {
- var self = $(this)
- self.data('stopTimeout', null)
- if (secs == 0)
- return self.shareBox('recordStop')
- self.shareBox('showStep', 3)
- self.find('#record-stop').text(sprintf('%d:%02d', Math.floor(secs/60), secs%60))
- var timeout = setTimeout(function () {
- self.shareBox('recordStopCountdown', secs - 1)
- }, 1000)
- self.data('stopTimeout', timeout)
- },
- recordStop: function (callback) {
- var self = $(this)
- var status = self.data('status')
- var _callback = callback || function () {}
- if (status == STOPPED) {
- return _callback(false)
- } else if (status == RECORDING) {
- var timeout = self.data('stopTimeout')
- if (timeout)
- clearTimeout(timeout)
- self.data('recordStop')(function () {
- self.data('status', STOPPED)
- self.shareBox('showStep', 4)
- self.find('#record-play').show()
- self.find('#record-play-stop').hide()
- _callback(true)
- })
- } else { // PLAYING
- self.data('playStop')(function () {
- self.find('#record-play').removeClass('playing')
- self.data('status', STOPPED)
- _callback(true)
- })
- }
- },
- recordPlay: function () {
- var self = $(this)
- var play = function () {
- self.data('playStart')(function () {
- self.find('#record-play').hide()
- self.find('#record-play-stop').show()
- self.data('status', PLAYING)
- }, function () {
- self.find('#record-play').show()
- self.find('#record-play-stop').hide()
- self.data('status', STOPPED)
- })
- }
- var status = self.data('status')
- if (status == STOPPED)
- play()
- else
- self.shareBox('recordStop', play)
- },
- recordDelete: function () {
- var self = $(this)
- self.data('recordReset')(function () {
- self.shareBox('showStep', 1)
- })
- },
- share: function () {
- var self = $(this)
- var step = self.data('step')
- var data = {
- bundlepath : self.data('bundlepath'),
- name : self.find('#pedalboard-share-name').val(),
- email : self.find('#pedalboard-share-email').val(),
- description: self.find('#pedalboard-share-comment').val(),
- title : self.find('#pedalboard-share-title').val()
- }
- if (self.find('#pedalboard-share-hidden').prop("checked")) {
- data.hidden = true
- }
- self.find('#record-share').attr('disabled', true)
- var hasAudio = (step == 4)
- var shareNow = function (data) {
- self.find('#record-rec').addClass("disabled").attr('disabled', true)
- self.find('#record-stop').addClass("disabled").attr('disabled', true)
- self.find('#record-play').addClass("disabled").attr('disabled', true)
- self.find('#record-play-stop').addClass("disabled").attr('disabled', true)
- self.find('#record-again').addClass("disabled").attr('disabled', true)
- self.find('#record-delete').addClass("disabled").attr('disabled', true)
- self.find('#share-wait-message').show().text("Uploading. Please wait...")
- self.shareBox('recordStop', function () {
- self.data('share')(data, function (resp) {
- self.find('#record-rec').removeClass("disabled").attr('disabled', false)
- self.find('#record-stop').removeClass("disabled").attr('disabled', false)
- self.find('#record-play').removeClass("disabled").attr('disabled', false)
- self.find('#record-play-stop').removeClass("disabled").attr('disabled', false)
- self.find('#record-again').removeClass("disabled").attr('disabled', false)
- self.find('#record-delete').removeClass("disabled").attr('disabled', false)
- if (resp.ok) {
- self.find('#record-step-' + step).hide()
- self.find('#record-share').attr('disabled', resp.ok).hide()
- self.find('#share-wait-message').hide()
- var pb_url = PEDALBOARDS_URL + "/pedalboards/" + resp.id
- self.find('#share-window-url').attr('value', pb_url)
- self.find('#share-window-fb').attr('href', "https://www.facebook.com/sharer/sharer.php?u="+pb_url)
- self.find('#share-window-tw').attr('href', "https://twitter.com/intent/tweet?source="+pb_url)
- if (hasAudio) {
- self.data('recordReset')(function () {
- self.find('#share-window-form').hide()
- self.find('#share-window-links').show()
- })
- } else {
- self.find('#share-window-form').hide()
- self.find('#share-window-links').show()
- }
- } else {
- new Notification('error', "Couldn't share pedalboard: " + resp.error)
- self.find('#record-share').attr('disabled', false)
- self.find('#share-wait-message').show().text("Upload failed!")
- }
- })
- })
- }
- if (hasAudio) {
- // User has recorded some sound
- self.data('recordDownload')(function (audioData) {
- data.audio = audioData.audio
- shareNow(data)
- })
- } else {
- // Just share without audio
- shareNow(data)
- }
- },
- open: function (bundlepath, title, stable) {
- var self = $(this)
- self.find('#record-share').show()
- self.find('#share-window-form').show()
- self.find('#share-window-links').hide()
- self.shareBox('showStep', 1)
- self.data('bundlepath', bundlepath)
- self.data('screenshotDone', false)
- self.find('#pedalboard-share-title').val(title)
- self.find('#record-share').attr('disabled', true)
- self.find('#share-wait-message').show().text("Waiting for screenshot...")
- self.find('.js-share').addClass('disabled')
- self.find('textarea').val('').focus()
- if (stable) {
- self.find('.unstable-warning').hide();
- } else {
- self.find('.unstable-warning').show();
- }
- self.show()
- var done = function () {
- self.data('screenshotDone', true)
- self.find('#share-wait-message').hide()
- self.shareBox('showStep', self.data('step'))
- }
- self.data('waitForScreenshot')(false, bundlepath, function (ok) {
- if (ok) {
- done()
- return
- }
- // 2nd try
- self.find('#share-wait-message').show().text("Generating screenshot...")
- self.data('waitForScreenshot')(true, bundlepath, function (ok) {
- if (ok) {
- done()
- return
- }
- // 3rd and final try
- self.find('#share-wait-message').show().text("Generating for screenshot... (final attempt)")
- self.data('waitForScreenshot')(true, bundlepath, function (ok) {
- // shit! just upload without screenshot then.. :(
- self.find('#share-wait-message').show().text("Generating for screenshot... failed!")
- done()
- })
- })
- })
- },
- close: function () {
- var self = $(this)
- var recordTimeout = self.data('recordTimeout')
- if (recordTimeout) {
- clearTimeout(recordTimeout)
- self.data('recordTimeout', null)
- }
- self.shareBox('recordStop', function (resetNeeded) {
- if (! resetNeeded) {
- self.hide()
- return
- }
- self.data('recordReset')(function () {
- self.hide()
- })
- })
- }
- })
|