25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
3.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. this.el = $(this.tpl)
  18. .appendTo(container)
  19. .data('ui', this);
  20. this.el.on('mouseenter', this.onMouseEnter.bind(this, true));
  21. this.el.on('mouseleave', this.onMouseEnter.bind(this, false));
  22. if (this.modal)
  23. this.el.addClass('modal');
  24. if (this.postRender)
  25. this.postRender();
  26. if (this.centered) {
  27. this.centeredX = true;
  28. this.centeredY = true;
  29. }
  30. if ((this.centeredX) || (this.centeredY))
  31. this.center(this.centeredX, this.centeredY);
  32. this.shown = this.el.is(':visible');
  33. },
  34. onMouseEnter: function (enter) {
  35. events.emit('onUiHover', enter);
  36. },
  37. setOptions: function (options) {
  38. this.options = options;
  39. },
  40. on: function (el, eventName, callback) {
  41. if (typeof (el) === 'string')
  42. el = this.find(el);
  43. else
  44. el = $(el);
  45. el.on(eventName, function () {
  46. let args = [].slice.call(arguments, 1);
  47. args.splice(0, 0, eventName);
  48. callback.apply(null, args);
  49. });
  50. },
  51. find: function (selector) {
  52. return this.el.find(selector);
  53. },
  54. center: function (x, y) {
  55. if (x !== false)
  56. x = true;
  57. if (y !== false)
  58. y = true;
  59. this.centeredX = x;
  60. this.centeredY = y;
  61. let el = this.el;
  62. let pat = el.parent();
  63. if (!pat[0])
  64. return;
  65. let posX = ~~((pat.width() / 2) - (el.width() / 2)) - 10;
  66. let posY = ~~((pat.height() / 2) - (el.height() / 2)) - 10;
  67. el.css('position', 'absolute');
  68. if (x)
  69. el.css('left', posX);
  70. if (y)
  71. el.css('top', posY);
  72. },
  73. show: function () {
  74. if (this.modal)
  75. $('.modal').hide();
  76. this.shown = true;
  77. this.el.show();
  78. if (this.onAfterShow)
  79. this.onAfterShow();
  80. if ((this.centeredX) || (this.centeredY))
  81. this.center(this.centeredX, this.centeredY);
  82. },
  83. hide: function () {
  84. if (this.beforeHide)
  85. this.beforeHide();
  86. this.shown = false;
  87. this.el.hide();
  88. },
  89. destroy: function () {
  90. this.offEvents();
  91. if (this.beforeDestroy)
  92. this.beforeDestroy();
  93. this.el.remove();
  94. },
  95. val: function (selector) {
  96. return this.find(selector).val();
  97. },
  98. setDisabled: function (isDisabled) {
  99. this.el.removeClass('disabled');
  100. if (isDisabled)
  101. this.el.addClass('disabled');
  102. },
  103. onEvent: function (eventName, callback) {
  104. let list = this.eventCallbacks[eventName] || (this.eventCallbacks[eventName] = []);
  105. let eventCallback = events.on(eventName, callback);
  106. list.push(eventCallback);
  107. return eventCallback;
  108. },
  109. offEvent: function (eventCallback) {
  110. for (let e in this.eventCallbacks) {
  111. this.eventCallbacks[e].forEach(function (c) {
  112. if (c === eventCallback)
  113. events.off(e, c);
  114. }, this);
  115. }
  116. },
  117. offEvents: function () {
  118. for (let e in this.eventCallbacks) {
  119. this.eventCallbacks[e].forEach(function (c) {
  120. events.off(e, c);
  121. }, this);
  122. }
  123. }
  124. };
  125. });