Element.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Element.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function(tinymce) {
  11. /**
  12. * Element class, this enables element blocking in IE. Element blocking is a method to block out select blockes that
  13. * gets visible though DIVs on IE 6 it uses a iframe for this blocking. This class also shortens the length of some DOM API calls
  14. * since it's bound to an element.
  15. *
  16. * @class tinymce.dom.Element
  17. * @example
  18. * // Creates an basic element for an existing element
  19. * var elm = new tinymce.dom.Element('someid');
  20. *
  21. * elm.setStyle('background-color', 'red');
  22. * elm.moveTo(10, 10);
  23. */
  24. /**
  25. * Constructs a new Element instance. Consult the Wiki for more details on this class.
  26. *
  27. * @constructor
  28. * @method Element
  29. * @param {String} id Element ID to bind/execute methods on.
  30. * @param {Object} settings Optional settings name/value collection.
  31. */
  32. tinymce.dom.Element = function(id, settings) {
  33. var t = this, dom, el;
  34. t.settings = settings = settings || {};
  35. t.id = id;
  36. t.dom = dom = settings.dom || tinymce.DOM;
  37. // Only IE leaks DOM references, this is a lot faster
  38. if (!tinymce.isIE)
  39. el = dom.get(t.id);
  40. tinymce.each(
  41. ('getPos,getRect,getParent,add,setStyle,getStyle,setStyles,' +
  42. 'setAttrib,setAttribs,getAttrib,addClass,removeClass,' +
  43. 'hasClass,getOuterHTML,setOuterHTML,remove,show,hide,' +
  44. 'isHidden,setHTML,get').split(/,/)
  45. , function(k) {
  46. t[k] = function() {
  47. var a = [id], i;
  48. for (i = 0; i < arguments.length; i++)
  49. a.push(arguments[i]);
  50. a = dom[k].apply(dom, a);
  51. t.update(k);
  52. return a;
  53. };
  54. });
  55. tinymce.extend(t, {
  56. /**
  57. * Adds a event handler to the element.
  58. *
  59. * @method on
  60. * @param {String} n Event name like for example "click".
  61. * @param {function} f Function to execute on the specified event.
  62. * @param {Object} s Optional scope to execute function on.
  63. * @return {function} Event handler function the same as the input function.
  64. */
  65. on : function(n, f, s) {
  66. return tinymce.dom.Event.add(t.id, n, f, s);
  67. },
  68. /**
  69. * Returns the absolute X, Y cordinate of the element.
  70. *
  71. * @method getXY
  72. * @return {Object} Objext with x, y cordinate fields.
  73. */
  74. getXY : function() {
  75. return {
  76. x : parseInt(t.getStyle('left')),
  77. y : parseInt(t.getStyle('top'))
  78. };
  79. },
  80. /**
  81. * Returns the size of the element by a object with w and h fields.
  82. *
  83. * @method getSize
  84. * @return {Object} Object with element size with a w and h field.
  85. */
  86. getSize : function() {
  87. var n = dom.get(t.id);
  88. return {
  89. w : parseInt(t.getStyle('width') || n.clientWidth),
  90. h : parseInt(t.getStyle('height') || n.clientHeight)
  91. };
  92. },
  93. /**
  94. * Moves the element to a specific absolute position.
  95. *
  96. * @method moveTo
  97. * @param {Number} x X cordinate of element position.
  98. * @param {Number} y Y cordinate of element position.
  99. */
  100. moveTo : function(x, y) {
  101. t.setStyles({left : x, top : y});
  102. },
  103. /**
  104. * Moves the element relative to the current position.
  105. *
  106. * @method moveBy
  107. * @param {Number} x Relative X cordinate of element position.
  108. * @param {Number} y Relative Y cordinate of element position.
  109. */
  110. moveBy : function(x, y) {
  111. var p = t.getXY();
  112. t.moveTo(p.x + x, p.y + y);
  113. },
  114. /**
  115. * Resizes the element to a specific size.
  116. *
  117. * @method resizeTo
  118. * @param {Number} w New width of element.
  119. * @param {Numner} h New height of element.
  120. */
  121. resizeTo : function(w, h) {
  122. t.setStyles({width : w, height : h});
  123. },
  124. /**
  125. * Resizes the element relative to the current sizeto a specific size.
  126. *
  127. * @method resizeBy
  128. * @param {Number} w Relative width of element.
  129. * @param {Numner} h Relative height of element.
  130. */
  131. resizeBy : function(w, h) {
  132. var s = t.getSize();
  133. t.resizeTo(s.w + w, s.h + h);
  134. },
  135. /**
  136. * Updates the element blocker in IE6 based on the style information of the element.
  137. *
  138. * @method update
  139. * @param {String} k Optional function key. Used internally.
  140. */
  141. update : function(k) {
  142. var b;
  143. if (tinymce.isIE6 && settings.blocker) {
  144. k = k || '';
  145. // Ignore getters
  146. if (k.indexOf('get') === 0 || k.indexOf('has') === 0 || k.indexOf('is') === 0)
  147. return;
  148. // Remove blocker on remove
  149. if (k == 'remove') {
  150. dom.remove(t.blocker);
  151. return;
  152. }
  153. if (!t.blocker) {
  154. t.blocker = dom.uniqueId();
  155. b = dom.add(settings.container || dom.getRoot(), 'iframe', {id : t.blocker, style : 'position:absolute;', frameBorder : 0, src : 'javascript:""'});
  156. dom.setStyle(b, 'opacity', 0);
  157. } else
  158. b = dom.get(t.blocker);
  159. dom.setStyles(b, {
  160. left : t.getStyle('left', 1),
  161. top : t.getStyle('top', 1),
  162. width : t.getStyle('width', 1),
  163. height : t.getStyle('height', 1),
  164. display : t.getStyle('display', 1),
  165. zIndex : parseInt(t.getStyle('zIndex', 1) || 0) - 1
  166. });
  167. }
  168. }
  169. });
  170. };
  171. })(tinymce);