wdDrag.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. var wd = wd || {};
  2. wd.drag = wd.drag || {};
  3. var ismousedown = functionKeyDown = false;
  4. var setDrag, DM, itemid = 0, readyMouseIcoLen = 12;
  5. function isEventTargetIsEditable(event) {
  6. var tg = event.target;
  7. return tg.nodeName == 'textarea' || (tg.nodeName == '#text' && $(tg).parent("[contenteditable]").length > 0) || isContenteditable(tg);
  8. };
  9. function isContenteditable(ele) {
  10. return $(ele).is("[contenteditable]");
  11. };
  12. function clearMouseIcon() {
  13. for (var iii = 1; iii <= readyMouseIcoLen; iii++) {
  14. $(".cursor-timing" + iii).removeClass("cursor-timing" + iii);
  15. }
  16. }
  17. function docMouseDown(event) {
  18. ismousedown = true;
  19. if (!isEventTargetIsEditable(event || window.event)) {
  20. $("[ssObjId] [editable]").removeAttr("contenteditable"); // ("[wdObjectId] [。Lin
  21. }
  22. }
  23. function isPointInElement(element, event) {
  24. if (!element || !event) {
  25. return false;
  26. }
  27. var rect = element.getBoundingClientRect();
  28. return event.clientX >= rect.left && event.clientX <= rect.right
  29. && event.clientY >= rect.top && event.clientY <= rect.bottom;
  30. }
  31. function moveDragPanel(event) {
  32. var panel = DM.dragingPanel;
  33. var currentdrager = DM.currentDrager;
  34. if (!panel || !currentdrager || !panel.ghost) {
  35. return;
  36. }
  37. panel.show();
  38. panel.move(event);
  39. var hoveredItem = null;
  40. var hoveredList = null;
  41. currentdrager.getLists().forEach(function (list) {
  42. if (!isPointInElement(list.element, event)) {
  43. return;
  44. }
  45. hoveredList = list;
  46. var items = list.visibleElements || list.getAllItems();
  47. for (var i = 0; i < items.length; i++) {
  48. if (items[i].isMouseOver(event) > 0) {
  49. hoveredItem = items[i];
  50. hoveredList = items[i].getDragList();
  51. break;
  52. }
  53. }
  54. });
  55. if (hoveredItem) {
  56. itemMouseEvent.call(hoveredItem, event);
  57. } else if (hoveredList) {
  58. if (panel.ghost.element.parentNode !== hoveredList.element) {
  59. hoveredList.element.appendChild(panel.ghost.element);
  60. }
  61. panel.allowDrop();
  62. }
  63. }
  64. function docMouseMove(event) {
  65. // clearSlct();
  66. var panel = DM.dragingPanel;
  67. var currentdrager = DM.currentDrager;
  68. var flist = (currentdrager || {}).focusedList;
  69. if (currentdrager && !currentdrager.editGroup && !currentdrager.editOrder && currentdrager.getLists().length < 2) { }
  70. else if (ismousedown && (panel || (currentdrager && flist && flist.currentMouseDownItem))) { //拖动中
  71. if ((panel = DM.dragingPanel) == null) {
  72. var ci = flist.currentMouseDownItem;
  73. var tcx = event.clientX;
  74. var tcy = event.clientY;
  75. var cx = ci.cx || tcx;
  76. var cy = ci.cy || tcy;
  77. var xdiff = cx - tcx; // 计算两个点的横坐标之差
  78. var ydiff = cy - tcy; // 计算两个点的纵坐标之差
  79. if (Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5) < 10) {
  80. return;
  81. }
  82. panel = DM.dragingPanel = new wd.drag.DragPanel();
  83. document.body.appendChild(panel.element);
  84. }
  85. moveDragPanel(event);
  86. }
  87. }
  88. function docMouseUp(event) {
  89. ismousedown = false;
  90. currentMouseDownItem = null;
  91. DM.dragingPanel != null && DM.dragingPanel.destroy();
  92. clearMouseIcon();
  93. }
  94. wd.drag.DragerManager = function () {
  95. console.log("new DragerManager");
  96. try {
  97. $("body").append("<link id='dragcss' rel='stylesheet' href='/ss/js/drag.css' />");
  98. $("head").append("<style>.subList .hLine-warning{width: calc(100% - 56px);float: right;} .vLine-warning{ display:inline-block;float:left;height:20px;position:unset;height:35px} .subList{min-height: 40px;}</style>");
  99. } catch (e) { }
  100. /**
  101. * 清除选中的文字
  102. */
  103. var clearSlct = "getSelection" in window ? function () {
  104. window.getSelection().removeAllRanges();
  105. }
  106. :
  107. function () {
  108. document.selection.empty();
  109. };
  110. // /**
  111. // *
  112. // * @param {Object} event
  113. // */
  114. // document.onselectstart = function (event) {
  115. // return false;
  116. // };
  117. /**
  118. *
  119. * @param {Object} event
  120. */
  121. document.addEventListener("mousedown", docMouseDown);
  122. /**
  123. *
  124. * @param {Object} event
  125. */
  126. document.addEventListener("mousemove", docMouseMove);
  127. /**
  128. * 触发移动时候的item的进出事件
  129. * @param {Object} dragList
  130. * @param {Object} panel
  131. * @param {Object} haschild
  132. * @param {Object} event
  133. */
  134. function eachListItem(dragList, panel, haschild, event) {
  135. var ghost = panel.ghost;
  136. var dr = dragList.drager;
  137. // var visibleItem=
  138. dragList.each(/*dragList.getItems()*/ dragList.visibleElements, function () {
  139. var location = this.isMouseOver(event);
  140. dr = this.drager;
  141. /*触发进出事件*/
  142. if (location > 0) { //这里触发item的鼠标进出事件,以后可能会调整,同时这里的逻辑有点问题但是能用,以后处理
  143. !this.isEnter && this.trigger("mouseover");
  144. this.isEnter && this.trigger("mousemove");
  145. this.isEnter = true; //进来了
  146. DM.dragingPanel.currentHoverItem = this;
  147. } else {
  148. this.isEnter && this.trigger("mouseleave");
  149. this.isEnter = false; //出去了
  150. }
  151. if (location > 0 && (this.drager.editGroup || this.drager.editOrder) && this.isExpand && !this.getDragList().isChild) { //鼠标在展开的group上面
  152. if (haschild) {
  153. panel.notAllowDrop();
  154. } else if (this.dragList.isMouseOver(event) > 0) {
  155. ghost.getDragList() != this.dragList && this.dragList.append(ghost);
  156. eachListItem(this.dragList, panel, haschild, event);
  157. }
  158. }
  159. ghost.lineMode();
  160. });
  161. !dr.editGroup && !dr.editOrder && (dragList.element.scrollTop = dragList.element.scrollHeight);
  162. };
  163. /**
  164. *
  165. * @param {Object} event
  166. */
  167. document.addEventListener("mouseup", docMouseUp);
  168. /**
  169. *
  170. * @param {Object} event
  171. */
  172. document.addEventListener("keydown", function (event) {
  173. functionKeyDown = event.ctrlKey || event.altKey || event.shiftKey;
  174. event.ctrlKey && event.keyCode === 65 && DM.currentDrager.focusedList != null && DM.currentDrager.focusedList.selectAll();
  175. });
  176. /**
  177. *
  178. * @param {Object} event
  179. */
  180. document.addEventListener("keyup", function (event) {
  181. functionKeyDown = event.ctrlKey || event.altKey || event.shiftKey;
  182. });
  183. };
  184. wd.drag.DragerManager.prototype = {
  185. constVars: {
  186. editOrder: "editOrder",
  187. editGroup: "editGroup"
  188. },
  189. instances: {},
  190. utils: {
  191. contains: function (arr, val) {
  192. if (typeof arr == 'object' && (arr instanceof Array || arr instanceof NodeList)) {
  193. for (var i = 0; i < arr.length; i++) {
  194. if (arr[i] == val) {
  195. return true;
  196. }
  197. }
  198. }
  199. return false;
  200. },
  201. parseForm2Url: function (form) {
  202. var arr = [];
  203. var action_ = form.getAttribute("action");
  204. var param = (action_ || "?").split("?");
  205. arr = arr.concat((param[1] || "").split("&"));
  206. var elements = form.querySelectorAll("input,select,textarea");
  207. for (var i = 0; i < elements; i++) {
  208. var feled = elements[i];
  209. switch (feled.type) {
  210. case undefined:
  211. case 'button':
  212. case 'file':
  213. case 'reset':
  214. case 'submit':
  215. break;
  216. case 'checkbox':
  217. case 'radio':
  218. if (!feled.checked) {
  219. break;
  220. }
  221. default:
  222. if (feled.value != null && feled.value != '' && feled.name != null && !feled.disabled) {
  223. arr.push(feled.name + "=" + feled.value);
  224. }
  225. }
  226. }
  227. return param[0] + "?" + arr.join("&");
  228. },
  229. containsKey: function (obj, key) {
  230. for (var key_ in obj) {
  231. if (key_ == key) {
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. },
  238. getDrager: function (name) {
  239. if (name) {
  240. return this.instances[name];
  241. }
  242. for (var key in this.instances) {
  243. return this.instances[key];
  244. }
  245. return
  246. },
  247. getCurrentFocusList: function () {
  248. return (this.currentDrager || {}).focusedList;
  249. },
  250. getCurrentSelectedItems: function () {
  251. if (this.getCurrentFocusList()) {
  252. return this.getCurrentFocusList().getSelectedItems();
  253. }
  254. },
  255. addItem: function (id, item, index, isCover) {
  256. return this.getDrager().addItem(id, item, index, isCover);
  257. },
  258. drager: function (name, group, functions, baseOptions) {
  259. this.itemSelector = "div[ssObjId]"; // [wdObjectID]。是 wdObjectID,不是 wdObjectId。Lin
  260. this.name = name;
  261. this.imgleft = 30;
  262. this.dragerManager = this;
  263. this.items = {};
  264. this.name = name;
  265. this.getItems = function () {
  266. var tmp = [];
  267. this.selectedList != null && (tmp = tmp.concat(this.selectedList.getAllItems()));
  268. this.optionList != null && (tmp = tmp.concat(this.optionList.getAllItems()));
  269. return tmp;
  270. }
  271. this.destroy = function () {
  272. this.selectedList && this.selectedList.destroy();
  273. this.optionList && this.optionList.destroy();
  274. delete DM.instances[this.name];
  275. }
  276. this.getLists = function () {
  277. var arr = [];
  278. this.selectedList && arr.push(this.selectedList);
  279. this.optionList && arr.push(this.optionList);
  280. return arr;
  281. }
  282. this.getSelectedResult = function () {
  283. return this.selectedList.getResult();
  284. }
  285. this.addItem = function (id, element, index, isCover) {
  286. return this.addItemTo(id, element, this.selectedList, index);
  287. }
  288. this.addItemToFocusedList = function (id, element, index, isCover) {
  289. this.addItemTo(id, element, this.focusedList, index, isCover);
  290. }
  291. this.addItemTo = function (id, element, list, index, isCover) {
  292. if (list == this.focusedList) { }
  293. else if (this.focusedList.isChild && this.focusedList.thisItem.getDragList() == list) {
  294. list = this.focusedList;
  295. }
  296. return list.insertItemByHtml(id, element, index, isCover);
  297. }
  298. this.init = function () {
  299. var this_ = this;
  300. for (var key in baseOptions) {
  301. this_[key] = baseOptions[key];
  302. }
  303. //处理functions
  304. this_.functions = functions || [];
  305. this_.group = group == true || group == "true";
  306. this_.editOrder = this_.functions.indexOf(DM.constVars.editOrder) > -1;
  307. this_.editGroup = this_.functions.indexOf(DM.constVars.editGroup) > -1;
  308. //获取查询表单
  309. var form = document.getElementsByName(name + "OptionForm")[0] || document.getElementById(name + "OptionForm");
  310. if (form) {
  311. this_.form = new wd.drag.Form(form);
  312. this_.form.drager = this;
  313. }
  314. var selectedList = document.getElementById(name + "Selected");
  315. if (selectedList) {
  316. var list = new wd.drag.DragList(name, selectedList);
  317. list.isSelectedList = true;
  318. var input = document.getElementsByName(name + "SelectedId")[0];
  319. input = input || document.getElementsByName(name + "SelectedId")[0]
  320. input = input || document.createElement("input");
  321. input.type = "hidden";
  322. input.name = name + "SelectedId";
  323. list.before(input);
  324. list.result_input = input;
  325. list.init();
  326. list.alignMode = this.selectedListAlignMode || this.alignMode || "vertical";
  327. this.selectedList = list;
  328. } else {
  329. console.log("没有找到selectedList")
  330. }
  331. var optionList = document.getElementById(name + "Option");
  332. if (optionList) {
  333. var list = new wd.drag.DragList(name, optionList);
  334. list.isOptionList = true;
  335. list.init();
  336. // list.functions = this.functions || [];
  337. // if(this.optionFunctions)
  338. // list.functions = list.functions.concat(this.optionFunctions);
  339. list.alignMode = this.optionListAlignMode || this.alignMode || "vertical";
  340. this.optionList = list;
  341. }
  342. //获取按钮区域
  343. function moveEnd(items) {
  344. DM.getDrager(name).optionList.updateInfo();
  345. DM.getDrager(name).optionList.clearSelect();
  346. DM.getDrager(name).optionList.trigger("dragend");
  347. DM.getDrager(name).selectedList.updateInfo();
  348. DM.getDrager(name).selectedList.clearSelect();
  349. DM.getDrager(name).selectedList.trigger("dragend");
  350. if (DM.getDrager(name).dragend) {
  351. var doms = [];
  352. $.each(items, function () {
  353. doms.push(this.element);
  354. });
  355. DM.getDrager(name).dragend.call(this, doms);
  356. };
  357. }
  358. var moveButtons = {
  359. 'PickAll': function () {
  360. var items = DM.getDrager(name).optionList.getSelectedItems();
  361. if (items.length < 1) {
  362. items = $(DM.getDrager(name).optionList.element).children("[ssObjId]:not(:hidden)"); // [wdObjectId]:。Lin
  363. }
  364. $.each(items, function () {
  365. var eee = this.confs ? this : this.element;
  366. if (eee && $(eee).not(":hidden")) {
  367. DM.getDrager(name).selectedList.append(eee);
  368. }
  369. });
  370. moveEnd(items);
  371. },
  372. 'DropAll': function () {
  373. var items = DM.getDrager(name).selectedList.getSelectedItems();
  374. if (items.length < 1) {
  375. items = $(DM.getDrager(name).selectedList.element).children("[ssObjId]:not(:hidden)"); // [wdObjectId]:。Lin
  376. }
  377. $.each(items, function () {
  378. var eee = this.confs ? this : this.element;
  379. if (eee && $(eee).not(":hidden")) {
  380. DM.getDrager(name).optionList.append(eee);
  381. }
  382. });
  383. moveEnd(items);
  384. }
  385. };
  386. for (var key in moveButtons) {
  387. console.log("#" + name + key)
  388. $("#" + name + key).click(moveButtons[key]);
  389. }
  390. this.getLists().length < 1 && console.info("没找到要初始化的Draglist\"" + name + "\"");
  391. }
  392. },
  393. getInstance: function (name, group, functions, baseOptions) {
  394. baseOptions = baseOptions || {};
  395. var this_ = this;
  396. return DM.instances[name] || function () {
  397. if (typeof (isWriteOut) == "undefined") {
  398. alert("注意下,这是旧的写法,以后不再自动初始化拖拽了,\n记得新的拖拽需要通过commonwrite输出^_^");
  399. return;
  400. }
  401. var drager = DM.instances[name] = new this_.drager(name, group, functions, baseOptions);
  402. drager.init();
  403. return drager
  404. }
  405. (name, group, functions, baseOptions);
  406. }
  407. };
  408. wd.drag.Shadow = function (element) {
  409. wd.drag.Base.call(this);
  410. this.drager = element.confs.drager;
  411. var clone = this.element = element.cloneNode(true);
  412. clone.confs = this;
  413. this.src = element;
  414. this.isShadow = true;
  415. // clone.style.opacity = "0.5";
  416. this.addClass("shadow");
  417. element.confs.isSelected() && this.removeClass(element.confs.getTopDragList({}).wdSelectedClass);
  418. $(this.element).find(".icon-checked").addClass('icon-unchecked').removeClass('icon-checked');
  419. this.getTopDragList = function (emptyObj) {
  420. if (!this.exist())
  421. return emptyObj;
  422. var list = this.getDragList();
  423. list.isChild && (list = list.thisItem.getDragList());
  424. return list;
  425. };
  426. this.getDragList = function () {
  427. return (this.element.parentNode || {}).confs;
  428. }
  429. };
  430. wd.drag.Base = function (element) {
  431. this.bindedEvent = (element || {}).bindedEvent = (element || {}).bindedEvent || {};
  432. this.isConfs = true;
  433. this.isshow = true;
  434. this.element = element;
  435. this.exist = function () {
  436. return $.contains(document.body, this.element);
  437. };
  438. this.getScrollInfo = function () {
  439. var scrollinfo = {
  440. left: 0,
  441. top: 0
  442. }
  443. getScroll(this.element);
  444. return scrollinfo;
  445. function getScroll(node) {
  446. // 非Element 终止递归
  447. if (!node) {
  448. return
  449. }
  450. if (node.nodeType !== 1) {
  451. return;
  452. }
  453. if (node.parentNode && node.parentNode.tagName == "body") {
  454. getScroll(node.parentNode);
  455. return;
  456. }
  457. scrollinfo.top += node.scrollTop;
  458. scrollinfo.left += node.scrollLeft; // position = fixed: 获取值后退出递归
  459. if (node.parentNode)
  460. getScroll(node.parentNode);
  461. }
  462. };
  463. this.offset = function () { /* 摘自jquery */
  464. var docElem,
  465. win,
  466. box = {
  467. top: 0,
  468. left: 0
  469. },
  470. elem = this.element,
  471. doc = elem && elem.ownerDocument;
  472. if (!doc) {
  473. return;
  474. }
  475. docElem = doc.documentElement;
  476. if (!DM.utils.contains(document.querySelectorAll("*"), this.element)) {
  477. return box;
  478. }
  479. if (typeof elem.getBoundingClientRect !== typeof undefined) {
  480. box = elem.getBoundingClientRect();
  481. }
  482. function getWindow(elem) {
  483. return (elem && elem == elem.window) ?
  484. elem :
  485. elem.nodeType === 9 ?
  486. elem.defaultView || elem.parentWindow :
  487. false;
  488. }
  489. win = getWindow(doc);
  490. return {
  491. top: box.top + (win.pageYOffset || docElem.scrollTop) - (docElem.clientTop || 0),
  492. left: box.left + (win.pageXOffset || docElem.scrollLeft) - (docElem.clientLeft || 0)
  493. };
  494. };
  495. this.hide = function () {
  496. $(this.element).hide();
  497. this.isshow = false;
  498. };
  499. this.show = function () {
  500. $(this.element).show();
  501. this.isshow = true;
  502. };
  503. this.find = function (selector) {
  504. return this.element.querySelectorAll(selector);
  505. };
  506. this.children = function (selector) {
  507. selector = selector || "*"
  508. var children = []
  509. $(this.element).children(selector).each(function () {
  510. children.push(this.confs == null ? new wd.drag.Base(this) : this.confs);
  511. });
  512. return children
  513. };
  514. this.is = function (selector) {
  515. return $(this.element).is(selector);
  516. };
  517. this.isDom = function (obj) {
  518. return typeof HTMLElement === 'object' ?
  519. obj instanceof HTMLElement :
  520. obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  521. };
  522. this.prev = function () {
  523. var ele = this.element.previousSibling;
  524. while (ele != null && ele.nodeType == 3) {
  525. ele = ele.previousSibling;
  526. }
  527. return ele == null ? null : (ele.confs || new wd.drag.Base(ele));
  528. };
  529. this.next = function () {
  530. var ele = this.element.nextSibling;
  531. while (ele != null && ele.nodeType == 3) {
  532. ele = ele.nextSibling;
  533. }
  534. return ele == null ? null : (ele.confs || new wd.drag.Base(ele));
  535. };
  536. this.prevAll = function (selector) {
  537. selector = selector || "*"
  538. var pe = this.element.parentNode;
  539. // var cs = pe.children;
  540. var cs = new wd.drag.Base(pe).children(selector);
  541. var arr = [];
  542. for (var i = 0; i < cs.length; i++) {
  543. var csi = cs[i];
  544. if (csi.element == this.element) {
  545. break;
  546. }
  547. arr.push(csi);
  548. }
  549. return arr;
  550. };
  551. this.nextAll = function (selector) {
  552. selector = selector || "*"
  553. var pe = this.element.parentNode;
  554. // var cs = pe.children;
  555. var cs = new wd.drag.Base(pe).children(selector);
  556. var arr = [];
  557. for (var i = cs.length - 1; i >= 0; i--) {
  558. var csi = cs[i];
  559. if (csi.element == this.element) {
  560. break;
  561. }
  562. arr.unshift(csi);
  563. }
  564. return arr;
  565. };
  566. this.remove = function () {
  567. if (this.bindedEvent) {
  568. for (var key in this.bindedEvent) {
  569. this.off(key);
  570. }
  571. }
  572. this.element.parentNode.removeChild(this.element);
  573. };
  574. this.isMouseOver = function (event) {
  575. event = event || window.event;
  576. var target = this.element;
  577. if (this.is("#ghost"))
  578. return 0;
  579. var sizeInfo = this.getSize();
  580. var result = 0;
  581. var cx = event.clientX;
  582. var cy = event.clientY;
  583. var left = sizeInfo.left;
  584. var top = sizeInfo.top;
  585. var width = sizeInfo.width;
  586. var height = sizeInfo.height;
  587. try {
  588. (cx >= left) && (cx <= (left + width)) && (cy >= top) && (cy <= (top + height)) && (result = 1);
  589. if (result == 0)
  590. return 0;
  591. if (cx >= left && cx <= parseInt(left + width / 2)) {
  592. result *= 1; //左边
  593. } else if (cx > parseInt(left + width / 2) && cx <= parseInt(left + width)) {
  594. result *= 2; //右边
  595. } else {
  596. result *= 0; //不在上面
  597. }
  598. if (cy >= top && cy <= parseInt(top + height / 3 * 2)) {
  599. result *= 3; //上半部分
  600. } else if (cy > parseInt(top + height / 3 * 2) && cy <= parseInt(top + height)) {
  601. result *= 4; //下半部分
  602. } else {
  603. result *= 0; //不在上面
  604. }
  605. } catch (e) {
  606. console.error(e)
  607. return 0;
  608. }
  609. //左上1*3=3,右上2*3=6,左下1*4=4,右下2*4=8,不在0*0
  610. return result;
  611. };
  612. this.isChildOf = function (b) {
  613. return this.isDom(b) && $.contains(b, this.element);
  614. };
  615. this.getStyles = function () {
  616. var _style;
  617. if (!this.exist()) {
  618. console.warn("注意,该节点不在文档里面,无法获取元素相关信息");
  619. offset = {
  620. top: this.element.offsetTop,
  621. left: this.element.offsetLeft
  622. }
  623. _style = this.element.style;
  624. } else {
  625. _style = window.getComputedStyle(this.element)
  626. offset = this.offset();
  627. }
  628. return _style;
  629. };
  630. this.getSize = function () {
  631. var offset,
  632. _style;
  633. if (!this.exist()) {
  634. console.warn("注意,该节点不在文档里面,无法获取元素相关信息");
  635. offset = {
  636. top: this.element.offsetTop,
  637. left: this.element.offsetLeft
  638. }
  639. } else {
  640. offset = this.offset();
  641. }
  642. _style = this.getStyles();
  643. var scrollinfo = this.getScrollInfo();
  644. var ele = this.element,
  645. sizeInfo = {},
  646. marginTop = parseInt(_style.marginTop.replace("px", "")) || 0,
  647. marginRight = parseInt(_style.marginRight.replace("px", "")) || 0,
  648. marginBottom = parseInt(_style.marginBottom.replace("px", "")) || 0,
  649. marginLeft = parseInt(_style.marginLeft.replace("px", "")) || 0,
  650. width = parseInt(_style.width.replace("px", "")) || 0,
  651. height = parseInt(_style.height.replace("px", "")) || 0,
  652. scrollleft = scrollinfo.left || document.documentElement.scrollLeft || document.body.scrollLeft || 0,
  653. scrolltop = scrollinfo.top || document.documentElement.scrollTop || document.body.scrollTop ||
  654. 0;
  655. // offset = $(this.element).offset();
  656. if (offset) {
  657. sizeInfo.top = offset.top - (document.documentElement.offsetTop || document.body.scrollTop); // 距离body的top
  658. sizeInfo.left = offset.left - (document.documentElement.offsetLeft || document.body.offsetLeft); // 距离body的left
  659. sizeInfo.width = width + marginLeft + marginRight; // 宽
  660. sizeInfo.height = height + marginTop + marginBottom; // 高
  661. sizeInfo.scrollleft = scrollleft;
  662. sizeInfo.scrolltop = scrolltop;
  663. }
  664. return sizeInfo;
  665. };
  666. this.each = $.each;
  667. this.off = function (eventNames, fn) {
  668. var ele = this.element;
  669. var enl = eventNames.split(" ");
  670. for (var i = 0; i < enl.length; i++) {
  671. var eventName = enl[i]
  672. if (this.bindedEvent[eventName]) {
  673. var fnNameL = this.bindedEvent[eventName];
  674. if (!fnNameL || fnNameL.length > 0) {
  675. this.each(fnNameL, function () {
  676. ele.removeEventListener && ele.removeEventListener(eventName, eval(this.toString()));
  677. ele.detachEvent && ele.detachEvent("on" + eventName, eval(this.toString()));
  678. window[this.toString()] == null;
  679. })
  680. if (fn) {
  681. fn.call(this, window.event);
  682. }
  683. }
  684. }
  685. }
  686. this.element && (this.element.bindedEvent = this.bindedEvent);
  687. return this;
  688. };
  689. this.trigger = function (eventNames) {
  690. var enl = eventNames.split(" ");
  691. for (var i = 0; i < enl.length; i++) {
  692. var eventName = enl[i];
  693. if (this.bindedEvent[eventName]) {
  694. var events = this.bindedEvent[eventName];
  695. this.each(events, function () {
  696. try {
  697. window[this.toString()].call(this, window.event);
  698. } catch (e) {
  699. console.error(e)
  700. }
  701. });
  702. }
  703. }
  704. return this;
  705. };
  706. this.on = function (eventNames, fn) {
  707. var ele = this.element;
  708. var this_ = this;
  709. var enl = eventNames.split(/\s+/i);
  710. for (var i = 0; i < enl.length; i++) {
  711. var eventName = enl[i];
  712. if (eventName == "")
  713. continue;
  714. var fnl = this.bindedEvent[eventName];
  715. if (!fnl) {
  716. fnl = this.bindedEvent[eventName] = [];
  717. }
  718. var fnName = "event_" + (this.id || "_") + "_" + new Date().getTime() + "_" + eventName + "_" + fnl.length;
  719. fnName = fnName.split(/\W+/).join("_");
  720. fnl.push(fnName);
  721. window[fnName] = function () {
  722. var event = window.event;
  723. if (fn.call(this_, event) === false) {
  724. event.preventDefault && event.preventDefault();
  725. event.stopPropagation && event.stopPropagation();
  726. event.cancelBubble && (event.cancelBubble = true);
  727. }
  728. };
  729. ele.addEventListener && ele.addEventListener(eventName, eval(fnName), false);
  730. ele.attachEvent && ele.attachEvent("on" + eventName, eval(fnName));
  731. }
  732. this.element && (this.element.bindedEvent = this.bindedEvent);
  733. return this;
  734. };
  735. this.addClass = function (name) {
  736. $(this.element).addClass(name);
  737. return this;
  738. };
  739. this.toggleClass = function (name) {
  740. if (!name) {
  741. return;
  742. }
  743. if (this.hasClass(name)) {
  744. this.removeClass(name);
  745. } else {
  746. this.addClass(name);
  747. }
  748. return this
  749. };
  750. this.hasClass = function (name) {
  751. return name && this.element.className.indexOf(name) > -1;
  752. };
  753. this.append = function (ele) {
  754. var e;
  755. if (this.isDom(ele)) {
  756. e = ele;
  757. } else if (this.isConfs) {
  758. e = ele.element;
  759. }
  760. $(this.element).append(e);
  761. };
  762. this.before = function (item) {
  763. if (!item) {
  764. return
  765. } else if (this.isDom(item)) {
  766. this.element.parentNode.insertBefore(item, this.element);
  767. } else if (item.isConfs) {
  768. this.element.parentNode.insertBefore(item.element, this.element);
  769. } else {
  770. console.log("??")
  771. }
  772. };
  773. this.after = function (item) {
  774. if (!item) {
  775. return
  776. } else if (this.isDom(item)) {
  777. this.element.parentNode.insertBefore(item, this.element.nextSibling);
  778. } else if (item.isConfs) {
  779. this.element.parentNode.insertBefore(item.element, this.element.nextSibling);
  780. }
  781. };
  782. this.removeClass = function (name) {
  783. $(this.element).removeClass(name);
  784. return this;
  785. };
  786. if (element && (element.confs || element.isConfs)) {
  787. var con = element.isConfs ? element : element.confs
  788. for (var key in con) {
  789. this.element.confs[key] = con[key];
  790. }
  791. // return;
  792. }
  793. };
  794. var dragCursor = ["cursor-vDragMultiple", "cursor-vDrag", "cursor-timing", "cursor-hDragMultiple", "cursor-hDrag", "cursor-timing"];
  795. wd.drag.DragPanel = function () {
  796. wd.drag.Base.call(this);
  797. var dragList = DM.currentDrager.focusedList;
  798. var ghost = this.ghost = new wd.drag.Ghost(dragList.currentMouseDownItem.element);
  799. dragList.element.appendChild(ghost.element);
  800. if (dragList.alignMode != "vertical") {
  801. // $(ghost.element).removeClass("hLine-warning").addClass("vLine-warning");
  802. // $(ghost.element).css({
  803. // "display": "inline-block",
  804. // "float": "left",
  805. // "height": "20px",
  806. // "position": "unset",
  807. // "height": "35px"
  808. // });
  809. }
  810. var box = this.element = dragList.element.cloneNode();
  811. // var styles = document.querySelectorAll("style");
  812. // this.each(styles, function () {
  813. // box.appendChild(this.cloneNode(true));
  814. // });
  815. // styles = document.querySelectorAll("link");
  816. // this.each(styles, function () {
  817. // box.appendChild(this.cloneNode(true));
  818. // });
  819. // styles = document.querySelectorAll("LINK");
  820. // this.each(styles, function () {
  821. // box.appendChild(this.cloneNode(true));
  822. // });
  823. box.confs = this;
  824. this.drager = dragList.drager;
  825. this.sizeInfo = dragList.currentMouseDownItem.getSize();
  826. this.left = dragList.currentMouseDownItem.cx - this.sizeInfo.left;
  827. this.top = dragList.currentMouseDownItem.cy - this.sizeInfo.top;
  828. box.style.zIndex = "9999";
  829. box.style.background = "white";
  830. box.style.border = "1px dashed black";
  831. box.style.borderRadius = ".3em";
  832. box.style.position = "fixed";
  833. box.style.padding = ".3em";
  834. box.id = "list";
  835. this.notAllowDrop = function () {
  836. this.isAllowDrop = false;
  837. this.mask.cursor_old = DM.dragingPanel.mask.style.cursor
  838. this.mask.style.cursor = "not-allowed";
  839. this.ghost.hide();
  840. };
  841. this.allowDrop = function () {
  842. this.isAllowDrop = true;
  843. this.mask.style.cursor = this.mask.cursor_old || "move";
  844. this.mask.cursor_old = null;
  845. this.ghost.show();
  846. };
  847. var items = this.items = [];
  848. var shadows = this.shadows = [];
  849. var count = 0;
  850. var its = dragList.getSelectedItems();
  851. its[0].hideDragList();
  852. var ss = its[0].getSize();
  853. this.each(its, function () {
  854. var item = this;
  855. var shadow = item.createShadow();
  856. if (item.isExpand)
  857. item.hideDragList();
  858. item.shadow = shadow;
  859. items.push(item);
  860. shadows.push(shadow);
  861. item.removeClass(item.getTopDragList({}).wdSelectedClass);
  862. $(item.element).find('.icon-checked').addClass('icon-unchecked').removeClass('icon-checked');
  863. // item.removeClass(item.getTopDragList({}).wdNormalClass);
  864. item.removeClass(item.getTopDragList({}).wdHoverClass);
  865. box.appendChild(item.element);
  866. });
  867. box.style.width = ss.width + 10 + "px";
  868. box.style.height = ss.height * its.length + "px";
  869. dragList.selectedItems = {};
  870. dragList.updateVisibleElements();
  871. dragList.trigger("mouseup");
  872. dragList.currentMouseDownItem = null;
  873. this.move = function (event) {
  874. var ele = this.element;
  875. ele.style.left = (event.clientX - this.left) + "px";
  876. ele.style.top = (event.clientY - this.top) + "px";
  877. };
  878. this.getDragList = function () {
  879. return (this.ghost.element.parentNode || {}).confs;
  880. };
  881. this.hasChild = function () {
  882. for (var i = 0; i < items.length; i++) {
  883. if (items[i].dragList && items[i].dragList.getItems().length > 0) {
  884. return true;
  885. }
  886. }
  887. return false;
  888. };
  889. var t = this.getDragList().alignMode != "vertical" ? 3 : 0;
  890. var c = items.length > 1 ? dragCursor[0 + t] : dragCursor[1 + t];
  891. this.destroy = function () {
  892. this.drager.selectedList && this.drager.selectedList.updateVisibleElements();
  893. this.drager.optionList && this.drager.optionList.updateVisibleElements();
  894. $("body,html").removeClass(dragCursor.join(" "));
  895. $(".notallow_item").removeClass("notallow_item");
  896. var this_ = this;
  897. var list = this.getDragList();
  898. this.each(this_.items, function () {
  899. try {
  900. var item__ = list.getItemById(this.id);
  901. if (item__.isGhost || item__.isShadow || item__ == this) { }
  902. else {
  903. item__.remove();
  904. console.log("找到重复的ID“" + this.id + "”,覆盖")
  905. }
  906. } catch (e) { }
  907. if (this_.ghost.isshow) {
  908. this_.ghost.before(this);
  909. this.select();
  910. } else {
  911. this.shadow.before(this)
  912. }
  913. })
  914. this.each(this_.shadows, function () {
  915. this.remove();
  916. })
  917. this.each(this.drager.getLists(), function () {
  918. var this_ = this;
  919. setTimeout(function () {
  920. this_.trigger("dragend");
  921. }, 1)
  922. });
  923. this.each(this.items, function () {
  924. var this_ = this;
  925. setTimeout(function () {
  926. this_.trigger("dragend");
  927. })
  928. });
  929. this.each(this.drager.getLists(), function () {
  930. this.clearSelect();
  931. });
  932. this.each(items, function () {
  933. this.select();
  934. });
  935. if (this.drager.dragend) {
  936. var doms = [];
  937. this.each(items, function () {
  938. doms.push(this.element);
  939. });
  940. this.drager.dragend.call(this, doms, this.ghost.element);
  941. };
  942. this.ghost.getDragList().focus();
  943. this.ghost.destroy();
  944. this.remove();
  945. DM.dragingPanel = null;
  946. };
  947. var mask = this.mask = $("<div style='clear:both;position:absolute;top:-1em;left:-1em;right:-1em;bottom:-1em;z-index:110000' class='panel'/>")[0];
  948. new wd.drag.Base(mask).on("mousewheel DOMMouseScroll", function (e) {
  949. function isCanScroll(obj) {
  950. if (obj) {
  951. var of = window.getComputedStyle(obj)["overflow-y"];
  952. return ("scroll|auto".indexOf(of) > -1) && (obj.scrollHeight > 0);
  953. } else {
  954. return false
  955. }
  956. };
  957. function findObjScrollParent(obj, i) {
  958. i = i || 0;
  959. i++;
  960. var par = obj.parentNode;
  961. if (obj.tagName == "HTML") {
  962. return obj.querySelector("body");
  963. }
  964. if (isCanScroll(par)) {
  965. return par;
  966. } else {
  967. return findObjScrollParent(par, i);
  968. }
  969. };
  970. var delta = (e.wheelDelta && (e.wheelDelta > 0 ? 1 : -1)) || // chrome & ie
  971. (e.detail && (e.detail > 0 ? -1 : 1)); // firefox
  972. findObjScrollParent(ghost.element).scrollTop -= delta * 35;
  973. ghost.getDragList().updateVisibleElements();
  974. });
  975. $("body,html").addClass(c);
  976. if (this.hasChild()) {
  977. $.each(this.drager.getLists(), function () {
  978. $(this.element).children(this.drager.itemSelector).addClass("notallow_item");
  979. });
  980. }
  981. box.appendChild(mask);
  982. document.body.appendChild(box);
  983. box.style.display = "none";
  984. DM.justEnd = true;
  985. };
  986. wd.drag.DragList = function (name, element) {
  987. wd.drag.Base.call(this);
  988. var drager = this.drager = DM.getInstance(name);
  989. this.visibleElements = [];
  990. this.wdNormalClass = element.getAttribute("ssNormClazz") || ""; // ("wdNormalClass")。Lin
  991. this.wdHoverClass = element.getAttribute("ssHovClazz") || ""; // ("wdHoverClass")。Lin
  992. this.wdSelectedClass = element.getAttribute("ssSelClazz") || ""; // ("wdSelectedClass")。Lin
  993. this.wdSelectedParentClass = element.getAttribute("wdSelectedParentClass") || "";
  994. this.element = element.isConfs ? element.element : element;
  995. this.isChild = false;
  996. this.isDragList = true;
  997. this.dragerName = name;
  998. this.id = this.element.id;
  999. this.element.confs = this;
  1000. this.unfocus = function () {
  1001. this.removeClass(this.wdSelectedParentClass);
  1002. this.clearSelect();
  1003. this.drager.focusedList = null;
  1004. DM.currentDrager = null;
  1005. };
  1006. this.focus = function () {
  1007. if (drager.focusedList && this != drager.focusedList) {
  1008. drager.focusedList.clearSelect();
  1009. drager.focusedList.unfocus();
  1010. }
  1011. drager.focusedList = this;
  1012. DM.currentDrager = this.drager;
  1013. DM.currentDrager.focusedList = this;
  1014. this.addClass(this.wdSelectedParentClass);
  1015. };
  1016. this.empty = function () {
  1017. this.each(this.getItems(), function () {
  1018. this.remove();
  1019. });
  1020. this.element.innerHTML = '';
  1021. this.trigger("dragend");
  1022. };
  1023. this.selectAll = function () {
  1024. this.each(this.element.children, function () {
  1025. this.confs && this.confs.select && this.confs.select();
  1026. });
  1027. };
  1028. this.clearSelect = function () {
  1029. this.each(this.element.children, function () {
  1030. if (this.confs && this.confs.exist() && !this.confs.isShadow && this.confs.isGhost != true) {
  1031. this.confs.unselect();
  1032. this.hasDragList && this.dragList && this.dragList.clearSelect();
  1033. }
  1034. });
  1035. this.selectedItems = {};
  1036. };
  1037. this.destroy = function () {
  1038. this.off("mousedown mouseup click dragend");
  1039. this.element.confs = null;
  1040. this.each(this.getItems(), function () {
  1041. this.destroy();
  1042. });
  1043. }
  1044. this.reload = function (fn) {
  1045. var this_ = this;
  1046. console.time()
  1047. this.init();
  1048. console.timeEnd();
  1049. this.each(this.drager.getLists(), function () {
  1050. this.each(this.getAllItems(), function () {
  1051. this.drager.items[this.id] = this;
  1052. })
  1053. });
  1054. fn && typeof fn == 'function' && fn();
  1055. }
  1056. this.shiftSelect = function () {
  1057. this.each(this.shiftSelectStartItem.nextAll(this.drager.itemSelector), function () {
  1058. this instanceof wd.drag.Item && this.select();
  1059. });
  1060. this.each(this.currentMouseDownItem.nextAll(this.drager.itemSelector), function () {
  1061. if (this instanceof wd.drag.Item)
  1062. if (this.isSelected()) {
  1063. this.unselect()
  1064. } else {
  1065. this.select();
  1066. }
  1067. });
  1068. this.shiftSelectStartItem.select();
  1069. this.currentMouseDownItem.select();
  1070. };
  1071. this.insertItemByHtml = function (objectid, html, index, isCover) {
  1072. var element = html;
  1073. !this.isDom(element) && (element = $(element)[0]);
  1074. objectid != null && element.setAttribute("ssObjId", objectid); // ("wdObjectID",。是 wdObjectID,不是 wdObjectId。Lin
  1075. var item = new wd.drag.Item(this.drager.name, element);
  1076. objectid && (item.id = objectid);
  1077. return this.insertItem(item, index, isCover);
  1078. };
  1079. this.insertItem = function (item, index, isCover) {
  1080. index = (index != null && index > -1) ? index : (this.getSelectedItems().length < 1 ? this.getItems().length : this.getSelectedItems()[0].getIndex());
  1081. this.drager.items[item.id] = item;
  1082. var items = this.getItems();
  1083. var old = this.getItemById(item.id)
  1084. if (old) {
  1085. console.log("找到重复的id:\"" + old.id + "\",覆盖(默认覆盖)?:" + ((isCover != false || isCover != "false") ? "覆盖" : "不覆盖"))
  1086. if (isCover != false || isCover != "false") {
  1087. old.remove();
  1088. } else {
  1089. console.warn("不覆盖,跳过处理")
  1090. return null;
  1091. }
  1092. }
  1093. var bool = false;
  1094. this.each(items, function () {
  1095. if (this.getIndex() == index) {
  1096. this.after(item);
  1097. bool = true;
  1098. return false;
  1099. }
  1100. });
  1101. !bool && this.append(item);
  1102. item.init();
  1103. item.trigger("dragend");
  1104. return item;
  1105. };
  1106. this.getSelectedItems = function () {
  1107. var arr = [];
  1108. for (var key in this.selectedItems) {
  1109. arr.push(this.selectedItems[key]);
  1110. }
  1111. return arr;
  1112. };
  1113. this.distnict = function () {
  1114. var ids = [];
  1115. this.each(this.getItems(), function () { });
  1116. };
  1117. this.updateInfo = function () {
  1118. this.each(this.getItems(), function () {
  1119. this.updateInfo();
  1120. });
  1121. };
  1122. this.getResult = function () {
  1123. this.updateInfo();
  1124. var result = [];
  1125. this.each(this.element.children, function () {
  1126. if (this.isGhost || this.isShadow || this.confs == null)
  1127. return;
  1128. var itemdata = {
  1129. ID: this.confs.id,
  1130. PID: this.confs.parentid || null
  1131. };
  1132. itemdata.PID == null && delete itemdata.PID;
  1133. itemdata.ID != null && result.push(itemdata);
  1134. if (this.confs.hasDragList && this.confs.dragList) {
  1135. this.confs.each(this.confs.dragList.getResult(), function () {
  1136. result.push(this);
  1137. });
  1138. }
  1139. });
  1140. return result;
  1141. };
  1142. this.getAllItems = function () {
  1143. var thisitems = this.getItems();
  1144. var tmp = [];
  1145. this.each(thisitems, function () {
  1146. this.hasDragList && this.dragList && (tmp = tmp.concat(this.dragList.getItems()));
  1147. });
  1148. return thisitems.concat(tmp);
  1149. }
  1150. this.getItemByIndex = function (index) {
  1151. if (this.getItems()[index])
  1152. return this.getItems()[index];
  1153. else
  1154. return null;
  1155. };
  1156. this.getItemListObj = function () {
  1157. var obj = {};
  1158. this.each(this.getItems(), function () {
  1159. obj[this.id] = this;
  1160. });
  1161. return obj;
  1162. }
  1163. this.getItemById = function (id) {
  1164. var result;
  1165. this.each(this.getItems(), function () {
  1166. if (this.id == id) {
  1167. result = this;
  1168. return false;
  1169. }
  1170. });
  1171. return result;
  1172. };
  1173. this.updateVisibleElements = function () {
  1174. if (this.alignMode == "vertical") {
  1175. this.visibleElements = this.getItems();
  1176. } else {
  1177. var start = this.getScrollInfo().top - 15;
  1178. var end = this.getSize().height + start + 15;
  1179. this.visibleElements = [];
  1180. while (start++ <= end) {
  1181. try {
  1182. this.visibleElements.push(this.element.querySelector("[ssObjId][tp='" + start + "']").confs); // ("[wdObjectID][。是 wdObjectID,不是 wdObjectId。Lin
  1183. } catch (e) { }
  1184. }
  1185. }
  1186. };
  1187. this.getItems = function () {
  1188. var result = [];
  1189. this.each(this.children(this.drager.itemSelector), function () {
  1190. (this.isItem || this.isShadow) && this.isConfs && result.push(this);
  1191. });
  1192. return result;
  1193. };
  1194. this.init = function () {
  1195. this.selectedItems = {};
  1196. var this_ = this;
  1197. var t = {};
  1198. this.on("selectstart", function (event) {
  1199. if (isEventTargetIsEditable(event)) {
  1200. return true;
  1201. }
  1202. event.preventDefault();
  1203. event.stopPropagation();
  1204. return false;
  1205. });
  1206. this.on("mousewheel DOMMouseScroll", function () {
  1207. this.updateVisibleElements();
  1208. })
  1209. this.on("mouseover", function (event) {
  1210. var panel = DM.dragingPanel;
  1211. if (panel && panel.drager == this.drager && !$.contains(this.element, panel.ghost.element)) {
  1212. this.append(panel.ghost.element);
  1213. }
  1214. return false;
  1215. });
  1216. this.each(this.children(this_.drager.itemSelector), function (index) {
  1217. // if(new wd.drag.Base(this).is(this_.drager.itemSelector)) {
  1218. var item = this.isItem ? this : new wd.drag.Item(name, this);
  1219. var id = item.id;
  1220. if (t[id] == null) {
  1221. t[id] = item;
  1222. item.init();
  1223. this_.drager.items[item.id] = item;
  1224. } else {
  1225. console.log("找到重复的ID“" + id + "”,删除")
  1226. item.remove();
  1227. }
  1228. if (item.id != null && !this_.isSelectedList && this_.drager.selectedList && this_.drager.selectedList.getItemById(item.id) != null) {
  1229. item.remove();
  1230. }
  1231. // }
  1232. });
  1233. if (drager.group) {
  1234. //判断是否存在环
  1235. var tmp = {};
  1236. this.each(this.getItems(), function () {
  1237. var t = tmp[this.id] = tmp[this.id] || {
  1238. id: this.id
  1239. };
  1240. if (this.parentid) {
  1241. tmp[this.parentid] = tmp[this.parentid] || {};
  1242. tmp[this.parentid].child = t;
  1243. }
  1244. });
  1245. try {
  1246. var items = this.getItemListObj();
  1247. JSON.stringify(tmp);
  1248. for (var key in items) {
  1249. if (items[key].parentid && this_.drager.items[items[key].parentid]) {
  1250. this_.drager.items[items[key].parentid].dragList.insertItem(items[key]);
  1251. }
  1252. }
  1253. } catch (e) {
  1254. console.log(items)
  1255. console.error("错误:上下级关系存在环,不进行分组放置处理");
  1256. }
  1257. } else {
  1258. this.each(this.find("[ssObjId]"), function () { // ("[wdObjectID]"),。是 wdObjectID,不是 wdObjectId。Lin
  1259. this_.append(this);
  1260. });
  1261. }
  1262. if (this.isChild || this.thisItem) {
  1263. this.element.id = this.thisItem.id + new Date().getTime();
  1264. this.isChild = true;
  1265. }
  1266. this.on("dragend", function () {
  1267. var its = this.getItems();
  1268. var l = 30; //个一组
  1269. var ii = 0;
  1270. while (ii < its.length) {
  1271. (function (arr) {
  1272. setTimeout(function () {
  1273. for (var i = 0; i < arr.length; i++) {
  1274. arr[i] && arr[i].trigger("dragend");
  1275. }
  1276. });
  1277. })(its.slice(ii, ii += l));
  1278. }
  1279. if (this.result_input) {
  1280. this.result_input.value = JSON.stringify(this.getResult());
  1281. }
  1282. this.updateInfo();
  1283. });
  1284. this.off("mousedown").on("mousedown", function (event) {
  1285. if (isEventTargetIsEditable(event)) {
  1286. return;
  1287. }
  1288. this.focus();
  1289. event.stopPropagation();
  1290. return false;
  1291. });
  1292. this.off("mouseup").on("mouseup", function () {
  1293. // event.stopPropagation();
  1294. // return false;
  1295. });
  1296. this.off("click").on("click", function (event) {
  1297. event.stopPropagation();
  1298. return false;
  1299. });
  1300. this.trigger("dragend");
  1301. this.updateVisibleElements();
  1302. };
  1303. };
  1304. var textWrapperClass = ["list", "list", "list-highlight", "list-highlight"];
  1305. var iconClassName = ["icon-doc", "icon-point", "icon-openFolder", "icon-folder"];
  1306. wd.drag.ShowBtn = function (itemelement) {
  1307. wd.drag.Base.call(this);
  1308. var element = this.element = $("<span>&nbsp;</span>")[0];
  1309. element.confs = this;
  1310. // element.style.cursor = 'pointer';
  1311. var itemEle = this.itemEle = itemelement;
  1312. this.init = function () {
  1313. // this.element.style.display = "inline-block";
  1314. // this.element.style.width = this.itemEle.confs.drager.imgleft + "px";
  1315. // this.element.style.background = "url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADoSURBVBgZBcExblNBGAbA2ceegTRBuIKOgiihSZNTcC5LUHAihNJR0kGKCDcYJY6D3/77MdOinTvzAgCw8ysThIvn/VojIyMjIyPP+bS1sUQIV2s95pBDDvmbP/mdkft83tpYguZq5Jh/OeaYh+yzy8hTHvNlaxNNczm+la9OTlar1UdA/+C2A4trRCnD3jS8BB1obq2Gk6GU6QbQAS4BUaYSQAf4bhhKKTFdAzrAOwAxEUAH+KEM01SY3gM6wBsEAQB0gJ+maZoC3gI6iPYaAIBJsiRmHU0AALOeFC3aK2cWAACUXe7+AwO0lc9eTHYTAAAAAElFTkSuQmCC) no-repeat right ";
  1316. // this.element.style.backgroundSize = 'auto 100%';
  1317. }
  1318. this.destroy = function () {
  1319. this.remove();
  1320. }
  1321. this.initEvent = function () {
  1322. this.on("click", function () {
  1323. if (functionKeyDown)
  1324. return;
  1325. var cfs = itemEle.confs
  1326. if ((cfs.dragList && cfs.isExpand) || (!cfs.drager.group || (cfs.dragList && cfs.dragList.getItems().length == 0))) {
  1327. cfs.hideDragList()
  1328. } else {
  1329. cfs.showDragList();
  1330. }
  1331. event.stopPropagation();
  1332. return false;
  1333. });
  1334. }
  1335. this.switchImg = function () {
  1336. var cfs = itemEle.confs;
  1337. if (!cfs.drager.group || (cfs.dragList && cfs.dragList.getItems() == 0)) { //不是分组,也没有子内容
  1338. this.fileStyle();
  1339. } else {
  1340. if (cfs.isExpand) {
  1341. this.expandFolderStyle();
  1342. } else {
  1343. this.normalFolderStyle();
  1344. }
  1345. }
  1346. };
  1347. this.toggleIcon = function (state) {
  1348. $(this.element).removeClass(iconClassName.join(" ")).addClass(iconClassName[state]);
  1349. $(this.element.textwrapper).removeClass(textWrapperClass.join(" ")).addClass(textWrapperClass[state]);
  1350. var cn = this.itemEle.confs;
  1351. if (state > 1) { //文件夹样式
  1352. cn.addClass("filelist");
  1353. } else {
  1354. cn.removeClass("filelist");
  1355. }
  1356. }
  1357. this.expandFolderStyle = function () {
  1358. this.toggleIcon(2)
  1359. };
  1360. this.normalFolderStyle = function () {
  1361. this.toggleIcon(3);
  1362. };
  1363. this.fileStyle = function () {
  1364. if (itemEle.confs.getDragList().isChild) {
  1365. this.toggleIcon(1);
  1366. } else {
  1367. this.toggleIcon(0);
  1368. }
  1369. };
  1370. itemEle.insertBefore(element, itemEle.childNodes[0]);
  1371. this.init();
  1372. return this;
  1373. };
  1374. wd.drag.Item = function (name, element) {
  1375. wd.drag.Base.call(this);
  1376. this.element = element.isConfs ? element.element : element;
  1377. this.element.confs = this;
  1378. this.isItem = true;
  1379. this.id = (this.element.getAttribute("ssObjId") || (itemid++) + "").split("#").join("").split(".").join(""); // ("wdObjectID")。是 wdObjectID,不是 wdObjectId。Lin
  1380. this.parentid = (this.element.getAttribute("ssPobjId") || "").split("#").join("").split(".").join(""); // ("wdParentObjectID")。是 wdParentObjectID,不是 wdParentObjectId。Lin
  1381. var drager = this.drager = DM.getInstance(name);
  1382. this.hasDragList = false;
  1383. this.createShadow = function () {
  1384. var clone = new wd.drag.Shadow(this.element);
  1385. this.element.parentNode.insertBefore(clone.element, this.element);
  1386. this.shadow = clone;
  1387. return clone;
  1388. };
  1389. this.isSelected = function () {
  1390. return this.hasClass(this.getTopDragList({}).wdSelectedClass);
  1391. };
  1392. this.getTopDragList = function (emptyObj) {
  1393. if (!this.exist())
  1394. console.log("!!!")
  1395. var list = this.getDragList();
  1396. if (list.isChild)
  1397. list = list.thisItem.getDragList();
  1398. return list;
  1399. }
  1400. this.select = function () {
  1401. if (!this.drager.editGroup && !this.drager.editOrder && this.drager.getLists().length < 2) { }
  1402. else {
  1403. this.isFunctionKeyDown = functionKeyDown || event.ctrlKey || event.altKey || event.shiftKey;
  1404. this.getDragList().selectedItems[this.id] = this;
  1405. this.addClass(this.getTopDragList({}).wdSelectedClass);
  1406. $(this.element).find(".icon-unchecked").addClass('icon-checked').removeClass('icon-unchecked');
  1407. }
  1408. };
  1409. this.unselect = function () {
  1410. this.isFunctionKeyDown = false;
  1411. delete this.getDragList().selectedItems[this.id];
  1412. this.removeClass(this.getTopDragList({}).wdSelectedClass);
  1413. $(this.element).find(".icon-checked").addClass('icon-unchecked').removeClass('icon-checked');
  1414. };
  1415. this.getParentItem = function () {
  1416. var list = this.getDragList();
  1417. if (list && list.isChild) {
  1418. return list.thisItem;
  1419. } else {
  1420. return null;
  1421. }
  1422. };
  1423. this.getDragList = function () {
  1424. return (this.element.parentNode || {}).confs;
  1425. };
  1426. this.hideDragList = function () {
  1427. this.isExpand = false;
  1428. if (this.getDragList() && !this.drager.group)
  1429. return;
  1430. if (this.getDragList().isChild && this.imgBtn) {
  1431. this.dragList.hide();
  1432. // this.dragList.element.style.height = "auto";
  1433. } else if (!this.getDragList().isChild && this.imgBtn) {
  1434. this.dragList.hide();
  1435. // this.dragList.element.style.height = "auto";
  1436. }
  1437. if (this.imgBtn)
  1438. this.imgBtn.switchImg();
  1439. this.getDragList().focus();
  1440. };
  1441. this.showDragList = function () {
  1442. if (!this.drager.group)
  1443. return;
  1444. if (DM.dragingPanel && DM.dragingPanel.hasChild())
  1445. return;
  1446. if (this.getDragList().isChild /*|| !(this.getDragList()instanceof wd.drag.DragList)*/)
  1447. return;
  1448. if (this.imgBtn) {
  1449. this.isExpand = true;
  1450. if (this.dragList.getItems().length > 0) {
  1451. this.dragList.element.style.height = "auto";
  1452. } else if (this.dragList.getSize().height == 0) {
  1453. this.dragList.element.style.minHeight = this.getSize().height + "px";
  1454. }
  1455. this.dragList.show();
  1456. this.dragList.focus();
  1457. this.imgBtn.switchImg();
  1458. }
  1459. };
  1460. this.getIndex = function () {
  1461. this.index = this.prevAll().length;
  1462. return this.index;
  1463. };
  1464. this.updateInfo = function () {
  1465. this.parentid = this.getDragList().thisItem == null ? null : this.getDragList().thisItem.id;
  1466. this.element.setAttribute("ssPobjId", this.parentid); // ("wdParentObjectID",。是 wdParentObjectID,不是 wdParentObjectId。Lin
  1467. this.index = this.prevAll().length;
  1468. if (!this.getDragList().isChild && this.dragList) {
  1469. this.dragList.isChild = true;
  1470. if (this.imgBtn) {
  1471. var len = this.dragList.children().length;
  1472. this.imgBtn.element.innerText = len > 0 ? len : "";
  1473. }
  1474. } else if (this.dragList) {
  1475. this.dragList.isChild = false;
  1476. }
  1477. if (this.dragList && this.dragList.getItems().length == 0) {
  1478. this.hideDragList();
  1479. }
  1480. if (this.imgBtn)
  1481. this.imgBtn.switchImg();
  1482. };
  1483. this.destroy = function () {
  1484. this.off("dblclick selectedClick click dragend mousedown mouseup mousemove mouseover mouseout mouseleave");
  1485. this.off("dblclick selectedClick click dragend mousedown mouseup mousemove mouseover mouseout mouseleave");
  1486. this.imgBtn && this.imgBtn.destroy();
  1487. this.hasDragList && this.dragList && (this.drager.functions.indexOf('group') > -1) && this.dragList.destroy();
  1488. this.element.confs = null;
  1489. }
  1490. this.init = function () {
  1491. var me = this;
  1492. this.isExpand = false;
  1493. if (this.drager.group) {
  1494. //初始化第二层,包住
  1495. var $div = $("<div>");
  1496. $div.attr("id", "wrapper_" + this.id);
  1497. $div.append($(this.element.childNodes));
  1498. $(this.element).append($div);
  1499. this.imgBtn = this.imgBtn || new wd.drag.ShowBtn(this.element);
  1500. this.imgBtn.element.textwrapper = $div[0];
  1501. this.dragList = new wd.drag.DragList(name, document.createElement("div"));
  1502. this.append(this.dragList.element);
  1503. this.dragList.thisItem = this;
  1504. this.dragList.addClass("subList");
  1505. this.dragList.hide();
  1506. this.imgBtn.initEvent();
  1507. this.hasDragList = true;
  1508. };
  1509. var _checkbox = this.checkbox = document.createElement("span");
  1510. // _checkbox.innerHTML = "";
  1511. // _checkbox.className='dragcheckbox';
  1512. // _checkbox.style.width="15px";
  1513. // _checkbox.style.border='1px dashed black';
  1514. _checkbox.style.cssText = 'display: inline-block; width: 28px;height: 40px;float: left;';
  1515. _checkbox.className = "icon-unchecked";
  1516. _checkbox.onmousedown = _checkbox.onmouseup = function (e) {
  1517. e.stopPropagation();
  1518. }
  1519. _checkbox.onclick = function (e) {
  1520. if (!me.isSelected()) {
  1521. me.select();
  1522. } else {
  1523. me.unselect();
  1524. }
  1525. e.stopPropagation();
  1526. }
  1527. $(this.element).find("." + _checkbox.className).length < 1 && $(this.element).prepend(_checkbox);
  1528. this.element.styleinfo = this.getStyles();
  1529. // this.element.style.position = "relative";
  1530. this.addClass(this.getTopDragList({}).wdNormalClass);
  1531. var this_ = this;
  1532. this_.element.setAttribute("tp", this.element.offsetTop);
  1533. this.off("dblclick").on("dblclick", function () {
  1534. if (this.imgBtn) {
  1535. this.imgBtn.trigger("click");
  1536. }
  1537. if (this.drager.onDblClick) {
  1538. this.drager.onDblClick.call(this);
  1539. }
  1540. });
  1541. this.off("selectedClick").on("selectedClick", function (event) {
  1542. this_.drager.selectedClick && this_.drager.selectedClick.call(this, this.element);
  1543. })
  1544. this.off("click").on("click", function (event) {
  1545. if (isEventTargetIsEditable(event)) {
  1546. return;
  1547. }
  1548. this.triggerSelectedClick && this.trigger("selectedClick");
  1549. this_.drager.itemClick && this_.drager.itemClick.call(this, this.element);
  1550. });
  1551. this.off("dragend").on("dragend", function () {
  1552. this.hasDragList && this.getDragList() && !this.getDragList().isChild && this.dragList.trigger("dragend");
  1553. if (this.getDragList() && this.getDragList().isChild) {
  1554. this.hideDragList();
  1555. } else if (this.isExpand) {
  1556. this.showDragList();
  1557. } else {
  1558. this.hideDragList();
  1559. }
  1560. if (this.getTopDragList({}).isSelectedList) {
  1561. this.each(this.find('[wdSelected="true"]'), function () {
  1562. this.style.display = "";
  1563. });
  1564. this.each(this.find('[wdOption="true"]'), function () {
  1565. this.display = this.style.display;
  1566. this.style.display = 'none';
  1567. });
  1568. } else if (this.getTopDragList({}).isOptionList) {
  1569. this.each(this.find('[wdOption="true"]'), function () {
  1570. this.style.display = "";
  1571. });
  1572. this.each(this.find('[wdSelected="true"]'), function () {
  1573. this.display = this.style.display;
  1574. this.style.display = 'none';
  1575. });
  1576. }
  1577. this.addClass(this.getTopDragList({}).wdNormalClass);
  1578. this.updateInfo();
  1579. });
  1580. this.off("mousedown").on("mousedown", function (event) {
  1581. if (isEventTargetIsEditable(event)) {
  1582. return;
  1583. }
  1584. $("[ssObjId] [editable][contenteditable]").blur().removeAttr("contenteditable"); // ("[wdObjectId] [。Lin
  1585. DM.justEnd = false;
  1586. this.cx = event.clientX;
  1587. this.cy = event.clientY;
  1588. this.getDragList().focus();
  1589. var selected = this.selected = this.isSelected();
  1590. this.isFunctionKeyDown = functionKeyDown || this.isFunctionKeyDown;
  1591. this.getDragList().currentMouseDownItem = this;
  1592. if (event.shiftKey) {
  1593. var s = this.getDragList().shiftSelectStartItem;
  1594. if (s) {
  1595. if (this.isChildOf(s.getDragList().element)) {
  1596. this.getDragList().shiftSelect();
  1597. } else {
  1598. this.getDragList().currentMouseDownItem = null;
  1599. return false;
  1600. }
  1601. }
  1602. } else {
  1603. this.getDragList().shiftSelectStartItem = null;
  1604. }
  1605. this.select();
  1606. if (selected) { //第二次或以上选中
  1607. this.triggerSelectedClick = true;
  1608. } else { //第一次选中
  1609. this.triggerSelectedClick = false;
  1610. }
  1611. this.getDragList().focus();
  1612. dragItemDown = true;
  1613. if (this.drager.list != null) {
  1614. return false;
  1615. }
  1616. dragStartPoint = {
  1617. x: event.clientX,
  1618. y: event.clientY
  1619. };
  1620. event.stopPropagation();
  1621. docMouseDown();
  1622. return false;
  1623. });
  1624. this.off("mouseup").on("mouseup", function (event) {
  1625. docMouseUp();
  1626. var isSelected = this.isSelected();
  1627. this.getDragList().shiftSelectStartItem = this.getDragList().shiftSelectStartItem || this;
  1628. this.getDragList().currentMouseDownItem = null;
  1629. if (!(event.ctrlKey || event.shiftKey)) {
  1630. this.getDragList().clearSelect();
  1631. this.select();
  1632. }
  1633. this.selected && event.ctrlKey && this.unselect();
  1634. event.stopPropagation();
  1635. return false;
  1636. });
  1637. this.off("mouseleave").on("mouseleave", function (event) {
  1638. if (isEventTargetIsEditable(event)) {
  1639. return;
  1640. }
  1641. this.addClass(this.getTopDragList({}).wdNormalClass);
  1642. this.removeClass(this.getTopDragList({}).wdHoverClass);
  1643. // this.element.style.border = "";
  1644. if (this.intervalid) {
  1645. clearInterval(this.intervalid);
  1646. this.intervalid = null;
  1647. }
  1648. if (this.timeoutid) {
  1649. clearTimeout(this.timeoutid);
  1650. this.timeoutid = null
  1651. }
  1652. clearMouseIcon();
  1653. this.isEnter = false;
  1654. // this.element.style.border = "";
  1655. });
  1656. this.off("mouseover").on("mouseover", function () {
  1657. this.addClass(this.getTopDragList({}).wdHoverClass);
  1658. itemMouseEvent.call(this, event);
  1659. this.isEnter = true;
  1660. return false;
  1661. });
  1662. // this.off("mousemove").on("mousemove", function () {
  1663. // !this.isEnter && itemMouseEvent.call(this, event);
  1664. // });
  1665. this.element.setAttribute("ssObjId", this.id); // ("wdObjectID",。是 wdObjectID,不是 wdObjectId。Lin
  1666. this.element.setAttribute("ssPobjId", this.parentid); // ("wdParentObjectID",。是 wdParentObjectID,不是 wdParentObjectId。Lin
  1667. this.drager.functions && this.drager.group && this.dragList && this.dragList.init();
  1668. var $editable = $(this.element).find("[editable]");
  1669. $editable.click(function (event) {
  1670. if (isContenteditable(this)) {
  1671. event.stopPropagation();
  1672. return false;
  1673. } else if (this_.triggerSelectedClick) {
  1674. if ($(event.target).is("[editable]")) {
  1675. $(event.target).attr("contenteditable", true);
  1676. event.target.focus();
  1677. }
  1678. }
  1679. });
  1680. var keyAction=function (event) {
  1681. var e = event || window.event;
  1682. var keycode = e.charCode || e.keyCode;
  1683. if (keycode == 13) {
  1684. if (window.event) {
  1685. window.event.returnValue = false;
  1686. } else {
  1687. e.preventDefault(); //for firefox
  1688. }
  1689. e.stopPropagation();
  1690. return false;
  1691. } else if (isContenteditable(this)) {
  1692. this.realContent = this.innerHtml;
  1693. try {
  1694. this_.drager.onItemInput && this_.drager.onItemInput.apply(this, [event,this_]);
  1695. } catch (e) {
  1696. console.error(e);
  1697. }
  1698. }
  1699. }
  1700. $editable.keyup(keyAction);
  1701. // $editable.keydown(keyAction);
  1702. $editable.each(function () {
  1703. this.commonHeight = $(this).height();
  1704. });
  1705. $editable.blur(function () {
  1706. this.realContent = this.innerHTML;
  1707. this.setAttribute("title",this.realContent);
  1708. console.log(this.realContent);
  1709. var content = wd.display.ellipsisContent(this, this.commonHeight);
  1710. this.innerHTML = content;
  1711. console.log("blur")
  1712. });
  1713. $editable.focus(function () {
  1714. if (!isContenteditable(this)) {
  1715. return;
  1716. }
  1717. console.log(this.realContent);
  1718. var content = this.realContent;
  1719. if (content) {
  1720. this.innerHTML = content;
  1721. console.log(content)
  1722. }
  1723. })
  1724. }
  1725. };
  1726. wd.drag.Form = function (element) {
  1727. wd.drag.Base.call(this);
  1728. this.element = element;
  1729. this.parseForm2Url = function () {
  1730. var form = this.element;
  1731. var arr = [];
  1732. var action_ = form.getAttribute("action");
  1733. var param = (action_ || "?").split("?");
  1734. arr = arr.concat((param[1] || "").split("&"));
  1735. var elements = form.querySelectorAll("input,select,textarea");
  1736. for (var i = 0; i < elements; i++) {
  1737. var feled = elements[i];
  1738. switch (feled.type) {
  1739. case undefined:
  1740. case 'button':
  1741. case 'file':
  1742. case 'reset':
  1743. case 'submit':
  1744. break;
  1745. case 'checkbox':
  1746. case 'radio':
  1747. if (!feled.checked) {
  1748. break;
  1749. }
  1750. default:
  1751. if (feled.value != null && feled.value != '' && feled.name != null && !feled.disabled) {
  1752. arr.push(feled.name + "=" + feled.value);
  1753. }
  1754. }
  1755. }
  1756. return param[0] + "?" + arr.join("&");
  1757. };
  1758. this.parseFormData = function () {
  1759. var form = this.element;
  1760. var params = {};
  1761. var elements = form.querySelectorAll("input,select,textarea");
  1762. for (var i = 0; i < elements.length; i++) {
  1763. var feled = elements[i];
  1764. console.log(feled.name, feled.value)
  1765. switch (feled.type) {
  1766. case undefined:
  1767. case 'button':
  1768. case 'file':
  1769. case 'reset':
  1770. case 'submit':
  1771. break;
  1772. case 'checkbox':
  1773. case 'radio':
  1774. if (!feled.checked) {
  1775. break;
  1776. }
  1777. default:
  1778. feled.value != null && feled.value != '' && feled.name != null && !feled.disabled && (params[feled.name] = feled.value);
  1779. }
  1780. }
  1781. console.log(params)
  1782. return params;
  1783. };
  1784. this.submit = function () {
  1785. var data = this.parseFormData(),
  1786. url = this.element.getAttribute("action"),
  1787. this_ = this,
  1788. meWin = window,
  1789. drager = this_.drager
  1790. wd.c.wdAjax({
  1791. url: url,
  1792. type: "post",
  1793. data: data,
  1794. async: false,
  1795. success: function (data) {
  1796. /// 增加,把 &lt;、&gt; 转为 <、> -- 解决 .querySelectorAll() 取不到 Dom 的问题。Lin
  1797. /* 再去掉,&lt;、&gt; 已经在上层转为 <、> 了 -- 再转,就成了 "\n\n\n\n学员文件人员班级部门初始岗位单位生产任务初始部门初始人员项目岗位来访校区专业\n\n\n"。Lin
  1798. var temp = document.createElement("div");
  1799. temp.innerHTML = data;
  1800. var output = temp.innerText;
  1801. temp = null;
  1802. */
  1803. ///
  1804. var resultdiv = document.createElement("div");
  1805. resultdiv.style.display = "none";
  1806. resultdiv.id = new Date().getTime();
  1807. /* 再改回,去掉 output 了 -- 见上面。Lin
  1808. resultdiv.innerHTML = output; // = data。Lin
  1809. */ resultdiv.innerHTML = data;
  1810. doms = resultdiv.querySelectorAll(drager.itemSelector);
  1811. drager.optionList.empty();
  1812. $.each(doms, function () {
  1813. drager.optionList.append(this);
  1814. });
  1815. var scripts = resultdiv.querySelectorAll("script");
  1816. for (var i = 0; i < scripts.length; i++) {
  1817. var s = scripts[i];
  1818. if (s.innerHTML != null /*&& s.innerHTML.indexOf(tokenCleanser) > -1*/) {
  1819. try {
  1820. meWin.eval(s.innerHTML);
  1821. } catch (e) {
  1822. console.error(e)
  1823. }
  1824. } else {
  1825. //$(s).remove();
  1826. }
  1827. }
  1828. drager.optionList.reload();
  1829. }
  1830. });
  1831. };
  1832. this.init = function () {
  1833. console.log(this.element.tagName)
  1834. this.element.tagName.toLowerCase() == "form" && (this.element.tagName = "div");
  1835. var this_ = this,
  1836. btn = this.element.querySelector("input[type='submit']");
  1837. this.on("submit onsubmit", function () {
  1838. this.submit();
  1839. });
  1840. this.element.submit = function () {
  1841. this_.submit();
  1842. };
  1843. if (btn != null) {
  1844. this.submitBtn = new wd.drag.Base(btn);
  1845. this.submitBtn.on("click", function (event) {
  1846. this_.trigger("submit");
  1847. event.stopPropagation();
  1848. event.preventDefault();
  1849. return false;
  1850. });
  1851. }
  1852. };
  1853. this.init();
  1854. };
  1855. wd.drag.Ghost = function (element) {
  1856. wd.drag.Base.call(this);
  1857. this.drager = element.confs.drager;
  1858. this.isGhost = true;
  1859. var sizeInfo = this.sizeInfo = element.confs.getSize();
  1860. var ele = this.element = document.createElement(element.tagName);
  1861. ele.className = element.className;
  1862. ele.confs = this;
  1863. ele.setAttribute("class", "" /*"hLine-warning"*/)
  1864. ele.style.cssText = "";
  1865. ele.innerHTML = "";
  1866. ele.id = "ghost";
  1867. this.dashMode = function () {
  1868. // ele.style.width = sizeInfo.width + "px";
  1869. // ele.style.height = sizeInfo.height + "px";
  1870. // ele.style.border = "1px dashed red";
  1871. // ele.style.borderRadius = "5px";
  1872. // ele.style.background = "white";
  1873. };
  1874. this.getDragList = function () {
  1875. return (this.element.parentNode || {}).confs;
  1876. };
  1877. this.getTopDragList = function () {
  1878. var list = this.getDragList();
  1879. if (list.isChild)
  1880. list = list.thisItem.getDragList();
  1881. return list;
  1882. };
  1883. this.lineMode = function () {
  1884. // ele.style.borderRadius = "0";
  1885. // ele.style.margin = 0;
  1886. // ele.style.background = "padding-box red";
  1887. // ele.style.borderStyle = "solid";
  1888. // ele.style.borderWidth = "2px";
  1889. // ele.style.padding = '0';
  1890. // if (this.getDragList() && this.getTopDragList({}).alignMode == 'vertical') {
  1891. // ele.style.width = "2px";
  1892. // ele.style.height = sizeInfo.height + "px";
  1893. // ele.style.minWidth = "0px";
  1894. // ele.style.borderColor = "red transparent";
  1895. // } else {
  1896. // ele.style.width = "auto";
  1897. // ele.style.height = "2px";
  1898. // ele.style.minHeight = "0px";
  1899. // ele.style.borderColor = "transparent red";
  1900. // }
  1901. }
  1902. this.destroy = function () {
  1903. this.interval && clearInterval(this.interval);
  1904. this.remove();
  1905. }
  1906. this.init = function () {
  1907. var this_ = this;
  1908. this.interval = setInterval(function () {
  1909. var allow = true;
  1910. if (DM.dragingPanel == null)
  1911. clearInterval(this_.interval);
  1912. if (DM.dragingPanel.isAllowDrop == false) {
  1913. allow = false;
  1914. } else if (this_.drager != this_.getDragList().drager) {
  1915. allow = false;
  1916. } else if (this_.drager.editGroup) { //最多功能的
  1917. allow = true;
  1918. } else if (this_.drager.editOrder) { //只能同级拖拽
  1919. } else { //不能上下拖拽
  1920. }
  1921. allow && DM.dragingPanel.allowDrop();
  1922. !allow && DM.dragingPanel.notAllowDrop();
  1923. }, 5);
  1924. this.hide();
  1925. }
  1926. this.init();
  1927. this.lineMode();
  1928. };
  1929. function itemMouseEvent(event) {
  1930. var panel = DM.dragingPanel;
  1931. if (panel != null) {
  1932. var item = this;
  1933. var location = item.isMouseOver(event);
  1934. var dragList = item.getDragList();
  1935. var ghost = panel.ghost;
  1936. if (item.drager.editOrder || item.drager.editGroup) {
  1937. ghost.addClass("ghost");
  1938. if (location && item.getTopDragList({}).alignMode != "horizontal") { //如果不是水平分布,就上下拖动排序
  1939. ghost.removeClass("vLine-warning").addClass("hLine-warning");
  1940. switch (location) {
  1941. case 3: //左上
  1942. case 6: //右上
  1943. item.before(ghost);
  1944. break;
  1945. case 4:
  1946. case 8:
  1947. item.after(ghost);
  1948. break;
  1949. }
  1950. } else if (location) { //不然就左右分布
  1951. ghost.removeClass("hLine-warning").addClass("vLine-warning");
  1952. switch (location) {
  1953. case 3: //左上
  1954. case 4: //左下
  1955. item.before(ghost);
  1956. break;
  1957. case 6: //右上
  1958. case 8: //右下
  1959. item.after(ghost);
  1960. break;
  1961. }
  1962. }
  1963. if (!item.drager.editGroup) { //只能同级拖拽
  1964. var allow = true;
  1965. item.each(panel.shadows, function () {
  1966. var shadow = this;
  1967. if (
  1968. (shadow.getDragList() != ghost.getDragList() && shadow.getTopDragList({}) == ghost.getTopDragList({})) //同列表不同级
  1969. ||
  1970. (shadow.getDragList().isChild && shadow.getTopDragList({}) != ghost.getTopDragList({})) //不同列表不同级
  1971. ) {
  1972. allow = false;
  1973. return false;
  1974. }
  1975. });
  1976. if (item.isShadow || item.isGhost) { }
  1977. else if (item.isExpand) { //展开了的
  1978. if (!allow) { }
  1979. else if (item.dragList.isMouseOver(event) && panel.hasChild()) {
  1980. allow = false;
  1981. } else {
  1982. allow = true;
  1983. }
  1984. } else {
  1985. //没展开但是不是editgroup不作处理
  1986. }
  1987. allow && DM.dragingPanel.allowDrop();
  1988. !allow && DM.dragingPanel.notAllowDrop();
  1989. } else { // editGroup的情况
  1990. if (item.isShadow || item.isGhost) { }
  1991. else if (!item.isExpand) { //没展开的
  1992. !item.getDragList().isChild && !panel.hasChild();
  1993. if (item.timeoutid == null) {
  1994. delay = 1000;
  1995. if (item.intervalid == null)
  1996. item.intervalid = setInterval(function () { //切换鼠标,达到gif效果
  1997. item.ri = item.ri || 0;
  1998. item.addClass("cursor-timing" + ((item.ri++) % readyMouseIcoLen + 1));
  1999. }, delay / readyMouseIcoLen);
  2000. var id = setTimeout(function () { //清除动画
  2001. item.drager.editGroup && item.showDragList && !item.getDragList().isChild && item.showDragList() && item.hasDragList && item.dragList.append(ghost);
  2002. clearInterval(item.intervalid);
  2003. item.timeoutid = null;
  2004. item.intervalid = null;
  2005. clearMouseIcon();
  2006. }, delay);
  2007. item.timeoutid = id;
  2008. }
  2009. panel.allowDrop();
  2010. }
  2011. }
  2012. } else { //既不是editorder也不是editgroup
  2013. var allow = true;
  2014. ghost.each(panel.shadows, function () {
  2015. var shadow = this;
  2016. if (ghost.getTopDragList() == shadow.getTopDragList()) {
  2017. allow = false;
  2018. return false;
  2019. }
  2020. });
  2021. allow && DM.dragingPanel.allowDrop();
  2022. !allow && DM.dragingPanel.notAllowDrop();
  2023. }
  2024. }
  2025. return false;
  2026. };
  2027. //绑定document的事件
  2028. var DM = window.DM = wd.drag.DM = DM || new wd.drag.DragerManager();
  2029. setDrag = function (name, group, functions, options, _window) {
  2030. return DM.getInstance(name, group, functions, options, _window);
  2031. };