backend.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. define(['yzn'], function(Yzn) {
  2. var Backend = {
  3. api: {
  4. //暂时不支持顶部菜单模式
  5. sidebar: function (params) {
  6. colorArr = ['red', 'orange', 'green', 'cyan', 'black', 'gray', 'blue'];
  7. $colorNums = colorArr.length;
  8. badgeList = {};
  9. $.each(params, function (k, v) {
  10. $url = Yzn.api.fixurl(k);
  11. if ($.isArray(v)) {
  12. $nums = typeof v[0] !== 'undefined' ? v[0] : 0;
  13. $color = typeof v[1] !== 'undefined' ? v[1] : colorArr[(!isNaN($nums) ? $nums : $nums.length) % $colorNums];
  14. $class = typeof v[2] !== 'undefined' ? v[2] : 'layui-badge';
  15. } else {
  16. $nums = v;
  17. $color = colorArr[(!isNaN($nums) ? $nums : $nums.length) % $colorNums];
  18. $class = 'layui-badge';
  19. }
  20. //必须nums大于0才显示
  21. badgeList[$url] = $nums > 0 ? '<small class="' + $class + ' layui-bg-' + $color + '">' + $nums + '</small>' : '';
  22. });
  23. $.each(badgeList, function (k, v) {
  24. var anchor = top.window.$("#sideMenu a.site-demo-active[menu-url='" + k + "']");
  25. if (anchor) {
  26. top.window.$(".pull-right-container", anchor).replaceWith(v);
  27. }
  28. });
  29. },
  30. replaceids: function(elem, url) {
  31. //如果有需要替换ids的
  32. if (url.indexOf("{ids}") > -1) {
  33. var ids = 0;
  34. var tableId = $(elem).data("table");
  35. if (tableId && $("#" + tableId).length > 0) {
  36. var Table = require("table");
  37. ids = Table.api.selectedids(tableId).join(",");
  38. }
  39. url = url.replace(/\{ids\}/g, ids);
  40. }
  41. return url;
  42. },
  43. addtabs: function(url, title, id) {
  44. url = Yzn.api.fixurl(url);
  45. if (parent.pearAdmin) {
  46. parent.pearAdmin.addTab(id, title, url);
  47. } else if (window.top.pearAdmin) {
  48. window.top.pearAdmin.addTab(id, title, url);
  49. }
  50. },
  51. refreshmenu: function () {
  52. top.window.$(".pear-menu").trigger("refresh");
  53. },
  54. gettablecolumnbutton: function (options) {
  55. if (typeof options.table !== 'undefined' && typeof options.tableKey !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  56. var tableOptions = layui.table.getOptions(options.table);
  57. if (tableOptions) {
  58. var columnObj = null;
  59. $.each(tableOptions.cols, function (i, columns) {
  60. $.each(columns, function (j, column) {
  61. if (typeof column.key !== 'undefined' && column.key === options.tableKey) {
  62. columnObj = column;
  63. return false;
  64. }
  65. });
  66. if (columnObj) {
  67. return false;
  68. }
  69. });
  70. if (columnObj) {
  71. return columnObj['operat'][options.buttonIndex];
  72. }
  73. }
  74. }
  75. return null;
  76. },
  77. },
  78. init: function() {
  79. //配置Toastr的参数
  80. Toastr.options.positionClass = Config.controllername === 'index' ? "toast-top-right-index" : "toast-top-right";
  81. //点击包含.btn-dialog的元素时弹出dialog
  82. $(document).on('click', '.btn-dialog,.dialogit', function(e) {
  83. var that = this;
  84. var options = $.extend({}, $(that).data() || {});
  85. var url = Backend.api.replaceids(that, $(that).data("url") || $(that).attr('href'));
  86. var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
  87. var button = Backend.api.gettablecolumnbutton(options);
  88. if (button && typeof button.callback === 'function') {
  89. options.callback = button.callback;
  90. }
  91. if (typeof options.confirm !== 'undefined') {
  92. Layer.confirm(options.confirm, function(index) {
  93. Backend.api.open(url, title, options);
  94. Layer.close(index);
  95. });
  96. } else {
  97. window[$(that).data("window") || 'self'].Backend.api.open(url, title, options);
  98. }
  99. return false;
  100. });
  101. //点击包含.btn-addtabs的元素时新增选项卡
  102. $(document).on('click', '.btn-addtabs,.addtabsit', function(e) {
  103. var that = this;
  104. var options = $.extend({}, $(that).data() || {});
  105. var url = Backend.api.replaceids(that, $(that).data("url") || $(that).attr('href'));
  106. var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
  107. var id = $(that).data("menu-id") || Math.floor(new Date().valueOf() * Math.random());
  108. if (!$(this).attr('data-menu-id')) {
  109. $(this).attr('data-menu-id', id);
  110. }
  111. if (typeof options.confirm !== 'undefined') {
  112. Layer.confirm(options.confirm, function(index) {
  113. Backend.api.addtabs(url, title, id);
  114. Layer.close(index);
  115. });
  116. } else {
  117. Backend.api.addtabs(url, title, id);
  118. }
  119. return false;
  120. });
  121. //点击包含.btn-ajax的元素时发送Ajax请求
  122. $(document).on('click', '.btn-ajax,.ajaxit', function(e) {
  123. var that = this;
  124. var options = $.extend({}, $(that).data() || {});
  125. if (typeof options.url === 'undefined' && $(that).attr("href")) {
  126. options.url = $(that).attr("href");
  127. }
  128. options.url = Backend.api.replaceids(this, options.url);
  129. var success = typeof options.success === 'function' ? options.success : null;
  130. var error = typeof options.error === 'function' ? options.error : null;
  131. delete options.success;
  132. delete options.error;
  133. var button = Backend.api.gettablecolumnbutton(options);
  134. if (button) {
  135. if (typeof button.success === 'function') {
  136. success = button.success;
  137. }
  138. if (typeof button.error === 'function') {
  139. error = button.error;
  140. }
  141. }
  142. //如果未设备成功的回调,设定了自动刷新的情况下自动进行刷新
  143. if (!success && typeof options.table !== 'undefined' && typeof options.refresh !== 'undefined' && options.refresh) {
  144. success = function() {
  145. layui.table.reload(options.table);
  146. }
  147. }
  148. if (typeof options.confirm !== 'undefined') {
  149. Layer.confirm(options.confirm, function(index) {
  150. Backend.api.ajax(options, success, error);
  151. Layer.close(index);
  152. });
  153. } else {
  154. Backend.api.ajax(options, success, error);
  155. }
  156. return false;
  157. });
  158. $(document).on('click', '.btn-click,.clickit', function (e) {
  159. var that = this;
  160. var options = $.extend({}, $(that).data() || {});
  161. var row = {};
  162. if (typeof options.table !== 'undefined') {
  163. var index = parseInt(options.rowIndex);
  164. var data = layui.table.getData(options.table)
  165. row = typeof data[index] !== 'undefined' ? data[index] : {};
  166. }
  167. var button = Backend.api.gettablecolumnbutton(options);
  168. var click = typeof button.click === 'function' ? button.click : $.noop;
  169. if (typeof options.confirm !== 'undefined') {
  170. Layer.confirm(options.confirm, function (index) {
  171. click.apply(that, [options, row, button]);
  172. Layer.close(index);
  173. });
  174. } else {
  175. click.apply(that, [options, row, button]);
  176. }
  177. return false;
  178. });
  179. // 放大图片
  180. $('body').on('click', '[data-image]', function() {
  181. var title = $(this).attr('data-image'),
  182. src = $(this).attr('src'),
  183. alt = $(this).attr('alt');
  184. var photos = {
  185. "title": title,
  186. "id": Math.random(),
  187. "data": [{
  188. "alt": alt,
  189. "pid": Math.random(),
  190. "src": src,
  191. "thumb": src
  192. }]
  193. };
  194. Layer.photos({
  195. photos: photos,
  196. anim: 5
  197. });
  198. return false;
  199. });
  200. var tips_index = 0;
  201. $(document).on('mouseenter', '[lay-tips]', function() {
  202. var content = $(this).attr('lay-tips');
  203. var direction = $(this).attr('lay-tips-direction') || 'top';
  204. var bgColor = $(this).attr('lay-tips-bg');
  205. var tipsMap = {
  206. 'top': 1,
  207. 'right': 2,
  208. 'bottom': 3,
  209. 'left': 4
  210. };
  211. var tipsDirection = tipsMap[direction] || 1;
  212. var options = {
  213. time: 0
  214. };
  215. // 如果有背景色,使用数组格式
  216. if (bgColor) {
  217. options.tips = [tipsDirection, bgColor];
  218. } else {
  219. options.tips = tipsDirection;
  220. }
  221. tips_index = layer.tips(content, this, options);
  222. }).on('mouseleave', '[lay-tips]', function() {
  223. layer.close(tips_index);
  224. });
  225. //修复含有fixed-footer类的body边距
  226. if ($(".fixed-footer").length > 0) {
  227. $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
  228. }
  229. //修复不在iframe时layer-footer隐藏的问题
  230. if ($(".layer-footer").length > 0 && self === top) {
  231. $(".layer-footer").show();
  232. }
  233. }
  234. }
  235. Backend.api = $.extend(Yzn.api, Backend.api);
  236. //将Backend渲染至全局,以便于在子框架中调用
  237. window.Backend = Backend;
  238. Backend.init();
  239. return Backend;
  240. })