slideResize.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/11/7
  7. * @version
  8. */
  9. /*
  10. * 拖动相关公共接口
  11. * 前台使用此接口时,注意在恢复系统默认设置时,
  12. * 清除相关缓存数据: project_view.js--->$('#property_default').click(callback)
  13. * */
  14. const SlideResize = (function() {
  15. //设置水平拖动条的宽度
  16. //@param {Object dom}resize滚动条
  17. function setResizeWidth (resize,otherIndex) {
  18. const fixedWidth = 5;
  19. //滚动条节点 及 同层非滚动条节点的索引
  20. let bros = resize.parent().children();
  21. // let index = bros.index(resize),
  22. otherIndex = otherIndex ? otherIndex : 1;
  23. const other = resize.parent().children(`:eq(${otherIndex})`);
  24. let resizeParentWidth = resize.parent().width();
  25. let resizeDecimalWidth = fixedWidth / resizeParentWidth,
  26. otherDecimalWidth = 1 - resizeDecimalWidth;
  27. if(bros.length > 2){ //当同层有多个子元素时,other 的宽度要排除其余的宽度
  28. for(let i = 1; i< bros.length;i++){
  29. if(i == otherIndex) continue;
  30. otherDecimalWidth = otherDecimalWidth - $(bros[i]).width()/resizeParentWidth
  31. }
  32. }
  33. let resizePercentWidth = resizeDecimalWidth * 100 + '%',
  34. otherPercentWidth = otherDecimalWidth * 100 + '%';
  35. resize.css('width', resizePercentWidth);
  36. other.css('width', otherPercentWidth);
  37. }
  38. let mouseMoveCount = 0;
  39. /*
  40. * div之间的水平拖动,适应各种情况
  41. * @param {Object}eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module)
  42. * resize, parent, left, right
  43. * @param {Object}limit: min, max
  44. * @param {Function}callback 回调
  45. * */
  46. function horizontalSlide(eleObj, limit, callback,otherIndex) {//otherIndex 材料计算,spread表格在最右加,要用这个来识别
  47. const triggerCBSize = 5;
  48. let drag = false,
  49. startPoint = 0,
  50. leftWidth = 0,
  51. rightWidth = 0,
  52. leftChange = 0,
  53. rightChange = 0,
  54. limitMax = 0;
  55. eleObj.resize.mousedown(function (e) {
  56. drag = true;
  57. startPoint = e.clientX;
  58. leftWidth = eleObj.left.width();
  59. rightWidth = eleObj.right.width();
  60. limitMax = eval(limit.max);
  61. });
  62. $('body').mousemove(function (e) {
  63. if (drag) {
  64. let moveSize = e.clientX - startPoint;
  65. leftChange = leftWidth + moveSize;
  66. leftChange = leftChange < limit.min ? limit.min : leftChange;
  67. leftChange = leftChange > limitMax ? limitMax - 5 : leftChange;
  68. rightChange = rightWidth - moveSize;
  69. rightChange = rightChange < limit.min ? limit.min : rightChange;
  70. rightChange = rightChange > limitMax ? limitMax - 5 : rightChange;
  71. let leftPercentWidth = leftChange / eleObj.parent.width() * 100 + '%',
  72. rightPercentWidth = rightChange / eleObj.parent.width() * 100 + '%';
  73. eleObj.left.css('width', leftPercentWidth);
  74. eleObj.right.css('width', rightPercentWidth);
  75. setResizeWidth(eleObj.resize,otherIndex);
  76. callback();
  77. /* mouseMoveCount += Math.abs(moveSize);
  78. if (mouseMoveCount > triggerCBSize && callback) {
  79. callback();
  80. mouseMoveCount = 0;
  81. }*/
  82. }
  83. });
  84. $('body').mouseup(function (e) {
  85. if (drag) {
  86. drag = false;
  87. mouseMoveCount = 0;
  88. //将宽度信息存储到localstorage
  89. let leftWidthInfo = eleObj.left[0].style.width;
  90. setLocalCache(`${eleObj.module}${eleObj.left.attr('id')}Width`, leftWidthInfo);
  91. let rightWidthInfo = eleObj.right[0].style.width;
  92. setLocalCache(`${eleObj.module}${eleObj.right.attr('id')}Width`, rightWidthInfo);
  93. }
  94. });
  95. }
  96. function loadHorizonWidth(module, resizes, eles, callback,otherIndex) {
  97. for (let ele of eles) {
  98. let cache = getLocalCache(`${module}${ele.attr('id')}Width`);
  99. if (cache) {
  100. ele.css('width', cache);
  101. }
  102. }
  103. for (let resize of resizes) {
  104. setResizeWidth(resize,otherIndex);
  105. }
  106. if (callback) {
  107. callback();
  108. }
  109. }
  110. /*
  111. * div上下拖动
  112. * @param {Object} eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module)
  113. * resize, top, topSpread, bottom, bottomSpread
  114. * @param {Object}limit: min, max, notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  115. * @param {Function}callback 回调
  116. * */
  117. function verticalSlide(eleObj, limit, callback) {
  118. const triggerCBSize = 5;
  119. let drag = false,
  120. startPoint = 0,
  121. topHeight = 0,
  122. bottomHeight = 0,
  123. topChange = 0,
  124. bottomChange = 0,
  125. limitMax = 0;
  126. eleObj.resize.mousedown(function (e) {
  127. drag = true;
  128. startPoint = e.clientY;
  129. topHeight = eleObj.top.height();
  130. bottomHeight = eleObj.bottom.height();
  131. limitMax = eval(limit.max);
  132. });
  133. $('body').mousemove(function (e) {
  134. if (drag) {
  135. let moveSize = e.clientY - startPoint;
  136. topChange = topHeight + moveSize;
  137. topChange = topChange < limit.min ? limit.min : topChange;
  138. topChange = topChange > limitMax ? limitMax : topChange;
  139. bottomChange = bottomHeight - moveSize;
  140. bottomChange = bottomChange < limit.min ? limit.min : bottomChange;
  141. bottomChange = bottomChange > limitMax ? limitMax : bottomChange;
  142. //设置上部分div高度
  143. eleObj.top.height(topChange);
  144. //设置上部分div内spread高度
  145. eleObj.topSpread.height(topChange - limit.notTopSpread);
  146. //设置下部分div高度
  147. eleObj.bottom.height(bottomChange);
  148. //设置下部分div内spread高度
  149. let notBottomHeight = limit.bottomNav?eval(limit.bottomNav):limit.notBottomSpread;
  150. if(Array.isArray(eleObj.bottomSpread)){
  151. for(let b of eleObj.bottomSpread){
  152. b.height(bottomChange - notBottomHeight);
  153. }
  154. }else {
  155. eleObj.bottomSpread.height(bottomChange - notBottomHeight);
  156. }
  157. mouseMoveCount += Math.abs(moveSize);
  158. if (mouseMoveCount > triggerCBSize && callback) {
  159. callback();
  160. mouseMoveCount = 0;
  161. }
  162. }
  163. });
  164. $('body').mouseup(function (e) {
  165. if (drag) {
  166. drag = false;
  167. mouseMoveCount = 0;
  168. //将高度信息存储到localstorage
  169. let topHeightInfo = eleObj.top.height();
  170. setLocalCache(`${eleObj.module}${eleObj.top.attr('id')}Height`, topHeightInfo);
  171. let bottomHeightInfo = eleObj.bottom.height();
  172. setLocalCache(`${eleObj.module}${eleObj.bottom.attr('id')}Height`, bottomHeightInfo);
  173. }
  174. });
  175. }
  176. /*
  177. * 加载上下高度
  178. * @param {String}module 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  179. * @param {Object}eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module) top, topSpread, bottom, bottomSpread
  180. * @param {Object}limit: totalHeight(实时的上下部分总高度) notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  181. * @param {Function}callback 回调
  182. * */
  183. function loadVerticalHeight(module, eleObj, limit, callback) {
  184. let topHeight = getLocalCache(`${module}${eleObj.top.attr('id')}Height`),
  185. bottomHeight = getLocalCache(`${module}${eleObj.bottom.attr('id')}Height`);
  186. //默认上下比例
  187. const topProp = 5;
  188. const bottomProp = 2;
  189. let topProportion = topProp / (topProp + bottomProp);
  190. if (topHeight !== null && bottomHeight !== null) {
  191. topHeight = parseFloat(topHeight);
  192. bottomHeight = parseFloat(bottomHeight);
  193. topProportion = topHeight / (topHeight + bottomHeight);
  194. }
  195. //设置当前窗口下的的上下部分高度
  196. let totalHeight = eval(limit.totalHeight);
  197. let curTopHeight = totalHeight * topProportion,
  198. curBottomHeight = totalHeight - curTopHeight;
  199. eleObj.top.height(curTopHeight);
  200. eleObj.topSpread.height(curTopHeight - limit.notTopSpread);
  201. eleObj.bottom.height(curBottomHeight);
  202. let notBottomHeight = limit.bottomNav?eval(limit.bottomNav):limit.notBottomSpread;
  203. if(Array.isArray(eleObj.bottomSpread)){
  204. for(let b of eleObj.bottomSpread){
  205. b.height(curBottomHeight - notBottomHeight);
  206. }
  207. }else {
  208. eleObj.bottomSpread.height(curBottomHeight - notBottomHeight);
  209. }
  210. if (callback) {
  211. callback();
  212. }
  213. }
  214. /*
  215. * 包含多个上下结构的拖动(n>= 2,只包含上下结构的地方可以用loadVerticalHeight)
  216. * 加载多个结构高度
  217. * @param {String}module所属模块,防止不同页面相同id导致localstorage数据被覆盖
  218. * @param {Array}eles 元素为结构对象 内部: {container: 外部div, spread: 该div的Spread, notSpread: 该div不含Spread的高度, defaultProportion: 默认高度比例0.6}
  219. * @param {String}totalHeight 各个结构div总高度-可编译字符串
  220. * @param {Function}callback 回调
  221. * */
  222. function loadMultiVerticalHeight(module, eles, totalHeight, callback) {
  223. //当前窗口拖动区域总高度
  224. totalHeight = eval(totalHeight);
  225. let cacheHeight = 0,//缓存中设置了高度的div总高度
  226. notCacheProportion = 0;//缓存中没设置高度的div占用的总高度比例(每个div都有默认的高度比例) (有了缓存高度的div就是被拖动过的,被拖动过的div,其默认高度比例就没用了)
  227. //获取缓存的总高度、未被拖动过的所有div的默认比例之和notCacheProportion (1-notCacheProportion)就是缓存高度的占总的比例
  228. for (let ele of eles) {
  229. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`);
  230. if (eleHeight !== null) {
  231. eleHeight = parseFloat(eleHeight);
  232. cacheHeight += eleHeight;
  233. } else {
  234. notCacheProportion += ele.defaultProportion;
  235. }
  236. }
  237. //缓存高度占总的比例
  238. let cacheProportion = 1 - notCacheProportion;
  239. for (let ele of eles) {
  240. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`),
  241. curHeight = 0;
  242. if (eleHeight !== null) {
  243. eleHeight = parseFloat(eleHeight);
  244. //ele在当前窗口大小中的高度
  245. curHeight = eleHeight / cacheHeight * cacheProportion * totalHeight;
  246. } else {
  247. curHeight = ele.defaultProportion * totalHeight;
  248. }
  249. ele.container.height(curHeight);
  250. ele.spread.height(curHeight - ele.notSpread);
  251. }
  252. if (callback) {
  253. callback();
  254. }
  255. }
  256. return {setResizeWidth, horizontalSlide, loadHorizonWidth, verticalSlide, loadVerticalHeight, loadMultiVerticalHeight}
  257. })();