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.
 
 
 

154 lines
2.8 KiB

  1. define([
  2. 'js/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. var 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 (e) {
  45. var args = [].slice.call(arguments, 1);
  46. args.splice(0, 0, event);
  47. callback.apply(null, [...args, e]);
  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. var el = this.el;
  61. var pat = el.parent();
  62. var posX = ~~((pat.width() / 2) - (el.width() / 2)) - 10;
  63. var 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. },
  76. hide: function () {
  77. if (this.beforeHide)
  78. this.beforeHide();
  79. this.shown = false;
  80. this.el.hide();
  81. },
  82. destroy: function () {
  83. this.offEvents();
  84. if (this.beforeDestroy)
  85. this.beforeDestroy();
  86. this.el.remove();
  87. },
  88. val: function (selector) {
  89. return this.find(selector).val();
  90. },
  91. setDisabled: function (isDisabled) {
  92. this.el.removeClass('disabled')
  93. if (isDisabled)
  94. this.el.addClass('disabled');
  95. },
  96. onEvent: function (event, callback) {
  97. var list = this.eventCallbacks[event] || (this.eventCallbacks[event] = []);
  98. var eventCallback = events.on(event, callback);
  99. list.push(eventCallback);
  100. return eventCallback;
  101. },
  102. offEvent: function (eventCallback) {
  103. for (var e in this.eventCallbacks) {
  104. this.eventCallbacks[e].forEach(function (c) {
  105. if (c == eventCallback)
  106. events.off(e, c);
  107. }, this);
  108. }
  109. },
  110. offEvents: function () {
  111. for (var e in this.eventCallbacks) {
  112. this.eventCallbacks[e].forEach(function (c) {
  113. events.off(e, c);
  114. }, this);
  115. }
  116. }
  117. };
  118. });