navbar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /** iwb_admin-v1.1.0 MIT License By http://www.iwbnet.com e-mail:yueyy@iwbnet.com */
  2. ;/**
  3. * Name:navbar.js
  4. * Author:Van
  5. * E-mail:yueyy@iwbnet.com
  6. * Website:http://www.iwbnet.com
  7. * LICENSE:MIT
  8. */
  9. layui.define(['layer', 'laytpl', 'element'], function (exports) {
  10. var $ = layui.jquery,
  11. layer = layui.layer,
  12. //modName = 'navbar',
  13. //win = $(window),
  14. doc = $(document),
  15. //laytpl = layui.laytpl,
  16. element = layui.element;
  17. var navbar = {
  18. v: '1.0.4',
  19. config: {
  20. data: undefined, //静态数据
  21. remote: {
  22. url: undefined, //接口地址
  23. type: 'post', //请求方式
  24. jsonp: false //跨域
  25. },
  26. cached: false, //是否缓存
  27. elem: undefined, //容器
  28. filter: 'iwbNavbar' //过滤器名称
  29. },
  30. set: function (options) {
  31. var that = this;
  32. that.config.data = undefined;
  33. $.extend(true, that.config, options);
  34. return that;
  35. },
  36. /**
  37. * 是否已设置了elem
  38. */
  39. hasElem: function () {
  40. var that = this,
  41. config = that.config;
  42. if (config.elem === undefined && doc.find('ul[iwb-navbar]').length === 0 && $(config.elem)) {
  43. layui.hint().error('Navbar error:请配置Navbar容器.');
  44. return false;
  45. }
  46. return true;
  47. },
  48. /**
  49. * 获取容器的jq对象
  50. */
  51. getElem: function () {
  52. var config = this.config;
  53. return (config.elem !== undefined && $(config.elem).length > 0) ? $(config.elem) : doc.find('ul[iwb-navbar]');
  54. },
  55. /**
  56. * 绑定特定a标签的点击事件
  57. */
  58. bind: function (callback, params) {
  59. var that = this,
  60. config = that.config;
  61. var defaults = {
  62. target: undefined,
  63. showTips: true,
  64. config:config
  65. };
  66. $.extend(true, defaults, params);
  67. var target = defaults.target === undefined ? doc : $(defaults.target);
  68. // if (!that.hasElem())
  69. // return that;
  70. // var _elem = that.getElem();
  71. target.find('a[iwb-target-tip]')
  72. .each(function() {
  73. var $that = $(this);
  74. var tipsId = undefined;
  75. if (defaults.showTips) {
  76. $that.hover(function() {
  77. var isSided = $(".iwb-side").hasClass("iwb-sided");
  78. if (isSided)
  79. tipsId = layer.tips($(this).children('cite').text(), this);
  80. },
  81. function() {
  82. if (tipsId)
  83. layer.close(tipsId);
  84. });
  85. }
  86. });
  87. target.find('a[iwb-target]').each(function () {
  88. var $that = $(this);
  89. var tipsId = undefined;
  90. if (defaults.showTips) {
  91. $that.hover(function () {
  92. var isSided = $(".iwb-side").hasClass("iwb-sided");
  93. if (isSided)
  94. tipsId = layer.tips($(this).children('cite').text(), this);
  95. }, function () {
  96. if (tipsId)
  97. layer.close(tipsId);
  98. });
  99. }
  100. $that.off('click').on('click', function () {
  101. var options = $that.data('options');
  102. var data=[];
  103. if (options !== undefined) {
  104. try {
  105. data = new Function('return ' + options)();
  106. } catch (e) {
  107. layui.hint().error('Navbar 组件a[data-options]配置项存在语法错误:' + options);
  108. }
  109. } else {
  110. data = {
  111. icon: $that.data('icon'),
  112. id: $that.data('id'),
  113. title: $that.data('title'),
  114. url: $that.data('url')
  115. };
  116. }
  117. typeof callback === 'function' && callback(data);
  118. });
  119. });
  120. $('#iwb-nav-fold-top').off('click').on('click', function () {
  121. toggleSideNav(doc);
  122. });
  123. $('#iwb-nav-fold-side').off('click').on('click', function () {
  124. toggleSideNav(doc);
  125. });
  126. return that;
  127. },
  128. /**
  129. * 渲染navbar
  130. */
  131. render: function (callback) {
  132. var that = this,
  133. config = that.config, //配置
  134. remote = config.remote, //远程参数配置
  135. //tpl = [
  136. // '{{# layui.each(d,function(index, item){ }}',
  137. // '{{# if(item.spread){ }}',
  138. // '<li class="layui-nav-item layui-nav-itemed">',
  139. // '{{# }else{ }}',
  140. // '<li class="layui-nav-item">',
  141. // '{{# } }}',
  142. // '{{# var hasChildren = item.children!==undefined && item.children.length>0; }}',
  143. // '{{# if(hasChildren){ }}',
  144. // '<a href="javascript:;">',
  145. // '{{# if (item.icon.indexOf("fa-") !== -1) { }}',
  146. // '<i class="fa {{item.icon}}" aria-hidden="true"></i>',
  147. // '{{# } else { }}',
  148. // '<i class="layui-icon">{{item.icon}}</i>',
  149. // '{{# } }}',
  150. // '<span> {{item.title}}</span>',
  151. // '</a>',
  152. // '{{# var children = item.children; }}',
  153. // '<dl class="layui-nav-child">',
  154. // '{{# layui.each(children,function(childIndex, child){ }}',
  155. // '<dd>',
  156. // '<a href="javascript:;" iwb-target data-options="{url:\'{{child.url}}\',icon:\'{{child.icon}}\',title:\'{{child.title}}\',id:\'{{child.id}}\'}">',
  157. // '{{# if (child.icon.indexOf("fa-") !== -1) { }}',
  158. // '<i class="fa {{child.icon}}" aria-hidden="true"></i>',
  159. // '{{# } else { }}',
  160. // '<i class="layui-icon">{{child.icon}}</i>',
  161. // '{{# } }}',
  162. // '<span> {{child.title}}</span>',
  163. // '</a>',
  164. // '</dd>',
  165. // '{{# }); }}',
  166. // '</dl>',
  167. // '{{# }else{ }}',
  168. // '<a href="javascript:;" iwb-target data-options="{url:\'{{item.url}}\',icon:\'{{item.icon}}\',title:\'{{item.title}}\',id:\'{{item.id}}\'}">',
  169. // '{{# if (item.icon.indexOf("fa-") !== -1) { }}',
  170. // '<i class="fa {{item.icon}}" aria-hidden="true"></i>',
  171. // '{{# } else { }}',
  172. // '<i class="layui-icon">{{item.icon}}</i>',
  173. // '{{# } }}',
  174. // '<span> {{item.title}}</span>',
  175. // '</a>',
  176. // '{{# } }}',
  177. // '</li>',
  178. // '{{# }); }}'
  179. //], //模板
  180. data1 = [];
  181. var navbarLoadIndex = layer.load(2);
  182. if (!that.hasElem())
  183. return that;
  184. var elem = that.getElem();
  185. //本地数据优先
  186. if (config.data !== undefined && config.data.length > 0) {
  187. data1 = config.data;
  188. } else {
  189. var dataType = remote.jsonp ? 'jsonp' : 'json';
  190. var options = {
  191. url: remote.url,
  192. type: remote.type,
  193. async: false,
  194. dataType:dataType,
  195. error: function (xhr, status, thrown) {
  196. layui.hint().error('Navbar error:AJAX请求出错.' + thrown);
  197. navbarLoadIndex && layer.close(navbarLoadIndex);
  198. },
  199. success: function (res) {
  200. data1 = res;
  201. }
  202. };
  203. $.extend(true, options, remote.jsonp ? {
  204. dataType: 'jsonp',
  205. jsonp: 'callback',
  206. jsonpCallback: 'jsonpCallback'
  207. } : {
  208. dataType: 'json'
  209. });
  210. $.support.cors = true;
  211. $.ajax(options);
  212. }
  213. var tIndex = setInterval(function () {
  214. if (data1 != null && data1.length > 0) {
  215. clearInterval(tIndex);
  216. var html = getHtml(data1);
  217. elem.html(html);
  218. element.init();
  219. childClickEvent();
  220. //绑定a标签的点击事件
  221. that.bind(function(data) {
  222. typeof callback === 'function' && callback(data);
  223. });
  224. //渲染模板
  225. //laytpl(tpl.join('')).render(_data, function (html) {
  226. // _elem.html(html);
  227. // element.init();
  228. // //绑定a标签的点击事件
  229. // that.bind(function (data) {
  230. // typeof callback === 'function' && callback(data);
  231. // });
  232. // //关闭等待层
  233. // navbarLoadIndex && layer.close(navbarLoadIndex);
  234. //});
  235. } else {
  236. layui.hint().error('Navbar error:AJAX 未取到数据');
  237. clearInterval(tIndex);
  238. }
  239. //关闭等待层
  240. navbarLoadIndex && layer.close(navbarLoadIndex);
  241. }, 50);
  242. return that;
  243. }
  244. };
  245. exports('navbar', navbar);
  246. });
  247. function childClickEvent(select) {
  248. select = select || ".menu_ul li a[iwb-hasChild]";
  249. var aLink = document.querySelectorAll(select);
  250. for (var i = 0, len = aLink.length; i < len; i++) {
  251. aLink[i].addEventListener('click', function () {
  252. //console.log(this.parentNode.children[1]);
  253. if (this.parentNode.children[1].style.display === 'block') {
  254. this.parentNode.children[1].style.display = 'none';
  255. //this.parentNode.children[0].children[2].children[0].className = 'fa fa-angle-down';
  256. this.parentNode.className = 'layui-nav-item';
  257. } else {
  258. this.parentNode.children[1].style.display = 'block';
  259. this.parentNode.className = 'layui-nav-item layui-nav-itemed';
  260. }
  261. }, false);
  262. }
  263. }
  264. function getHtml(data) {
  265. var ulHtml = '<ul class="layui-nav layui-nav-tree" lay-filter="iwbNavbar" iwb-navbar>';
  266. ulHtml += '<li class="layui-nav-item"><a id="iwb-nav-fold-side" title="切换左侧菜单" style="color: #fef;text-align:center;"><i class="iwb-nav-toggle-side layui-icon" aria-hidden="true">&#xe65f;</i></a></li>';
  267. for (var i = 0; i < data.length; i++) {
  268. var child = data[i];
  269. //if (child.spread) {
  270. // ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
  271. //} else {
  272. // ulHtml += '<li class="layui-nav-item">';
  273. //}
  274. ulHtml += '<li class="layui-nav-item">';
  275. if (child.children !== undefined && child.children.length > 0) {
  276. //console.log(i + "---child");
  277. ulHtml += '<a href="javascript:;" iwb-hasChild iwb-target-tip>';
  278. ulHtml += getIcon(child.icon);
  279. ulHtml += '<cite>' + child.title + '</cite>';
  280. ulHtml += '<span class="layui-nav-more"></span>';
  281. ulHtml += '</a>';
  282. ulHtml += '<ul class="menu_ul layui-nav-child">';
  283. ulHtml += getChildHtml(child.children);
  284. ulHtml += '</ul>';
  285. } else {
  286. //ulHtml += '<a href="javascript:;" iwb-target data-options="{url:\'' +
  287. // child.href +
  288. // '\',icon:\'' +
  289. // child.icon +
  290. // '\',title:\'' +
  291. // child.title +
  292. // '\',id:\'' +
  293. // child.id +
  294. // '\'}">';
  295. ulHtml += '<a href="javascript:;" data-url="' +
  296. child.href +
  297. '" data-icon="' +
  298. child.icon +
  299. '" data-title="' +
  300. child.title +
  301. '" data-id="' +
  302. child.id +
  303. '" iwb-target iwb-target-tip>';
  304. ulHtml += getIcon(child.icon);
  305. ulHtml += '<cite>' + child.title + '</cite>';
  306. ulHtml += '</a>';
  307. }
  308. ulHtml += '</li>';
  309. }
  310. ulHtml += '</ul>';
  311. //console.log(ulHtml);
  312. return ulHtml;
  313. }
  314. function getChildHtml(data) {
  315. var ulHtml = "";
  316. for (var i = 0; i < data.length; i++) {
  317. var child = data[i];
  318. if (child.children !== undefined && child.children.length > 0) {
  319. //ulHtml += '<dd title="' + child.title + '">';
  320. ulHtml += '<li>';
  321. ulHtml += '<a class="table" href="javascript:;" iwb-hasChild iwb-target-tip>';
  322. ulHtml += getIcon(child.icon);
  323. ulHtml += '<cite>' + child.title + '</cite>';
  324. ulHtml += '<span class="layui-nav-more"></span>';
  325. ulHtml += '</a>';
  326. ulHtml += '<ul class="layui-nav-child" style="display: none;">';
  327. ulHtml += getChildHtml(child.children);
  328. ulHtml += '</ul>';
  329. //ulHtml += '</dd>';
  330. ulHtml += '</li>';
  331. } else {
  332. //ulHtml += '<dd title="' + child.title + '">';
  333. ulHtml += '<li class="layui-nav-item">';
  334. //ulHtml += '<a class="table" href="javascript:;" iwb-target data-options="{url:\'' +
  335. // child.href +
  336. // '\',icon:\'' +
  337. // child.icon +
  338. // '\',title:\'' +
  339. // child.title +
  340. // '\',id:\'' +
  341. // child.id +
  342. // '\'}">';
  343. ulHtml += '<a href="javascript:;" data-url="' +
  344. child.href +
  345. '" data-icon="' +
  346. child.icon +
  347. '" data-title="' +
  348. child.title +
  349. '" data-id="' +
  350. child.id +
  351. '" iwb-target iwb-target-tip >';
  352. ulHtml += getIcon(child.icon);
  353. ulHtml += '<cite>' + child.title + '</cite>';
  354. ulHtml += '</a>';
  355. //ulHtml += '</dd>';
  356. ulHtml += '</li>';
  357. }
  358. }
  359. return ulHtml;
  360. }
  361. function getIcon(icon) {
  362. var ulHtml = "";
  363. if (icon !== undefined && icon !== '' && icon != null) {
  364. if (icon.indexOf('fa-') !== -1) {
  365. ulHtml = '<i class="fa ' + icon + '" aria-hidden="true" data-icon="' + icon + '"></i>';
  366. } else {
  367. ulHtml = '<i class="layui-icon" data-icon="' + icon + '">' + icon + '</i>';
  368. }
  369. }
  370. return ulHtml;
  371. }
  372. function toggleSideNav(doc) {
  373. var icon1 = "iwb-nav-toggle-top layui-icon layui-icon-shrink-right",
  374. icon2 = "iwb-nav-toggle-top layui-icon layui-icon-spread-left",
  375. icon3 = "&#xe65f;",
  376. icon4 = "&#xe671;";
  377. var display = $('.iwb-side-fold span').css('display');
  378. var menu1 = doc.find('i.iwb-nav-toggle-top');
  379. var menu2 = doc.find('i.iwb-nav-toggle-side');
  380. if (display === 'inline' || display === 'inline-block') {
  381. $('.iwb-side-fold span').css('display', 'none');
  382. }
  383. if (display === 'none') {
  384. $('.iwb-side-fold span').css('display', 'inline-block');
  385. }
  386. var side = doc.find('div.iwb-side');
  387. if (side.hasClass('iwb-sided')) {
  388. side.removeClass('iwb-sided');
  389. doc.find('div.layui-body').removeClass('iwb-body-folded');
  390. doc.find('div.layui-footer').removeClass('iwb-footer-folded');
  391. menu1.attr('class', icon1);
  392. menu2.html(icon3);
  393. $('#logo span').css('display', 'block');
  394. //$('#logo').css('width', '200px').removeClass("iwb-header-sided");
  395. $('#logo').removeClass("iwb-header-sided");
  396. $('.tplay-left-icon').css('display', 'none');
  397. //$('.layui-layout-left').css('left', '220px');
  398. } else {
  399. side.addClass('iwb-sided');
  400. doc.find('div.layui-body').addClass('iwb-body-folded');
  401. doc.find('div.layui-footer').addClass('iwb-footer-folded');
  402. menu1.attr('class', icon2);
  403. menu2.html(icon4);
  404. $('#logo span').css('display', 'none');
  405. //$('#logo').css('width', '50px').addClass("iwb-header-sided");
  406. $('#logo').addClass("iwb-header-sided");
  407. $('.tplay-left-icon').css('display', 'block');
  408. //$('.layui-layout-left').css('left', '50px');
  409. }
  410. }