SplitContainer.js.uncompressed.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. define("dijit/layout/SplitContainer", [
  2. "dojo/_base/array", // array.forEach array.indexOf array.some
  3. "dojo/cookie", // cookie
  4. "dojo/_base/declare", // declare
  5. "dojo/dom", // dom.setSelectable
  6. "dojo/dom-class", // domClass.add
  7. "dojo/dom-construct", // domConstruct.create domConstruct.destroy
  8. "dojo/dom-geometry", // domGeometry.marginBox domGeometry.position
  9. "dojo/dom-style", // domStyle.style
  10. "dojo/_base/event", // event.stop
  11. "dojo/_base/kernel", // kernel.deprecated
  12. "dojo/_base/lang", // lang.extend lang.hitch
  13. "dojo/on",
  14. "dojo/sniff", // has("mozilla")
  15. "../registry", // registry.getUniqueId()
  16. "../_WidgetBase",
  17. "./_LayoutWidget"
  18. ], function(array, cookie, declare, dom, domClass, domConstruct, domGeometry, domStyle,
  19. event, kernel, lang, on, has, registry, _WidgetBase, _LayoutWidget){
  20. // module:
  21. // dijit/layout/SplitContainer
  22. //
  23. // FIXME: make it prettier
  24. // FIXME: active dragging upwards doesn't always shift other bars (direction calculation is wrong in this case)
  25. // FIXME: sizeWidth should be a CSS attribute (at 7 because css wants it to be 7 until we fix to css)
  26. //
  27. var SplitContainer = declare("dijit.layout.SplitContainer", _LayoutWidget, {
  28. // summary:
  29. // Deprecated. Use `dijit/layout/BorderContainer` instead.
  30. // description:
  31. // A Container widget with sizing handles in-between each child.
  32. // Contains multiple children widgets, all of which are displayed side by side
  33. // (either horizontally or vertically); there's a bar between each of the children,
  34. // and you can adjust the relative size of each child by dragging the bars.
  35. //
  36. // You must specify a size (width and height) for the SplitContainer.
  37. //
  38. // See `SplitContainer.ChildWidgetProperties` for details on the properties that can be set on
  39. // children of a `SplitContainer`.
  40. // tags:
  41. // deprecated
  42. constructor: function(){
  43. kernel.deprecated("dijit.layout.SplitContainer is deprecated", "use BorderContainer with splitter instead", 2.0);
  44. },
  45. // activeSizing: Boolean
  46. // If true, the children's size changes as you drag the bar;
  47. // otherwise, the sizes don't change until you drop the bar (by mouse-up)
  48. activeSizing: false,
  49. // sizerWidth: Integer
  50. // Size in pixels of the bar between each child
  51. sizerWidth: 7,
  52. // orientation: String
  53. // either 'horizontal' or vertical; indicates whether the children are
  54. // arranged side-by-side or up/down.
  55. orientation: 'horizontal',
  56. // persist: Boolean
  57. // Save splitter positions in a cookie
  58. persist: true,
  59. baseClass: "dijitSplitContainer",
  60. postMixInProperties: function(){
  61. this.inherited("postMixInProperties",arguments);
  62. this.isHorizontal = (this.orientation == 'horizontal');
  63. },
  64. postCreate: function(){
  65. this.inherited(arguments);
  66. this.sizers = [];
  67. // overflow has to be explicitly hidden for splitContainers using gekko (trac #1435)
  68. // to keep other combined css classes from inadvertantly making the overflow visible
  69. if(has("mozilla")){
  70. this.domNode.style.overflow = '-moz-scrollbars-none'; // hidden doesn't work
  71. }
  72. // create the fake dragger
  73. if(typeof this.sizerWidth == "object"){
  74. try{ //FIXME: do this without a try/catch
  75. this.sizerWidth = parseInt(this.sizerWidth.toString());
  76. }catch(e){ this.sizerWidth = 7; }
  77. }
  78. var sizer = this.ownerDocument.createElement('div');
  79. this.virtualSizer = sizer;
  80. sizer.style.position = 'relative';
  81. // #1681: work around the dreaded 'quirky percentages in IE' layout bug
  82. // If the splitcontainer's dimensions are specified in percentages, it
  83. // will be resized when the virtualsizer is displayed in _showSizingLine
  84. // (typically expanding its bounds unnecessarily). This happens because
  85. // we use position: relative for .dijitSplitContainer.
  86. // The workaround: instead of changing the display style attribute,
  87. // switch to changing the zIndex (bring to front/move to back)
  88. sizer.style.zIndex = 10;
  89. sizer.className = this.isHorizontal ? 'dijitSplitContainerVirtualSizerH' : 'dijitSplitContainerVirtualSizerV';
  90. this.domNode.appendChild(sizer);
  91. dom.setSelectable(sizer, false);
  92. },
  93. destroy: function(){
  94. delete this.virtualSizer;
  95. if(this._ownconnects){
  96. var h;
  97. while(h = this._ownconnects.pop()){ h.remove(); }
  98. }
  99. this.inherited(arguments);
  100. },
  101. startup: function(){
  102. if(this._started){ return; }
  103. array.forEach(this.getChildren(), function(child, i, children){
  104. // attach the children and create the draggers
  105. this._setupChild(child);
  106. if(i < children.length-1){
  107. this._addSizer();
  108. }
  109. }, this);
  110. if(this.persist){
  111. this._restoreState();
  112. }
  113. this.inherited(arguments);
  114. },
  115. _setupChild: function(/*dijit/_WidgetBase*/ child){
  116. this.inherited(arguments);
  117. child.domNode.style.position = "absolute";
  118. domClass.add(child.domNode, "dijitSplitPane");
  119. },
  120. _onSizerMouseDown: function(e){
  121. if(e.target.id){
  122. for(var i=0;i<this.sizers.length;i++){
  123. if(this.sizers[i].id == e.target.id){
  124. break;
  125. }
  126. }
  127. if(i<this.sizers.length){
  128. this.beginSizing(e,i);
  129. }
  130. }
  131. },
  132. _addSizer: function(index){
  133. index = index === undefined ? this.sizers.length : index;
  134. // TODO: use a template for this!!!
  135. var sizer = this.ownerDocument.createElement('div');
  136. sizer.id=registry.getUniqueId('dijit_layout_SplitterContainer_Splitter');
  137. this.sizers.splice(index,0,sizer);
  138. this.domNode.appendChild(sizer);
  139. sizer.className = this.isHorizontal ? 'dijitSplitContainerSizerH' : 'dijitSplitContainerSizerV';
  140. // add the thumb div
  141. var thumb = this.ownerDocument.createElement('div');
  142. thumb.className = 'thumb';
  143. sizer.appendChild(thumb);
  144. // FIXME: are you serious? why aren't we using mover start/stop combo?
  145. this.connect(sizer, "onmousedown", '_onSizerMouseDown');
  146. dom.setSelectable(sizer, false);
  147. },
  148. removeChild: function(widget){
  149. // summary:
  150. // Remove sizer, but only if widget is really our child and
  151. // we have at least one sizer to throw away
  152. if(this.sizers.length){
  153. var i = array.indexOf(this.getChildren(), widget);
  154. if(i != -1){
  155. if(i == this.sizers.length){
  156. i--;
  157. }
  158. domConstruct.destroy(this.sizers[i]);
  159. this.sizers.splice(i,1);
  160. }
  161. }
  162. // Remove widget and repaint
  163. this.inherited(arguments);
  164. if(this._started){
  165. this.layout();
  166. }
  167. },
  168. addChild: function(/*dijit/_WidgetBase*/ child, /*Integer?*/ insertIndex){
  169. // summary:
  170. // Add a child widget to the container
  171. // child:
  172. // a widget to add
  173. // insertIndex:
  174. // postion in the "stack" to add the child widget
  175. this.inherited(arguments);
  176. if(this._started){
  177. // Do the stuff that startup() does for each widget
  178. var children = this.getChildren();
  179. if(children.length > 1){
  180. this._addSizer(insertIndex);
  181. }
  182. // and then reposition (ie, shrink) every pane to make room for the new guy
  183. this.layout();
  184. }
  185. },
  186. layout: function(){
  187. // summary:
  188. // Do layout of panels
  189. // base class defines this._contentBox on initial creation and also
  190. // on resize
  191. this.paneWidth = this._contentBox.w;
  192. this.paneHeight = this._contentBox.h;
  193. var children = this.getChildren();
  194. if(!children.length){ return; }
  195. //
  196. // calculate space
  197. //
  198. var space = this.isHorizontal ? this.paneWidth : this.paneHeight;
  199. if(children.length > 1){
  200. space -= this.sizerWidth * (children.length - 1);
  201. }
  202. //
  203. // calculate total of SizeShare values
  204. //
  205. var outOf = 0;
  206. array.forEach(children, function(child){
  207. outOf += child.sizeShare;
  208. });
  209. //
  210. // work out actual pixels per sizeshare unit
  211. //
  212. var pixPerUnit = space / outOf;
  213. //
  214. // set the SizeActual member of each pane
  215. //
  216. var totalSize = 0;
  217. array.forEach(children.slice(0, children.length - 1), function(child){
  218. var size = Math.round(pixPerUnit * child.sizeShare);
  219. child.sizeActual = size;
  220. totalSize += size;
  221. });
  222. children[children.length-1].sizeActual = space - totalSize;
  223. //
  224. // make sure the sizes are ok
  225. //
  226. this._checkSizes();
  227. //
  228. // now loop, positioning each pane and letting children resize themselves
  229. //
  230. var pos = 0;
  231. var size = children[0].sizeActual;
  232. this._movePanel(children[0], pos, size);
  233. children[0].position = pos;
  234. pos += size;
  235. // if we don't have any sizers, our layout method hasn't been called yet
  236. // so bail until we are called..TODO: REVISIT: need to change the startup
  237. // algorithm to guaranteed the ordering of calls to layout method
  238. if(!this.sizers){
  239. return;
  240. }
  241. array.some(children.slice(1), function(child, i){
  242. // error-checking
  243. if(!this.sizers[i]){
  244. return true;
  245. }
  246. // first we position the sizing handle before this pane
  247. this._moveSlider(this.sizers[i], pos, this.sizerWidth);
  248. this.sizers[i].position = pos;
  249. pos += this.sizerWidth;
  250. size = child.sizeActual;
  251. this._movePanel(child, pos, size);
  252. child.position = pos;
  253. pos += size;
  254. }, this);
  255. },
  256. _movePanel: function(panel, pos, size){
  257. var box;
  258. if(this.isHorizontal){
  259. panel.domNode.style.left = pos + 'px'; // TODO: resize() takes l and t parameters too, don't need to set manually
  260. panel.domNode.style.top = 0;
  261. box = {w: size, h: this.paneHeight};
  262. if(panel.resize){
  263. panel.resize(box);
  264. }else{
  265. domGeometry.setMarginBox(panel.domNode, box);
  266. }
  267. }else{
  268. panel.domNode.style.left = 0; // TODO: resize() takes l and t parameters too, don't need to set manually
  269. panel.domNode.style.top = pos + 'px';
  270. box = {w: this.paneWidth, h: size};
  271. if(panel.resize){
  272. panel.resize(box);
  273. }else{
  274. domGeometry.setMarginBox(panel.domNode, box);
  275. }
  276. }
  277. },
  278. _moveSlider: function(slider, pos, size){
  279. if(this.isHorizontal){
  280. slider.style.left = pos + 'px';
  281. slider.style.top = 0;
  282. domGeometry.setMarginBox(slider, { w: size, h: this.paneHeight });
  283. }else{
  284. slider.style.left = 0;
  285. slider.style.top = pos + 'px';
  286. domGeometry.setMarginBox(slider, { w: this.paneWidth, h: size });
  287. }
  288. },
  289. _growPane: function(growth, pane){
  290. if(growth > 0){
  291. if(pane.sizeActual > pane.sizeMin){
  292. if((pane.sizeActual - pane.sizeMin) > growth){
  293. // stick all the growth in this pane
  294. pane.sizeActual = pane.sizeActual - growth;
  295. growth = 0;
  296. }else{
  297. // put as much growth in here as we can
  298. growth -= pane.sizeActual - pane.sizeMin;
  299. pane.sizeActual = pane.sizeMin;
  300. }
  301. }
  302. }
  303. return growth;
  304. },
  305. _checkSizes: function(){
  306. var totalMinSize = 0;
  307. var totalSize = 0;
  308. var children = this.getChildren();
  309. array.forEach(children, function(child){
  310. totalSize += child.sizeActual;
  311. totalMinSize += child.sizeMin;
  312. });
  313. // only make adjustments if we have enough space for all the minimums
  314. if(totalMinSize <= totalSize){
  315. var growth = 0;
  316. array.forEach(children, function(child){
  317. if(child.sizeActual < child.sizeMin){
  318. growth += child.sizeMin - child.sizeActual;
  319. child.sizeActual = child.sizeMin;
  320. }
  321. });
  322. if(growth > 0){
  323. var list = this.isDraggingLeft ? children.reverse() : children;
  324. array.forEach(list, function(child){
  325. growth = this._growPane(growth, child);
  326. }, this);
  327. }
  328. }else{
  329. array.forEach(children, function(child){
  330. child.sizeActual = Math.round(totalSize * (child.sizeMin / totalMinSize));
  331. });
  332. }
  333. },
  334. beginSizing: function(e, i){
  335. // summary:
  336. // Begin dragging the splitter between child[i] and child[i+1]
  337. var children = this.getChildren();
  338. this.paneBefore = children[i];
  339. this.paneAfter = children[i+1];
  340. this.paneBefore.sizeBeforeDrag = this.paneBefore.sizeActual;
  341. this.paneAfter.sizeBeforeDrag = this.paneAfter.sizeActual;
  342. this.paneAfter.positionBeforeDrag = this.paneAfter.position;
  343. this.isSizing = true;
  344. this.sizingSplitter = this.sizers[i];
  345. this.sizingSplitter.positionBeforeDrag = domStyle.get(this.sizingSplitter,(this.isHorizontal ? "left" : "top"));
  346. if(!this.cover){
  347. this.cover = domConstruct.create('div', {
  348. style: {
  349. position:'absolute',
  350. zIndex:5,
  351. top: 0,
  352. left: 0,
  353. width: "100%",
  354. height: "100%"
  355. }
  356. }, this.domNode);
  357. }else{
  358. this.cover.style.zIndex = 5;
  359. }
  360. this.sizingSplitter.style.zIndex = 6;
  361. // startPoint is the e.pageX or e.pageY at start of drag
  362. this.startPoint = this.lastPoint = (this.isHorizontal ? e.pageX : e.pageY);
  363. // Calculate maximum to the left or right that splitter is allowed to be dragged
  364. // minDelta is negative to indicate left/upward drag where end.pageX < start.pageX.
  365. this.maxDelta = this.paneAfter.sizeActual - this.paneAfter.sizeMin;
  366. this.minDelta = -1 * (this.paneBefore.sizeActual - this.paneBefore.sizeMin);
  367. if(!this.activeSizing){
  368. this._showSizingLine();
  369. }
  370. // attach mouse events
  371. this._ownconnects = [
  372. on(this.ownerDocument.documentElement, "mousemove", lang.hitch(this, "changeSizing")),
  373. on(this.ownerDocument.documentElement, "mouseup", lang.hitch(this, "endSizing"))
  374. ];
  375. event.stop(e);
  376. },
  377. changeSizing: function(e){
  378. // summary:
  379. // Called on mousemove while dragging the splitter
  380. if(!this.isSizing){ return; }
  381. // lastPoint is the most recent e.pageX or e.pageY during the drag
  382. this.lastPoint = this.isHorizontal ? e.pageX : e.pageY;
  383. var delta = Math.max(Math.min(this.lastPoint - this.startPoint, this.maxDelta), this.minDelta);
  384. if(this.activeSizing){
  385. this._updateSize(delta);
  386. }else{
  387. this._moveSizingLine(delta);
  388. }
  389. event.stop(e);
  390. },
  391. endSizing: function(){
  392. if(!this.isSizing){ return; }
  393. if(this.cover){
  394. this.cover.style.zIndex = -1;
  395. }
  396. if(!this.activeSizing){
  397. this._hideSizingLine();
  398. }
  399. var delta = Math.max(Math.min(this.lastPoint - this.startPoint, this.maxDelta), this.minDelta);
  400. this._updateSize(delta);
  401. this.isSizing = false;
  402. if(this.persist){
  403. this._saveState(this);
  404. }
  405. var h;
  406. while(h = this._ownconnects.pop()){ h.remove(); }
  407. },
  408. _updateSize: function(/*Number*/ delta){
  409. // summary:
  410. // Resets sizes of panes before and after splitter being dragged.
  411. // Called during a drag, for active sizing, or at the end of a drag otherwise.
  412. // delta: Number
  413. // Change in slider position compared to start of drag. But note that
  414. // this function may be called multiple times during drag.
  415. this.paneBefore.sizeActual = this.paneBefore.sizeBeforeDrag + delta;
  416. this.paneAfter.position = this.paneAfter.positionBeforeDrag + delta;
  417. this.paneAfter.sizeActual = this.paneAfter.sizeBeforeDrag - delta;
  418. array.forEach(this.getChildren(), function(child){
  419. child.sizeShare = child.sizeActual;
  420. });
  421. if(this._started){
  422. this.layout();
  423. }
  424. },
  425. _showSizingLine: function(){
  426. // summary:
  427. // Show virtual splitter, for non-active resizing
  428. this._moveSizingLine(0);
  429. domGeometry.setMarginBox(this.virtualSizer,
  430. this.isHorizontal ? { w: this.sizerWidth, h: this.paneHeight } : { w: this.paneWidth, h: this.sizerWidth });
  431. this.virtualSizer.style.display = 'block';
  432. },
  433. _hideSizingLine: function(){
  434. this.virtualSizer.style.display = 'none';
  435. },
  436. _moveSizingLine: function(/*Number*/ delta){
  437. // summary:
  438. // Called for non-active resizing, to move the virtual splitter without adjusting the size of the panes
  439. var pos = delta + this.sizingSplitter.positionBeforeDrag;
  440. domStyle.set(this.virtualSizer,(this.isHorizontal ? "left" : "top"),pos+"px");
  441. },
  442. _getCookieName: function(i){
  443. return this.id + "_" + i;
  444. },
  445. _restoreState: function(){
  446. array.forEach(this.getChildren(), function(child, i){
  447. var cookieName = this._getCookieName(i);
  448. var cookieValue = cookie(cookieName);
  449. if(cookieValue){
  450. var pos = parseInt(cookieValue);
  451. if(typeof pos == "number"){
  452. child.sizeShare = pos;
  453. }
  454. }
  455. }, this);
  456. },
  457. _saveState: function(){
  458. if(!this.persist){
  459. return;
  460. }
  461. array.forEach(this.getChildren(), function(child, i){
  462. cookie(this._getCookieName(i), child.sizeShare, {expires:365});
  463. }, this);
  464. }
  465. });
  466. SplitContainer.ChildWidgetProperties = {
  467. // summary:
  468. // These properties can be specified for the children of a SplitContainer.
  469. // sizeMin: [deprecated] Integer
  470. // Minimum size (width or height) of a child of a SplitContainer.
  471. // The value is relative to other children's sizeShare properties.
  472. sizeMin: 10,
  473. // sizeShare: [deprecated] Integer
  474. // Size (width or height) of a child of a SplitContainer.
  475. // The value is relative to other children's sizeShare properties.
  476. // For example, if there are two children and each has sizeShare=10, then
  477. // each takes up 50% of the available space.
  478. sizeShare: 10
  479. };
  480. // Since any widget can be specified as a SplitContainer child, mix them
  481. // into the base widget class. (This is a hack, but it's effective.)
  482. // This is for the benefit of the parser. Remove for 2.0. Also, hide from doc viewer.
  483. lang.extend(_WidgetBase, /*===== {} || =====*/ SplitContainer.ChildWidgetProperties);
  484. return SplitContainer;
  485. });