onelevel.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /** iwb_admin-v1.1.0 MIT License By http://www.iwbnet.com e-mail:yueyy@iwbnet.com */
  2. ;/**
  3. * Name:onelevel.js
  4. * Author:Van
  5. * E-mail:yueyy@iwbnet.com
  6. * Website:http://www.iwbnet.com
  7. * LICENSE:MIT
  8. */
  9. layui.define(['jquery', 'laytpl', 'element'], function (exports) {
  10. var $ = layui.jquery,
  11. _modName = 'oneLevel',
  12. _win = $(window),
  13. _doc = $(document),
  14. laytpl = layui.laytpl;
  15. var oneLevel = {
  16. v: '1.0.1',
  17. config: {
  18. elem: undefined,
  19. data: undefined,
  20. remote: {
  21. url: undefined,
  22. type: 'POST'
  23. },
  24. onClicked: undefined
  25. },
  26. set: function (options) {
  27. var that = this;
  28. $.extend(true, that.config, options);
  29. return that;
  30. },
  31. /**
  32. * 是否已设置了elem
  33. */
  34. hasElem: function () {
  35. var that = this,
  36. _config = that.config;
  37. if (_config.elem === undefined && _doc.find('ul[iwb-one-level]').length === 0 && $(_config.elem)) {
  38. //console.log('One-Level error:请配置One-Level容器.');
  39. //do something..
  40. return false;
  41. }
  42. return true;
  43. },
  44. /**
  45. * 获取容器的jq对象
  46. */
  47. getElem: function () {
  48. var _config = this.config;
  49. return (_config.elem !== undefined && $(_config.elem).length > 0) ? $(_config.elem) : _doc.find('ul[iwb-one-level]');
  50. },
  51. render: function () {
  52. var that = this,
  53. _config = that.config, //配置
  54. _remote = _config.remote, //远程参数配置
  55. _tpl = [
  56. '{{# layui.each(d,function(index, item){ }}',
  57. '<li class="layui-nav-item">',
  58. '<a href="javascript:;" data-title="{{item.title}}" data-icon="{{item.icon}}" data-id="{{item.id}}" >',
  59. '{{# if (item.icon.indexOf("fa-") !== -1) { }}',
  60. '<i class="fa {{item.icon}}" aria-hidden="true"></i>',
  61. '{{# } else { }}',
  62. '<i class="layui-icon">{{item.icon}}</i>',
  63. '{{# } }}',
  64. '<span> {{item.title}}</span>',
  65. '</a>',
  66. '</li>',
  67. '{{# }); }}',
  68. ], //模板
  69. _data = [];
  70. var navbarLoadIndex = layer.load(2);
  71. if (!that.hasElem())
  72. return that;
  73. var _elem = that.getElem();
  74. //本地数据优先
  75. if (_config.data !== undefined && _config.data.length > 0) {
  76. _data = _config.data;
  77. } else {
  78. var dataType = _remote.jsonp ? 'jsonp' : 'json';
  79. var options = {
  80. url: _remote.url,
  81. type: _remote.type,
  82. error: function (xhr, status, thrown) {
  83. layui.hint().error('One-Level error:AJAX请求出错.' + thrown);
  84. },
  85. success: function (res) {
  86. _data = res;
  87. }
  88. };
  89. $.extend(true, options, _remote.jsonp ? {
  90. dataType: 'jsonp',
  91. jsonp: 'callback',
  92. jsonpCallback: 'jsonpCallback'
  93. } : {
  94. dataType: 'json'
  95. });
  96. $.support.cors = true;
  97. $.ajax(options);
  98. }
  99. var tIndex = setInterval(function () {
  100. if (_data.length > 0)
  101. clearInterval(tIndex);
  102. //渲染模板
  103. laytpl(_tpl.join('')).render(_data, function (html) {
  104. _elem.html(html);
  105. layui.element.init();
  106. typeof _config.onClicked === 'function' && _elem.children('li.layui-nav-item').off('click').on('click', function () {
  107. var _a = $(this).children('a'),
  108. id = _a.data('id');
  109. _config.onClicked(id);
  110. });
  111. //关闭等待层
  112. navbarLoadIndex && layer.close(navbarLoadIndex);
  113. typeof _config.renderAfter === 'function' && _config.renderAfter(_elem);
  114. });
  115. }, 50);
  116. return that;
  117. }
  118. };
  119. exports('onelevel', oneLevel);
  120. });