hLayout.js 7.1 KB

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