backend.js 12 KB

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