banks.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. JqueryClass('bankBox', {
  4. init: function (options) {
  5. var self = $(this)
  6. options = $.extend({
  7. bankCanvas: self.find('#bank-list .js-canvas'),
  8. addButton: self.find('#js-add-bank'),
  9. pedalboardCanvas: self.find('#bank-pedalboards'),
  10. initMessage: self.find('#bank-init'),
  11. searchForm: self.find('#bank-pedalboards-search'),
  12. searchBox: self.find('input[type=search]'),
  13. resultCanvasUser: self.find('#bank-pedalboards-result .js-user-pedalboards'),
  14. resultCanvasFactory: self.find('#bank-pedalboards-result .js-factory-pedalboards'),
  15. bankTitle: self.find('#bank-title'),
  16. saving: $('#banks-saving'),
  17. previousBankTitle: null,
  18. list: function (callback) {
  19. callback([])
  20. },
  21. search: function (local, query, callback) {
  22. callback([])
  23. },
  24. load: function (callback) {
  25. callback([])
  26. },
  27. save: function (data, callback) {
  28. callback(true)
  29. },
  30. copyFactoryPedalboard: function (bundlepath, title, callback) {
  31. callback({})
  32. },
  33. isMainWindow: true,
  34. windowName: "Banks"
  35. }, options)
  36. self.data(options)
  37. options.pedalboardCanvas.hide()
  38. options.searchForm.hide()
  39. options.resultCanvasUser.hide()
  40. options.resultCanvasFactory.hide()
  41. options.initMessage.show()
  42. options.addButton.click(function () {
  43. self.bankBox('create')
  44. })
  45. var searcher = new PedalboardSearcher($.extend({
  46. searchbox: options.searchBox,
  47. mode: 'installed',
  48. skipBroken: true,
  49. render: function (pedalboard, url) {
  50. var rendered = self.bankBox('renderPedalboard', pedalboard)
  51. rendered.draggable({
  52. cursor: "grabbing !important",
  53. revert: 'invalid',
  54. connectToSortable: options.pedalboardCanvas,
  55. helper: function () {
  56. var helper = rendered.clone().appendTo(self)
  57. helper.addClass('mod-banks-drag-item')
  58. helper.removeClass('js-pedalboard-item')
  59. helper.find('.js-remove').hide()
  60. return helper
  61. }
  62. })
  63. if (pedalboard.factory) {
  64. self.data('resultCanvasFactory').append(rendered)
  65. } else {
  66. self.data('resultCanvasUser').append(rendered)
  67. }
  68. },
  69. cleanResults: function () {
  70. self.data('resultCanvasUser').html('')
  71. self.data('resultCanvasFactory').html('')
  72. }
  73. }, options))
  74. options.pedalboardCanvas.sortable({
  75. cursor: "grabbing !important",
  76. revert: true,
  77. receive: function (e, ui) {
  78. // the next update call will be acting on a cloned item, not this one.
  79. // as such, we need to cache these values and set them on the newly cloned object for the update
  80. self.droppedBundle = ui.item.data('pedalboardBundle')
  81. self.droppedTitle = ui.item.data('pedalboardTitle')
  82. self.isFactoryPedalboard = ui.item.data('isFactoryPedalboard')
  83. if (self.isFactoryPedalboard) {
  84. self.clonedFactoryItem = ui.item.clone()
  85. }
  86. },
  87. update: function (e, ui) {
  88. if (self.droppedBundle) {
  89. // cloned item that got moved into the central area, re-set data attributes
  90. ui.item.data('pedalboardBundle', self.droppedBundle)
  91. ui.item.data('pedalboardTitle', self.droppedTitle)
  92. ui.item.data('isFactoryPedalboard', self.isFactoryPedalboard)
  93. // if factory pedalboard was dropped, handle user-data copying
  94. if (self.isFactoryPedalboard) {
  95. self.data('copyFactoryPedalboard')(self.droppedBundle, self.droppedTitle, function(pb) {
  96. ui.item.data('pedalboardBundle', pb.bundlepath)
  97. ui.item.data('pedalboardTitle', pb.title)
  98. ui.item.data('isFactoryPedalboard', false)
  99. ui.item.find('.js-title').text(pb.title)
  100. // add the newly copied pedalboard to the user-data canvas area
  101. var clone = self.clonedFactoryItem
  102. clone.draggable({
  103. cursor: "grabbing !important",
  104. revert: 'invalid',
  105. connectToSortable: options.pedalboardCanvas,
  106. helper: function () {
  107. var helper = clone.clone().appendTo(self)
  108. helper.addClass('mod-banks-drag-item')
  109. helper.removeClass('js-pedalboard-item')
  110. helper.find('.js-remove').hide()
  111. return helper
  112. }
  113. })
  114. clone.data('pedalboardBundle', pb.bundlepath)
  115. clone.data('pedalboardTitle', pb.title)
  116. clone.data('isFactoryPedalboard', false)
  117. clone.find('.js-title').text(pb.title)
  118. self.data('resultCanvasUser').append(clone)
  119. self.clonedFactoryItem = null
  120. self.bankBox('save')
  121. })
  122. }
  123. }
  124. ui.item.removeClass('js-pedalboard-item')
  125. // TODO the code below is repeated. The former click event is not triggered because
  126. // the element is cloned
  127. ui.item.find('.js-remove').show().click(function () {
  128. ui.item.animate({
  129. opacity: 0,
  130. height: 0
  131. }, function () {
  132. ui.item.remove()
  133. self.bankBox('save')
  134. })
  135. })
  136. // if this is a factory pedalboard drop, do not save just yet
  137. if (!self.isFactoryPedalboard) {
  138. self.bankBox('save')
  139. }
  140. self.droppedBundle = self.droppedTitle = null
  141. self.isFactoryPedalboard = false
  142. },
  143. })
  144. options.bankCanvas.sortable({
  145. handle: '.move',
  146. update: function () {
  147. self.bankBox('save')
  148. }
  149. })
  150. options.open = function () {
  151. searcher.search()
  152. self.bankBox('load')
  153. return false
  154. }
  155. self.window(options)
  156. },
  157. load: function () {
  158. var self = $(this)
  159. self.data('loading', true)
  160. if (self.data('loaded')) {
  161. self.data('currentBank', null)
  162. self.data('pedalboardCanvas').html('').hide()
  163. self.data('searchForm').hide()
  164. self.data('resultCanvasUser').hide()
  165. self.data('resultCanvasFactory').hide()
  166. self.data('bankTitle').hide()
  167. } else {
  168. self.data('loaded', true)
  169. }
  170. self.data('load')(function (banks) {
  171. self.data('bankCanvas').html('')
  172. if (banks.length > 0) {
  173. var bank, previousBankTitle = self.data('previousBankTitle')
  174. self.data('currentBank', null)
  175. self.data('previousBankTitle', null)
  176. for (var i = 0; i < banks.length; i++) {
  177. bank = self.bankBox('renderBank', banks[i], i)
  178. if (previousBankTitle == banks[i].title) {
  179. self.bankBox('selectBank', bank)
  180. }
  181. }
  182. }
  183. self.data('loading', false)
  184. })
  185. },
  186. save: function () {
  187. var self = $(this)
  188. var serialized = []
  189. self.data('bankCanvas').children().each(function () {
  190. var bank = $(this)
  191. var pedalboards = (bank.data('selected') ? self.data('pedalboardCanvas') : bank.data('pedalboards'))
  192. var pedalboardData = []
  193. pedalboards.children().each(function () {
  194. var bundle = $(this).data('pedalboardBundle')
  195. var title = $(this).data('pedalboardTitle')
  196. if (!bundle) {
  197. return
  198. }
  199. pedalboardData.push({
  200. bundle: bundle,
  201. title: title,
  202. })
  203. })
  204. serialized.push({
  205. title: bank.find('.js-bank-title').text(),
  206. pedalboards: pedalboardData,
  207. })
  208. });
  209. self.data('saving').html('Auto saving banks...').show()
  210. self.data('save')(serialized, function (ok) {
  211. if (ok)
  212. self.data('saving').html('Auto saving banks... Done!').show()
  213. else {
  214. self.data('saving').html('Auto saving banks... Error!').show()
  215. new Notification('error', 'Error saving banks!')
  216. }
  217. if (self.data('savingTimeout')) {
  218. clearTimeout(self.data('savingTimeout'))
  219. }
  220. var timeout = setTimeout(function () {
  221. self.data('savingTimeout', null)
  222. self.data('saving').hide()
  223. }, 500)
  224. self.data('savingTimeout', timeout)
  225. })
  226. // Update displayed indexes
  227. self.data('pedalboardCanvas').children().each(function (i) {
  228. var pedalboard = $(this)
  229. var index = pedalboard.find(".js-index")
  230. index.html((i+1) + ".&nbsp;")
  231. })
  232. },
  233. create: function () {
  234. var self = $(this)
  235. if (self.data('resultCanvasUser').children().length + self.data('resultCanvasFactory').children().length === 0) {
  236. new Notification('error', 'Before creating banks you must save a pedalboard first.')
  237. return;
  238. }
  239. if (self.data('loading')) {
  240. return
  241. }
  242. var bankData = {
  243. 'title': '',
  244. 'pedalboards': [],
  245. }
  246. var bank = self.bankBox('renderBank', bankData)
  247. self.bankBox('editBank', bank)
  248. self.bankBox('selectBank', bank)
  249. },
  250. renderBank: function (bankData) {
  251. var self = $(this)
  252. var bank = $(Mustache.render(TEMPLATES.bank_item, bankData))
  253. self.data('bankCanvas').append(bank)
  254. bank.data('selected', false)
  255. bank.data('pedalboards', $('<div>'))
  256. bank.data('title', bankData.title)
  257. var i, pedalboardData, rendered
  258. for (i = 0; i < bankData.pedalboards.length; i++) {
  259. rendered = self.bankBox('renderPedalboard', bankData.pedalboards[i], i+1)
  260. rendered.find('.js-remove').show()
  261. rendered.appendTo(bank.data('pedalboards'))
  262. }
  263. bank.click(function () {
  264. if (self.data('loading')) {
  265. self.data('previousBankTitle', bankData.title)
  266. return
  267. }
  268. if (bank.hasClass('selected'))
  269. self.bankBox('editBank', bank)
  270. else
  271. self.bankBox('selectBank', bank)
  272. })
  273. bank.find('.js-remove').click(function () {
  274. if (self.data('loading')) {
  275. return false
  276. }
  277. self.bankBox('removeBank', bank)
  278. return false
  279. })
  280. return bank
  281. },
  282. selectBank: function (bank) {
  283. var self = $(this)
  284. var pedalboards = bank.data('pedalboards')
  285. var canvas = self.data('pedalboardCanvas')
  286. var current = self.data('currentBank')
  287. if (current) {
  288. // Save the pedalboards of the current bank
  289. current.data('pedalboards').append(canvas.children())
  290. current.data('selected', false)
  291. // addressing is already saved, every time select is changed
  292. }
  293. if (pedalboards.children().length == 0) {
  294. new Notification('warning', 'This bank is empty - drag pedalboards from the right panel', 5000)
  295. }
  296. canvas.append(bank.data('pedalboards').children())
  297. // Hide initial message
  298. self.data('initMessage').hide()
  299. // Show everything else
  300. canvas.show()
  301. self.data('searchForm').show()
  302. self.data('resultCanvasUser').show()
  303. self.data('resultCanvasFactory').show()
  304. // Mark this bank as selected
  305. self.data('currentBank', bank)
  306. self.data('previousBankTitle', bank.data('title'))
  307. bank.data('selected', true)
  308. self.data('bankCanvas').children().removeClass('selected')
  309. bank.addClass('selected')
  310. // Replace the title string
  311. self.data('bankTitle').find('h1').text(bank.data('title') || "Untitled")
  312. self.data('bankTitle').show()
  313. },
  314. editBank: function (bank) {
  315. var self = $(this)
  316. var titleBox = bank.find('.js-bank-title')
  317. if (titleBox.data('editing'))
  318. return true
  319. titleBox.data('editing', true)
  320. var title = titleBox.html()
  321. titleBox.html('')
  322. var editBox = $('<input>')
  323. editBox.val(title)
  324. editBox.addClass('edit-bank')
  325. titleBox.append(editBox)
  326. var finish = function () {
  327. var title = editBox.val() || 'Untitled'
  328. titleBox.data('editing', false)
  329. titleBox.html(title)
  330. bank.data('title', title)
  331. self.data('bankTitle').find('h1').text(title)
  332. self.data('previousBankTitle', title)
  333. self.bankBox('save')
  334. }
  335. editBox.keydown(function (e) {
  336. if (e.keyCode == 13) {
  337. finish()
  338. }
  339. })
  340. editBox.blur(finish)
  341. editBox.focus()
  342. },
  343. removeBank: function (bank) {
  344. var msg = "Deleting bank \""+bank.find('.js-bank-title').html()+"\". Confirm?"
  345. if (confirm(msg) != true) {
  346. return;
  347. }
  348. var self = $(this)
  349. var count = bank.data('pedalboards').children().length
  350. if (count > 1 && !confirm(sprintf('There are %d pedalboards in this bank, are you sure you want to delete it?', count)))
  351. return
  352. if (bank.data('selected')) {
  353. self.data('currentBank', null)
  354. self.data('previousBankTitle', null)
  355. self.data('pedalboardCanvas').html('').hide()
  356. self.data('searchForm').hide()
  357. self.data('resultCanvasUser').hide()
  358. self.data('resultCanvasFactory').hide()
  359. self.data('bankTitle').hide()
  360. }
  361. bank.animate({
  362. opacity: 0,
  363. height: 0
  364. }, function () {
  365. bank.remove();
  366. self.bankBox('save')
  367. })
  368. },
  369. renderPedalboard: function (pedalboard, index) {
  370. var self = $(this)
  371. var metadata = {
  372. index: index ? (index + ".") : "",
  373. title: pedalboard.title,
  374. image: "/img/loading-pedalboard.gif",
  375. }
  376. var rendered = $(Mustache.render(TEMPLATES.bank_pedalboard, metadata))
  377. if (!index) {
  378. rendered.addClass('js-pedalboard-item')
  379. rendered.removeClass('clearfix')
  380. }
  381. // Assign remove functionality. If removal is not desired (it's a search result),
  382. // then the remove clickable element will be hidden
  383. rendered.find('.js-remove').click(function () {
  384. rendered.animate({
  385. opacity: 0,
  386. height: 0
  387. }, function () {
  388. rendered.remove()
  389. self.bankBox('save')
  390. })
  391. })
  392. rendered.data('pedalboardBundle', pedalboard.bundle)
  393. rendered.data('pedalboardTitle', pedalboard.title)
  394. rendered.data('isFactoryPedalboard', pedalboard.factory)
  395. wait_for_pedalboard_screenshot(pedalboard.bundle, pedalboard.version, function (resp) {
  396. var img = rendered.find('.img img');
  397. if (resp.ok) {
  398. img.attr("src", "/pedalboard/image/thumbnail.png?bundlepath="+escape(pedalboard.bundle)+"&tstamp="+resp.ctime+"&v="+pedalboard.version)
  399. img.css({ top: (img.parent().height() - img.height()) / 2 })
  400. } else {
  401. img.attr("src", "/img/icons/broken_image.svg")
  402. img.css({'width': '100px'})
  403. }
  404. })
  405. return rendered
  406. }
  407. })