HLayout.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var md;
  2. function startHLayoutEvents() {
  3. // Global mouse event;
  4. $(document).on('mousemove',function(e) {
  5. if(md == undefined) md = {x:e.clientX,y:e.clientY};
  6. md.deltaX = e.clientX - md.x;
  7. md.deltaY = e.clientY - md.y;
  8. md.x = e.clientX;
  9. md.y = e.clientY;
  10. });
  11. var $grab = undefined;
  12. $(document).on('mousedown','.hpane > header',function(e){
  13. if(e.which != 1) return;
  14. if(!$(e.target).is(".hpane > header")) return;
  15. e.preventDefault();
  16. e.stopPropagation();
  17. $grab = $(this).parent();
  18. $parent = $grab.parent();
  19. var next = $grab.next();
  20. var origW = $grab.width();
  21. var origH = $grab.height();
  22. // Check if we are root?
  23. var dim = {w: $grab.width(), h: $grab.height()};
  24. var pos = $grab.position();
  25. $($grab).detach().appendTo('body').addClass("detached").addClass("moving").css({width:dim.w,height:dim.h});
  26. $('.hpane:not(.detached)').addClass("discover");
  27. // All childs to pointer event none
  28. if($parent.is('section')) {
  29. // We aren't
  30. $child = $parent.children('.hpane, section');
  31. $child.attr('style','');
  32. $child.css('flex-basis',$parent.css('flex-basis'));
  33. $child.detach();
  34. $parent.replaceWith($child);
  35. }
  36. })
  37. var $sectionResize = null;
  38. $(document).on('mousedown','section',function(e) {
  39. var $target = $(e.target);
  40. if(!$target.is("section"))return;
  41. e.preventDefault();
  42. $sectionResize = $target;
  43. $sectionResize.addClass("resizing");
  44. });
  45. $(document).on('mousemove',function(e) {
  46. if($sectionResize == null) return;
  47. var $first = $sectionResize.children(':nth-child(1)');
  48. var $second = $sectionResize.children(':nth-child(2)');
  49. var common;
  50. var common2;
  51. if( $sectionResize.is(".horizontal")) {common = "height",common2 = "deltaY"};
  52. if( $sectionResize.is(".vertical")) {common = "width", common2 = "deltaX"};
  53. var parentValue = $sectionResize[common]();
  54. var hFirst = $first[common]();
  55. var hSec = $second[common]();
  56. var newVal1 = (hFirst + md[common2]) / parentValue * 100;
  57. var newVal2 = (hSec -md[common2]) / parentValue * 100;
  58. $first.css('flex-basis',newVal1 + "%");
  59. $second.css('flex-basis',newVal2 + "%");
  60. });
  61. $(document).on('mouseup',function(e) {
  62. if($sectionResize) {
  63. $sectionResize.removeClass("resizing");
  64. }
  65. $sectionResize = null;
  66. });
  67. $(document).on('mousemove',function(e) {
  68. if($grab == undefined) return;
  69. var pos = $grab.position();
  70. var nx = pos.left + md.deltaX;
  71. var ny = pos.top + md.deltaY;
  72. var offX = $grab.width() / 2;
  73. var offY = 0;
  74. $grab.css({left: e.clientX - offX, top: e.clientY -offY});
  75. // Check target
  76. var $target = $(e.target);
  77. if(!$target.is(".hpane")) {
  78. $target = $target.parents(".hpane");
  79. }
  80. if($target.is(".hpane:not(.preview)")){
  81. var placement = checkAttach($target,e);
  82. if(placement != 0 ) {
  83. previewPanel($grab,$target,placement);
  84. } else {
  85. removePreviews();
  86. }
  87. } else {
  88. removePreviews();
  89. }
  90. });
  91. $(document).on('mouseup',function(e) {
  92. if($grab == undefined) return;
  93. removePreviews();
  94. $grab.removeClass("moving");
  95. $('.hpane').removeClass("discover");
  96. $target = $(e.target);
  97. if(!$target.is(".hpane")) $target = $target.parents('.hpane');
  98. if($target.is(".hpane")){
  99. var placement = checkAttach($target,e);
  100. if(placement != 0 ) {
  101. $grab.removeClass("detached");
  102. $grab.attr('style','');
  103. attachPanel($grab,$target,placement);
  104. }
  105. }
  106. $grab = undefined;
  107. return;
  108. });
  109. }
  110. function recalcFlex() {
  111. $('section.horizontal > section, section.horizontal > .hpane').each(function() {
  112. var parentH = $(this).parent().height();
  113. var curH = $(this).height();
  114. var flex = 100 * curH / parentH;
  115. $(this).css({'flex-basis':flex + "%"});
  116. });
  117. $('section.vertical > section, section.vertical > .hpane').each(function() {
  118. var parentW = $(this).parent().width();
  119. var curW = $(this).width();
  120. var flex = 100 * curW / parentW;
  121. $(this).css({'flex-basis':flex + "%"});
  122. });
  123. }
  124. recalcFlex();
  125. function checkAttach($target,e) {
  126. var tW = $target.width()/3;
  127. var tH = $target.height()/3;
  128. var targetPos = $target.offset();
  129. var dim = { w: $target.width(), h: $target.height()};
  130. var rPos = { x: e.pageX - targetPos.left, y: e.pageY - targetPos.top};
  131. //Calc dists and check the closest one
  132. if(rPos.x > tW && rPos.x < dim.w - tW && rPos.y < tH) { // 1 Top
  133. return 1;
  134. } else if(rPos.x > tW && rPos.x < dim.w - tW && rPos.y > dim.h - tH){ // 3 Bottom
  135. return 3;
  136. } else if(rPos.x < tW) { // 2 Left
  137. return 2;
  138. } else if( rPos.x > dim.w - tW){ // 4 Right
  139. return 4;
  140. }
  141. return 0;
  142. }
  143. function removePreviews() {
  144. $('.attach-preview').hide();
  145. }
  146. function previewPanel($grab,$target,attach) {
  147. $previewDiv = $(".attach-preview");
  148. if($previewDiv.length == 0 ) {
  149. $previewDiv = $('<div class="attach-preview" style="display:none;position:fixed"></div>').appendTo('body');
  150. }
  151. $previewDiv.show();
  152. var dim = {w:$target.width(),h:$target.height()};
  153. var pos = $target.offset();
  154. if((attach % 2) == 1) {
  155. $previewDiv.css('height',dim.h/3);
  156. $previewDiv.css("width",dim.w);
  157. $previewDiv.css("left",pos.left);
  158. if(attach == 1) {
  159. $previewDiv.css('top',pos.top);
  160. } else {
  161. $previewDiv.css('top',pos.top + dim.h - dim.h/3);
  162. }
  163. } else if((attach % 2) == 0) {
  164. $previewDiv.css("height",dim.h);
  165. $previewDiv.css('width',dim.w/3);
  166. $previewDiv.css("top",pos.top);
  167. if(attach == 2) {
  168. $previewDiv.css('left',pos.left);
  169. } else {
  170. $previewDiv.css('left',pos.left + dim.w - dim.w/3);
  171. }
  172. }
  173. }
  174. function attachPanel(what,target, attach) {
  175. if(attach){
  176. var type
  177. if((attach % 2) == 1)type = "horizontal";
  178. if((attach % 2) == 0)type = "vertical";
  179. $target = $(target);
  180. $parent = $(target).parent();
  181. $sib = $(target);
  182. if($sib.next().is("section, .hpane,.splitter")) {
  183. $section = $("<section class='" + type +"' style='flex-basis:" + $target.css('flex-basis') + "'></section>").prependTo($parent);
  184. } else {
  185. $section = $("<section class='" + type + "' style='flex-basis:" + $target.css('flex-basis') +" '></section>").appendTo($parent);
  186. }
  187. $sib.detach();
  188. what.css('flex-basis','33%');
  189. $sib.css('flex-basis','66%');
  190. if(attach < 3) {
  191. $section.append(what);
  192. //$section.append("<div class='splitter'></div>");
  193. $section.append($sib);
  194. } else {
  195. $section.append($sib);
  196. //$section.append("<div class='splitter'></div>");
  197. $section.append(what);
  198. }
  199. }
  200. }
  201. $(function() {
  202. startHLayoutEvents();
  203. });