PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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