slideResize.js 13 KB

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