navbar.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /** navbar.js By Beginner */
  2. layui.define(['element', 'common'], function(exports) {
  3. "use strict";
  4. var $ = layui.jquery,
  5. //layer = parent.layer === undefined ? layui.layer : parent.layer,
  6. element = layui.element,
  7. common = layui.common,
  8. cacheName = 'tb_navbar';
  9. // ReSharper disable once InconsistentNaming
  10. var Navbar = function() {
  11. /**
  12. * 默认配置
  13. */
  14. this.config = {
  15. elem: undefined, //容器
  16. data: undefined, //数据源
  17. url: undefined, //数据源地址
  18. type: 'post', //读取方式
  19. cached: false, //是否使用缓存
  20. spreadOne: false, //设置是否只展开一个二级菜单
  21. async:true//是否异步请求
  22. };
  23. this.v = '0.0.1';
  24. };
  25. Navbar.prototype.render = function() {
  26. var that = this;
  27. var config = that.config;
  28. if(typeof(config.elem) !== 'string' && typeof(config.elem) !== 'object') {
  29. common.throwError('Navbar error: elem参数未定义或设置出错,具体设置格式请参考文档API.');
  30. }
  31. var $container;
  32. if(typeof(config.elem) === 'string') {
  33. $container = $('' + config.elem + '');
  34. }
  35. if(typeof(config.elem) === 'object') {
  36. $container = config.elem;
  37. }
  38. // ReSharper disable once UsageOfPossiblyUnassignedValue
  39. // ReSharper disable once QualifiedExpressionMaybeNull
  40. if($container.length === 0) {
  41. common.throwError('Navbar error:找不到elem参数配置的容器,请检查.');
  42. }
  43. if(config.data === undefined && config.url === undefined) {
  44. common.throwError('Navbar error:请为Navbar配置数据源.');
  45. }
  46. var html;
  47. if(config.data !== undefined && typeof(config.data) === 'object') {
  48. html = getHtml(config.data);
  49. $container.html(html);
  50. element.init();
  51. that.config.elem = $container;
  52. } else {
  53. if(config.cached) {
  54. var cacheNavbar = layui.data(cacheName);
  55. if(cacheNavbar.navbar === undefined) {
  56. $.ajax({
  57. type: config.type,
  58. url: config.url,
  59. async: config.async,
  60. dataType: 'json',
  61. success: function(result) {
  62. //添加缓存
  63. layui.data(cacheName, {
  64. key: 'navbar',
  65. value: result
  66. });
  67. var html = getHtml(result);
  68. $container.html(html);
  69. element.init();
  70. },
  71. error: function(xhr, status, error) {
  72. common.msgError('Navbar error:' + error);
  73. },
  74. complete: function() {
  75. that.config.elem = $container;
  76. }
  77. });
  78. } else {
  79. html = getHtml(cacheNavbar.navbar);
  80. $container.html(html);
  81. element.init();
  82. that.config.elem = $container;
  83. }
  84. } else {
  85. //清空缓存
  86. layui.data(cacheName, null);
  87. $.ajax({
  88. type: config.type,
  89. url: config.url,
  90. async: false, //_config.async,
  91. dataType: 'json',
  92. success: function(result) {
  93. var html = getHtml(result);
  94. $container.html(html);
  95. element.init();
  96. },
  97. error: function(xhr, status, error) {
  98. common.msgError('Navbar error:' + error);
  99. },
  100. complete: function() {
  101. that.config.elem = $container;
  102. }
  103. });
  104. }
  105. }
  106. //只展开一个二级菜单
  107. if(config.spreadOne){
  108. var $ul = $container.children('ul');
  109. $ul.find('li.layui-nav-item').each(function(){
  110. $(this).on('click',function(){
  111. $(this).siblings().removeClass('layui-nav-itemed');
  112. });
  113. });
  114. }
  115. return that;
  116. };
  117. /**
  118. * 配置Navbar
  119. * @param {Object} options
  120. */
  121. Navbar.prototype.set = function(options) {
  122. var that = this;
  123. that.config.data = undefined;
  124. $.extend(true, that.config, options);
  125. return that;
  126. };
  127. /**
  128. * 绑定事件
  129. * @param {String} events
  130. * @param {Function} callback
  131. */
  132. Navbar.prototype.on = function(events, callback) {
  133. var that = this;
  134. var con = that.config.elem;
  135. if(typeof(events) !== 'string') {
  136. common.throwError('Navbar error:事件名配置出错,请参考API文档.');
  137. }
  138. var lIndex = events.indexOf('(');
  139. var eventName = events.substr(0, lIndex);
  140. //var filter = events.substring(lIndex + 1, events.indexOf(')'));
  141. if(eventName === 'click') {
  142. if(con.attr('lay-filter') !== undefined) {
  143. con.children('ul').find('li').each(function() {
  144. var $this = $(this);
  145. if ($this.find('dl').length > 0) {
  146. //var $dd =
  147. $this.find('dd').each(function() {
  148. $(this).on('click', function() {
  149. var $a = $(this).children('a');
  150. var href = $a.data('url');
  151. var icon = $a.children('i:first').data('icon');
  152. var title = $a.children('cite').text();
  153. var data = {
  154. elem: $a,
  155. field: {
  156. href: href,
  157. icon: icon,
  158. title: title
  159. }
  160. }
  161. callback(data);
  162. });
  163. });
  164. } else {
  165. $this.on('click', function() {
  166. var $a = $this.children('a');
  167. var href = $a.data('url');
  168. var icon = $a.children('i:first').data('icon');
  169. var title = $a.children('cite').text();
  170. var data = {
  171. elem: $a,
  172. field: {
  173. href: href,
  174. icon: icon,
  175. title: title
  176. }
  177. }
  178. callback(data);
  179. });
  180. }
  181. });
  182. }
  183. }
  184. };
  185. /**
  186. * 清除缓存
  187. */
  188. Navbar.prototype.cleanCached = function(){
  189. layui.data(cacheName,null);
  190. };
  191. /**
  192. * 获取html字符串
  193. * @param {Object} data
  194. */
  195. //function getHtml(data) {
  196. // var ulHtml = '<ul class="layui-nav layui-nav-tree beg-navbar">';
  197. // for(var i = 0; i < data.length; i++) {
  198. // if(data[i].spread) {
  199. // ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
  200. // } else {
  201. // ulHtml += '<li class="layui-nav-item">';
  202. // }
  203. // if(data[i].children !== undefined && data[i].children.length > 0) {
  204. // ulHtml += '<a href="javascript:;">';
  205. // if(data[i].icon !== undefined && data[i].icon !== '') {
  206. // if(data[i].icon.indexOf('fa-') !== -1) {
  207. // ulHtml += '<i class="fa ' + data[i].icon + '" aria-hidden="true" data-icon="' + data[i].icon + '"></i>';
  208. // } else {
  209. // ulHtml += '<i class="layui-icon" data-icon="' + data[i].icon + '">' + data[i].icon + '</i>';
  210. // }
  211. // }
  212. // ulHtml += '<cite>' + data[i].title + '</cite>';
  213. // ulHtml += '</a>';
  214. // ulHtml += '<dl class="layui-nav-child">';
  215. // for(var j = 0; j < data[i].children.length; j++) {
  216. // ulHtml += '<dd title="'+data[i].children[j].title+'">';
  217. // ulHtml += '<a href="javascript:;" data-url="' + data[i].children[j].href + '">';
  218. // if(data[i].children[j].icon !== undefined && data[i].children[j].icon !== '') {
  219. // if(data[i].children[j].icon.indexOf('fa-') !== -1) {
  220. // ulHtml += '<i class="fa ' + data[i].children[j].icon + '" data-icon="' + data[i].children[j].icon + '" aria-hidden="true"></i>';
  221. // } else {
  222. // ulHtml += '<i class="layui-icon" data-icon="' + data[i].children[j].icon + '">' + data[i].children[j].icon + '</i>';
  223. // }
  224. // }
  225. // ulHtml += '<cite>' + data[i].children[j].title + '</cite>';
  226. // ulHtml += '</a>';
  227. // ulHtml += '</dd>';
  228. // }
  229. // ulHtml += '</dl>';
  230. // } else {
  231. // var dataUrl = (data[i].href !== undefined && data[i].href !== '') ? 'data-url="' + data[i].href + '"' : '';
  232. // ulHtml += '<a href="javascript:;" ' + dataUrl + '>';
  233. // if(data[i].icon !== undefined && data[i].icon !== '') {
  234. // if(data[i].icon.indexOf('fa-') !== -1) {
  235. // ulHtml += '<i class="fa ' + data[i].icon + '" aria-hidden="true" data-icon="' + data[i].icon + '"></i>';
  236. // } else {
  237. // ulHtml += '<i class="layui-icon" data-icon="' + data[i].icon + '">' + data[i].icon + '</i>';
  238. // }
  239. // }
  240. // ulHtml += '<cite>' + data[i].title + '</cite>';
  241. // ulHtml += '</a>';
  242. // }
  243. // ulHtml += '</li>';
  244. // }
  245. // ulHtml += '</ul>';
  246. // return ulHtml;
  247. //}
  248. function getHtml(data) {
  249. var ulHtml = '<ul class="layui-nav layui-nav-tree beg-navbar">';
  250. for (var i = 0; i < data.length; i++) {
  251. if (data[i].spread) {
  252. ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
  253. } else {
  254. ulHtml += '<li class="layui-nav-item">';
  255. }
  256. if (data[i].children !== undefined && data[i].children.length > 0) {
  257. console.log(i+"---child");
  258. ulHtml += '<a href="javascript:;">';
  259. ulHtml += getIcon(data[i].icon);
  260. ulHtml += '<cite>' + data[i].title + '</cite>';
  261. ulHtml += '</a>';
  262. ulHtml += '<dl class="layui-nav-child">';
  263. ulHtml += getChildHtml(data[i].children);
  264. ulHtml += '</dl>';
  265. } else {
  266. var dataUrl = (data[i].href !== undefined && data[i].href !== '') ? 'data-url="' + data[i].href + '"' : '';
  267. ulHtml += '<a href="javascript:;" ' + dataUrl + '>';
  268. ulHtml += getIcon(data[i].icon);
  269. ulHtml += '<cite>' + data[i].title + '</cite>';
  270. ulHtml += '</a>';
  271. }
  272. ulHtml += '</li>';
  273. }
  274. ulHtml += '</ul>';
  275. console.log(ulHtml);
  276. return ulHtml;
  277. }
  278. function getChildHtml(data) {
  279. var ulHtml = "";
  280. for (var i = 0; i < data.length; i++) {
  281. if (data[i].children !== undefined && data[i].children.length > 0) {
  282. ulHtml += '<a href="javascript:;">';
  283. ulHtml += getIcon(data[i].icon);
  284. ulHtml += '<cite>' + data[i].title + '</cite>';
  285. ulHtml += '</a>';
  286. //ulHtml += '<dl class="layui-nav-child">';
  287. ulHtml += getChildHtml(data[i].children);
  288. //ulHtml += '</dl>';
  289. } else {
  290. ulHtml += '<dd title="' + data[i].title + '">';
  291. ulHtml += '<a href="javascript:;" data-url="' + data[i].href + '">';
  292. ulHtml += getIcon(data[i].icon);
  293. ulHtml += '<cite>' + data[i].title + '</cite>';
  294. ulHtml += '</a>';
  295. ulHtml += '</dd>';
  296. }
  297. }
  298. return ulHtml;
  299. }
  300. function getIcon(icon) {
  301. var ulHtml = "";
  302. if (icon !== undefined && icon !== ''&& icon!=null) {
  303. if (icon.indexOf('fa-') !== -1) {
  304. ulHtml = '<i class="fa ' + icon + '" aria-hidden="true" data-icon="' + icon + '"></i>';
  305. } else {
  306. ulHtml = '<i class="layui-icon" data-icon="' + icon + '">' + icon + '</i>';
  307. }
  308. }
  309. return ulHtml;
  310. }
  311. var navbar = new Navbar();
  312. exports('navbar', function(options) {
  313. return navbar.set(options);
  314. });
  315. });