window.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. var WINDOWMANAGER = null;
  4. function WindowManager() {
  5. var self = this
  6. WINDOWMANAGER = self
  7. this.windows = []
  8. this.register = function (window) {
  9. self.windows.push(window)
  10. window.bind('windowopen', function () {
  11. self.closeWindows(window, true)
  12. })
  13. }
  14. this.registerShopify = function(ui) {
  15. this.shopifyUI = ui;
  16. }
  17. this.closeWindows = function (window, forced) {
  18. for (var i = 0; i < self.windows.length; i++) {
  19. var win = self.windows[i]
  20. // Close window if not current and forced or not main and current window is not plugin info modal opened from cloud-plugins-library tab
  21. if (win != window && (forced || ! win.data('isMainWindow')) && !(win.selector === '#cloud-plugins-library' && window && window.hasClass('plugin-info'))) {
  22. win.window('close')
  23. }
  24. }
  25. if (self.shopifyUI)
  26. self.shopifyUI.destroyComponent('cart');
  27. }
  28. $(document).bind('keydown', function (e) {
  29. if (e.keyCode == 27) {
  30. self.closeWindows()
  31. }
  32. })
  33. }
  34. (function ($) {
  35. /*
  36. * window
  37. */
  38. var methods = {
  39. init: function (options) {
  40. var self = $(this)
  41. options = $.extend({
  42. isMainWindow: false,
  43. windowName: "Unknown",
  44. windowManager: WINDOWMANAGER
  45. }, options)
  46. var trigger = options.trigger
  47. self.data('trigger', trigger)
  48. self.data('isMainWindow', options.isMainWindow)
  49. self.data('windowName', options.windowName)
  50. if (options.open) {
  51. self.bind('windowopen', options.open)
  52. }
  53. if (options.close) {
  54. self.bind('windowclose', options.close)
  55. }
  56. if (options.preopen) {
  57. self.data('preopen', options.preopen)
  58. }
  59. self.hide()
  60. self.data('initialized', true)
  61. options.windowManager.register(self)
  62. self.find('.js-close').click(function () {
  63. self.window('close')
  64. return false
  65. })
  66. if (trigger) {
  67. trigger.removeClass('selected')
  68. trigger.click(function () {
  69. self.window('toggle')
  70. })
  71. }
  72. //self.click(function() { return false })
  73. // TODO this shouldn't be too hardcoded
  74. self.data('defaultIcon', $('#mod-plugins'))
  75. return self
  76. },
  77. open: function (closure, force) {
  78. var self = $(this)
  79. if (!force && self.data('preopen')) {
  80. self.data('preopen')(function () {
  81. self.window('open', closure, true)
  82. })
  83. return
  84. }
  85. if (!self.data('initialized'))
  86. self.window()
  87. if (closure) {
  88. self.bind('windowopen', closure)
  89. return
  90. }
  91. self.window('unfade')
  92. if (self.is(':visible'))
  93. return
  94. self.css('z-index', 1000)
  95. self.show()
  96. self.trigger('windowopen')
  97. var trigger = self.data('trigger')
  98. if (trigger) {
  99. trigger.addClass('selected')
  100. self.data('defaultIcon').removeClass('selected')
  101. }
  102. },
  103. close: function (closure) {
  104. var self = $(this)
  105. if (closure) {
  106. self.bind('windowclose', closure)
  107. return
  108. }
  109. if (!self.is(':visible'))
  110. return
  111. self.hide()
  112. var trigger = self.data('trigger')
  113. if (trigger) {
  114. trigger.removeClass('selected')
  115. self.data('defaultIcon').addClass('selected')
  116. }
  117. self.trigger('windowclose')
  118. },
  119. toggle: function () {
  120. var self = $(this)
  121. if (self.is(':visible')) {
  122. if (self == self.data('defaultIcon'))
  123. self.window('close')
  124. }
  125. else
  126. self.window('open')
  127. },
  128. fade: function () {
  129. var self = $(this)
  130. if (self.is(':visible'))
  131. self.animate({
  132. opacity: 0.1
  133. }, 400)
  134. },
  135. unfade: function () {
  136. var self = $(this)
  137. if (self.is(':visible'))
  138. self.animate({
  139. opacity: 1
  140. }, 400)
  141. else
  142. self.css('opacity', 1)
  143. }
  144. }
  145. $.fn.window = function (method) {
  146. if (methods[method]) {
  147. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  148. } else if (typeof method === 'object' || !method) {
  149. return methods.init.apply(this, arguments);
  150. } else {
  151. $.error('Method ' + method + ' does not exist on jQuery.window');
  152. }
  153. }
  154. })(jQuery);
  155. //to open link in a new window
  156. jQuery('a[target^="_new"]').click(function() {
  157. return openWindow(this.href);
  158. })
  159. function openWindow(url) {
  160. if (window.innerWidth <= 640) {
  161. // if width is smaller then 640px, create a temporary a elm that will open the link in new tab
  162. var a = document.createElement('a');
  163. a.setAttribute("href", url);
  164. a.setAttribute("target", "_blank");
  165. var dispatch = document.createEvent("HTMLEvents");
  166. dispatch.initEvent("click", true, true);
  167. a.dispatchEvent(dispatch);
  168. }
  169. else {
  170. var width = window.innerWidth * 0.66 ;
  171. // define the height in
  172. var height = width * window.innerHeight / window.innerWidth ;
  173. // Ratio the hight to the width as the user screen ratio
  174. window.open(url , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
  175. }
  176. return false;
  177. }