Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

214 rader
4.0 KiB

  1. define([
  2. 'js/system/events'
  3. ], function (
  4. events
  5. ) {
  6. return {
  7. centeredX: false,
  8. centeredY: false,
  9. el: null,
  10. options: null,
  11. shown: true,
  12. eventCallbacks: {},
  13. render: function () {
  14. let container = '.ui-container';
  15. if (this.container)
  16. container += ' > ' + this.container;
  17. if (this.beforeRender)
  18. this.beforeRender();
  19. this.el = $(this.tpl)
  20. .appendTo(container)
  21. .data('ui', this);
  22. this.el.on('mouseenter', this.onMouseEnter.bind(this, true));
  23. this.el.on('mouseleave', this.onMouseEnter.bind(this, false));
  24. if (this.modal)
  25. this.el.addClass('modal');
  26. if (this.hasClose)
  27. this.buildClose();
  28. if (this.postRender)
  29. this.postRender();
  30. if (this.centered) {
  31. this.centeredX = true;
  32. this.centeredY = true;
  33. }
  34. if ((this.centeredX) || (this.centeredY))
  35. this.center(this.centeredX, this.centeredY);
  36. this.registerUiEvents();
  37. this.shown = this.el.is(':visible');
  38. events.emit('onAfterRenderUi', {
  39. ui: this
  40. });
  41. },
  42. registerUiEvents: function () {
  43. this.find('.btn').on('click', events.emit.bind(events, 'onClickButton'));
  44. this.find('.tab').on('click', events.emit.bind(events, 'onClickTab'));
  45. },
  46. onMouseEnter: function (enter) {
  47. events.emit('onUiHover', enter);
  48. },
  49. setOptions: function (options) {
  50. this.options = options;
  51. },
  52. on: function (el, eventName, callback) {
  53. if (typeof (el) === 'string')
  54. el = this.find(el);
  55. else
  56. el = $(el);
  57. el.on(eventName, function (e) {
  58. callback(e, eventName);
  59. });
  60. },
  61. find: function (selector) {
  62. return this.el.find(selector);
  63. },
  64. center: function (x, y) {
  65. if (x !== false)
  66. x = true;
  67. if (y !== false)
  68. y = true;
  69. this.centeredX = x;
  70. this.centeredY = y;
  71. let el = this.el;
  72. let pat = el.parent();
  73. if (!pat[0])
  74. return;
  75. let posX = ~~((pat.width() / 2) - (el.width() / 2));
  76. let posY = ~~((pat.height() / 2) - (el.height() / 2));
  77. el.css('position', 'absolute');
  78. if (x)
  79. el.css('left', posX);
  80. if (y)
  81. el.css('top', posY);
  82. },
  83. show: function () {
  84. if (this.shown)
  85. return;
  86. if (this.modal) {
  87. //Close any other open modal
  88. $('.modal').toArray().forEach(el => {
  89. const ui = $(el).data('ui');
  90. if (ui.shown)
  91. ui.hide();
  92. });
  93. }
  94. this.shown = true;
  95. if (this.isFlex)
  96. this.el.css('display', 'flex');
  97. else
  98. this.el.show();
  99. if (this.onAfterShow)
  100. this.onAfterShow();
  101. if ((this.centeredX) || (this.centeredY))
  102. this.center(this.centeredX, this.centeredY);
  103. events.emit('onShowUi', this);
  104. },
  105. hide: function () {
  106. if (!this.shown)
  107. return;
  108. if (this.beforeHide)
  109. this.beforeHide();
  110. this.shown = false;
  111. this.el.hide();
  112. if (this.afterHide)
  113. this.afterHide();
  114. events.emit('onHideUi', this);
  115. },
  116. destroy: function () {
  117. this.offEvents();
  118. if (this.beforeDestroy)
  119. this.beforeDestroy();
  120. this.el.remove();
  121. },
  122. val: function (selector) {
  123. return this.find(selector).val();
  124. },
  125. setDisabled: function (isDisabled) {
  126. this.el.removeClass('disabled');
  127. if (isDisabled)
  128. this.el.addClass('disabled');
  129. },
  130. onEvent: function (eventName, callback) {
  131. let list = this.eventCallbacks[eventName] || (this.eventCallbacks[eventName] = []);
  132. let eventCallback = events.on(eventName, callback);
  133. list.push(eventCallback);
  134. return eventCallback;
  135. },
  136. offEvent: function (eventCallback) {
  137. for (let e in this.eventCallbacks) {
  138. this.eventCallbacks[e].forEach(function (c) {
  139. if (c === eventCallback)
  140. events.off(e, c);
  141. }, this);
  142. }
  143. },
  144. offEvents: function () {
  145. for (let e in this.eventCallbacks) {
  146. this.eventCallbacks[e].forEach(function (c) {
  147. events.off(e, c);
  148. }, this);
  149. }
  150. },
  151. toggle: function () {
  152. if (!this.shown)
  153. this.show();
  154. else
  155. this.hide();
  156. events.emit('onToggleUi', this);
  157. },
  158. buildClose: function () {
  159. $('<div class="btn btnClose">X</div>')
  160. .appendTo(this.find('.heading').eq(0))
  161. .on('click', this.toggle.bind(this));
  162. }
  163. };
  164. });