backend.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. },
  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.columnIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  116. var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
  117. if (tableOptions) {
  118. var button = tableOptions.columns[0][options.columnIndex]['buttons'][options.buttonIndex];
  119. if (button && typeof button.callback === 'function') {
  120. options.callback = button.callback;
  121. }
  122. }
  123. }
  124. if (typeof options.confirm !== 'undefined') {
  125. Layer.confirm(options.confirm, function (index) {
  126. Backend.api.open(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr('title'), options);
  127. Layer.close(index);
  128. });
  129. } else {
  130. Backend.api.open(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr('title'), options);
  131. }
  132. return false;
  133. });
  134. //点击包含.btn-addtabs的元素时新增选项卡
  135. $(document).on('click', '.btn-addtabs,.addtabsit', function (e) {
  136. var that = this;
  137. var options = $.extend({}, $(that).data() || {});
  138. if (typeof options.confirm !== 'undefined') {
  139. Layer.confirm(options.confirm, function (index) {
  140. Backend.api.addtabs(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr("title"));
  141. Layer.close(index);
  142. });
  143. } else {
  144. Backend.api.addtabs(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr("title"));
  145. }
  146. return false;
  147. });
  148. //点击包含.btn-ajax的元素时发送Ajax请求
  149. $(document).on('click', '.btn-ajax,.ajaxit', function (e) {
  150. var that = this;
  151. var options = $.extend({}, $(that).data() || {});
  152. if (typeof options.url === 'undefined' && $(that).attr("href")) {
  153. options.url = $(that).attr("href");
  154. }
  155. options.url = Backend.api.replaceids(this, options.url);
  156. var success = typeof options.success === 'function' ? options.success : null;
  157. var error = typeof options.error === 'function' ? options.error : null;
  158. delete options.success;
  159. delete options.error;
  160. if (typeof options.tableId !== 'undefined' && typeof options.columnIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  161. var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
  162. if (tableOptions) {
  163. var button = tableOptions.columns[0][options.columnIndex]['buttons'][options.buttonIndex];
  164. if (button && typeof button.success === 'function') {
  165. success = button.success;
  166. }
  167. if (button && typeof button.error === 'function') {
  168. error = button.error;
  169. }
  170. }
  171. }
  172. //如果未设备成功的回调,设定了自动刷新的情况下自动进行刷新
  173. if (!success && typeof options.tableId !== 'undefined' && typeof options.refresh !== 'undefined' && options.refresh) {
  174. $("#" + options.tableId).bootstrapTable('refresh');
  175. }
  176. if (typeof options.confirm !== 'undefined') {
  177. Layer.confirm(options.confirm, function (index) {
  178. Backend.api.ajax(options, success, error);
  179. Layer.close(index);
  180. });
  181. } else {
  182. Backend.api.ajax(options, success, error);
  183. }
  184. return false;
  185. });
  186. //修复含有fixed-footer类的body边距
  187. if ($(".fixed-footer").size() > 0) {
  188. $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
  189. }
  190. //修复不在iframe时layer-footer隐藏的问题
  191. if ($(".layer-footer").size() > 0 && self === top) {
  192. $(".layer-footer").show();
  193. }
  194. }
  195. };
  196. Backend.api = $.extend(Fast.api, Backend.api);
  197. //将Moment渲染至全局,以便于在子框架中调用
  198. window.Moment = Moment;
  199. //将Backend渲染至全局,以便于在子框架中调用
  200. window.Backend = Backend;
  201. Backend.init();
  202. return Backend;
  203. });