snapshot.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. function SnapshotsManager(options) {
  4. var self = this;
  5. options = $.extend({
  6. pedalPresetsWindow: $('<div>'),
  7. pedalPresetsList: $('<div>'),
  8. pedalPresetsOverlay: $('<div>'),
  9. renamedCallback: function (name) {},
  10. hardwareManager: null,
  11. canFeedback: true,
  12. currentlyAddressed: false,
  13. editingElem: null,
  14. presetCount: 0,
  15. }, options)
  16. $('body').keydown(function (e) {
  17. if (e.keyCode == 27) { // esc
  18. self.hideRenameOverlay()
  19. options.pedalPresetsWindow.hide()
  20. }
  21. })
  22. options.pedalPresetsOverlay
  23. .hide()
  24. .blur(self.pedalPresetRenamed)
  25. .keydown(function (e) {
  26. if (e.keyCode == 27) { // esc
  27. return self.hideRenameOverlay()
  28. }
  29. if (e.keyCode == 13) { // enter
  30. return self.pedalPresetRenamed()
  31. }
  32. return true
  33. })
  34. options.pedalPresetsWindow.keyup(function (e) {
  35. if (e.keyCode == 38 || e.keyCode == 40) { // up and down the list
  36. options.pedalPresetsList.find('option:selected').click()
  37. return false
  38. }
  39. return true
  40. })
  41. options.pedalPresetsWindow.find('.js-cancel').click(function () {
  42. self.hideRenameOverlay()
  43. options.pedalPresetsWindow.hide()
  44. return false
  45. })
  46. options.pedalPresetsWindow.find('.js-rename').click(function (e) {
  47. if (options.currentlyAddressed) {
  48. return self.prevent(e)
  49. }
  50. if (options.editingElem != null) {
  51. console.log("Note: rename click ignored")
  52. return false
  53. }
  54. var selected = options.editingElem = options.pedalPresetsList.find('option:selected')
  55. var selectedHtml = selected.html()
  56. var name = selectedHtml.substring(selectedHtml.indexOf(".") + 1);
  57. options.pedalPresetsOverlay.css({
  58. position: "absolute",
  59. width: selected.width()+2,
  60. height: selected.height()+2,
  61. top: selected.position().top,
  62. left: selected.position().left
  63. }).prop("value", name).show().focus()
  64. return false
  65. })
  66. options.pedalPresetsWindow.find('.js-delete').click(function (e) {
  67. self.hideRenameOverlay()
  68. var selected = options.pedalPresetsList.find('option:selected')
  69. var selectId = selected.val()
  70. if (options.presetCount <= 1) {
  71. return self.prevent(e, "Cannot delete last remaining snapshot")
  72. } else if (options.currentlyAddressed) {
  73. return self.prevent(e)
  74. }
  75. $.ajax({
  76. url: '/snapshot/remove',
  77. type: 'GET',
  78. data: {
  79. id: selectId,
  80. },
  81. success: function () {
  82. selected.remove()
  83. options.renamedCallback()
  84. options.pedalPresetsWindow.find('.js-delete').addClass('disabled')
  85. options.presetCount -= 1
  86. if (options.presetCount <= 1) {
  87. options.pedalPresetsWindow.find('.js-assign-all').addClass('disabled')
  88. }
  89. // Replace options value and text so we get a sequential list 1, 2, 3, etc.
  90. var i = 0
  91. options.pedalPresetsList.children().each(function(option) {
  92. var optionHtml = $(this).html()
  93. var prtitle = optionHtml.substring(optionHtml.indexOf(".") + 1)
  94. $(this).html((i+1) + "." + prtitle)
  95. $(this).val(i)
  96. i++
  97. })
  98. },
  99. error: function () {
  100. new Notification('error', "Failed to delete snapshot", 5000)
  101. },
  102. cache: false,
  103. })
  104. return false
  105. })
  106. options.pedalPresetsWindow.find('.js-assign').click(function () {
  107. self.hideRenameOverlay()
  108. // TODO
  109. console.log(this)
  110. return false
  111. })
  112. options.pedalPresetsWindow.find('.js-assign-all').click(function (e) {
  113. self.hideRenameOverlay()
  114. if ($(this).hasClass("disabled")) {
  115. return self.prevent(e, "Cannot assign list with only 1 snapshot")
  116. }
  117. var port = {
  118. name: 'Snapshots',
  119. symbol: ':presets',
  120. ranges: {
  121. minimum: -1,
  122. maximum: 0,
  123. default: -1,
  124. },
  125. comment: "",
  126. designation: "",
  127. properties: ["enumeration", "integer"],
  128. value: -1,
  129. format: null,
  130. scalePoints: [],
  131. }
  132. options.hardwareManager.open("/pedalboard", port, "Pedalboard")
  133. options.pedalPresetsWindow.hide()
  134. return false
  135. })
  136. this.start = function (currentId, currentlyAddressed, canFeedback) {
  137. // clear old entries
  138. options.pedalPresetsList.find('option').remove()
  139. // save state
  140. options.canFeedback = canFeedback
  141. options.currentlyAddressed = currentlyAddressed
  142. self.getPedalPresetList(function (presets) {
  143. options.presetCount = Object.keys(presets).length
  144. if (options.presetCount == 0) {
  145. return new Notification("error", "No pedalboard snapshots available")
  146. }
  147. if (options.presetCount <= 1) {
  148. options.pedalPresetsWindow.find('.js-assign-all').addClass('disabled')
  149. } else {
  150. options.pedalPresetsWindow.find('.js-assign-all').removeClass('disabled')
  151. }
  152. if (options.currentlyAddressed) {
  153. options.pedalPresetsWindow.find('.js-delete').addClass('disabled')
  154. options.pedalPresetsWindow.find('.js-rename').addClass('disabled')
  155. } else {
  156. if (options.presetCount <= 1) {
  157. options.pedalPresetsWindow.find('.js-delete').addClass('disabled')
  158. } else {
  159. options.pedalPresetsWindow.find('.js-delete').removeClass('disabled')
  160. }
  161. options.pedalPresetsWindow.find('.js-rename').removeClass('disabled')
  162. }
  163. // add new ones
  164. for (var i in presets) {
  165. var elem = $('<option value="'+i+'">'+(parseInt(i)+1)+"."+presets[i]+'</option>')
  166. if (currentId == i && ! options.currentlyAddressed) {
  167. elem.prop('selected', 'selected')
  168. }
  169. elem.click(self.optionClicked)
  170. elem.appendTo(options.pedalPresetsList)
  171. }
  172. options.pedalPresetsWindow.show()
  173. options.pedalPresetsWindow.focus()
  174. options.pedalPresetsWindow.find('.preset-list').focus()
  175. })
  176. }
  177. this.prevent = function (e, customMessage) {
  178. var img = $('<img>').attr('src', 'img/icn-blocked.png')
  179. $('body').append(img)
  180. img.css({
  181. position: 'absolute',
  182. top: e.pageY - img.height() / 2,
  183. left: e.pageX - img.width() / 2,
  184. zIndex: 99999
  185. })
  186. setTimeout(function () {
  187. img.remove()
  188. }, 500)
  189. new Notification("warn", customMessage || "Cannot change snapshots while addressed to hardware", 3000)
  190. return false
  191. }
  192. /*
  193. * Load a snapshot on the mod
  194. */
  195. this.loadPreset = function(presetId, presetName) {
  196. $.ajax({
  197. url: '/snapshot/load',
  198. type: 'GET',
  199. data: {
  200. id: presetId,
  201. },
  202. success: function () {
  203. new Notification("info", "Snapshot " + presetName + " loaded", 2000)
  204. },
  205. error: function () {
  206. new Notification('error', "Failed to load snapshot", 5000)
  207. },
  208. cache: false,
  209. })
  210. }
  211. this.optionClicked = function (e) {
  212. self.hideRenameOverlay()
  213. var selectId = $(this).val()
  214. var selectedHtml = $(this).html()
  215. var prtitle = selectedHtml.substring(selectedHtml.indexOf(".") + 1)
  216. if (options.currentlyAddressed) {
  217. if (!options.canFeedback) {
  218. options.pedalPresetsList.find('option:selected').removeProp('selected')
  219. return self.prevent(e)
  220. }
  221. } else if (options.presetCount > 1) {
  222. options.pedalPresetsWindow.find('.js-delete').removeClass('disabled')
  223. }
  224. self.loadPreset(selectId, prtitle)
  225. }
  226. this.hideRenameOverlay = function () {
  227. options.editingElem = null
  228. options.pedalPresetsOverlay.prop("value","").hide()
  229. return false
  230. }
  231. this.pedalPresetRenamed = function () {
  232. if (options.editingElem == null) {
  233. console.log("FIXME: bad state reached")
  234. return false
  235. }
  236. var text = options.pedalPresetsOverlay.hide().val()
  237. var elem = options.editingElem
  238. var prId = elem.val()
  239. options.editingElem = null
  240. if (text == "") {
  241. return false
  242. }
  243. $.ajax({
  244. url: '/snapshot/rename',
  245. type: 'GET',
  246. data: {
  247. id : prId,
  248. title: text,
  249. },
  250. success: function (resp) {
  251. if (!resp.ok) {
  252. return
  253. }
  254. elem.html((parseInt(prId)+1) + "." + resp.title)
  255. options.renamedCallback(resp.title)
  256. },
  257. error: function () {
  258. new Notification('error', "Failed to rename snapshot", 5000)
  259. },
  260. cache: false,
  261. })
  262. return false
  263. }
  264. this.getPedalPresetList = function (callback) {
  265. $.ajax({
  266. url: '/snapshot/list',
  267. type: 'GET',
  268. success: function (resp) {
  269. callback(resp)
  270. },
  271. error: function () {
  272. new Bug("Failed to get pedalboard snapshot list")
  273. },
  274. cache: false,
  275. dataType: 'json'
  276. })
  277. }
  278. }