configurableTab.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /// <reference path="wdDrag.js" />
  2. if (!wd) {
  3. var wd = {};
  4. }
  5. wd.configurableTab = wd.configurableTab || {};
  6. wd.configurableTab.editTab = function () {
  7. var isEditing = $('.tab-div>ul').attr('editing');
  8. $('.tab-div > ul').attr('editing', (isEditing&&isEditing=="1")?"0":"1");
  9. var primaryDisplayCount = 0;
  10. var secondaryDisplayCount = 0;
  11. // console.log("editTabClick")
  12. // console.log(window.document);
  13. if (isEditing=="1") {
  14. // destroyDrag();
  15. DM.getDrager().destroy();
  16. $('li[shadow=true]').remove();
  17. //移除拖放添加的属性
  18. $('li[wdSecondaryTab]').each(function () {
  19. var hasItem = false;
  20. $(this).find('li input').each(function () {
  21. if ($(this).prop('checked')) {
  22. hasItem = true;
  23. }
  24. })
  25. if (!hasItem)
  26. $(this).css('display', 'none');
  27. })
  28. // $('li[wdSecondaryTab=true] li').each(function (index, e) {
  29. // var target = $(e);
  30. // var check = target.children('input').prop('checked')
  31. // target.css('display', check ? 'inline-block' : 'none');
  32. // });
  33. //隐藏更多
  34. $('li[wdSecondaryTab] ul').removeProp('style')
  35. //移除tab item多余的class并去除编辑使用的辅助元素
  36. $('[ssTabClick]').each(function () { // ('[wdTabClick]').。Lin
  37. $(this).prop('class', 'tab-normal');
  38. var text = $(this).children('span.handle').text();
  39. var statePoint=$(this).children(".statePoint");
  40. $(this).text(text);
  41. if(statePoint.length>0){
  42. $(this).append(statePoint.clone());
  43. }
  44. })
  45. // $('.tab-div .tab-secondary ul').removeAttr('style');
  46. // $('.tab-div .item').each(function() {
  47. //
  48. // $(this).removeClass('item');
  49. //
  50. // })
  51. var tabConfig = wd.configurableTab.saveTabConfig($('.tab-div>ul').get(0));
  52. $.each(tabConfig.base,
  53. function (i, e) {
  54. if (e.display) {
  55. primaryDisplayCount++;
  56. }
  57. });
  58. $.each(tabConfig.more,
  59. function (i, e) {
  60. if (e.display) {
  61. secondaryDisplayCount++;
  62. }
  63. });
  64. //保存配置
  65. wd.display.initTab();
  66. // $('[wdSecondaryTab]').bind('mouseover', function () {
  67. // $(this).find('ul').css('display', 'block')
  68. // })
  69. // $('[wdSecondaryTab]').bind('mouseout', function () {
  70. // $(this).find('ul').css('display', 'none')
  71. // })
  72. } else {
  73. //重新复制tab item
  74. // $('[wdTabClick]').each(function() {
  75. // var t = $(this);
  76. // var n = $('<li></li>');
  77. // n.prop('style',t.prop('style'));
  78. // n.prop('class',t.prop('class'));
  79. // n.attr('wdTabClick',t.attr('wdTabClick'));
  80. // n.attr('name',t.attr('name'));
  81. // n.attr('name')
  82. // n.text(t.text());
  83. // t.replaceWith(n);
  84. // })
  85. // $('.tab-div ul').addClass('dragList');
  86. $('.tab-div ul').not("#tabOption").attr("id", "tabSelected");
  87. window.isWriteOut = true;
  88. var tabobj=wd.display.getTabInstance();
  89. if(!tabobj)return;
  90. $('.tab-div>ul>li').show().each(function (index, e) {
  91. if ($(e).attr('wdSecondaryTab')) {
  92. $(e).find('li').each(function () {
  93. var t = $(this);
  94. var n = $('<li></li>');
  95. // n.prop('style', t.prop('style'));
  96. // n.prop('class', t.prop('class'));
  97. n.prop('class', tabobj.styles.wdNormalMoreClass);
  98. /* 再改,规范 <li wdTabClick= 命名。Lin
  99. n.attr('wdTabClick', t.attr('wdTabClick'));
  100. */ n.attr('ssTabClick', t.attr('ssTabClick'));
  101. n.attr('name', t.attr('name'));
  102. // n.addClass('tab item')
  103. n.addClass('item')
  104. var text = $(this).text();
  105. $('<input type="checkbox" />').prop('checked', $(this).css('display') !== 'none').appendTo(n);
  106. n.append('<span class="handle">' + text + '</span>')
  107. // n.css('display', 'block');
  108. n.on("click","[type=checkbox]",function(){
  109. window.event.stopPropagation();
  110. })
  111. if(t.children(".statePoint").length>0){
  112. n.append(t.children(".statePoint").clone());
  113. }
  114. t.replaceWith(n);
  115. })
  116. } else {
  117. var t = $(this);
  118. var n = $('<li></li>');
  119. // n.prop('style', t.prop('style'));
  120. // n.prop('class', t.prop('class'));
  121. n.prop('class', tabobj.styles.wdNormalClass);
  122. /* 再改,规范 <li wdTabClick= 命名。Lin
  123. n.attr('wdTabClick', t.attr('wdTabClick'));
  124. */ n.attr('ssTabClick', t.attr('ssTabClick'));
  125. n.attr('name', t.attr('name'));
  126. // n.addClass('tab item')
  127. n.addClass('item')
  128. var text = $(e).text();
  129. // $(e).empty();
  130. //$('<input type="checkbox" />').prop('checked', $(e).css('display') !== 'none').appendTo($(e))
  131. n.append('<span class="handle">' + text + '</span>')
  132. if(t.children(".statePoint").length>0){
  133. n.append(t.children(".statePoint").clone());
  134. }
  135. t.replaceWith(n);
  136. }
  137. // $(e).css('display', 'block')
  138. $(e).show();
  139. })
  140. $('.tab-div>ul>li').first().before($('<li shadow="true" style="width: 10px;padding: 0;" class="item '+tabobj.styles.wdNormalClass+'"></li>').hide());
  141. if($('.tab-div>ul>li:not([shadow=true])').length==1){
  142. $('li[shadow=true]').show();
  143. }
  144. //开启拖动
  145. var dr = DM.getInstance("tab",false, ["editOrder"], {
  146. selectedListAlignMode: "horizontal",
  147. itemSelector: "li",
  148. });
  149. dr.dragend = function () {
  150. console.log(arguments);
  151. setTimeout(function () {
  152. var tabobj=wd.display.getTabInstance();
  153. $('li[wdSecondaryTab=true] ul li').each(function () {
  154. if ($(this).find('input').length == 0) {
  155. if (this.confs.imgBtn) {
  156. this.confs.imgBtn.after($('<input type="checkbox" checked />')[0])
  157. } else {
  158. $(this).prepend('<input type="checkbox" checked />')
  159. }
  160. }
  161. }).removeAttr("style").attr("class","item").addClass(tabobj.styles.wdNormalMoreClass);
  162. $('.tab-div>ul>li:not([shadow=true])').each(function () {
  163. if (!$(this).attr('wdSecondaryTab')) {
  164. $(this).find('input').remove();
  165. }
  166. }).removeAttr("style").attr("class","item").addClass(tabobj.styles.wdNormalClass);
  167. //全部在更多无法拖出问题
  168. if($('.tab-div>ul>li:not([shadow=true])').length==1){
  169. $('.tab-div>ul>li[shadow=true]').show();
  170. }else{
  171. $('.tab-div>ul>li[shadow=true]').hide();
  172. }
  173. }, 10)
  174. }
  175. if (more && more.confs)
  176. more.confs.destroy();
  177. if (dr.optionList){
  178. dr.optionList.element.style.minHeight = '35px';
  179. dr.optionList.element.style.minWidth = '35px';
  180. }
  181. //显示隐藏的选项卡
  182. // $('li[wdSecondaryTab],li[wdSecondaryTab] ul').css('display', 'block');
  183. $('li[wdSecondaryTab],li[wdSecondaryTab] ul').show();
  184. // $('[wdSecondaryTab]').unbind('mouseover')
  185. // $('[wdSecondaryTab]').unbind('mouseout')
  186. }
  187. }
  188. //保存配置
  189. wd.configurableTab.saveTabConfig = function (tab) {
  190. var result={base:[],more:[]};
  191. //遍历tab的所以项
  192. $('.tab-div>ul').each(function (e) {
  193. $(this).children('li').each(function () {
  194. if ($(this).attr('wdSecondaryTab')) {
  195. $(this).find('li').each(function () {
  196. result.more.push({
  197. desc: $(this).text(),
  198. display: $(this).css('display') != 'none',
  199. name: $(this).attr('name')
  200. })
  201. })
  202. } else {
  203. result.base.push({
  204. desc: $(this).text(),
  205. display: $(this).css('display') != 'none',
  206. name: $(this).attr('name')
  207. })
  208. }
  209. })
  210. });
  211. var bjid = wd.configurableTab.bjid;
  212. var token;
  213. var ps=window.location.href.replace(/.*\?/,"").split("&");
  214. for(var i=0;i<ps.length;i++){
  215. if(ps[i].indexOf("ssToken=")>-1){ // ("token=")>。Lin
  216. token=ps[i].replace("ssToken\=",''); // ("token\=",。Lin
  217. break;
  218. }
  219. }
  220. $.ajax({
  221. url: '/service?ssServ=saveTabConfig&bjid='+ bjid +"&tokenstring="+ token,
  222. async: false,
  223. type: 'post',
  224. data:{"data":JSON.stringify(result)} ,
  225. })
  226. return result;
  227. }
  228. wd.configurableTab.init = function (yyid, bjid) {
  229. //在弹出窗口添加按钮
  230. console.log("wd.configurableTab.init");
  231. var wdDialogId = wd.display.getwdDialogId();
  232. if (wdDialogId) {
  233. this.wdDialogId=wdDialogId;
  234. var w = wd.topWindow.dhxWins.window(wdDialogId);
  235. wd.display.removeDialogLine(w);
  236. if (w && w.isAddTabEditButton == null) {
  237. w.button("wdTab").show();
  238. w.button("wdTab_save").hide();
  239. w.button("wdTab").onclick = function () {
  240. console.log(111111)
  241. this.hide();
  242. w.button("wdTab_save").show();
  243. if (wd.topWindow.$tabList != null) {
  244. var id=null;
  245. for (var i = wd.topWindow.$tabList.length-1; i >=0 ; i--) {
  246. //只修改当前弹窗的tab
  247. if(!id){id=wd.topWindow.$tabList[i].wdDialogId}
  248. if(id==wd.topWindow.$tabList[i].wdDialogId){
  249. wd.topWindow.$tabList[i].editTab();
  250. }
  251. }
  252. }
  253. }
  254. w.button("wdTab_save").onclick = function () {
  255. this.hide();
  256. w.button("wdTab").show()
  257. if (wd.topWindow.$tabList != null) {
  258. var id=null;
  259. for (var i = wd.topWindow.$tabList.length-1; i >=0 ; i--) {
  260. //只修改当前弹窗的tab
  261. if(!id){id=wd.topWindow.$tabList[i].wdDialogId}
  262. if(id==wd.topWindow.$tabList[i].wdDialogId){
  263. wd.topWindow.$tabList[i].editTab();
  264. }
  265. }
  266. // for (var i = 0; i < wd.topWindow.$tabList.length; i++) {
  267. // wd.topWindow.$tabList[i].editTab();
  268. // }
  269. }
  270. }
  271. w.isAddTabEditButton = true;
  272. }
  273. }
  274. if (!more) {
  275. more = document.createElement("li");
  276. more.setAttribute('class', 'tab-normal tab-secondary')
  277. more.setAttribute('wdSecondaryTab', 'true')
  278. more.appendChild(document.createTextNode("更多"))
  279. }
  280. wd.configurableTab.bjid = bjid;
  281. if (!bjid) {
  282. wd.display.initTab();
  283. return;
  284. }
  285. //为每个tab item填写name
  286. $('li[ssTabClick]').each(function (index) { // ('li[wdTabClick]').。Lin
  287. if(!$(this).attr('name'))
  288. $(this).attr('name', "tab_" + index);
  289. })
  290. $('.tab-div>ul').each(function () {
  291. var tab = $(this).css('display', 'none');
  292. var token;
  293. var ps=window.location.href.replace(/.*\?/,"").split("&");
  294. for(var i=0;i<ps.length;i++){
  295. if(ps[i].indexOf("ssToken=")>-1){ // ("token=")>。Lin
  296. token=ps[i].replace("ssToken\=",''); // ("token\=",。Lin
  297. break;
  298. }
  299. }
  300. $.ajax({
  301. url: '/service?ssServ=loadTabConfig&bjid='+ bjid +"&tokenstring="+ token,
  302. type: 'get',
  303. async: false,
  304. dataType: 'json',
  305. success: function (data, status) {
  306. if (data&&data.base) {
  307. wd.configurableTab.initTab(data, tab[0])
  308. } else {
  309. if(tab.find("[wdSecondaryTab]").length==0&&more){
  310. tab.append(more);
  311. }
  312. var baseList=[];
  313. var moreList=[];
  314. tab.find("li[name]").each(function(index,ele){
  315. if(ele.getAttribute("wdTabSite")){
  316. var ddd=ele.getAttribute("wdTabSite").split(":");
  317. if(ddd[0]=="base")baseList.push({index:parseInt(ddd[1]),ele:ele})
  318. if(ddd[0]=="more")moreList.push({index:parseInt(ddd[1]),ele:ele})
  319. }
  320. })
  321. //排序
  322. baseList.sort(function(a,b){
  323. return a.index-b.index;
  324. })
  325. moreList.sort(function(a,b){
  326. return a.index-b.index;
  327. })
  328. for(var i=1;i<baseList.length;i++){
  329. $(baseList[i-1]["ele"]).after(baseList[i]["ele"])
  330. }
  331. var secondaryLi=tab.find("[wdSecondaryTab]");
  332. if(secondaryLi.children("ul").length==0){
  333. secondaryLi.append("<ul id=\"tabOption\"></ul>");
  334. }
  335. for(var i=0;i<moreList.length;i++){
  336. secondaryLi.children("ul").append(moreList[i]["ele"]);
  337. }
  338. if($('[wdSecondaryTab]').find("li").length==0)
  339. $('[wdSecondaryTab]').css('display', 'none');
  340. }
  341. }
  342. })
  343. tab.css('display', 'block');
  344. wd.display.initTab();
  345. })
  346. }
  347. var more = null;
  348. wd.configurableTab.initTab = function (tabConfig, tab) {
  349. var allName = {};
  350. var lis = $(tab).find('li[name]').remove();
  351. var tabItemMap = {};
  352. lis.each(function () {
  353. allName[$(this).attr('name')] = 0;
  354. tabItemMap[$(this).attr('name')] = this;
  355. });
  356. if (more&&more.parentNode) {
  357. more.parentNode.removeChild(more);
  358. }
  359. more =$('<li class="tab-normal tab-secondary" wdSecondaryTab="true">更多<ul id="tabOption"></ul></li>')[0];
  360. var secondaryTab =more.querySelector("#tabOption");
  361. primaryDisplayCount = 0;
  362. secondaryDisplayCount = 0;
  363. for(var key in tabConfig){
  364. var itemList=tabConfig[key];
  365. for (var i = 0; i < itemList.length; i++) {
  366. var item=itemList[i];
  367. var name=item.name;
  368. if (0==allName[name]) {
  369. allName[name]=1;
  370. var display=item.display;
  371. if(!display){
  372. tabItemMap[name].style.display="none";
  373. }
  374. if(key=="base"){
  375. tab.appendChild(tabItemMap[name]);
  376. if(display){
  377. primaryDisplayCount++;
  378. }
  379. }else if(key=="more"){
  380. secondaryTab.appendChild(tabItemMap[name]);
  381. if(display){
  382. secondaryDisplayCount++;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. //other as the more
  389. for(var key in allName){
  390. if(0==allName[key]){
  391. secondaryTab.appendChild(tabItemMap[key]);
  392. // tabItemMap[allName[i]].style.display="none";
  393. secondaryDisplayCount++;
  394. }
  395. }
  396. tab.appendChild(more);
  397. if (secondaryDisplayCount == 0) {
  398. more.style.display = 'none'
  399. }
  400. if (secondaryDisplayCount >= primaryDisplayCount) {
  401. // secondaryTab.style.left = "0px";
  402. delete secondaryTab.style.right
  403. // for (i = 0; i < secondaryTab.children.length; i++) {
  404. // secondaryTab.children[i].style.float = "left"
  405. // }
  406. }
  407. }
  408. if (wd.topWindow.$tabList == null)
  409. wd.topWindow.$tabList = [];
  410. wd.topWindow.$tabList.push(wd.configurableTab); //这样窗口菜单编辑按钮可以访问到所有的tab。
  411. $(window).unload(function() {
  412. try{
  413. var index = wd.topWindow.$tabList.indexOf(wd.configurableTab);
  414. if (index != -1)
  415. wd.topWindow.$tabList.splice(index, 1);
  416. }catch(e){
  417. console.log(e)
  418. }
  419. })