index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. import menulist from "./components/index/Menulist.js";
  2. import breadcrumb from "./components/index/Breadcrumb.js";
  3. import tabs from "./components/index/Tabs.js";
  4. import themesetting from "./components/index/ThemeSetting.js";
  5. import message from "./components/index/Message.js";
  6. import fullscreen from "./components/index/Fullscreen.js";
  7. import trash from "./components/index/Trash.js";
  8. import platform from "./components/index/Platform.js";
  9. import userinfo from "./components/index/Userinfo.js";
  10. import {getUniqid} from "./util.js";
  11. const findBreadcrumbById=function(tree, targetId, result = []) {
  12. for (let i = 0; i < tree.length; i++) {
  13. const node = tree[i];
  14. if (node.id === targetId) {
  15. result.push(node);
  16. if (node.pid !== 0) {
  17. findBreadcrumbById(tree, node.pid, result);
  18. }
  19. break;
  20. } else if (node.childlist.length > 0) {
  21. findBreadcrumbById(node.childlist, targetId, result);
  22. if (result.length > 0) {
  23. result.push(node);
  24. break;
  25. }
  26. }
  27. }
  28. return result.reverse();
  29. }
  30. const findTabsMenu=function(menu)
  31. {
  32. if(menu.childlist.length===0){
  33. return menu;
  34. }
  35. let r;
  36. if(menu.childlist.length>0){
  37. for(let i in menu.childlist){
  38. r=findTabsMenu(menu.childlist[i]);
  39. if(r.childlist.length===0){
  40. break;
  41. }
  42. }
  43. }
  44. return r;
  45. }
  46. const inJson=function(json, id) {
  47. for (var i = 0; i < json.length; i++) {
  48. if (json[i].id === id) {
  49. return true;
  50. } else if (json[i].childlist && json[i].childlist.length > 0) {
  51. if (inJson(json[i].childlist, id)) {
  52. return true;
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. const findChildMenu=function (menu){
  59. for(let i_0=0;i_0<Yunqi.data.menulist.length;i_0++){
  60. if(Yunqi.data.menulist[i_0].id==menu.id){
  61. return Yunqi.data.menulist[i_0].childlist;
  62. }else if(Yunqi.data.menulist[i_0].childlist.length>0){
  63. let menulist1=Yunqi.data.menulist[i_0].childlist;
  64. for(let i_1=0;i_1<menulist1.length;i_1++){
  65. if(menulist1[i_1].id==menu.id){
  66. return menulist1;
  67. }else if(menulist1[i_1].childlist.length>0){
  68. if(inJson(menulist1[i_1].childlist,menu.id)){
  69. return menulist1;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. export default{
  77. components:{
  78. 'Menulist':menulist,
  79. 'Breadcrumb':breadcrumb,
  80. 'Tabs':tabs,
  81. 'ThemeSetting':themesetting,
  82. 'Message':message,
  83. 'Fullscreen':fullscreen,
  84. 'Trash':trash,
  85. 'Platform':platform,
  86. 'Userinfo':userinfo
  87. },
  88. data(){
  89. return {
  90. documentWidth:'',
  91. contentWidth:'',
  92. contentHeight:'',
  93. menuHeight: '',
  94. layerExpandHeight:0,
  95. elementUi:'',
  96. menuList:[],
  97. childMenuList:[],
  98. breadcrumb:[],
  99. activeTab:'',
  100. activeLayer:'',
  101. openMenu:'',
  102. layerList:[],
  103. tabList:[],
  104. imageList:[],
  105. mainFrameExpand:false,
  106. logo_img:''
  107. }
  108. },
  109. onLoad:function (){
  110. this.elementUi=Yunqi.getElementUi();
  111. this.changeLogo();
  112. this.menuList=Yunqi.data.menulist;
  113. this.initMainContentFrame();
  114. },
  115. onShow:function (){
  116. this.clickMenu(Yunqi.data.selected);
  117. if(Yunqi.data.referer.url){
  118. this.clickMenu(Yunqi.data.referer);
  119. }
  120. },
  121. methods:{
  122. changeLogo:function (){
  123. if(this.elementUi.dark){
  124. this.logo_img=Yunqi.data.site.logo_white;
  125. return;
  126. }
  127. if(this.elementUi.layout=='vertical'){
  128. this.logo_img=Yunqi.data.site.logo;
  129. return;
  130. }
  131. this.logo_img=Yunqi.data.site.logo_white;
  132. },
  133. initMainContentFrame:function (){
  134. this.setMainContentFrame();
  135. //监听屏幕宽度变化
  136. window.addEventListener('resize',()=>{
  137. let contentWidth=document.documentElement.clientWidth;
  138. if(contentWidth<800){
  139. this.elementUi.is_menu_collapse=true;
  140. }else{
  141. this.elementUi.is_menu_collapse=false;
  142. }
  143. this.setMainContentFrame();
  144. });
  145. },
  146. setMainContentFrame:function (){
  147. let documentWidth=document.documentElement.clientWidth;
  148. let contentWidth=document.documentElement.clientWidth;
  149. if(documentWidth>600){
  150. contentWidth=contentWidth-25;
  151. if(this.elementUi.layout=='vertical' || this.elementUi.layout=='classic'){
  152. if(this.elementUi.is_menu_collapse){
  153. contentWidth=contentWidth-65;
  154. }else{
  155. contentWidth=contentWidth-210;
  156. }
  157. }
  158. if(this.elementUi.layout=='columns'){
  159. contentWidth=contentWidth-70;
  160. if(this.childMenuList.length>0 && this.elementUi.is_menu_collapse){
  161. contentWidth=contentWidth-65;
  162. }
  163. if(this.childMenuList.length>0 && !this.elementUi.is_menu_collapse){
  164. contentWidth=contentWidth-210;
  165. }
  166. }
  167. }
  168. let contentHeight=document.documentElement.clientHeight-75;
  169. if(this.elementUi.tabs){
  170. contentHeight=contentHeight-40;
  171. }
  172. if(this.elementUi.footer){
  173. contentHeight=contentHeight-32;
  174. }
  175. let menuHeight=document.documentElement.clientHeight-55;
  176. let layerExpandHeight=document.documentElement.clientHeight-92;
  177. this.parseZoom(documentWidth,contentWidth,contentHeight,menuHeight,layerExpandHeight);
  178. },
  179. parseZoom:function (documentWidth,contentWidth,contentHeight,menuHeight,layerExpandHeight){
  180. let zoom=1;
  181. this.documentWidth=documentWidth/zoom;
  182. this.contentWidth=contentWidth/zoom;
  183. this.contentHeight=contentHeight/zoom;
  184. this.menuHeight=menuHeight/zoom;
  185. this.layerExpandHeight=layerExpandHeight/zoom;
  186. },
  187. isChildMenu:function (id){
  188. let breadcrumb=findBreadcrumbById(Yunqi.data.menulist,this.activeTab.id,[]);
  189. for(let i in breadcrumb){
  190. if(id==breadcrumb[i].id){
  191. return true;
  192. }
  193. }
  194. return false;
  195. },
  196. layerExpand:function (layer){
  197. layer.expand=!layer.expand;
  198. let tab=document.getElementById('layer-'+layer.id).contentWindow;
  199. tab.Yunqi.app.setContentHeight_();
  200. },
  201. maximize:function (){
  202. let id=this.activeTab.id;
  203. let tab=document.getElementById('addtabs-'+id).contentWindow;
  204. let expand=tab.document.getElementById('mainFrameExpand');
  205. if(expand){
  206. expand.click();
  207. }else{
  208. this.mainFrameExpand=true;
  209. }
  210. tab.Yunqi.app.setContentHeight_();
  211. },
  212. minimize:function (){
  213. let id=this.activeTab.id;
  214. let tab=document.getElementById('addtabs-'+id).contentWindow;
  215. let expand=tab.document.getElementById('mainFrameExpand');
  216. if(expand){
  217. expand.click();
  218. }else{
  219. this.mainFrameExpand=false;
  220. }
  221. tab.Yunqi.app.setContentHeight_();
  222. },
  223. expand:function(){
  224. this.mainFrameExpand=true;
  225. let id=this.activeTab.id;
  226. let tab=document.getElementById('addtabs-'+id).contentWindow;
  227. tab.Yunqi.app.setContentHeight_();
  228. },
  229. compress:function () {
  230. this.mainFrameExpand=false;
  231. let id=this.activeTab.id;
  232. let tab=document.getElementById('addtabs-'+id).contentWindow;
  233. tab.Yunqi.app.setContentHeight_();
  234. },
  235. setBreadcrumb:function (){
  236. if(this.activeTab.id==Yunqi.data.selected.id){
  237. this.breadcrumb=[Yunqi.data.selected];
  238. }else{
  239. let breadcrumb=findBreadcrumbById(Yunqi.data.menulist,this.activeTab.id,[]);
  240. breadcrumb.unshift(Yunqi.data.selected);
  241. this.breadcrumb=breadcrumb;
  242. }
  243. },
  244. onBreadcrumbClick:function (){
  245. },
  246. changeSubMenu:function (menu){
  247. this.clickMenu(findTabsMenu(menu));
  248. },
  249. clickMenu:function (menu){
  250. menu.id=menu.id || getUniqid();
  251. this.openMenu=menu;
  252. if(menu.menutype=='tab'){
  253. this.addTabs(menu);
  254. }
  255. if(menu.menutype=='layer'){
  256. this.openLayer(menu);
  257. }
  258. if(menu.menutype=='blank'){
  259. window.open(menu.url,"_blank");
  260. }
  261. if(this.documentWidth<=600){
  262. this.elementUi.is_menu_collapse=true;
  263. }
  264. },
  265. openLayer:function(menu){
  266. if(this.documentWidth<=600){
  267. menu.expand=true;
  268. }
  269. let list=this.layerList;
  270. //如果已经显示了就返回,并找到要隐藏的layer
  271. let index=-1;
  272. for(let i in list){
  273. if(list[i].id==menu.id && list[i].show){
  274. return;
  275. }
  276. if(list[i].id==menu.id){
  277. index=i;
  278. }
  279. }
  280. if(index!=-1){
  281. list[index].show=true;
  282. let app=Yunqi.getApp(list[index].id);
  283. app && app.onShow();
  284. }
  285. if(index==-1){
  286. menu.show=true;
  287. list.push(menu);
  288. }
  289. this.activeLayer=menu;
  290. this.$refs.tabs.tabAdd(menu);
  291. },
  292. hideLayer:function (layer){
  293. layer.show=false;
  294. let app=Yunqi.getApp(layer.id);
  295. app && app.onHide();
  296. let list=this.layerList;
  297. for(let i=list.length;i>0;i--){
  298. let j=i-1;
  299. if(list[j].show){
  300. this.$refs.tabs.tabAdd(list[j]);
  301. return;
  302. }
  303. }
  304. this.$refs.tabs.tabAdd(this.activeTab);
  305. },
  306. calculateLayerIndex:function(index){
  307. let list=this.layerList;
  308. let r=-1;
  309. for(let i=0;i<list.length;i++){
  310. if(list[i].show){
  311. r=i;
  312. break;
  313. }
  314. }
  315. if(index<=r){
  316. return true;
  317. }
  318. return false;
  319. },
  320. addTabs:function (menu){
  321. //弹窗模式下禁止增加tab
  322. for(let i=0;i<this.layerList.length;i++){
  323. if(this.layerList[i].show){
  324. Yunqi.message.errors(__('请先关闭弹窗'));
  325. return;
  326. }
  327. }
  328. if(this.activeTab.id==menu.id){
  329. return;
  330. }
  331. let list=this.tabList;
  332. let showIndex=-1;
  333. for(let i in list){
  334. if(list[i].id==menu.id){
  335. showIndex=i;
  336. }
  337. }
  338. //触发onHide事件
  339. if(this.activeTab){
  340. let last=document.getElementById('addtabs-'+this.activeTab.id);
  341. last.style.display='none';
  342. let app=Yunqi.getApp(this.activeTab.id);
  343. app && app.onHide();
  344. }
  345. //已经存在tab
  346. if(showIndex!=-1){
  347. let active=document.getElementById('addtabs-'+menu.id);
  348. if(active){
  349. active.style.display='block';
  350. }
  351. let app=Yunqi.getApp(menu.id);
  352. app && app.onShow();
  353. }
  354. if(showIndex==-1){
  355. let iframe=document.createElement("iframe");
  356. iframe.id="addtabs-"+menu.id;
  357. iframe.src=menu.url;
  358. iframe.style='border:0px;margin:0;padding:0;';
  359. iframe.border='0';
  360. iframe.scrollingX='no';
  361. iframe.scrollingY='auto';
  362. iframe.allowTransparency='yes';
  363. document.getElementById('main-content').appendChild(iframe);
  364. list.push(menu);
  365. }
  366. this.activeTab=menu;
  367. if(this.elementUi.layout=='columns'){
  368. this.childMenuList=findChildMenu(menu);
  369. this.setMainContentFrame();
  370. }
  371. //修改地址栏
  372. let url=menu.url;
  373. if(url.indexOf('?')!=-1){
  374. url+='&ref=addtabs';
  375. }else{
  376. url+='?ref=addtabs';
  377. }
  378. history.pushState({},0,url);
  379. this.$refs.tabs.tabAdd(menu);
  380. this.setBreadcrumb();
  381. },
  382. closeLayer:function(options,callback){
  383. //默认关闭焦点的那层
  384. let id,menu,index=-1;
  385. let list=this.layerList;
  386. if(!options){
  387. id=this.activeLayer.id;
  388. }else{
  389. if(typeof options=='object'){
  390. if(options.id){
  391. id=options.id;
  392. }else if(options.index){
  393. id=list[options.index].id;
  394. }else if(options.url){
  395. for(let i=0;i<list.length;i++){
  396. if(list[i].url==options.url){
  397. id=list[i].id;
  398. break;
  399. }
  400. }
  401. }
  402. }else{
  403. id=options;
  404. }
  405. }
  406. if(!id){
  407. return;
  408. }
  409. for(let i=0;i<list.length;i++){
  410. if(list[i].id==id){
  411. index=i;
  412. menu=list[i];
  413. }
  414. }
  415. if(index!=-1){
  416. menu.show=false;
  417. let app=Yunqi.getApp(menu.id);
  418. app && app.onUnload();
  419. menu.close(callback);
  420. list.splice(index,1);
  421. this.$refs.tabs.tabRemove(menu);
  422. for(let i=list.length;i>0;i--){
  423. let j=i-1;
  424. if(list[j].show){
  425. this.$refs.tabs.tabAdd(list[j]);
  426. return;
  427. }
  428. }
  429. this.$refs.tabs.tabAdd(this.activeTab);
  430. }
  431. },
  432. closeTabs:function (options,callback){
  433. //弹窗模式下禁止增加tab
  434. for(let i=0;i<this.layerList.length;i++){
  435. if(this.layerList[i].show){
  436. Yunqi.message.errors(__('请先关闭弹窗'));
  437. return;
  438. }
  439. }
  440. //默认关闭焦点的那层
  441. let id,menu,index=-1;
  442. let list=this.tabList;
  443. if(!options){
  444. id=this.activeTab.id;
  445. }else{
  446. if(typeof options=='object'){
  447. if(options.id){
  448. id=options.id;
  449. }else if(options.index){
  450. id=list[options.index].id;
  451. }else if(options.url){
  452. for(let i=0;i<list.length;i++){
  453. if(list[i].url==options.url){
  454. id=list[i].id;
  455. break;
  456. }
  457. }
  458. }
  459. }else{
  460. id=options;
  461. }
  462. }
  463. if(!id){
  464. return;
  465. }
  466. for(let i=0;i<list.length;i++){
  467. if(list[i].id==id){
  468. index=i;
  469. menu=list[i];
  470. }
  471. }
  472. if(index!=-1){
  473. let app=Yunqi.getApp(id);
  474. app && app.onUnload();
  475. list.splice(index,1);
  476. document.getElementById('addtabs-'+id).remove();
  477. if(this.activeTab.id==menu.id){
  478. this.activeTab='';
  479. }
  480. this.$refs.tabs.tabRemove(menu);
  481. menu.close && menu.close(callback);
  482. }
  483. },
  484. previewImg:function (img){
  485. if(typeof img=='string'){
  486. img=img.split(',');
  487. }
  488. this.imageList=img;
  489. }
  490. }
  491. }