You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

228 lines
12 KiB

  1. /*
  2. * jQuery.splitter.js - two-pane splitter window plugin
  3. *
  4. * version 1.51 (2009/01/09)
  5. *
  6. * Dual licensed under the MIT and GPL licenses:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.gnu.org/licenses/gpl.html
  9. */
  10. /**
  11. * The splitter() plugin implements a two-pane resizable splitter window.
  12. * The selected elements in the jQuery object are converted to a splitter;
  13. * each selected element should have two child elements, used for the panes
  14. * of the splitter. The plugin adds a third child element for the splitbar.
  15. *
  16. * For more details see: http://methvin.com/splitter/
  17. *
  18. *
  19. * @example $('#MySplitter').splitter();
  20. * @desc Create a vertical splitter with default settings
  21. *
  22. * @example $('#MySplitter').splitter({type: 'h', accessKey: 'M'});
  23. * @desc Create a horizontal splitter resizable via Alt+Shift+M
  24. *
  25. * @name splitter
  26. * @type jQuery
  27. * @param Object options Options for the splitter (not required)
  28. * @cat Plugins/Splitter
  29. * @return jQuery
  30. * @author Dave Methvin (dave.methvin@gmail.com)
  31. */
  32. ;
  33. (function($) {
  34. $.fn.splitter = function(args) {
  35. args = args || {};
  36. return this.each(function() {
  37. var zombie; // left-behind splitbar for outline resizes
  38. function startSplitMouse(evt) {
  39. if (opts.outline)
  40. zombie = zombie || bar.clone(false).insertAfter(A);
  41. panes.css("-webkit-user-select", "none"); // Safari selects A/B text on a move
  42. bar.addClass(opts.activeClass);
  43. $('<div class="splitterMask"></div>').insertAfter(bar);
  44. A._posSplit = A[0][opts.pxSplit] - evt[opts.eventPos];
  45. $(document)
  46. .bind("mousemove", doSplitMouse)
  47. .bind("mouseup", endSplitMouse);
  48. }
  49. function doSplitMouse(evt) {
  50. var newPos = A._posSplit + evt[opts.eventPos];
  51. if (opts.outline) {
  52. newPos = Math.max(0, Math.min(newPos, splitter._DA - bar._DA));
  53. bar.css(opts.origin, newPos);
  54. } else
  55. resplit(newPos);
  56. }
  57. function endSplitMouse(evt) {
  58. $('div.splitterMask').remove();
  59. bar.removeClass(opts.activeClass);
  60. var newPos = A._posSplit + evt[opts.eventPos];
  61. if (opts.outline) {
  62. zombie.remove();
  63. zombie = null;
  64. resplit(newPos);
  65. }
  66. panes.css("-webkit-user-select", "text"); // let Safari select text again
  67. $(document)
  68. .unbind("mousemove", doSplitMouse)
  69. .unbind("mouseup", endSplitMouse);
  70. }
  71. function resplit(newPos) {
  72. // Constrain new splitbar position to fit pane size limits
  73. newPos = Math.max(A._min, splitter._DA - B._max,
  74. Math.min(newPos, A._max, splitter._DA - bar._DA - B._min));
  75. // Resize/position the two panes
  76. bar._DA = bar[0][opts.pxSplit]; // bar size may change during dock
  77. bar.css(opts.origin, newPos).css(opts.fixed, splitter._DF);
  78. A.css(opts.origin, 0).css(opts.split, newPos).css(opts.fixed, splitter._DF);
  79. B.css(opts.origin, newPos + bar._DA)
  80. .css(opts.split, splitter._DA - bar._DA - newPos).css(opts.fixed, splitter._DF);
  81. // IE fires resize for us; all others pay cash
  82. if (!$.browser.msie)
  83. panes.trigger("resize");
  84. }
  85. function dimSum(jq, dims) {
  86. // Opera returns -1 for missing min/max width, turn into 0
  87. var sum = 0;
  88. for (var i = 1; i < arguments.length; i++)
  89. sum += Math.max(parseInt(jq.css(arguments[i])) || 0, 0);
  90. return sum;
  91. }
  92. // Determine settings based on incoming opts, element classes, and defaults
  93. var vh = (args.splitHorizontal ? 'h' : args.splitVertical ? 'v' : args.type) || 'v';
  94. var opts = $.extend({
  95. activeClass: 'active', // class name for active splitter
  96. pxPerKey: 8, // splitter px moved per keypress
  97. tabIndex: 0, // tab order indicator
  98. accessKey: '' // accessKey for splitbar
  99. }, {
  100. v: { // Vertical splitters:
  101. keyLeft: 39, keyRight: 37, cursor: "e-resize",
  102. splitbarClass: "vsplitbar", outlineClass: "voutline",
  103. type: 'v', eventPos: "pageX", origin: "left",
  104. split: "width", pxSplit: "offsetWidth", side1: "Left", side2: "Right",
  105. fixed: "height", pxFixed: "offsetHeight", side3: "Top", side4: "Bottom"
  106. },
  107. h: { // Horizontal splitters:
  108. keyTop: 40, keyBottom: 38, cursor: "n-resize",
  109. splitbarClass: "hsplitbar", outlineClass: "houtline",
  110. type: 'h', eventPos: "pageY", origin: "top",
  111. split: "height", pxSplit: "offsetHeight", side1: "Top", side2: "Bottom",
  112. fixed: "width", pxFixed: "offsetWidth", side3: "Left", side4: "Right"
  113. }
  114. }[vh], args);
  115. // Create jQuery object closures for splitter and both panes
  116. var splitter = $(this).css({position: "relative"});
  117. var panes = $(">*", splitter[0]).css({
  118. position: "absolute", // positioned inside splitter container
  119. "z-index": "1", // splitbar is positioned above
  120. "-moz-outline-style": "none" // don't show dotted outline
  121. });
  122. var A = $(panes[0]); // left or top
  123. var B = $(panes[1]); // right or bottom
  124. // Focuser element, provides keyboard support; title is shown by Opera accessKeys
  125. var focuser = $('<a href="javascript:void(0)"></a>')
  126. .attr({accessKey: opts.accessKey, tabIndex: opts.tabIndex, title: opts.splitbarClass})
  127. .bind($.browser.opera ? "click" : "focus", function() {
  128. this.focus();
  129. bar.addClass(opts.activeClass)
  130. })
  131. .bind("keydown", function(e) {
  132. var key = e.which || e.keyCode;
  133. var dir = key == opts["key" + opts.side1] ? 1 : key == opts["key" + opts.side2] ? -1 : 0;
  134. if (dir)
  135. resplit(A[0][opts.pxSplit] + dir * opts.pxPerKey, false);
  136. })
  137. .bind("blur", function() {
  138. bar.removeClass(opts.activeClass)
  139. });
  140. // Splitbar element, can be already in the doc or we create one
  141. var bar = $(panes[2] || '<div></div>')
  142. .insertAfter(A).css("z-index", "100").append(focuser)
  143. .attr({"class": opts.splitbarClass, unselectable: "on"})
  144. .css({position: "absolute", "user-select": "none", "-webkit-user-select": "none",
  145. "-khtml-user-select": "none", "-moz-user-select": "none", "top": "0px"})
  146. .bind("mousedown", startSplitMouse);
  147. // Use our cursor unless the style specifies a non-default cursor
  148. if (/^(auto|default|)$/.test(bar.css("cursor")))
  149. bar.css("cursor", opts.cursor);
  150. // Cache several dimensions for speed, rather than re-querying constantly
  151. bar._DA = bar[0][opts.pxSplit];
  152. splitter._PBF = $.boxModel ? dimSum(splitter, "border" + opts.side3 + "Width", "border" + opts.side4 + "Width") : 0;
  153. splitter._PBA = $.boxModel ? dimSum(splitter, "border" + opts.side1 + "Width", "border" + opts.side2 + "Width") : 0;
  154. A._pane = opts.side1;
  155. B._pane = opts.side2;
  156. $.each([A,B], function() {
  157. this._min = opts["min" + this._pane] || dimSum(this, "min-" + opts.split);
  158. this._max = opts["max" + this._pane] || dimSum(this, "max-" + opts.split) || 9999;
  159. this._init = opts["size" + this._pane] === true ?
  160. parseInt($.curCSS(this[0], opts.split)) : opts["size" + this._pane];
  161. });
  162. // Determine initial position, get from cookie if specified
  163. var initPos = A._init;
  164. if (!isNaN(B._init)) // recalc initial B size as an offset from the top or left side
  165. initPos = splitter[0][opts.pxSplit] - splitter._PBA - B._init - bar._DA;
  166. if (opts.cookie) {
  167. if (!$.cookie)
  168. alert('jQuery.splitter(): jQuery cookie plugin required');
  169. var ckpos = parseInt($.cookie(opts.cookie));
  170. if (!isNaN(ckpos))
  171. initPos = ckpos;
  172. $(window).bind("unload", function() {
  173. var state = String(bar.css(opts.origin)); // current location of splitbar
  174. $.cookie(opts.cookie, state, {expires: opts.cookieExpires || 365,
  175. path: opts.cookiePath || document.location.pathname});
  176. });
  177. }
  178. if (isNaN(initPos)) // King Solomon's algorithm
  179. initPos = Math.round((splitter[0][opts.pxSplit] - splitter._PBA - bar._DA) / 2);
  180. // Resize event propagation and splitter sizing
  181. if (opts.anchorToWindow) {
  182. // Account for margin or border on the splitter container and enforce min height
  183. splitter._hadjust = dimSum(splitter, "borderTopWidth", "borderBottomWidth", "marginBottom");
  184. splitter._hmin = Math.max(dimSum(splitter, "minHeight"), 20);
  185. $(window).bind("resize",
  186. function() {
  187. var top = splitter.offset().top;
  188. var wh = $(window).height();
  189. splitter.css("height", Math.max(wh - top - splitter._hadjust, splitter._hmin) + "px");
  190. if (!$.browser.msie) splitter.trigger("resize");
  191. }).trigger("resize");
  192. }
  193. else if (opts.resizeToWidth && !$.browser.msie)
  194. $(window).bind("resize", function() {
  195. splitter.trigger("resize");
  196. });
  197. // Resize event handler; triggered immediately to set initial position
  198. splitter.bind("resize",
  199. function(e, size) {
  200. // Custom events bubble in jQuery 1.3; don't Yo Dawg
  201. if (e.target != this) return;
  202. // Determine new width/height of splitter container
  203. splitter._DF = splitter[0][opts.pxFixed] - splitter._PBF;
  204. splitter._DA = splitter[0][opts.pxSplit] - splitter._PBA;
  205. // Bail if splitter isn't visible or content isn't there yet
  206. if (splitter._DF <= 0 || splitter._DA <= 0) return;
  207. // Re-divvy the adjustable dimension; maintain size of the preferred pane
  208. resplit(!isNaN(size) ? size : (!(opts.sizeRight || opts.sizeBottom) ? A[0][opts.pxSplit] :
  209. splitter._DA - B[0][opts.pxSplit] - bar._DA));
  210. }).trigger("resize", [initPos]);
  211. });
  212. };
  213. })(jQuery);