Equencing.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /**
  2. * 图片滑动模块
  3. * action: 优化
  4. * date:2020-09-19 许祖兴 <uxing.xu@lettered.cn>
  5. * from:https://www.jq22.com/jquery-info19741
  6. */
  7. ;layui.define(['jquery', 'form', 'layer', 'admin'], function (e
  8. ) {
  9. "use strict";
  10. layui.link(layui.cache.base + 'Equencing/css/style.css'); // 默认风格
  11. var $ = layui.jquery, form = layui.form, layer = layui.layer, hint = (layui.util, layui.hint());
  12. var Eq = {
  13. config: {},
  14. cache: {},
  15. index: layui.table ? layui.table.index + 1e4 : 0,
  16. set: function (e) {
  17. var i = this;
  18. return i.config = $.extend({}, i.config, e), i
  19. },
  20. on: function (e, t) {
  21. return layui.onevent.call(this, y, e, t)
  22. }
  23. },
  24. // 外露接口
  25. c = function () {
  26. var e = this, t = e.config, v = e.vars, i = t.id || t.index;
  27. return i && (c.that[i] = e, c.config[i] = t, c.vars[i] = v), {
  28. config: t, vars: v, update: function (d) {
  29. e.update.call(e, d)
  30. }, clearData: function () {
  31. return e.clearData.call(e, t)
  32. }, getData: function () {
  33. return e.getData.call(e, t)
  34. }, addItem: function (d) {
  35. return e.addItem.call(e, d)
  36. }, delItem: function (d) {
  37. return e.delItem.call(e, d)
  38. }
  39. }
  40. }, s = function (e) {
  41. var t = c.config[e];
  42. return t || hint.error("The ID option was not found in the table instance"), t || null
  43. }, F = function (e) {
  44. var i = this;
  45. // 配置合并
  46. i.index = ++Eq.index, i.config = $.extend({}, i.config, Eq.config, e), i.render()
  47. }, l = "drag-images";
  48. F.prototype.config = {
  49. // 页面图片列表ID
  50. elem: "img_ul",
  51. // 图片数据数组
  52. data: [],
  53. // 数量
  54. count: 1,
  55. // 预览/查看图片
  56. viewimg: true,
  57. // 预览切换
  58. view_toggle: true,
  59. // 预览缩放
  60. view_zoom: true,
  61. // 预览旋转
  62. view_rotate: true,
  63. // item
  64. IdClass: "__item__",
  65. }
  66. , F.prototype.dataArr = []
  67. , F.prototype.coverBox = null
  68. , F.prototype.itemBox = null
  69. , F.prototype.itemIdArr = []
  70. , F.prototype.chekObj = {}
  71. , F.prototype.imgNum = 0
  72. // 获取最新数据
  73. F.prototype.getData = function () {
  74. var that = this, opts = that.config;
  75. that.dataArr.length = 0;
  76. for (var i = 0; i < that.itemIdArr.length; i++) {
  77. that.dataArr.push(opts.data[that.itemIdArr[i]])
  78. }
  79. if (that.dataArr.length > 0){
  80. return opts.count > 1 ? JSON.stringify(that.dataArr) : that.dataArr[0] || "";
  81. }
  82. return "";
  83. },
  84. // 删除
  85. F.prototype.delItem = function (index) {
  86. var that = this, opts = that.config;
  87. // 框
  88. that.itemBox.find("#"+opts.elem +'_item' + index).remove();
  89. // 删除序号
  90. that.itemIdArr.splice(index, 1);
  91. that.redraw(0, that.itemIdArr.length,200);
  92. that.info(this);
  93. },
  94. // 清空item
  95. F.prototype.clear = function () {
  96. var that = this, opts = that.config;
  97. that.itemBox.find("." + opts.IdClass).remove();
  98. opts.data.length = 0;
  99. that.itemIdArr.length = 0;
  100. that.chekObj = {};
  101. that.box_h = "20";
  102. that.coverBox.height(that.box_h + "px");
  103. },
  104. F.prototype.update = function(e){
  105. var that = this;
  106. // 清空
  107. e = e || [], that.config.data = [],that.itemIdArr = [],that.chekObj ={},that.dataArr = [];
  108. that.itemBox.find("." + that.config.IdClass).remove();
  109. // 再添加
  110. $.each(e, (i,v) => {
  111. that.addItem(v);
  112. });
  113. },
  114. // 添加图片
  115. F.prototype.addItem = function (imgsrc) {
  116. var that = this, opts = that.config;
  117. // 追加数据
  118. if( opts.data.indexOf(imgsrc) == -1){
  119. opts.data.push(imgsrc);
  120. // html处理
  121. var imgnum = opts.data.length - 1;
  122. that.itemIdArr.push(imgnum);
  123. that.itemBox.append(
  124. '<div class="item ' + opts.IdClass + '" el="' + opts.elem + '" id="' + opts.elem + '_item' + imgnum + '" item="' + imgnum + '">' + that.buildItemStr(imgsrc) + '</div>'
  125. );
  126. that.redraw(imgnum, 1, 0);
  127. that.info(this);
  128. }
  129. },
  130. // 以下都是渲染
  131. // 构建Item 内容
  132. F.prototype.buildItemStr = function (imgsrc) {
  133. return '<div class="picbox">'
  134. + '<a class="viewimg">'
  135. + '<img class="input-img-preview" src="' + imgsrc + '" ondragstart="return false;" />'
  136. + '</a>'
  137. + '<i class="del eq-item-del layui-icon layui-icon-close-fill"></i>'
  138. + '</div>'
  139. },
  140. F.prototype.info = function () {
  141. var that = this, opts = that.config;
  142. if (that.item_w === undefined || that.item_w === null) {
  143. // 每个item占的横向位置
  144. that.item_w = that.itemBox.find("." + opts.IdClass).outerWidth(true);
  145. // 每个item占的竖向位置
  146. that.item_h = that.itemBox.find("." + opts.IdClass).outerHeight(true);
  147. that.coverBox.find(".ident").css({height: that.item_h + "px"});
  148. that.coverBox.find(".morexy").css({width: that.item_w + "px", height: that.item_h + "px"});
  149. }
  150. // item数量
  151. that.imgNum = that.itemBox.find("." + opts.IdClass).length;
  152. // 共分多少列
  153. that.col_len = Math.floor(that.box_w / that.item_w);
  154. // 共分多少行
  155. that.few_len = Math.ceil(that.imgNum / that.col_len);
  156. that.box_h = that.item_h * that.few_len + 20;
  157. that.coverBox.height(that.box_h + "px");
  158. return true;
  159. },
  160. // 绘制/移动
  161. F.prototype.draw = function (dom, col, few, slidetime) {
  162. dom.css({
  163. "transition-duration": slidetime + "ms",
  164. "transform": "translate(" + col + "px," + few + "px)",
  165. });
  166. },
  167. // 通用绘制
  168. F.prototype.redraw = function (strati, ilen, slidetime) {
  169. var that = this, opts = that.config;
  170. for (var j = strati; j < strati + ilen; j++) {
  171. that.computat(j, opts.elem + "_item" + that.itemIdArr[j], slidetime);
  172. }
  173. },
  174. // 计算位置
  175. F.prototype.computat = function (index, domid, slidetime) {
  176. var that = this, item = $("#" + domid);
  177. item.attr({"item": index,});
  178. if (that.chekObj[domid] !== undefined) {
  179. that.chekObj[domid] = index;
  180. }
  181. var col_aliquot = index % that.col_len;
  182. var row_aliquot = Math.floor(index / that.col_len);
  183. var index_col = that.item_w * (col_aliquot);
  184. var index_few = that.item_h * row_aliquot;
  185. that.draw(item, index_col, index_few, slidetime);
  186. item.attr({
  187. "col": index_col,
  188. "few": index_few
  189. })
  190. },
  191. F.prototype.render = function () {
  192. var that = this, opts = that.config;
  193. //-//-//-//-开始-//-//-//-//
  194. that.coverBox = $("#" + opts.elem);
  195. that.coverBox.append("<div class='itemlist' style='height:100%'></div>" +
  196. "<div class='ident'></div>" +
  197. "<div class='morexy'></div>"
  198. );
  199. that.itemBox = that.coverBox.find(".itemlist");
  200. // 页面dom绘制
  201. var html = '';
  202. for (var j = 0; j < opts.data.length; j++) {
  203. html += '<div class="item ' + opts.IdClass + '" el="' + opts.elem + '" id="' + opts.elem + '_item' + j + '" item="' + j + '">'
  204. + that.buildItemStr(opts.data[j]) + '</div>';
  205. }
  206. // 渲染
  207. that.itemBox.html(html);
  208. // render
  209. form.render();
  210. // 取总宽度
  211. that.box_w = that.coverBox.width();
  212. // 首次绘制
  213. if (that.info(this)) {
  214. that.chekObj = {};
  215. that.itemIdArr = [];
  216. // 新数组
  217. that.dataArr = [];
  218. for (var i = 0; i < that.imgNum; i++) {
  219. that.itemIdArr.push(i)
  220. }
  221. that.redraw(0, that.itemIdArr.length, 0);
  222. }
  223. // 浏览器尺寸改变时
  224. $(window).resize(function () {
  225. // 取总宽度
  226. that.box_w = that.coverBox.width();
  227. if (that.info(this)) {
  228. that.redraw(0, that.itemIdArr.length, 0);
  229. }
  230. }.bind(this));
  231. //-//-//-//-拖动排序-//-//-//-//
  232. // 鼠标初始位置
  233. var startX = null;
  234. var startY = null;
  235. // 选中元素item
  236. var moveDom = null;
  237. var checkItem = null;
  238. // 初始index/当前点击元素的item值
  239. var startIndex = null;
  240. // 是否多选框选中
  241. var isSelected = false;
  242. // 初始坐标位置
  243. var oldCol = null;
  244. var oldFew = null;
  245. // 标识
  246. var identDom = that.coverBox.find(".ident");
  247. // 多选
  248. var multipleBox = that.coverBox.find(".morexy");
  249. // 是否拖动
  250. var isDrag = false;
  251. // 标识初始位置
  252. var identMobieX = null;
  253. var identMobieY = null;
  254. // 初始化
  255. function mouseinit() {
  256. moveDom = null;
  257. isDrag = false;
  258. checkItem.css({"opacity": "1", "z-index": ""});
  259. multipleBox.hide();
  260. identDom.hide();
  261. }
  262. // 获取选中元素id
  263. function getchekobj() {
  264. var domidarr = [];
  265. for (var key in that.chekObj) {
  266. domidarr.push("#" + key);
  267. }
  268. var chekDomObj = $(domidarr.join(','));
  269. if (domidarr.length == 0) {
  270. chekDomObj = checkItem;
  271. }
  272. return chekDomObj;
  273. }
  274. // 松开手指返回原位
  275. $(document).on("mouseup", function () {
  276. if (moveDom != null) {
  277. that.draw(moveDom, oldCol, oldFew, 0);
  278. mouseinit();
  279. if (isSelected) {
  280. for (var arri in that.chekObj) {
  281. var elem = $("#" + arri);
  282. that.draw(elem, elem.attr("col"), elem.attr("few"), 300);
  283. }
  284. }
  285. }
  286. });
  287. // 拖动方法
  288. that.coverBox.on({
  289. mousedown: function (e) {
  290. e.preventDefault();
  291. if (e.target.localName == "input") {
  292. return false;
  293. }
  294. var isIdclass = $(e.target).hasClass(opts.IdClass);
  295. var pIdClass = $(e.target).parents("." + opts.IdClass)[0];
  296. if (!isIdclass && pIdClass == undefined) {
  297. return false;
  298. }
  299. if (isIdclass) {
  300. checkItem = $(e.currentTarget);
  301. } else {
  302. checkItem = $(pIdClass);
  303. }
  304. if (checkItem.find("input.checkbox").prop("checked")) {
  305. isSelected = true;
  306. moveDom = multipleBox;
  307. } else {
  308. isSelected = false;
  309. moveDom = checkItem;
  310. }
  311. startX = e.pageX;
  312. startY = e.pageY;
  313. startIndex = parseInt(checkItem.attr("item"));
  314. oldCol = parseInt(checkItem.attr("col"));
  315. oldFew = parseInt(checkItem.attr("few"));
  316. checkItem = getchekobj();
  317. checkItem.css({"opacity": "0.8", "z-index": "10"});
  318. that.draw(identDom, oldCol, oldFew, 0);
  319. },
  320. mousemove: function (e) {
  321. if (moveDom == null) {
  322. return;
  323. }
  324. if (e.which != 1) {
  325. return;
  326. }
  327. // 拖动距离
  328. var gapX = e.pageX - startX;
  329. var gapY = e.pageY - startY;
  330. // 偏移距离
  331. var mobiex = oldCol + gapX;
  332. var mobiey = oldFew + gapY;
  333. // 横向是否超出边界
  334. if (mobiex > that.box_w - that.item_w) {
  335. mobiex = that.box_w - that.item_w;
  336. } else if (mobiex < 0) {
  337. mobiex = 0;
  338. }
  339. // 竖向是否超出边界
  340. if (mobiey > that.box_h - that.item_h) {
  341. mobiey = that.box_h - that.item_h;
  342. } else if (mobiey < 0) {
  343. mobiey = 0;
  344. }
  345. if (Math.abs(gapX) > 10 || Math.abs(gapY) > 10) {
  346. if (!isDrag) {
  347. if (isSelected) {
  348. that.draw(moveDom, oldCol, oldFew, 0);
  349. for (var arri in that.chekObj) {
  350. $("#" + arri).css({"opacity": "0.25", "z-index": "10"});
  351. that.draw($("#" + arri), oldCol, oldFew, 300);
  352. }
  353. moveDom.show();
  354. }
  355. }
  356. // item移动
  357. that.draw(moveDom, mobiex, mobiey, 0);
  358. // 标识移动
  359. identMobieX = Math.abs(Math.ceil((mobiex - that.item_w / 2) / that.item_w));
  360. identMobieY = Math.abs(Math.ceil((mobiey - that.item_h / 2) / that.item_h));
  361. identDom.show();
  362. that.draw(identDom, identMobieX * that.item_w, identMobieY * that.item_h, 0);
  363. isDrag = true;
  364. }
  365. },
  366. mouseup: function (e) {
  367. if (moveDom == null) {
  368. return;
  369. } else if (isDrag) {
  370. that.draw(moveDom, oldCol, oldFew, 0);
  371. var toposion = identMobieY * that.col_len + identMobieX;
  372. // 多选
  373. if (isSelected) {
  374. var min = that.itemIdArr.length;
  375. var max = 0;
  376. var transsh = [];
  377. for (var indexi in that.chekObj) {
  378. that.itemBox.find("#" + indexi).css({"opacity": "1", "z-index": ""});
  379. for (var i = 0; i < that.itemIdArr.length; i++) {
  380. if (i == that.chekObj[indexi]) {
  381. min = i < min ? i : min;
  382. max = i + 1 > max ? i + 1 : max;
  383. transsh.push(that.itemIdArr[i]);
  384. that.itemIdArr[i] = null;
  385. }
  386. }
  387. }
  388. for (var i = 0; i < transsh.length; i++) {
  389. that.itemIdArr.splice(toposion + i, 0, transsh[i]);
  390. }
  391. // transsh.length=0;
  392. for (var i = that.itemIdArr.length - 1; i >= 0; i--) {
  393. if (that.itemIdArr[i] == null) {
  394. that.itemIdArr.splice(i, 1);
  395. }
  396. }
  397. min = toposion < min ? toposion : min;
  398. max = toposion > max ? toposion : max;
  399. if (max - min != transsh.length) {
  400. that.redraw(min, max - min, 200);
  401. } else {
  402. for (var arri in that.chekObj) {
  403. var elem = $("#" + arri);
  404. that.draw(elem, elem.attr("col"), elem.attr("few"), 300);
  405. }
  406. }
  407. } else {
  408. var difference = toposion - startIndex;
  409. if (difference > 1) {
  410. // 往后
  411. var changesitem = that.itemIdArr.splice(startIndex, 1)[0];
  412. that.itemIdArr.splice(toposion - 1, 0, changesitem);
  413. that.redraw(startIndex, startIndex + difference, 200);
  414. } else if (difference < 0) {
  415. // 往前
  416. var changesitem = that.itemIdArr.splice(startIndex, 1)[0];
  417. that.itemIdArr.splice(toposion, 0, changesitem);
  418. difference = Math.abs(difference) + 1;
  419. that.redraw(toposion, toposion + difference, 200);
  420. }
  421. }
  422. mouseinit();
  423. }
  424. },
  425. mouseleave: function (e) {
  426. if (moveDom == null) {
  427. return;
  428. }
  429. },
  430. });
  431. }, c.that = {}, c.config = {}
  432. , Eq.update = function (e, t) {
  433. var i = s(e);
  434. if (i) {
  435. var a = c.that[e];
  436. return a.update(t), c.call(a)
  437. }
  438. }, Eq.getData = function (e, t) {
  439. var i = s(e);
  440. if (i) {
  441. var a = c.that[e];
  442. return a.getData(t), c.call(a)
  443. }
  444. }, Eq.addItem = function (e, t) {
  445. var i = s(e);
  446. if (i) {
  447. var a = c.that[e];
  448. return a.addItem(t), c.call(a)
  449. }
  450. }, Eq.delItem = function (e, t) {
  451. var i = s(e);
  452. if (i) {
  453. var a = c.that[e];
  454. return a.delItem(t), c.call(a)
  455. }
  456. }, Eq.clearData = function (e, t) {
  457. var i = s(e);
  458. if (i) {
  459. var a = c.that[e];
  460. return a.clearData(t), c.call(a)
  461. }
  462. }, Eq.render = function (e) {
  463. var t = new F(e);
  464. return c.call(t)
  465. }, e('Equencing', Eq)
  466. });