backend.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. define(['fast', 'moment', 'clipboard'], function (Fast, Moment, Clipboard) {
  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. },
  103. init: function () {
  104. //公共代码
  105. //添加ios-fix兼容iOS下的iframe
  106. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  107. $("html").addClass("ios-fix");
  108. }
  109. //配置Toastr的参数
  110. Toastr.options.positionClass = Config.controllername === 'index' ? "toast-top-right-index" : "toast-top-right";
  111. //点击包含.btn-dialog的元素时弹出dialog
  112. $(document).on('click', '.btn-dialog,.dialogit', function (e) {
  113. var that = this;
  114. var options = $.extend({}, $(that).data() || {});
  115. if (typeof options.tableId !== 'undefined' && typeof options.fieldIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  116. var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
  117. if (tableOptions) {
  118. var columnObj = null;
  119. $.each(tableOptions.columns, function (i, columns) {
  120. $.each(columns, function (j, column) {
  121. if (typeof column.fieldIndex !== 'undefined' && column.fieldIndex === options.fieldIndex) {
  122. columnObj = column;
  123. return false;
  124. }
  125. });
  126. if (columnObj) {
  127. return false;
  128. }
  129. });
  130. if (columnObj) {
  131. var button = columnObj['buttons'][options.buttonIndex];
  132. if (button && typeof button.callback === 'function') {
  133. options.callback = button.callback;
  134. }
  135. }
  136. }
  137. }
  138. if (typeof options.confirm !== 'undefined') {
  139. Layer.confirm(options.confirm, function (index) {
  140. Backend.api.open(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr('title'), options);
  141. Layer.close(index);
  142. });
  143. } else {
  144. Backend.api.open(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr('title'), options);
  145. }
  146. return false;
  147. });
  148. //点击包含.btn-addtabs的元素时新增选项卡
  149. $(document).on('click', '.btn-addtabs,.addtabsit', function (e) {
  150. var that = this;
  151. var options = $.extend({}, $(that).data() || {});
  152. if (typeof options.confirm !== 'undefined') {
  153. Layer.confirm(options.confirm, function (index) {
  154. Backend.api.addtabs(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr("title"));
  155. Layer.close(index);
  156. });
  157. } else {
  158. Backend.api.addtabs(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr("title"));
  159. }
  160. return false;
  161. });
  162. //点击包含.btn-ajax的元素时发送Ajax请求
  163. $(document).on('click', '.btn-ajax,.ajaxit', function (e) {
  164. var that = this;
  165. var options = $.extend({}, $(that).data() || {});
  166. if (typeof options.url === 'undefined' && $(that).attr("href")) {
  167. options.url = $(that).attr("href");
  168. }
  169. options.url = Backend.api.replaceids(this, options.url);
  170. var success = typeof options.success === 'function' ? options.success : null;
  171. var error = typeof options.error === 'function' ? options.error : null;
  172. delete options.success;
  173. delete options.error;
  174. if (typeof options.tableId !== 'undefined' && typeof options.fieldIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  175. var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
  176. if (tableOptions) {
  177. var columnObj = null;
  178. $.each(tableOptions.columns, function (i, columns) {
  179. $.each(columns, function (j, column) {
  180. if (typeof column.fieldIndex !== 'undefined' && column.fieldIndex === options.fieldIndex) {
  181. columnObj = column;
  182. return false;
  183. }
  184. });
  185. if (columnObj) {
  186. return false;
  187. }
  188. });
  189. if (columnObj) {
  190. var button = columnObj['buttons'][options.buttonIndex];
  191. if (button && typeof button.success === 'function') {
  192. success = button.success;
  193. }
  194. if (button && typeof button.error === 'function') {
  195. error = button.error;
  196. }
  197. }
  198. }
  199. }
  200. //如果未设备成功的回调,设定了自动刷新的情况下自动进行刷新
  201. if (!success && typeof options.tableId !== 'undefined' && typeof options.refresh !== 'undefined' && options.refresh) {
  202. $("#" + options.tableId).bootstrapTable('refresh');
  203. }
  204. if (typeof options.confirm !== 'undefined') {
  205. Layer.confirm(options.confirm, function (index) {
  206. Backend.api.ajax(options, success, error);
  207. Layer.close(index);
  208. });
  209. } else {
  210. Backend.api.ajax(options, success, error);
  211. }
  212. return false;
  213. });
  214. //修复含有fixed-footer类的body边距
  215. if ($(".fixed-footer").size() > 0) {
  216. $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
  217. }
  218. //修复不在iframe时layer-footer隐藏的问题
  219. if ($(".layer-footer").size() > 0 && self === top) {
  220. $(".layer-footer").show();
  221. }
  222. //检查是否有需要弹出的公告窗口
  223. if (self != top && Config.notice_id) {
  224. Fast.api.open('notice/show/ids/' + Config.notice_id, '公告', {});
  225. }
  226. // 复制功能
  227. var clipboard = new Clipboard('.copy');
  228. clipboard.on('success', function (e) {
  229. e.clearSelection();
  230. Toastr.success('复制成功!');
  231. });
  232. clipboard.on('error', function (e) {
  233. Toastr.error('复制失败!请手动复制!');
  234. });
  235. }
  236. };
  237. Backend.api = $.extend(Fast.api, Backend.api);
  238. //将Moment渲染至全局,以便于在子框架中调用
  239. window.Moment = Moment;
  240. //将Backend渲染至全局,以便于在子框架中调用
  241. window.Backend = Backend;
  242. Backend.init();
  243. return Backend;
  244. });