common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. /* The method below is already implemented in modgui.js.
  4. * The reason it's there and not here is because modgui.js is a standalone implementation
  5. * of modgui LV2 standards and should not depend of anything from this package (it's also used in modsdk)
  6. * We kept it commented here because this is where it used to belong, and it's weird to have this so core
  7. * function being declared only in modgui.js.
  8. */
  9. /*
  10. function JqueryClass(name, methods) {
  11. (function($) {
  12. $.fn[name] = function(method) {
  13. if (methods[method]) {
  14. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  15. } else if (typeof method === 'object' || !method) {
  16. return methods.init.apply(this, arguments);
  17. } else {
  18. $.error( 'Method ' + method + ' does not exist on jQuery.' + name );
  19. }
  20. }
  21. })(jQuery);
  22. }
  23. */
  24. (function ($) {
  25. $.fn.cleanableInput = function (options) {
  26. var self = $(this)
  27. var remove = $('<span class="input-clean"></span>')
  28. remove.insertAfter(self)
  29. var position = function () {
  30. remove.show()
  31. /* remove.css('left', self.position().left + self.width() - 3)
  32. remove.css('top', self.position().top + self.height() - 22)
  33. */ }
  34. remove.click(function () {
  35. self.val('')
  36. remove.hide()
  37. self.trigger('keypress')
  38. })
  39. if (self.val().length == 0)
  40. remove.hide()
  41. else
  42. position()
  43. self.keyup(function () {
  44. if (self.val().length > 0)
  45. position()
  46. else
  47. remove.hide()
  48. })
  49. }
  50. })(jQuery);
  51. (function ($) {
  52. $.extend($.expr[":"], {
  53. scrollable: function (element) {
  54. var vertically_scrollable, horizontally_scrollable;
  55. if ($(element).css('overflow') == 'scroll' || $(element).css('overflowX') == 'scroll' || $(element).css('overflowY') == 'scroll') return true;
  56. vertically_scrollable = (element.clientHeight < element.scrollHeight) && (
  57. $.inArray($(element).css('overflowY'), ['scroll', 'auto']) != -1 || $.inArray($(element).css('overflow'), ['scroll', 'auto']) != -1);
  58. if (vertically_scrollable) return true;
  59. horizontally_scrollable = (element.clientWidth < element.scrollWidth) && (
  60. $.inArray($(element).css('overflowX'), ['scroll', 'auto']) != -1 || $.inArray($(element).css('overflow'), ['scroll', 'auto']) != -1);
  61. return horizontally_scrollable;
  62. }
  63. });
  64. })(jQuery)
  65. function setCookie(name, value, days) {
  66. if (days) {
  67. var date = new Date();
  68. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  69. var expires = "; expires=" + date.toGMTString();
  70. } else var expires = "";
  71. document.cookie = name + "=" + value + expires + "; path=/";
  72. }
  73. function getCookie(c_name, defaultValue) {
  74. if (document.cookie.length > 0) {
  75. c_start = document.cookie.indexOf(c_name + "=");
  76. if (c_start != -1) {
  77. c_start = c_start + c_name.length + 1;
  78. c_end = document.cookie.indexOf(";", c_start);
  79. if (c_end == -1) {
  80. c_end = document.cookie.length;
  81. }
  82. return unescape(document.cookie.substring(c_start, c_end));
  83. }
  84. }
  85. if (defaultValue)
  86. return defaultValue
  87. return "";
  88. }
  89. function compareVersions(a, b, len) {
  90. if (!a && !b) {
  91. return 0
  92. }
  93. if (!b) {
  94. return 1
  95. }
  96. if (!a) {
  97. return -1
  98. }
  99. if (! len) {
  100. len = 4
  101. }
  102. for (var i = 0; i < len; i++) {
  103. if (a[i] > b[i]) {
  104. return 1
  105. }
  106. if (a[i] < b[i]) {
  107. return -1
  108. }
  109. }
  110. return 0
  111. }
  112. function renderTime(time) {
  113. var months = ['Jan',
  114. 'Feb',
  115. 'Mar',
  116. 'Apr',
  117. 'May',
  118. 'Jun',
  119. 'Jul',
  120. 'Aug',
  121. 'Sep',
  122. 'Oct',
  123. 'Nov',
  124. 'Dec'
  125. ]
  126. return sprintf('%s %02d %02d:%02d',
  127. months[time.getMonth()],
  128. time.getDate(),
  129. time.getHours(),
  130. time.getMinutes())
  131. }
  132. function remove_from_array(array, element) {
  133. var index = array.indexOf(element)
  134. if (index > -1)
  135. array.splice(index, 1)
  136. }
  137. var pending_pedalboard_screenshots = []
  138. function wait_for_pedalboard_screenshot(bundlepath, version, callback) {
  139. // allow to cache request if no screenshot is being currently generated
  140. var cache = pending_pedalboard_screenshots.indexOf(bundlepath) < 0;
  141. $.ajax({
  142. url: "/pedalboard/image/check?bundlepath="+escape(bundlepath)+'&v='+version.toString(),
  143. success: function (resp) {
  144. if (resp.status == 1) {
  145. // success
  146. remove_from_array(pending_pedalboard_screenshots, bundlepath)
  147. callback({'ok':true,'ctime':resp.ctime})
  148. return
  149. }
  150. if (resp.status == 0) {
  151. // pending
  152. setTimeout(function() {
  153. wait_for_pedalboard_screenshot(bundlepath, version, callback)
  154. }, 1000)
  155. return
  156. }
  157. // error
  158. callback({'ok':false})
  159. },
  160. error: function () {
  161. callback({'ok':false})
  162. },
  163. cache: cache,
  164. global: false,
  165. dataType: 'json'
  166. })
  167. }