layer.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * $Id: TinyMCE_DOMUtils.class.js 91 2006-10-02 14:53:22Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
  6. */
  7. mox.require([
  8. 'mox.DOM',
  9. 'mox.geom.Rect'
  10. ], function() {
  11. // Shorten class names
  12. var DOM = mox.DOM;
  13. var Rect = mox.geom.Rect;
  14. /**#@+
  15. * @class Layer class.
  16. * @member mox.dom.Layer
  17. * @base mox.geom.Rect
  18. */
  19. mox.create('mox.dom.Layer:mox.geom.Rect', {
  20. /**
  21. * Constructor for the Layer. This class enables you to construct
  22. * floating layers that is visible on top of select input fields, flashes and iframes.
  23. *
  24. * @param {string} id Unique ID name for the layer.
  25. * @param {boolean} bm Block mode, defaults to true.
  26. * @constructor
  27. */
  28. Layer : function(id, bm) {
  29. this.parent(null, null, null, null);
  30. this.id = id;
  31. this.events = false;
  32. this.element = null;
  33. this.blockMode = typeof(bm) != 'undefined' ? bm : false;
  34. this.lastRect = new Rect(null, null, null, null);
  35. },
  36. /**#@+
  37. * @method
  38. */
  39. /**
  40. * Moves the layer relative to the specified HTML element.
  41. *
  42. * @param {HTMLElement} re Element to move the layer relative to.
  43. * @param {string} p Position of the layer tl = top left, tr = top right, bl = bottom left, br = bottom right.
  44. * @param {string} rf Reference point inside element default to tl. Values: tl = top left, tr = top right, bl = bottom left, br = bottom right.
  45. */
  46. moveRelativeTo : function(re, p, rp) {
  47. var x, y, r = DOM.getRect(re);
  48. this.updateRect();
  49. this.moveTo(0, 0);
  50. p = this.getLocation(p);
  51. rp = r.getLocation(rp);
  52. x = rp.x - p.x;
  53. y = rp.y - p.y;
  54. this.moveTo(x, y);
  55. },
  56. /**
  57. * Moves the layer relative in pixels.
  58. *
  59. * @param {int} x Horizontal relative position in pixels.
  60. * @param {int} y Vertical relative position in pixels.
  61. */
  62. moveBy : function(x, y) {
  63. this.updateRect();
  64. this.parent(x, y);
  65. this.update();
  66. },
  67. /**
  68. * Moves the layer absolute in pixels.
  69. *
  70. * @param {int} x Horizontal absolute position in pixels.
  71. * @param {int} y Vertical absolute position in pixels.
  72. */
  73. moveTo : function(x, y) {
  74. this.parent(x, y);
  75. this.update();
  76. },
  77. /**
  78. * Resizes the layer by the specified relative width and height.
  79. *
  80. * @param {int} w Relative width value.
  81. * @param {int} h Relative height value.
  82. */
  83. resizeBy : function(w, h) {
  84. this.updateRect();
  85. this.parent(w, h);
  86. this.update();
  87. },
  88. /**
  89. * Resizes the layer to the specified width and height.
  90. *
  91. * @param {int} w Width value.
  92. * @param {int} h Height value.
  93. */
  94. resizeTo : function(w, h) {
  95. this.parent(w, h);
  96. this.update();
  97. },
  98. /**
  99. * Set style.
  100. */
  101. setStyle : function(n, v) {
  102. DOM.setStyle(this.getElement(), n, v);
  103. },
  104. getStyle : function(n) {
  105. return DOM.getStyle(this.getElement(), n);
  106. },
  107. /**
  108. * Shows the layer.
  109. */
  110. show : function() {
  111. DOM.show(this.getElement());
  112. },
  113. /**
  114. * Hides the layer.
  115. */
  116. hide : function() {
  117. DOM.hide(this.getElement());
  118. },
  119. /**
  120. * Returns true/false if the layer is hidden or not.
  121. *
  122. * @return {bool} true/false if it's hidden or not.
  123. */
  124. isHidden : function() {
  125. return this.getStyle('display') == 'none';
  126. },
  127. /**
  128. * Sets the visbility of the current layer. This will set the visibility style attribute of the element.
  129. *
  130. * @param {bool} s Visibility state true/false if it should be visible.
  131. * @return {bool} Input state.
  132. */
  133. setVisible : function(s) {
  134. this.setStyle('visibility', s ? 'visible' : 'hidden');
  135. return s;
  136. },
  137. /**
  138. * Returns true/false if the layer is visible or not.
  139. *
  140. * @return true/false if it's visible or not.
  141. * @type boolean
  142. */
  143. isVisible : function() {
  144. return this.getStyle('visibility') == 'visible';
  145. },
  146. /**
  147. * Returns the DOM element that the layer is binded to.
  148. *
  149. * @return DOM HTML element.
  150. * @type HTMLElement
  151. */
  152. getElement : function() {
  153. //if (!this.element)
  154. // this.element = document.getElementById(this.id);
  155. return document.getElementById(this.id);
  156. },
  157. getEl : function() {
  158. return this.getElement();
  159. },
  160. /**
  161. * Sets the block mode. If you set this property to true a control box blocker iframe
  162. * will be added to the document since MSIE has a issue where select boxes are visible
  163. * through layers.
  164. *
  165. * @param {boolean} s Block mode state, true is the default value.
  166. */
  167. setBlockMode : function(s, u) {
  168. b = this.getBlocker();
  169. this.blockMode = s;
  170. if (u) {
  171. b.style.left = this.x + 'px';
  172. b.style.top = this.y + 'px';
  173. b.style.width = this.w + 'px';
  174. b.style.height = this.h + 'px';
  175. }
  176. },
  177. /**
  178. * Updates the internal rect with the values from the DOM element.
  179. */
  180. updateRect : function(f) {
  181. if (this.x == null || f)
  182. DOM.getRect(this.getElement(), true, this);
  183. },
  184. /**
  185. * Updates the layers DOM element to the internal rectangle cordinates.
  186. * This method will also update the blocker iframe element in IE if that feature is enabled.
  187. */
  188. update : function() {
  189. var e = this.getElement(), b;
  190. if (this.x != this.lastRect.x)
  191. e.style.left = this.x + 'px';
  192. if (this.y != this.lastRect.y)
  193. e.style.top = this.y + 'px';
  194. if (this.w >= 0 && this.w != this.lastRect.w && this.w != null)
  195. e.style.width = this.w + 'px';
  196. if (this.h >= 0 && this.h != this.lastRect.h && this.h != null)
  197. e.style.height = this.h + 'px';
  198. if (this.blockMode) {
  199. b = this.getBlocker();
  200. if (this.x != this.lastRect.x)
  201. b.style.left = this.x + 'px';
  202. if (this.y != this.lastRect.y)
  203. b.style.top = this.y + 'px';
  204. if (this.w != this.lastRect.w && this.w != null)
  205. b.style.width = this.w + 'px';
  206. if (this.h != this.lastRect.h && this.h != null)
  207. b.style.height = this.h + 'px';
  208. b.style.zIndex = (parseInt(this.getEl().style.zIndex) - 1);
  209. b.style.display = e.style.display;
  210. }
  211. this.lastRect.set(this.x, this.y, this.w, this.h);
  212. },
  213. /**
  214. * Returns the blocker DOM element, this is a invisible iframe.
  215. *
  216. * @return DOM HTML element.
  217. * @type HTMLElement
  218. */
  219. getBlocker : function() {
  220. var d, b;
  221. if (this.blockMode) {
  222. d = document;
  223. b = d.getElementById(this.id + "_blocker");
  224. if (!b) {
  225. b = d.createElement("iframe");
  226. b.setAttribute('id', this.id + "_blocker");
  227. b.style.cssText = 'display: none; position: absolute; left: 0; top: 0;';
  228. b.src = 'about:blank';
  229. b.frameBorder = '0';
  230. b.scrolling = 'no';
  231. d.body.appendChild(b);
  232. }
  233. return b;
  234. }
  235. return null;
  236. },
  237. /**
  238. * Returns true/false if a element exists for the layer.
  239. *
  240. * @return true/false if a element exists for the layer.
  241. * @type boolean
  242. */
  243. exists : function() {
  244. return document.getElementById(this.id) != null;
  245. },
  246. /**
  247. * Parses a int value this method will return 0 if the string is empty.
  248. *
  249. * @param {string} s String to parse value of.
  250. * @return Parsed number.
  251. * @type int
  252. */
  253. parseInt : function(s) {
  254. if (s == null || s == '')
  255. return 0;
  256. return parseInt(s);
  257. },
  258. /**
  259. * Parses a float value this method will return 0 if the string is empty.
  260. *
  261. * @param {string} s String to parse value of.
  262. * @return Parsed number.
  263. * @type int
  264. */
  265. parseFloat : function(s) {
  266. if (s == null || s == '' || s == '0')
  267. return 0;
  268. return parseFloat(s);
  269. },
  270. /**
  271. * Removes the element for the layer from DOM and also the blocker iframe.
  272. */
  273. remove : function() {
  274. var e = this.getElement(), b = this.getBlocker();
  275. if (e)
  276. e.parentNode.removeChild(e);
  277. if (b)
  278. b.parentNode.removeChild(b);
  279. }
  280. });
  281. /**#@-*/
  282. });