admin.js 24 KB

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