slideResize.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. }, 30);
  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. }
  115. }
  116. for (let resize of resizes) {
  117. setResizeWidth(resize,otherIndex);
  118. }
  119. if (callback) {
  120. callback();
  121. }
  122. }
  123. /*
  124. * div上下拖动
  125. * @param {Object} eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module)
  126. * resize, top, topSpread, bottom, bottomSpread
  127. * @param {Object}limit: min, max, notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  128. * @param {Function}callback 回调
  129. * */
  130. function verticalSlide(eleObj, limit, callback) {
  131. const triggerCBSize = 5;
  132. let drag = false,
  133. startPoint = 0,
  134. topHeight = 0,
  135. bottomHeight = 0,
  136. topChange = 0,
  137. bottomChange = 0,
  138. limitMax = 0;
  139. eleObj.resize.mousedown(function (e) {
  140. drag = true;
  141. startPoint = e.clientY;
  142. topHeight = eleObj.top.height();
  143. bottomHeight = eleObj.bottom.height();
  144. limitMax = eval(limit.max);
  145. });
  146. $('body').mousemove(function (e) {
  147. if (drag) {
  148. let moveSize = e.clientY - startPoint;
  149. topChange = topHeight + moveSize;
  150. topChange = topChange < limit.min ? limit.min : topChange;
  151. topChange = topChange > limitMax ? limitMax : topChange;
  152. bottomChange = bottomHeight - moveSize;
  153. bottomChange = bottomChange < limit.min ? limit.min : bottomChange;
  154. bottomChange = bottomChange > limitMax ? limitMax : bottomChange;
  155. //设置上部分div高度
  156. eleObj.top.height(topChange);
  157. //设置上部分div内spread高度
  158. eleObj.topSpread.height(topChange - limit.notTopSpread);
  159. //设置下部分div高度
  160. eleObj.bottom.height(bottomChange);
  161. //设置下部分div内spread高度
  162. let notBottomHeight = limit.bottomNav?eval(limit.bottomNav):limit.notBottomSpread;
  163. if(Array.isArray(eleObj.bottomSpread)){
  164. for(let b of eleObj.bottomSpread){
  165. b.height(bottomChange - notBottomHeight);
  166. }
  167. }else {
  168. eleObj.bottomSpread.height(bottomChange - notBottomHeight);
  169. }
  170. mouseMoveCount += Math.abs(moveSize);
  171. deBounce(function () {
  172. if (callback) {
  173. callback();
  174. mouseMoveCount = 0;
  175. }
  176. }, 30);
  177. /*if (mouseMoveCount > triggerCBSize && callback) {
  178. callback();
  179. mouseMoveCount = 0;
  180. }*/
  181. }
  182. });
  183. $('body').mouseup(function (e) {
  184. if (drag) {
  185. drag = false;
  186. mouseMoveCount = 0;
  187. //将高度信息存储到localstorage
  188. let topHeightInfo = eleObj.top.height();
  189. setLocalCache(`${eleObj.module}${eleObj.top.attr('id')}Height`, topHeightInfo);
  190. let bottomHeightInfo = eleObj.bottom.height();
  191. setLocalCache(`${eleObj.module}${eleObj.bottom.attr('id')}Height`, bottomHeightInfo);
  192. }
  193. });
  194. }
  195. /*
  196. * 加载上下高度
  197. * @param {String}module 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  198. * @param {Object}eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module) top, topSpread, bottom, bottomSpread
  199. * @param {Object}limit: totalHeight(实时的上下部分总高度) notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  200. * @param {Function}callback 回调
  201. * */
  202. function loadVerticalHeight(module, eleObj, limit, callback) {
  203. let topHeight = getLocalCache(`${module}${eleObj.top.attr('id')}Height`),
  204. bottomHeight = getLocalCache(`${module}${eleObj.bottom.attr('id')}Height`);
  205. //默认上下比例
  206. const topProp = 5;
  207. const bottomProp = 2;
  208. let topProportion = topProp / (topProp + bottomProp);
  209. if (topHeight !== null && bottomHeight !== null) {
  210. topHeight = parseFloat(topHeight);
  211. bottomHeight = parseFloat(bottomHeight);
  212. topProportion = topHeight / (topHeight + bottomHeight);
  213. }
  214. //设置当前窗口下的的上下部分高度
  215. let totalHeight = eval(limit.totalHeight);
  216. let curTopHeight = totalHeight * topProportion,
  217. curBottomHeight = totalHeight - curTopHeight;
  218. eleObj.top.height(curTopHeight);
  219. eleObj.topSpread.height(curTopHeight - limit.notTopSpread);
  220. eleObj.bottom.height(curBottomHeight);
  221. let notBottomHeight = limit.bottomNav?eval(limit.bottomNav):limit.notBottomSpread;
  222. if(Array.isArray(eleObj.bottomSpread)){
  223. for(let b of eleObj.bottomSpread){
  224. b.height(curBottomHeight - notBottomHeight);
  225. }
  226. }else {
  227. eleObj.bottomSpread.height(curBottomHeight - notBottomHeight);
  228. }
  229. if (callback) {
  230. callback();
  231. }
  232. }
  233. /*
  234. * 包含多个上下结构的拖动(n>= 2,只包含上下结构的地方可以用loadVerticalHeight)
  235. * 加载多个结构高度
  236. * @param {String}module所属模块,防止不同页面相同id导致localstorage数据被覆盖
  237. * @param {Array}eles 元素为结构对象 内部: {container: 外部div, spread: 该div的Spread, notSpread: 该div不含Spread的高度, defaultProportion: 默认高度比例0.6}
  238. * @param {String}totalHeight 各个结构div总高度-可编译字符串
  239. * @param {Function}callback 回调
  240. * */
  241. function loadMultiVerticalHeight(module, eles, totalHeight, callback) {
  242. //当前窗口拖动区域总高度
  243. totalHeight = eval(totalHeight);
  244. let cacheHeight = 0,//缓存中设置了高度的div总高度
  245. notCacheProportion = 0;//缓存中没设置高度的div占用的总高度比例(每个div都有默认的高度比例) (有了缓存高度的div就是被拖动过的,被拖动过的div,其默认高度比例就没用了)
  246. //获取缓存的总高度、未被拖动过的所有div的默认比例之和notCacheProportion (1-notCacheProportion)就是缓存高度的占总的比例
  247. for (let ele of eles) {
  248. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`);
  249. if (eleHeight !== null) {
  250. eleHeight = parseFloat(eleHeight);
  251. cacheHeight += eleHeight;
  252. } else {
  253. notCacheProportion += ele.defaultProportion;
  254. }
  255. }
  256. //缓存高度占总的比例
  257. let cacheProportion = 1 - notCacheProportion;
  258. for (let ele of eles) {
  259. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`),
  260. curHeight = 0;
  261. if (eleHeight !== null) {
  262. eleHeight = parseFloat(eleHeight);
  263. //ele在当前窗口大小中的高度
  264. curHeight = eleHeight / cacheHeight * cacheProportion * totalHeight;
  265. } else {
  266. curHeight = ele.defaultProportion * totalHeight;
  267. }
  268. ele.container.height(curHeight);
  269. ele.spread.height(curHeight - ele.notSpread);
  270. }
  271. if (callback) {
  272. callback();
  273. }
  274. }
  275. return {setResizeWidth, horizontalSlide, loadHorizonWidth, verticalSlide, loadVerticalHeight, loadMultiVerticalHeight}
  276. })();