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.
 
 
 

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