admin.js 24 KB

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