admin.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /**
  2. @Name:layuiAdmin iframe版核心模块
  3. @Author:贤心
  4. @Site:http://www.layui.com/admin/
  5. @License:LPPL
  6. */
  7. config.history_link = '';
  8. config.history_text = '';
  9. layui.define('view', function(exports){
  10. var $ = layui.jquery
  11. ,laytpl = layui.laytpl
  12. ,element = layui.element
  13. ,setter = layui.setter
  14. ,view = layui.view
  15. ,device = layui.device()
  16. ,$win = $(window), $body = $('body')
  17. ,container = $('#'+ setter.container)
  18. ,SHOW = 'layui-show', HIDE = 'layui-hide', THIS = 'layui-this', DISABLED = 'layui-disabled', TEMP = 'template'
  19. ,APP_BODY = '#LAY_app_body', APP_FLEXIBLE = 'LAY_app_flexible'
  20. ,FILTER_TAB_TBAS = 'layadmin-layout-tabs'
  21. ,APP_SPREAD_SM = 'layadmin-side-spread-sm', TABS_BODY = 'layadmin-tabsbody-item'
  22. ,ICON_SHRINK = 'layui-icon-shrink-right', ICON_SPREAD = 'layui-icon-spread-left'
  23. ,SIDE_SHRINK = 'layadmin-side-shrink', SIDE_MENU = 'LAY-system-side-menu'
  24. //通用方法
  25. ,admin = {
  26. v: '1.1.0 std'
  27. //数据的异步请求
  28. ,req: view.req
  29. //发送验证码
  30. ,sendAuthCode: function(options){
  31. options = $.extend({
  32. seconds: 60
  33. ,elemPhone: '#LAY_phone'
  34. ,elemVercode: '#LAY_vercode'
  35. }, options);
  36. var seconds = options.seconds
  37. ,btn = $(options.elem)
  38. ,token = null
  39. ,timer, countDown = function(loop){
  40. seconds--;
  41. if(seconds < 0){
  42. btn.removeClass(DISABLED).html('获取验证码');
  43. seconds = options.seconds;
  44. clearInterval(timer);
  45. } else {
  46. btn.addClass(DISABLED).html(seconds + '秒后重获');
  47. }
  48. if(!loop){
  49. timer = setInterval(function(){
  50. countDown(true);
  51. }, 1000);
  52. }
  53. };
  54. options.elemPhone = $(options.elemPhone);
  55. options.elemVercode = $(options.elemVercode);
  56. btn.on('click', function(){
  57. var elemPhone = options.elemPhone
  58. ,value = elemPhone.val();
  59. if(seconds !== options.seconds || $(this).hasClass(DISABLED)) return;
  60. if(!/^1\d{10}$/.test(value)){
  61. elemPhone.focus();
  62. return layer.msg('请输入正确的手机号')
  63. };
  64. if(typeof options.ajax === 'object'){
  65. var success = options.ajax.success;
  66. delete options.ajax.success;
  67. }
  68. admin.req($.extend(true, {
  69. url: '/auth/code'
  70. ,type: 'get'
  71. ,data: {
  72. phone: value
  73. }
  74. ,success: function(res){
  75. layer.msg('验证码已发送至你的手机,请注意查收', {
  76. icon: 1
  77. ,shade: 0
  78. });
  79. options.elemVercode.focus();
  80. countDown();
  81. success && success(res);
  82. }
  83. }, options.ajax));
  84. });
  85. }
  86. //屏幕类型
  87. ,screen: function(){
  88. var width = $win.width()
  89. if(width >= 1200){
  90. return 3; //大屏幕
  91. } else if(width >= 992){
  92. return 2; //中屏幕
  93. } else if(width >= 768){
  94. return 1; //小屏幕
  95. } else {
  96. return 0; //超小屏幕
  97. }
  98. }
  99. //清除本地 token,并跳转到登入页
  100. ,exit: view.exit
  101. //侧边伸缩
  102. ,sideFlexible: function(status){
  103. var app = container
  104. ,iconElem = $('#'+ APP_FLEXIBLE)
  105. ,screen = admin.screen();
  106. //设置状态,PC:默认展开、移动:默认收缩
  107. if(status === 'spread'){
  108. //切换到展开状态的 icon,箭头:←
  109. iconElem.removeClass(ICON_SPREAD).addClass(ICON_SHRINK);
  110. //移动:从左到右位移;PC:清除多余选择器恢复默认
  111. if(screen < 2){
  112. app.addClass(APP_SPREAD_SM);
  113. } else {
  114. app.removeClass(APP_SPREAD_SM);
  115. }
  116. app.removeClass(SIDE_SHRINK)
  117. } else {
  118. //切换到搜索状态的 icon,箭头:→
  119. iconElem.removeClass(ICON_SHRINK).addClass(ICON_SPREAD);
  120. //移动:清除多余选择器恢复默认;PC:从右往左收缩
  121. if(screen < 2){
  122. app.removeClass(SIDE_SHRINK);
  123. } else {
  124. app.addClass(SIDE_SHRINK);
  125. }
  126. app.removeClass(APP_SPREAD_SM)
  127. }
  128. layui.event.call(this, setter.MOD_NAME, 'side({*})', {
  129. status: status
  130. });
  131. }
  132. //xss 转义
  133. ,escape: function(html){
  134. return String(html || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
  135. .replace(/</g, '&lt;').replace(/>/g, '&gt;')
  136. .replace(/'/g, '&#39;').replace(/"/g, '&quot;');
  137. }
  138. //事件监听
  139. ,on: function(events, callback){
  140. return layui.onevent.call(this, setter.MOD_NAME, events, callback);
  141. }
  142. //弹出面板
  143. ,popup: view.popup
  144. //右侧面板
  145. ,popupRight: function(options){
  146. //layer.close(admin.popup.index);
  147. return admin.popup.index = layer.open($.extend({
  148. type: 1
  149. ,id: 'LAY_adminPopupR'
  150. ,anim: -1
  151. ,title: false
  152. ,closeBtn: false
  153. ,offset: 'r'
  154. ,shade: 0.1
  155. ,shadeClose: true
  156. ,skin: 'layui-anim layui-anim-rl layui-layer-adminRight'
  157. ,area: '300px'
  158. }, options));
  159. }
  160. //主题设置
  161. ,theme: function(options){
  162. var theme = setter.theme
  163. ,local = layui.data(setter.tableName)
  164. ,id = 'LAY_layadmin_theme'
  165. ,style = document.createElement('style')
  166. ,styleText = laytpl([
  167. //主题色
  168. '.layui-side-menu,'
  169. ,'.layadmin-pagetabs .layui-tab-title li:after,'
  170. ,'.layadmin-pagetabs .layui-tab-title li.layui-this:after,'
  171. ,'.layui-layer-admin .layui-layer-title,'
  172. ,'.layadmin-side-shrink .layui-side-menu .layui-nav>.layui-nav-item>.layui-nav-child'
  173. ,'{background-color:{{d.color.main}} !important;}'
  174. //选中色
  175. ,'.layui-nav-tree .layui-this,'
  176. ,'.layui-nav-tree .layui-this>a,'
  177. ,'.layui-nav-tree .layui-nav-child dd.layui-this,'
  178. ,'.layui-nav-tree .layui-nav-child dd.layui-this a'
  179. ,'{background-color:{{d.color.selected}} !important;}'
  180. //logo
  181. ,'.layui-layout-admin .layui-logo{background-color:{{d.color.logo || d.color.main}} !important;}'
  182. //头部色
  183. ,'{{# if(d.color.header){ }}'
  184. ,'.layui-layout-admin .layui-header{background-color:{{ d.color.header }};}'
  185. ,'.layui-layout-admin .layui-header a,'
  186. ,'.layui-layout-admin .layui-header a cite{color: #f8f8f8;}'
  187. ,'.layui-layout-admin .layui-header a:hover{color: #fff;}'
  188. ,'.layui-layout-admin .layui-header .layui-nav .layui-nav-more{border-top-color: #fbfbfb;}'
  189. ,'.layui-layout-admin .layui-header .layui-nav .layui-nav-mored{border-color: transparent; border-bottom-color: #fbfbfb;}'
  190. ,'.layui-layout-admin .layui-header .layui-nav .layui-this:after, .layui-layout-admin .layui-header .layui-nav-bar{background-color: #fff; background-color: rgba(255,255,255,.5);}'
  191. ,'.layadmin-pagetabs .layui-tab-title li:after{display: none;}'
  192. ,'{{# } }}'
  193. ].join('')).render(options = $.extend({}, local.theme, options))
  194. ,styleElem = document.getElementById(id);
  195. //添加主题样式
  196. if('styleSheet' in style){
  197. style.setAttribute('type', 'text/css');
  198. style.styleSheet.cssText = styleText;
  199. } else {
  200. style.innerHTML = styleText;
  201. }
  202. style.id = id;
  203. styleElem && $body[0].removeChild(styleElem);
  204. $body[0].appendChild(style);
  205. $body.attr('layadmin-themealias', options.color.alias);
  206. //本地存储记录
  207. local.theme = local.theme || {};
  208. layui.each(options, function(key, value){
  209. local.theme[key] = value;
  210. });
  211. layui.data(setter.tableName, {
  212. key: 'theme'
  213. ,value: local.theme
  214. });
  215. }
  216. //初始化主题
  217. ,initTheme: function(index){
  218. var theme = setter.theme;
  219. index = index || 0;
  220. if(theme.color[index]){
  221. theme.color[index].index = index;
  222. admin.theme({
  223. color: theme.color[index]
  224. });
  225. }
  226. }
  227. //记录最近一次点击的页面标签数据
  228. ,tabsPage: {}
  229. //获取页面标签主体元素
  230. ,tabsBody: function(index){
  231. return $(APP_BODY).find('.'+ TABS_BODY).eq(index || 0);
  232. }
  233. //切换页面标签主体
  234. ,tabsBodyChange: function(index, options){
  235. options = options || {};
  236. admin.tabsBody(index).addClass(SHOW).siblings().removeClass(SHOW);
  237. events.rollPage('auto', index);
  238. //执行 {setter.MOD_NAME}.tabsPage 下的事件
  239. layui.event.call(this, setter.MOD_NAME, 'tabsPage({*})', {
  240. url: options.url
  241. ,text: options.text
  242. });
  243. }
  244. //resize事件管理
  245. ,resize: function(fn){
  246. var router = layui.router()
  247. ,key = router.path.join('-');
  248. $win.off('resize', admin.resizeFn[key]);
  249. fn(), admin.resizeFn[key] = fn;
  250. $win.on('resize', admin.resizeFn[key]);
  251. }
  252. ,resizeFn: {}
  253. ,runResize: function(){
  254. var router = layui.router()
  255. ,key = router.path.join('-');
  256. admin.resizeFn[key] && admin.resizeFn[key]();
  257. }
  258. ,delResize: function(){
  259. var router = layui.router()
  260. ,key = router.path.join('-');
  261. $win.off('resize', admin.resizeFn[key])
  262. delete admin.resizeFn[key];
  263. }
  264. //关闭当前 pageTabs
  265. ,closeThisTabs: function(){
  266. if(!admin.tabsPage.index) return;
  267. $(TABS_HEADER).eq(admin.tabsPage.index).find('.layui-tab-close').trigger('click');
  268. }
  269. //全屏
  270. ,fullScreen: function(){
  271. var ele = document.documentElement
  272. ,reqFullScreen = ele.requestFullScreen || ele.webkitRequestFullScreen
  273. || ele.mozRequestFullScreen || ele.msRequestFullscreen;
  274. if(typeof reqFullScreen !== 'undefined' && reqFullScreen) {
  275. reqFullScreen.call(ele);
  276. };
  277. }
  278. //退出全屏
  279. ,exitScreen: function(){
  280. var ele = document.documentElement
  281. if (document.exitFullscreen) {
  282. document.exitFullscreen();
  283. } else if (document.mozCancelFullScreen) {
  284. document.mozCancelFullScreen();
  285. } else if (document.webkitCancelFullScreen) {
  286. document.webkitCancelFullScreen();
  287. } else if (document.msExitFullscreen) {
  288. document.msExitFullscreen();
  289. }
  290. }
  291. //……
  292. };
  293. //事件
  294. var events = admin.events = {
  295. //伸缩
  296. flexible: function(othis){
  297. var iconElem = othis.find('#'+ APP_FLEXIBLE)
  298. ,isSpread = iconElem.hasClass(ICON_SPREAD);
  299. admin.sideFlexible(isSpread ? 'spread' : null);
  300. }
  301. //刷新
  302. ,refresh: function(){
  303. var ELEM_IFRAME = '.layadmin-iframe'
  304. ,length = $('.'+ TABS_BODY).length;
  305. if(admin.tabsPage.index >= length){
  306. admin.tabsPage.index = length - 1;
  307. }
  308. var iframe = admin.tabsBody(admin.tabsPage.index).find(ELEM_IFRAME);
  309. iframe[0].contentWindow.location.reload(true);
  310. }
  311. //输入框搜索
  312. ,serach: function(othis){
  313. othis.off('keypress').on('keypress',function(e){
  314. if(!this.value.replace(/\s/g, '')) return;
  315. //回车跳转
  316. if(e.keyCode === 13){
  317. var href = othis.attr('lay-action')
  318. ,text = othis.attr('lay-text') || '搜索';
  319. href = href + this.value;
  320. text = text + ' <span style="color: #FF5722;">'+ admin.escape(this.value) +'</span>';
  321. //打开标签页
  322. layui.index.openTabsPage(href, text);
  323. //如果搜索关键词已经打开,则刷新页面即可
  324. events.serach.keys || (events.serach.keys = {});
  325. events.serach.keys[admin.tabsPage.index] = this.value;
  326. if(this.value === events.serach.keys[admin.tabsPage.index]){
  327. events.refresh(othis);
  328. }
  329. //清空输入框
  330. this.value = '';
  331. }
  332. });
  333. }
  334. //点击消息
  335. ,message: function(othis){
  336. othis.find('.layui-badge-dot').remove();
  337. }
  338. //弹出主题面板
  339. ,theme: function(){
  340. admin.popupRight({
  341. id: 'LAY_adminPopupTheme'
  342. ,success: function(){
  343. view(this.id).render('system/theme')
  344. }
  345. });
  346. }
  347. //便签
  348. ,note: function(othis){
  349. var mobile = admin.screen() < 2
  350. ,note = layui.data(setter.tableName).note;
  351. events.note.index = admin.popup({
  352. title: '便签'
  353. ,shade: 0
  354. ,offset: [
  355. '41px'
  356. ,(mobile ? null : (othis.offset().left - 250) + 'px')
  357. ]
  358. ,anim: -1
  359. ,id: 'LAY_adminNote'
  360. ,skin: 'layadmin-note layui-anim layui-anim-upbit'
  361. ,content: '<textarea placeholder="内容"></textarea>'
  362. ,resize: false
  363. ,success: function(layero, index){
  364. var textarea = layero.find('textarea')
  365. ,value = note === undefined ? '便签中的内容会存储在本地,这样即便你关掉了浏览器,在下次打开时,依然会读取到上一次的记录。是个非常小巧实用的本地备忘录' : note;
  366. textarea.val(value).focus().on('keyup', function(){
  367. layui.data(setter.tableName, {
  368. key: 'note'
  369. ,value: this.value
  370. });
  371. });
  372. }
  373. })
  374. }
  375. //全屏
  376. ,fullscreen: function(othis){
  377. var SCREEN_FULL = 'layui-icon-screen-full'
  378. ,SCREEN_REST = 'layui-icon-screen-restore'
  379. ,iconElem = othis.children("i");
  380. if(iconElem.hasClass(SCREEN_FULL)){
  381. admin.fullScreen();
  382. iconElem.addClass(SCREEN_REST).removeClass(SCREEN_FULL);
  383. } else {
  384. admin.exitScreen();
  385. iconElem.addClass(SCREEN_FULL).removeClass(SCREEN_REST);
  386. }
  387. }
  388. //弹出关于面板
  389. ,about: function(){
  390. admin.popupRight({
  391. id: 'LAY_adminPopupAbout'
  392. ,success: function(){
  393. view(this.id).render('system/about');
  394. }
  395. });
  396. }
  397. //弹出更多面板
  398. ,more: function(){
  399. admin.popupRight({
  400. id: 'LAY_adminPopupMore'
  401. ,success: function(){
  402. view(this.id).render('system/more');
  403. }
  404. });
  405. }
  406. //返回上一页
  407. ,back: function(){
  408. history.back();
  409. //history.go(-1);
  410. return;
  411. if (config.history_link) {
  412. var btn = $(".layadmin-iframe").contents().find(".btn-danger");
  413. if (btn.length) {
  414. var link = btn.attr('href');
  415. //$('.layadmin-iframe').attr('src', btn.attr('href'));
  416. } else {
  417. //$('.layadmin-iframe').attr('src', config.history_link);
  418. var link = config.history_link;
  419. }
  420. var text = config.history_text;
  421. var topLayui = parent === self ? layui : top.layui;
  422. topLayui.index.openTabsPage(link, text);
  423. } else {
  424. history.go(-1);
  425. }
  426. }
  427. //主题设置
  428. ,setTheme: function(othis){
  429. var index = othis.data('index')
  430. ,nextIndex = othis.siblings('.layui-this').data('index');
  431. if(othis.hasClass(THIS)) return;
  432. othis.addClass(THIS).siblings('.layui-this').removeClass(THIS);
  433. admin.initTheme(index);
  434. }
  435. //左右滚动页面标签
  436. ,rollPage: function(type, index){
  437. var tabsHeader = $('#LAY_app_tabsheader')
  438. ,liItem = tabsHeader.children('li')
  439. ,scrollWidth = tabsHeader.prop('scrollWidth')
  440. ,outerWidth = tabsHeader.outerWidth()
  441. ,tabsLeft = parseFloat(tabsHeader.css('left'));
  442. //右左往右
  443. if(type === 'left'){
  444. if(!tabsLeft && tabsLeft <=0) return;
  445. //当前的left减去可视宽度,用于与上一轮的页标比较
  446. var prefLeft = -tabsLeft - outerWidth;
  447. liItem.each(function(index, item){
  448. var li = $(item)
  449. ,left = li.position().left;
  450. if(left >= prefLeft){
  451. tabsHeader.css('left', -left);
  452. return false;
  453. }
  454. });
  455. } else if(type === 'auto'){ //自动滚动
  456. (function(){
  457. var thisLi = liItem.eq(index), thisLeft;
  458. if(!thisLi[0]) return;
  459. thisLeft = thisLi.position().left;
  460. //当目标标签在可视区域左侧时
  461. if(thisLeft < -tabsLeft){
  462. return tabsHeader.css('left', -thisLeft);
  463. }
  464. //当目标标签在可视区域右侧时
  465. if(thisLeft + thisLi.outerWidth() >= outerWidth - tabsLeft){
  466. var subLeft = thisLeft + thisLi.outerWidth() - (outerWidth - tabsLeft);
  467. liItem.each(function(i, item){
  468. var li = $(item)
  469. ,left = li.position().left;
  470. //从当前可视区域的最左第二个节点遍历,如果减去最左节点的差 > 目标在右侧不可见的宽度,则将该节点放置可视区域最左
  471. if(left + tabsLeft > 0){
  472. if(left - tabsLeft > subLeft){
  473. tabsHeader.css('left', -left);
  474. return false;
  475. }
  476. }
  477. });
  478. }
  479. }());
  480. } else {
  481. //默认向左滚动
  482. liItem.each(function(i, item){
  483. var li = $(item)
  484. ,left = li.position().left;
  485. if(left + li.outerWidth() >= outerWidth - tabsLeft){
  486. tabsHeader.css('left', -left);
  487. return false;
  488. }
  489. });
  490. }
  491. }
  492. //向右滚动页面标签
  493. ,leftPage: function(){
  494. events.rollPage('left');
  495. }
  496. //向左滚动页面标签
  497. ,rightPage: function(){
  498. events.rollPage();
  499. }
  500. //关闭当前标签页
  501. ,closeThisTabs: function(){
  502. var topAdmin = parent === self ? admin : parent.layui.admin;
  503. topAdmin.closeThisTabs();
  504. }
  505. //关闭其它标签页
  506. ,closeOtherTabs: function(type){
  507. var TABS_REMOVE = 'LAY-system-pagetabs-remove';
  508. if(type === 'all'){
  509. $(TABS_HEADER+ ':gt(0)').remove();
  510. $(APP_BODY).find('.'+ TABS_BODY+ ':gt(0)').remove();
  511. $(TABS_HEADER).eq(0).trigger('click');
  512. } else {
  513. $(TABS_HEADER).each(function(index, item){
  514. if(index && index != admin.tabsPage.index){
  515. $(item).addClass(TABS_REMOVE);
  516. admin.tabsBody(index).addClass(TABS_REMOVE);
  517. }
  518. });
  519. $('.'+ TABS_REMOVE).remove();
  520. }
  521. }
  522. //关闭全部标签页
  523. ,closeAllTabs: function(){
  524. events.closeOtherTabs('all');
  525. //location.hash = '';
  526. }
  527. //遮罩
  528. ,shade: function(){
  529. admin.sideFlexible();
  530. }
  531. //呼出IM 示例
  532. ,im: function(){
  533. admin.popup({
  534. id: 'LAY-popup-layim-demo' //定义唯一ID,防止重复弹出
  535. ,shade: 0
  536. ,area: ['800px', '300px']
  537. ,title: '面板外的操作示例'
  538. ,offset: 'lb'
  539. ,success: function(){
  540. //将 views 目录下的某视图文件内容渲染给该面板
  541. layui.view(this.id).render('layim/demo').then(function(){
  542. layui.use('im');
  543. });
  544. }
  545. })
  546. }
  547. };
  548. //初始
  549. !function(){
  550. //主题初始化,本地主题记录优先,其次为 initColorIndex
  551. var local = layui.data(setter.tableName);
  552. if(local.theme){
  553. admin.theme(local.theme);
  554. } else if(setter.theme){
  555. admin.initTheme(setter.theme.initColorIndex);
  556. }
  557. //常规版默认开启多标签页
  558. if(!('pageTabs' in layui.setter)) layui.setter.pageTabs = true;
  559. //不开启页面标签时
  560. if(!setter.pageTabs){
  561. $('#LAY_app_tabs').addClass(HIDE);
  562. container.addClass('layadmin-tabspage-none');
  563. }
  564. //低版本IE提示
  565. if(device.ie && device.ie < 10){
  566. view.error('IE'+ device.ie + '下访问可能不佳,推荐使用:Chrome / Firefox / Edge 等高级浏览器', {
  567. offset: 'auto'
  568. ,id: 'LAY_errorIE'
  569. });
  570. }
  571. }();
  572. //admin.prevRouter = {}; //上一个路由
  573. //监听 tab 组件切换,同步 index
  574. element.on('tab('+ FILTER_TAB_TBAS +')', function(data){
  575. admin.tabsPage.index = data.index;
  576. });
  577. //监听选项卡切换,改变菜单状态
  578. admin.on('tabsPage(setMenustatus)', function(router){
  579. var pathURL = router.url, getData = function(item){
  580. return {
  581. list: item.children('.layui-nav-child')
  582. ,a: item.children('*[lay-href]')
  583. }
  584. }
  585. ,sideMenu = $('#'+ SIDE_MENU)
  586. ,SIDE_NAV_ITEMD = 'layui-nav-itemed'
  587. //捕获对应菜单
  588. ,matchMenu = function(list){
  589. list.each(function(index1, item1){
  590. var othis1 = $(item1)
  591. ,data1 = getData(othis1)
  592. ,listChildren1 = data1.list.children('dd')
  593. ,matched1 = pathURL === data1.a.attr('lay-href');
  594. listChildren1.each(function(index2, item2){
  595. var othis2 = $(item2)
  596. ,data2 = getData(othis2)
  597. ,listChildren2 = data2.list.children('dd')
  598. ,matched2 = pathURL === data2.a.attr('lay-href');
  599. listChildren2.each(function(index3, item3){
  600. var othis3 = $(item3)
  601. ,data3 = getData(othis3)
  602. ,matched3 = pathURL === data3.a.attr('lay-href');
  603. if(matched3){
  604. var selected = data3.list[0] ? SIDE_NAV_ITEMD : THIS;
  605. othis3.addClass(selected).siblings().removeClass(selected); //标记选择器
  606. return false;
  607. }
  608. });
  609. if(matched2){
  610. var selected = data2.list[0] ? SIDE_NAV_ITEMD : THIS;
  611. othis2.addClass(selected).siblings().removeClass(selected); //标记选择器
  612. return false
  613. }
  614. });
  615. if(matched1){
  616. var selected = data1.list[0] ? SIDE_NAV_ITEMD : THIS;
  617. othis1.addClass(selected).siblings().removeClass(selected); //标记选择器
  618. return false;
  619. }
  620. });
  621. }
  622. //重置状态
  623. sideMenu.find('.'+ THIS).removeClass(THIS);
  624. //移动端点击菜单时自动收缩
  625. if(admin.screen() < 2) admin.sideFlexible();
  626. //开始捕获
  627. matchMenu(sideMenu.children('li'));
  628. });
  629. //监听侧边导航点击事件
  630. element.on('nav(layadmin-system-side-menu)', function(elem){
  631. if(elem.siblings('.layui-nav-child')[0] && container.hasClass(SIDE_SHRINK)){
  632. admin.sideFlexible('spread');
  633. layer.close(elem.data('index'));
  634. };
  635. admin.tabsPage.type = 'nav';
  636. });
  637. //监听选项卡的更多操作
  638. element.on('nav(layadmin-pagetabs-nav)', function(elem){
  639. var dd = elem.parent();
  640. dd.removeClass(THIS);
  641. dd.parent().removeClass(SHOW);
  642. });
  643. //同步路由
  644. var setThisRouter = function(othis){
  645. var layid = othis.attr('lay-id')
  646. ,attr = othis.attr('lay-attr')
  647. ,index = othis.index();
  648. admin.tabsBodyChange(index, {
  649. url: attr
  650. });
  651. //location.hash = layid === setter.entry ? '/' : attr;
  652. }
  653. ,TABS_HEADER = '#LAY_app_tabsheader>li';
  654. //标签页标题点击
  655. $body.on('click', TABS_HEADER, function(){
  656. var othis = $(this)
  657. ,index = othis.index();
  658. admin.tabsPage.type = 'tab';
  659. admin.tabsPage.index = index;
  660. setThisRouter(othis);
  661. });
  662. //监听 tabspage 删除
  663. element.on('tabDelete('+ FILTER_TAB_TBAS +')', function(obj){
  664. var othis = $(TABS_HEADER+ '.layui-this');
  665. obj.index && admin.tabsBody(obj.index).remove();
  666. setThisRouter(othis);
  667. //移除resize事件
  668. admin.delResize();
  669. });
  670. //页面跳转
  671. $body.on('click', '*[lay-href]', function(){
  672. var othis = $(this)
  673. ,href = othis.attr('lay-href')
  674. ,text = othis.attr('lay-text')
  675. ,router = layui.router();
  676. admin.tabsPage.elem = othis;
  677. //admin.prevRouter[router.path[0]] = router.href; //记录上一次各菜单的路由信息
  678. config.history_link = href;
  679. config.history_text = text || othis.text();
  680. //执行跳转
  681. var topLayui = parent === self ? layui : top.layui;
  682. topLayui.index.openTabsPage(href, config.history_text);
  683. });
  684. //点击事件
  685. $body.on('click', '*[layadmin-event]', function(){
  686. var othis = $(this)
  687. ,attrEvent = othis.attr('layadmin-event');
  688. events[attrEvent] && events[attrEvent].call(this, othis);
  689. });
  690. //tips
  691. $body.on('mouseenter', '*[lay-tips]', function(){
  692. var othis = $(this);
  693. if(othis.parent().hasClass('layui-nav-item') && !container.hasClass(SIDE_SHRINK)) return;
  694. var tips = othis.attr('lay-tips')
  695. ,offset = othis.attr('lay-offset')
  696. ,direction = othis.attr('lay-direction')
  697. ,index = layer.tips(tips, this, {
  698. tips: direction || 1
  699. ,time: -1
  700. ,success: function(layero, index){
  701. if(offset){
  702. layero.css('margin-left', offset + 'px');
  703. }
  704. }
  705. });
  706. othis.data('index', index);
  707. }).on('mouseleave', '*[lay-tips]', function(){
  708. layer.close($(this).data('index'));
  709. });
  710. //窗口resize事件
  711. var resizeSystem = layui.data.resizeSystem = function(){
  712. //layer.close(events.note.index);
  713. layer.closeAll('tips');
  714. if(!resizeSystem.lock){
  715. setTimeout(function(){
  716. admin.sideFlexible(admin.screen() < 2 ? '' : 'spread');
  717. delete resizeSystem.lock;
  718. }, 100);
  719. }
  720. resizeSystem.lock = true;
  721. }
  722. $win.on('resize', layui.data.resizeSystem);
  723. //接口输出
  724. exports('admin', admin);
  725. });