SharedWidgetHolder.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) Business Objects 2006. All rights reserved. */
  2. if (typeof(bobj.crv.SharedWidgetHolder) == 'undefined') {
  3. bobj.crv.SharedWidgetHolder = {};
  4. bobj.crv.SharedWidgetHolder._registry = {};
  5. }
  6. /**
  7. * Constructor. SharedWidgetHolder is a placeholder for a widget.
  8. * SharedWidgetHolder instances belong to groups, in which one managed
  9. * widget will be shown in one placeholder at a time. When show(true) is called
  10. * for any member of the group, the managed widget will be displayed in that
  11. * member.
  12. *
  13. * @param id [String] DHTML id
  14. * @param group [String] Group the new instance will belong to
  15. * @param width [int | String] Width of the placeholder
  16. * @param height [int | String] Height of the placeholder
  17. * @param resizeWidget [bool] Resize to the placeholder's dimensions
  18. */
  19. bobj.crv.newSharedWidgetHolder = function(kwArgs) {
  20. var mb = MochiKit.Base;
  21. var ms = MochiKit.Signal;
  22. kwArgs = mb.update({
  23. id: bobj.uniqueId(),
  24. group: 'SharedWidgetHolder',
  25. width: null,
  26. height: null,
  27. resizeWidget: true
  28. }, kwArgs);
  29. var o = newWidget(kwArgs.id);
  30. o.widgetType = 'SharedWidgetHolder';
  31. bobj.fillIn(o, kwArgs);
  32. o._setVisible = o.show;
  33. o._resizeHolder = o.resize;
  34. o._initHolder = o.init;
  35. mb.update(o, bobj.crv.SharedWidgetHolder);
  36. o._register();
  37. return o;
  38. };
  39. bobj.crv.SharedWidgetHolder.init = function() {
  40. this._initHolder();
  41. var regInfo = this._regInfo;
  42. if (regInfo.managedWidget && this === regInfo.visibleHolder) {
  43. regInfo.managedWidget.init();
  44. this.resize();
  45. }
  46. };
  47. bobj.crv.SharedWidgetHolder.getHTML = function() {
  48. var ISNUMBER = bobj.isNumber;
  49. var ISSTRING = bobj.isString;
  50. var vis = this.isHoldingWidget() ? 'visible' : 'hidden';
  51. var innerHTML = this.isHoldingWidget() ? this._regInfo.managedWidget.getHTML() : '';
  52. var style = {visibility: vis};
  53. var width = this.width;
  54. if (ISNUMBER(width)) {
  55. width = width + 'px';
  56. }
  57. if (ISSTRING(width)) {
  58. style.width = width;
  59. }
  60. var height = this.height;
  61. if (ISNUMBER(height)) {
  62. height = height + 'px';
  63. }
  64. if (ISSTRING(height)) {
  65. style.height = height;
  66. }
  67. return bobj.html.DIV({id: this.id, style: style}, innerHTML);
  68. };
  69. /**
  70. * Private. Register this instance in a group.
  71. */
  72. bobj.crv.SharedWidgetHolder._register = function() {
  73. var registry = bobj.crv.SharedWidgetHolder._registry;
  74. var holderInfo = registry[this.group];
  75. if (!holderInfo) {
  76. holderInfo = {
  77. managedWidget: null,
  78. visibleHolder: this
  79. };
  80. registry[this.group] = holderInfo;
  81. }
  82. this._regInfo = holderInfo;
  83. };
  84. /**
  85. * @return [bool] True if and only if this instance is currently holding a
  86. * non-null managed widget. Only one member of a group can return
  87. * true at any given time.
  88. */
  89. bobj.crv.SharedWidgetHolder.isHoldingWidget = function() {
  90. var regInfo = this._regInfo;
  91. return ((regInfo.visibleHolder === this) && regInfo.managedWidget);
  92. };
  93. /**
  94. * @return [Widget] The widget that is managed by the holder group that this
  95. * instance belongs to.
  96. */
  97. bobj.crv.SharedWidgetHolder.getManagedWidget = function() {
  98. return this._regInfo.managedWidget;
  99. };
  100. /**
  101. * Set the widget that is managed by the group that this isntance belongs to.
  102. * The widget will be displayed in the currently visible member of the group.
  103. *
  104. * @param widget [Widget] The widget that should be managed.
  105. */
  106. bobj.crv.SharedWidgetHolder.setManagedWidget = function(widget) {
  107. var regInfo = this._regInfo;
  108. var oldWidget = regInfo.managedWidget;
  109. regInfo.managedWidget = widget;
  110. if (oldWidget && oldWidget.layer) {
  111. MochiKit.DOM.removeElement(oldWidget.layer);
  112. }
  113. if (!regInfo.visibleHolder) {
  114. regInfo.visibleHolder = this;
  115. }
  116. var holder = regInfo.visibleHolder;
  117. if (holder.layer && widget && widget.layer) {
  118. holder.layer.appendChild(widget.layer);
  119. holder.resize();
  120. }
  121. };
  122. /**
  123. * Alias for setManagedWidget. Allows instantiation using bobj.crv.createWidget();
  124. */
  125. bobj.crv.SharedWidgetHolder.addChild = bobj.crv.SharedWidgetHolder.setManagedWidget;
  126. /**
  127. * Show or hide the managed widget in this holder instance.
  128. *
  129. * @param show [bool] If true, the managed widget will be displayed in this
  130. * instance. If false and this holder is currently showing
  131. * the managed widget, the widget will be hidden.
  132. */
  133. bobj.crv.SharedWidgetHolder.show = function(show) {
  134. var regInfo = this._regInfo;
  135. if (show && (regInfo.visibleHolder !== this) && regInfo.managedWidget) {
  136. this.layer.appendChild(regInfo.managedWidget.layer);
  137. regInfo.visibleHolder._setVisible(false);
  138. regInfo.visibleHolder = this;
  139. this.resize();
  140. }
  141. this._setVisible(show);
  142. };
  143. /**
  144. * Resize the holder instance. If resizeWidget property is true, the managed
  145. * widget will be resized to the dimensions of the placeholder.
  146. *
  147. * @param w [width, optional] Width in pixels
  148. * @param h [height, optional] Height in pixels
  149. */
  150. bobj.crv.SharedWidgetHolder.resize = function(w, h) {
  151. this._resizeHolder(w, h);
  152. if (this.resizeWidget && this.isHoldingWidget()) {
  153. this._regInfo.managedWidget.resize(this.getWidth(), this.getHeight());
  154. }
  155. };