admin.js 32 KB

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