You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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