backend.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. define(['fast', 'moment'], function (Fast, Moment) {
  2. var Backend = {
  3. api: {
  4. sidebar: function (params) {
  5. colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
  6. $colorNums = colorArr.length;
  7. badgeList = {};
  8. $.each(params, function (k, v) {
  9. $url = Fast.api.fixurl(k);
  10. if ($.isArray(v))
  11. {
  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] : 'label';
  15. } else
  16. {
  17. $nums = v;
  18. $color = colorArr[(!isNaN($nums) ? $nums : $nums.length) % $colorNums];
  19. $class = 'label';
  20. }
  21. //必须nums大于0才显示
  22. badgeList[$url] = $nums > 0 ? '<small class="' + $class + ' pull-right bg-' + $color + '">' + $nums + '</small>' : '';
  23. });
  24. $.each(badgeList, function (k, v) {
  25. var anchor = top.window.$("li a[addtabs][url='" + k + "']");
  26. if (anchor) {
  27. top.window.$(".pull-right-container", anchor).html(v);
  28. top.window.$(".nav-addtabs li a[node-id='" + anchor.attr("addtabs") + "'] .pull-right-container").html(v);
  29. }
  30. });
  31. },
  32. addtabs: function (url, title, icon) {
  33. var dom = "a[url='{url}']"
  34. var leftlink = top.window.$(dom.replace(/\{url\}/, url));
  35. if (leftlink.size() > 0) {
  36. leftlink.trigger("click");
  37. } else {
  38. url = Fast.api.fixurl(url);
  39. leftlink = top.window.$(dom.replace(/\{url\}/, url));
  40. if (leftlink.size() > 0) {
  41. var event = leftlink.parent().hasClass("active") ? "dblclick" : "click";
  42. leftlink.trigger(event);
  43. } else {
  44. var baseurl = url.substr(0, url.indexOf("?") > -1 ? url.indexOf("?") : url.length);
  45. leftlink = top.window.$(dom.replace(/\{url\}/, baseurl));
  46. //能找到相对地址
  47. if (leftlink.size() > 0) {
  48. icon = typeof icon !== 'undefined' ? icon : leftlink.find("i").attr("class");
  49. title = typeof title !== 'undefined' ? title : leftlink.find("span:first").text();
  50. leftlink.trigger("fa.event.toggleitem");
  51. }
  52. var navnode = top.window.$(".nav-tabs ul li a[node-url='" + url + "']");
  53. if (navnode.size() > 0) {
  54. navnode.trigger("click");
  55. } else {
  56. //追加新的tab
  57. var id = Math.floor(new Date().valueOf() * Math.random());
  58. icon = typeof icon !== 'undefined' ? icon : 'fa fa-circle-o';
  59. title = typeof title !== 'undefined' ? title : '';
  60. top.window.$("<a />").append('<i class="' + icon + '"></i> <span>' + title + '</span>').prop("href", url).attr({url: url, addtabs: id}).addClass("hide").appendTo(top.window.document.body).trigger("click");
  61. }
  62. }
  63. }
  64. },
  65. closetabs: function (url) {
  66. if (typeof url === 'undefined') {
  67. top.window.$("ul.nav-addtabs li.active .close-tab").trigger("click");
  68. } else {
  69. var dom = "a[url='{url}']"
  70. var navlink = top.window.$(dom.replace(/\{url\}/, url));
  71. if (navlink.size() === 0) {
  72. url = Fast.api.fixurl(url);
  73. navlink = top.window.$(dom.replace(/\{url\}/, url));
  74. if (navlink.size() === 0) {
  75. } else {
  76. var baseurl = url.substr(0, url.indexOf("?") > -1 ? url.indexOf("?") : url.length);
  77. navlink = top.window.$(dom.replace(/\{url\}/, baseurl));
  78. //能找到相对地址
  79. if (navlink.size() === 0) {
  80. navlink = top.window.$(".nav-tabs ul li a[node-url='" + url + "']");
  81. }
  82. }
  83. }
  84. if (navlink.size() > 0 && navlink.attr('addtabs')) {
  85. top.window.$("ul.nav-addtabs li#tab_" + navlink.attr('addtabs') + " .close-tab").trigger("click");
  86. }
  87. }
  88. },
  89. replaceids: function (elem, url) {
  90. //如果有需要替换ids的
  91. if (url.indexOf("{ids}") > -1) {
  92. var ids = 0;
  93. var tableId = $(elem).data("table-id");
  94. if (tableId && $(tableId).size() > 0 && $(tableId).data("bootstrap.table")) {
  95. var Table = require("table");
  96. ids = Table.api.selectedids($(tableId)).join(",");
  97. }
  98. url = url.replace(/\{ids\}/g, ids);
  99. }
  100. return url;
  101. },
  102. refreshmenu: function () {
  103. top.window.$(".sidebar-menu").trigger("refresh");
  104. }
  105. },
  106. init: function () {
  107. //公共代码
  108. //添加ios-fix兼容iOS下的iframe
  109. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  110. $("html").addClass("ios-fix");
  111. }
  112. //配置Toastr的参数
  113. Toastr.options.positionClass = Config.controllername === 'index' ? "toast-top-right-index" : "toast-top-right";
  114. //点击包含.btn-dialog的元素时弹出dialog
  115. $(document).on('click', '.btn-dialog,.dialogit', function (e) {
  116. var that = this;
  117. var options = $.extend({}, $(that).data() || {});
  118. if (typeof options.tableId !== 'undefined' && typeof options.fieldIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  119. var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
  120. if (tableOptions) {
  121. var columnObj = null;
  122. $.each(tableOptions.columns, function (i, columns) {
  123. $.each(columns, function (j, column) {
  124. if (typeof column.fieldIndex !== 'undefined' && column.fieldIndex === options.fieldIndex) {
  125. columnObj = column;
  126. return false;
  127. }
  128. });
  129. if (columnObj) {
  130. return false;
  131. }
  132. });
  133. if (columnObj) {
  134. var button = columnObj['buttons'][options.buttonIndex];
  135. if (button && typeof button.callback === 'function') {
  136. options.callback = button.callback;
  137. }
  138. }
  139. }
  140. }
  141. if (typeof options.confirm !== 'undefined') {
  142. Layer.confirm(options.confirm, function (index) {
  143. Backend.api.open(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr('title'), options);
  144. Layer.close(index);
  145. });
  146. } else {
  147. Backend.api.open(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr('title'), options);
  148. }
  149. return false;
  150. });
  151. //点击包含.btn-addtabs的元素时新增选项卡
  152. $(document).on('click', '.btn-addtabs,.addtabsit', function (e) {
  153. var that = this;
  154. var options = $.extend({}, $(that).data() || {});
  155. if (typeof options.confirm !== 'undefined') {
  156. Layer.confirm(options.confirm, function (index) {
  157. Backend.api.addtabs(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr("title"));
  158. Layer.close(index);
  159. });
  160. } else {
  161. Backend.api.addtabs(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr("title"));
  162. }
  163. return false;
  164. });
  165. //点击包含.btn-ajax的元素时发送Ajax请求
  166. $(document).on('click', '.btn-ajax,.ajaxit', function (e) {
  167. var that = this;
  168. var options = $.extend({}, $(that).data() || {});
  169. if (typeof options.url === 'undefined' && $(that).attr("href")) {
  170. options.url = $(that).attr("href");
  171. }
  172. options.url = Backend.api.replaceids(this, options.url);
  173. var success = typeof options.success === 'function' ? options.success : null;
  174. var error = typeof options.error === 'function' ? options.error : null;
  175. delete options.success;
  176. delete options.error;
  177. if (typeof options.tableId !== 'undefined' && typeof options.fieldIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  178. var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
  179. if (tableOptions) {
  180. var columnObj = null;
  181. $.each(tableOptions.columns, function (i, columns) {
  182. $.each(columns, function (j, column) {
  183. if (typeof column.fieldIndex !== 'undefined' && column.fieldIndex === options.fieldIndex) {
  184. columnObj = column;
  185. return false;
  186. }
  187. });
  188. if (columnObj) {
  189. return false;
  190. }
  191. });
  192. if (columnObj) {
  193. var button = columnObj['buttons'][options.buttonIndex];
  194. if (button && typeof button.success === 'function') {
  195. success = button.success;
  196. }
  197. if (button && typeof button.error === 'function') {
  198. error = button.error;
  199. }
  200. }
  201. }
  202. }
  203. //如果未设备成功的回调,设定了自动刷新的情况下自动进行刷新
  204. if (!success && typeof options.tableId !== 'undefined' && typeof options.refresh !== 'undefined' && options.refresh) {
  205. $("#" + options.tableId).bootstrapTable('refresh');
  206. }
  207. if (typeof options.confirm !== 'undefined') {
  208. Layer.confirm(options.confirm, function (index) {
  209. Backend.api.ajax(options, success, error);
  210. Layer.close(index);
  211. });
  212. } else {
  213. Backend.api.ajax(options, success, error);
  214. }
  215. return false;
  216. });
  217. //修复含有fixed-footer类的body边距
  218. if ($(".fixed-footer").size() > 0) {
  219. $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
  220. }
  221. //修复不在iframe时layer-footer隐藏的问题
  222. if ($(".layer-footer").size() > 0 && self === top) {
  223. $(".layer-footer").show();
  224. }
  225. }
  226. };
  227. Backend.api = $.extend(Fast.api, Backend.api);
  228. //将Moment渲染至全局,以便于在子框架中调用
  229. window.Moment = Moment;
  230. //将Backend渲染至全局,以便于在子框架中调用
  231. window.Backend = Backend;
  232. Backend.init();
  233. return Backend;
  234. });