Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

splitter.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Roundcube splitter GUI class
  3. *
  4. * @licstart The following is the entire license notice for the
  5. * JavaScript code in this file.
  6. *
  7. * Copyright (c) 2006-2014, The Roundcube Dev Team
  8. *
  9. * The JavaScript code in this page is free software: you can redistribute it
  10. * and/or modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation, either version 3 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * @licend The above is the entire license notice
  15. * for the JavaScript code in this file.
  16. *
  17. * @constructor
  18. */
  19. function rcube_splitter(attrib)
  20. {
  21. this.p1id = attrib.p1;
  22. this.p2id = attrib.p2;
  23. this.id = attrib.id ? attrib.id : this.p1id + '_' + this.p2id + '_splitter';
  24. this.orientation = attrib.orientation;
  25. this.horizontal = (this.orientation == 'horizontal' || this.orientation == 'h');
  26. this.pos = attrib.start ? attrib.start * 1 : 0;
  27. this.relative = attrib.relative ? true : false;
  28. this.drag_active = false;
  29. this.callback = attrib.callback;
  30. var me = this;
  31. this.init = function()
  32. {
  33. this.p1 = document.getElementById(this.p1id);
  34. this.p2 = document.getElementById(this.p2id);
  35. // create and position the handle for this splitter
  36. this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
  37. this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
  38. if (this.horizontal) {
  39. var top = this.p1pos.top + this.p1.offsetHeight;
  40. this.layer = new rcube_layer(this.id, {x: 0, y: top, height: 10,
  41. width: '100%', vis: 1, parent: this.p1.parentNode});
  42. }
  43. else {
  44. var left = this.p1pos.left + this.p1.offsetWidth;
  45. this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10,
  46. height: '100%', vis: 1, parent: this.p1.parentNode});
  47. }
  48. this.elm = this.layer.elm;
  49. this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
  50. this.elm.unselectable = 'on';
  51. // add the mouse event listeners
  52. $(this.elm).mousedown(onDragStart);
  53. if (bw.ie)
  54. $(window).resize(onResize);
  55. // read saved position from cookie
  56. var cookie = rcmail.get_cookie(this.id);
  57. if (cookie && !isNaN(cookie)) {
  58. this.pos = parseFloat(cookie);
  59. this.resize();
  60. }
  61. else if (this.pos) {
  62. this.resize();
  63. this.set_cookie();
  64. }
  65. };
  66. /**
  67. * Set size and position of all DOM objects
  68. * according to the saved splitter position
  69. */
  70. this.resize = function()
  71. {
  72. if (this.horizontal) {
  73. var lh = this.layer.height;
  74. this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
  75. this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
  76. this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
  77. if (bw.ie) {
  78. var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
  79. this.p2.style.height = (new_height > 0 ? new_height : 0) + 'px';
  80. }
  81. }
  82. else {
  83. this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
  84. this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
  85. this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
  86. if (bw.ie) {
  87. var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10) ;
  88. this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
  89. }
  90. }
  91. $(this.p2).resize();
  92. $(this.p1).resize();
  93. };
  94. /**
  95. * Handler for mousedown events
  96. */
  97. function onDragStart(e)
  98. {
  99. me.drag_active = true;
  100. // disable text selection while dragging the splitter
  101. if (bw.konq || bw.chrome || bw.safari)
  102. document.body.style.webkitUserSelect = 'none';
  103. me.p1pos = me.relative ? $(me.p1).position() : $(me.p1).offset();
  104. me.p2pos = me.relative ? $(me.p2).position() : $(me.p2).offset();
  105. // start listening to mousemove events
  106. $(document).on('mousemove.' + me.id, onDrag).on('mouseup.' + me.id, onDragStop);
  107. // enable dragging above iframes
  108. $('iframe').each(function() {
  109. $('<div class="iframe-splitter-fix"></div>')
  110. .css({background: '#fff',
  111. width: this.offsetWidth+'px', height: this.offsetHeight+'px',
  112. position: 'absolute', opacity: '0.001', zIndex: 1000
  113. })
  114. .css($(this).offset())
  115. .appendTo('body');
  116. });
  117. };
  118. /**
  119. * Handler for mousemove events
  120. */
  121. function onDrag(e)
  122. {
  123. if (!me.drag_active)
  124. return false;
  125. // with timing events dragging action is more responsive
  126. window.clearTimeout(me.ts);
  127. me.ts = window.setTimeout(function() { onDragAction(e); }, 1);
  128. return false;
  129. };
  130. function onDragAction(e)
  131. {
  132. var pos = rcube_event.get_mouse_pos(e);
  133. if (me.relative) {
  134. var parent = $(me.p1.parentNode).offset();
  135. pos.x -= parent.left;
  136. pos.y -= parent.top;
  137. }
  138. if (me.horizontal) {
  139. if (((pos.y - me.layer.height * 1.5) > me.p1pos.top) && ((pos.y + me.layer.height * 1.5) < (me.p2pos.top + me.p2.offsetHeight))) {
  140. me.pos = pos.y;
  141. me.resize();
  142. }
  143. }
  144. else if (((pos.x - me.layer.width * 1.5) > me.p1pos.left) && ((pos.x + me.layer.width * 1.5) < (me.p2pos.left + me.p2.offsetWidth))) {
  145. me.pos = pos.x;
  146. me.resize();
  147. }
  148. me.p1pos = me.relative ? $(me.p1).position() : $(me.p1).offset();
  149. me.p2pos = me.relative ? $(me.p2).position() : $(me.p2).offset();
  150. };
  151. /**
  152. * Handler for mouseup events
  153. */
  154. function onDragStop(e)
  155. {
  156. me.drag_active = false;
  157. // resume the ability to highlight text
  158. if (bw.konq || bw.chrome || bw.safari)
  159. document.body.style.webkitUserSelect = 'auto';
  160. // cancel the listening for drag events
  161. $(document).off('.' + me.id);
  162. // remove temp divs
  163. $('div.iframe-splitter-fix').remove();
  164. me.set_cookie();
  165. if (typeof me.callback == 'function')
  166. me.callback(me);
  167. return bw.safari ? true : rcube_event.cancel(e);
  168. };
  169. /**
  170. * Handler for window resize events
  171. */
  172. function onResize(e)
  173. {
  174. if (me.horizontal) {
  175. var new_height = parseInt(me.p2.parentNode.offsetHeight, 10) - parseInt(me.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
  176. me.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
  177. }
  178. else {
  179. var new_width = parseInt(me.p2.parentNode.offsetWidth, 10) - parseInt(me.p2.style.left, 10);
  180. me.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
  181. }
  182. };
  183. /**
  184. * Saves splitter position in cookie
  185. */
  186. this.set_cookie = function()
  187. {
  188. var exp = new Date();
  189. exp.setYear(exp.getFullYear() + 1);
  190. rcmail.set_cookie(this.id, this.pos, exp);
  191. };
  192. } // end class rcube_splitter