/branches/jsdoc_tk_gui/setup/workingDirectory/Webeo/gui/tools/Position.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 176 lines · 60 code · 22 blank · 94 comment · 7 complexity · e7e4827b73c2a92f54d29826d97996e0 MD5 · raw file

  1. ek.register("gui.tools.Position");
  2. ek.setVersion("gui.tools.Position", "0.5");
  3. /**
  4. * @fileoverview
  5. * The Position class allow you to place a Dom Object somewhere in the document
  6. *@author Sébastien Bordes => Sebastien dot Bordes at webeo dot fr
  7. *@version 1.0
  8. */
  9. /**
  10. * Field used to get the current version of this component
  11. * @type String
  12. * @see #getVersion
  13. */
  14. Position.version = "1.0";
  15. /**
  16. * Field used to say if the left gap must be applied to the left side or the right
  17. * @private
  18. * @type boolean
  19. */
  20. Position.prototype.isLeft = false;
  21. /**
  22. * Field used to store the horizontal gap in pixels
  23. * @private
  24. * @type int
  25. */
  26. Position.prototype.leftPos = 10;
  27. /**
  28. * Field used to say if the top gap must be applied to the top side or the bottom
  29. * @private
  30. * @type boolean
  31. */
  32. Position.prototype.isTop = false;
  33. /**
  34. * Field used to store the vertical gap in pixels
  35. * @private
  36. * @type int
  37. */
  38. Position.prototype.topPos = 5;
  39. /**
  40. * Indicate if the position must be horizontally centered
  41. * @private
  42. * @type boolean
  43. */
  44. Position.prototype.isLeftCentered = false;
  45. /**
  46. * Indicate if the position must be vertically centered
  47. * @private
  48. * @type boolean
  49. */
  50. Position.prototype.isTopCentered = false;
  51. /**
  52. * Indicate if the position is relative or aboslute
  53. * @private
  54. * @type boolean
  55. */
  56. Position.prototype.isRelative = false;
  57. /**
  58. * Construct a Position object
  59. * @class
  60. * This class is used to create a position for a dom object
  61. * Retourne les coordonnées d'un él?ment en fonction d'un autre
  62. * @constructor
  63. * @param isLeft
  64. * @param leftPos ==> décalage horizontal du tooltip
  65. * @param isTop
  66. * @param topPos ==> décalage vertical du tooltip
  67. * @param isLeftCentered ==> Centré horizontalement
  68. * @param isTopCentered ==> Centré verticalement
  69. * @param isRelative ==> Is relative or absolute
  70. * @return a new Position Object
  71. */
  72. function Position (isLeft, leftPos, isTop, topPos, isLeftCentered, isTopCentered, isRelative){
  73. this.isLeft = (isLeft)? isLeft : false;
  74. this.leftPos = (leftPos != undefined)? leftPos : 10;
  75. this.isTop = (isTop)? isTop : false;
  76. this.topPos = (topPos != undefined)? topPos : 5;
  77. this.isLeftCentered = (isLeftCentered)? isLeftCentered : false;
  78. this.isTopCentered = (isTopCentered)? isTopCentered : false;
  79. this.isRelative = (isRelative)? isRelative : false;
  80. }
  81. /**
  82. * Return the left position
  83. * @param offset the offset
  84. * @param width
  85. * @param ownWidth
  86. * @return the left position in pixels , for example: "20"
  87. */
  88. Position.prototype.getLeftPosition = function (offset, width, ownWidth){
  89. if(this.isRelative)
  90. return this.leftPos;
  91. if(this.isLeftCentered)
  92. return offset + (width/2) + this.leftPos - (ownWidth/2);
  93. else
  94. return offset + ( (this.isLeft) ? 0 : width ) + this.leftPos ;
  95. }
  96. /**
  97. * Return the top position
  98. * @param offset
  99. * @param height
  100. * @param ownHeight
  101. * @return the top position in pixels , for example: "60"
  102. */
  103. Position.prototype.getTopPosition = function (offset, height, ownHeight){
  104. if(this.isRelative)
  105. return this.topPos;
  106. if(this.isTopCentered)
  107. return offset + (height/2) + this.topPos - (ownHeight/2);
  108. else{
  109. return offset + ( (this.isTop) ? 0 : height ) + this.topPos ;
  110. }
  111. }
  112. /**
  113. * Return or display the version of Position Object
  114. * @param alert : must show an alert message wi the current version or not
  115. * @return the current version if alert = true
  116. */
  117. Position.prototype.getVersion = function (alert){
  118. if(alert)
  119. alert('Position.js => version: ' +this.version);
  120. else
  121. return this.version;
  122. }
  123. /**
  124. * Construct a PositionRenderer object
  125. * @class
  126. * This class is used to set the position of a dom element in fucntion of a relative element
  127. * @constructor
  128. */
  129. function PositionRenderer (dom, relativeDom, position){
  130. this.dom = Dom.getElement(dom);
  131. this.relativeDom = Dom.getElement(relativeDom);
  132. this.position = (position) ? position: new Position();
  133. this.render();
  134. }
  135. /**
  136. * Change the position of an element
  137. *@param position the new position of the element
  138. */
  139. PositionRenderer.prototype.setPosition = function (position){
  140. this.position = (position) ? position: new Position();
  141. this.render();
  142. }
  143. /**
  144. * Render the dom element
  145. *@private
  146. */
  147. PositionRenderer.prototype.render = function (){
  148. var ll = Dom.getStyle(this.dom, "width");
  149. this.dom.style.left = this.position.getLeftPosition(this.relativeDom.offsetLeft, this.relativeDom.offsetWidth, ll.substring(0,ll.indexOf('px')) ) +"px";
  150. var tt = Dom.getStyle(this.dom, "height");
  151. this.dom.style.top = this.position.getTopPosition(this.relativeDom.offsetTop, this.relativeDom.offsetHeight , tt.substring(0,tt.indexOf('px')) ) +"px";
  152. this.dom.style.position = (this.position.isRelative) ? "relative" : "absolute";
  153. alert('render left='+this.dom.style.left +' top='+this.dom.style.top);
  154. }