9ccd9453ff367197f25e7a5a6d0dd54ded0bc807.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * layui 扩展侧边菜单栏组件
  3. * Autor:masterbeginner / Fhua
  4. * Date : 16-11-26
  5. */
  6. //配置navbar
  7. //navbar.set({ elem: '#admin-navbar-side', url: 'datas/nav.json' });
  8. //渲染navbar
  9. //navbar.render();
  10. //监听点击事件
  11. //navbar.on('click(side)', function(data) { });
  12. layui.define(['element', 'layer', 'common'], function (exports) {
  13. var $ = layui.jquery,
  14. layer = layui.layer,
  15. element = layui.element(),
  16. common = layui.common,
  17. cacheName = 'tb_navbar';
  18. var Navbar = function () {
  19. /**
  20. * 默认配置
  21. */
  22. this.config = {
  23. elem: undefined, //容器
  24. data: undefined, //数据源
  25. url: undefined, //数据源地址
  26. type: 'GET', //读取方式
  27. cached: false //是否使用缓存
  28. };
  29. this.v = '0.0.1';
  30. };
  31. Navbar.prototype.render = function () {
  32. var _that = this;
  33. var _config = _that.config;
  34. if (typeof (_config.elem) !== 'string' && typeof (_config.elem) !== 'object') {
  35. common.layerAlertE('Navbar error: elem参数未定义或设置出错.', '出错');
  36. }
  37. var $container;
  38. if (typeof (_config.elem) === 'string') {
  39. $container = $('' + _config.elem + '');
  40. }
  41. if (typeof (_config.elem) === 'object') {
  42. $container = _config.elem;
  43. }
  44. if ($container.length === 0) {
  45. common.layerAlertE('Navbar error:找不到elem参数配置的容器,请检查.', '出错');
  46. }
  47. if (_config.data === undefined && _config.url === undefined) {
  48. common.layerAlertE('Navbar error:请为Navbar配置数据源.', '出错')
  49. }
  50. if (_config.data !== undefined && typeof (_config.data) === 'object') {
  51. var html = getHtml(_config.data);
  52. $container.html(html);
  53. element.init();
  54. _that.config.elem = $container;
  55. } else {
  56. if (_config.cached) {
  57. var cacheNavbar = layui.data(cacheName);
  58. if (cacheNavbar.navbar === undefined) {
  59. $.ajax({
  60. type: _config.type,
  61. url: _config.url,
  62. async: false, //_config.async,
  63. dataType: 'json',
  64. success: function (result, status, xhr) {
  65. //添加缓存
  66. layui.data(cacheName, {
  67. key: 'navbar',
  68. value: result
  69. });
  70. var html = getHtml(result);
  71. $container.html(html);
  72. element.init();
  73. },
  74. error: function (xhr, status, error) {
  75. common.msgError('Navbar error:' + error);
  76. },
  77. complete: function (xhr, status) {
  78. _that.config.elem = $container;
  79. }
  80. });
  81. } else {
  82. var html = getHtml(cacheNavbar.navbar);
  83. $container.html(html);
  84. element.init();
  85. _that.config.elem = $container;
  86. }
  87. } else {
  88. //清空缓存
  89. layui.data(cacheName, null);
  90. $.ajax({
  91. type: _config.type,
  92. url: _config.url,
  93. async: false, //_config.async,
  94. dataType: 'json',
  95. success: function (result, status, xhr) {
  96. var html = getHtml(result);
  97. $container.html(html);
  98. element.init();
  99. },
  100. error: function (xhr, status, error) {
  101. common.msgError('Navbar error:' + error);
  102. },
  103. complete: function (xhr, status) {
  104. _that.config.elem = $container;
  105. }
  106. });
  107. }
  108. }
  109. return _that;
  110. };
  111. /**
  112. * 配置Navbar
  113. * @param {Object} options
  114. */
  115. Navbar.prototype.set = function (options) {
  116. var that = this;
  117. //采用递归方式合并两个对象,并修改第一个对象。
  118. $.extend(that.config, options);
  119. return that;
  120. };
  121. Navbar.prototype.on = function (events, callback) {
  122. var that = this;
  123. var _con = that.config.elem;
  124. if (typeof (events) !== 'string') {
  125. common.layerAlertE('Navbar error:事件名配置出错.', '出错');
  126. }
  127. var lIndex = events.indexOf('(');
  128. var eventName = events.substr(0, lIndex);
  129. var filter = events.substring(lIndex + 1, events.indexOf(')'));
  130. if (eventName === 'click') {
  131. if (_con.attr('lay-filter') !== undefined) {
  132. _con.children('ul').find('li').each(function () {
  133. var $this = $(this);
  134. if ($this.find('dl').length > 0) {
  135. var $dd = $this.find('dd').each(function () {
  136. $(this).on('click', function () {
  137. var $a = $(this).children('a');
  138. var href = $a.data('url');
  139. var icon = $a.children('i').attr('class');
  140. var title = $a.children('cite').text();
  141. var data = {
  142. elem: $a,
  143. field: {
  144. href: href,
  145. icon: icon,
  146. title: title
  147. }
  148. }
  149. callback(data);
  150. //$(this).parent('dl').parent('li').addClass('layui-this').siblings().removeClass('layui-this');
  151. });
  152. });
  153. } else {
  154. $this.on('click', function () {
  155. var $a = $this.children('a');
  156. var href = $a.data('url');
  157. var icon = $a.children('i').attr('class');
  158. var title = $a.children('cite').text();
  159. var data = {
  160. elem: $a,
  161. field: {
  162. href: href,
  163. icon: icon,
  164. title: title
  165. }
  166. }
  167. callback(data);
  168. //$this.addClass('layui-this').siblings().removeClass('layui-this');
  169. });
  170. }
  171. });
  172. }
  173. }
  174. };
  175. /**
  176. * 获取html字符串
  177. * @param {Object} data
  178. */
  179. function getHtml(data) {
  180. var ulHtml = '<div id="sidebar" class="sidebar-fold"><i class="fa fa-bars"></i></div><ul class="layui-nav layui-nav-tree admin-nav-tree">';
  181. for (var i = 0; i < data.length; i++) {
  182. if (data[i].spread) {
  183. ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
  184. } else {
  185. ulHtml += '<li class="layui-nav-item">';
  186. }
  187. if (data[i].children !== undefined && data[i].children.length > 0) {
  188. ulHtml += '<a href="javascript:;">';
  189. if (data[i].icon !== undefined && data[i].icon !== '') {
  190. if (data[i].icon.indexOf('fa-') !== -1) {
  191. ulHtml += '<i class="' + data[i].icon + '" aria-hidden="true"></i>';
  192. } else {
  193. ulHtml += '<i class="layui-icon">' + data[i].icon + '</i>';
  194. }
  195. }
  196. ulHtml += '<cite>' + data[i].title + '</cite>'
  197. ulHtml += '</a>';
  198. ulHtml += '<dl class="layui-nav-child">'
  199. for (var j = 0; j < data[i].children.length; j++) {
  200. ulHtml += '<dd>';
  201. ulHtml += '<a href="javascript:;" data-url="' + data[i].children[j].href + '">';
  202. if (data[i].children[j].icon !== undefined && data[i].children[j].icon !== '') {
  203. if (data[i].children[j].icon.indexOf('fa-') !== -1) {
  204. ulHtml += '<i class="' + data[i].children[j].icon + '" aria-hidden="true"></i>';
  205. } else {
  206. ulHtml += '<i class="layui-icon">' + data[i].children[j].icon + '</i>';
  207. }
  208. }
  209. ulHtml += '<cite>' + data[i].children[j].title + '</cite>';
  210. ulHtml += '</a>';
  211. ulHtml += '</dd>';
  212. }
  213. ulHtml += '</dl>';
  214. } else {
  215. var dataUrl = (data[i].href !== undefined && data[i].href !== '') ? 'data-url="' + data[i].href + '"' : '';
  216. ulHtml += '<a href="javascript:;" ' + dataUrl + '>';
  217. if (data[i].icon !== undefined && data[i].icon !== '') {
  218. if (data[i].icon.indexOf('fa-') !== -1) {
  219. ulHtml += '<i class="' + data[i].icon + '" aria-hidden="true"></i>';
  220. } else {
  221. ulHtml += '<i class="layui-icon">' + data[i].icon + '</i>';
  222. }
  223. }
  224. ulHtml += '<cite>' + data[i].title + '</cite>'
  225. ulHtml += '</a>';
  226. }
  227. ulHtml += '</li>';
  228. }
  229. ulHtml += '</ul>';
  230. return ulHtml;
  231. }
  232. var navbar = new Navbar();
  233. exports('navbar', function (options) {
  234. return navbar.set(options);
  235. });
  236. //exports('navbar', navbar);
  237. });