cc-manager.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. function ControlChainDeviceManager(options) {
  4. var self = this;
  5. options = $.extend({
  6. devicesIcon: $('<div>'),
  7. devicesWindow: $('<div>'),
  8. updateInfoWindow: $('<div>'),
  9. setIconTooltip: function (msg) {},
  10. showNotification: function (msg, timeout) {},
  11. cancelDownload: function (callback) { callback() },
  12. }, options)
  13. this.connectedDevices = []
  14. this.devicesListElem = options.devicesWindow.find('.mod-devices-window-list > ul')
  15. options.devicesIcon.statusTooltip()
  16. options.devicesIcon.statusTooltip('message', "No Control Chain devices connected", true)
  17. options.devicesIcon.click(function () {
  18. if (options.devicesWindow.is(':visible')) {
  19. options.devicesWindow.hide()
  20. } else if (self.connectedDevices.length > 0) {
  21. options.devicesWindow.show()
  22. }
  23. })
  24. options.updateInfoWindow.find('.js-cancel').click(function () {
  25. options.cancelDownload(function () {
  26. options.updateInfoWindow.hide()
  27. })
  28. return false
  29. })
  30. this.showUpdateWindow = function () {
  31. options.updateInfoWindow.show()
  32. }
  33. this.hideUpdateWindow = function () {
  34. options.updateInfoWindow.hide()
  35. }
  36. this.deviceAdded = function (dev_uri, label, version) {
  37. var item = [dev_uri, label, version]
  38. self.connectedDevices.push(item)
  39. self._devicesUpdated()
  40. options.showNotification('New Control Chain device connected:<br/>' + label + ' v' + version)
  41. }
  42. this.deviceRemoved = function (dev_uri, label, version) {
  43. var item
  44. for (var i in self.connectedDevices) {
  45. item = self.connectedDevices[i]
  46. if (item[0] == dev_uri && item[1] == label && item[2] == version) {
  47. self.connectedDevices.splice(i, 1)
  48. break
  49. }
  50. }
  51. self._devicesUpdated()
  52. options.showNotification('Control Chain device disconnected:<br/>' + label + ' v' + version)
  53. }
  54. this._devicesUpdated = function () {
  55. var count = self.connectedDevices.length
  56. if (count == 0) {
  57. options.devicesIcon.statusTooltip('message', "No Control Chain devices connected", true)
  58. options.devicesWindow.hide()
  59. self.devicesListElem.html("<li>No Control Chain devices connected</li>")
  60. } else {
  61. var item, lihtml = ""
  62. for (var i in self.connectedDevices) {
  63. item = self.connectedDevices[i]
  64. lihtml += "<li>" +
  65. "<b>" + item[1] + "</b><br/>" +
  66. "URI: " + item[0] + "<br/>" +
  67. "Version: " + item[2] +
  68. "</li>"
  69. }
  70. self.devicesListElem.html(lihtml)
  71. var msg
  72. if (count == 1) {
  73. msg = "1 Control Chain device connected"
  74. } else {
  75. msg = sprintf("%d Control Chain devices connected", count)
  76. }
  77. options.devicesIcon.statusTooltip('message', msg, true)
  78. }
  79. }
  80. }