backend.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.length > 0) {
  34. leftlink.trigger("click");
  35. } else {
  36. url = Fast.api.fixurl(url);
  37. leftlink = top.window.$(dom.replace(/\{url\}/, url));
  38. if (leftlink.length > 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.length > 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.length > 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.length === 0) {
  73. url = Fast.api.fixurl(url);
  74. navlink = top.window.$(dom.replace(/\{url\}/, url));
  75. if (navlink.length === 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.length === 0) {
  81. navlink = top.window.$(".nav-tabs ul li a[node-url='" + url + "']");
  82. }
  83. }
  84. }
  85. if (navlink.length > 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).length > 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. gettablecolumnbutton: function (options) {
  107. if (typeof options.tableId !== 'undefined' && typeof options.fieldIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
  108. var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
  109. if (tableOptions) {
  110. var columnObj = null;
  111. $.each(tableOptions.columns, function (i, columns) {
  112. $.each(columns, function (j, column) {
  113. if (typeof column.fieldIndex !== 'undefined' && column.fieldIndex === options.fieldIndex) {
  114. columnObj = column;
  115. return false;
  116. }
  117. });
  118. if (columnObj) {
  119. return false;
  120. }
  121. });
  122. if (columnObj) {
  123. return columnObj['buttons'][options.buttonIndex];
  124. }
  125. }
  126. }
  127. return null;
  128. },
  129. },
  130. init: function () {
  131. //公共代码
  132. //添加ios-fix兼容iOS下的iframe
  133. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  134. $("html").addClass("ios-fix");
  135. }
  136. //配置Toastr的参数
  137. Toastr.options.positionClass = Config.controllername === 'index' ? "toast-top-right-index" : "toast-top-right";
  138. //点击包含.btn-dialog的元素时弹出dialog
  139. $(document).on('click', '.btn-dialog,.dialogit', function (e) {
  140. var that = this;
  141. var options = $.extend({}, $(that).data() || {});
  142. var url = Backend.api.replaceids(that, $(that).data("url") || $(that).attr('href'));
  143. var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
  144. var button = Backend.api.gettablecolumnbutton(options);
  145. if (button && typeof button.callback === 'function') {
  146. options.callback = button.callback;
  147. }
  148. if (typeof options.confirm !== 'undefined') {
  149. Layer.confirm(options.confirm, function (index) {
  150. Backend.api.open(url, title, options);
  151. Layer.close(index);
  152. });
  153. } else {
  154. window[$(that).data("window") || 'self'].Backend.api.open(url, title, options);
  155. }
  156. return false;
  157. });
  158. //点击包含.btn-addtabs的元素时新增选项卡
  159. $(document).on('click', '.btn-addtabs,.addtabsit', function (e) {
  160. var that = this;
  161. var options = $.extend({}, $(that).data() || {});
  162. var url = Backend.api.replaceids(that, $(that).data("url") || $(that).attr('href'));
  163. var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
  164. var icon = $(that).attr("icon") || $(that).data("icon");
  165. if (typeof options.confirm !== 'undefined') {
  166. Layer.confirm(options.confirm, function (index) {
  167. Backend.api.addtabs(url, title, icon);
  168. Layer.close(index);
  169. });
  170. } else {
  171. Backend.api.addtabs(url, title, icon);
  172. }
  173. return false;
  174. });
  175. //点击包含.btn-ajax的元素时发送Ajax请求
  176. $(document).on('click', '.btn-ajax,.ajaxit', function (e) {
  177. var that = this;
  178. var options = $.extend({}, $(that).data() || {});
  179. if (typeof options.url === 'undefined' && $(that).attr("href")) {
  180. options.url = $(that).attr("href");
  181. }
  182. options.url = Backend.api.replaceids(this, options.url);
  183. var success = typeof options.success === 'function' ? options.success : null;
  184. var error = typeof options.error === 'function' ? options.error : null;
  185. delete options.success;
  186. delete options.error;
  187. var button = Backend.api.gettablecolumnbutton(options);
  188. if (button) {
  189. if (typeof button.success === 'function') {
  190. success = button.success;
  191. }
  192. if (typeof button.error === 'function') {
  193. error = button.error;
  194. }
  195. }
  196. //如果未设备成功的回调,设定了自动刷新的情况下自动进行刷新
  197. if (!success && typeof options.tableId !== 'undefined' && typeof options.refresh !== 'undefined' && options.refresh) {
  198. success = function () {
  199. $("#" + options.tableId).bootstrapTable('refresh');
  200. }
  201. }
  202. if (typeof options.confirm !== 'undefined') {
  203. Layer.confirm(options.confirm, function (index) {
  204. Backend.api.ajax(options, success, error);
  205. Layer.close(index);
  206. });
  207. } else {
  208. Backend.api.ajax(options, success, error);
  209. }
  210. return false;
  211. });
  212. $(document).on('click', '.btn-click,.clickit', function (e) {
  213. var that = this;
  214. var options = $.extend({}, $(that).data() || {});
  215. var row = {};
  216. if (typeof options.tableId !== 'undefined') {
  217. var index = parseInt(options.rowIndex);
  218. var data = $("#" + options.tableId).bootstrapTable('getData');
  219. row = typeof data[index] !== 'undefined' ? data[index] : {};
  220. }
  221. var button = Backend.api.gettablecolumnbutton(options);
  222. var click = typeof button.click === 'function' ? button.click : $.noop;
  223. if (typeof options.confirm !== 'undefined') {
  224. Layer.confirm(options.confirm, function (index) {
  225. click.apply(that, [options, row, button]);
  226. Layer.close(index);
  227. });
  228. } else {
  229. click.apply(that, [options, row, button]);
  230. }
  231. return false;
  232. });
  233. //修复含有fixed-footer类的body边距
  234. if ($(".fixed-footer").length > 0) {
  235. $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
  236. }
  237. //修复不在iframe时layer-footer隐藏的问题
  238. if ($(".layer-footer").length > 0 && self === top) {
  239. $(".layer-footer").show();
  240. }
  241. //tooltip和popover
  242. if (!('ontouchstart' in document.documentElement)) {
  243. $('body').tooltip({selector: '[data-toggle="tooltip"]', trigger: 'hover'});
  244. }
  245. $('body').popover({selector: '[data-toggle="popover"]'});
  246. }
  247. };
  248. Backend.api = $.extend(Fast.api, Backend.api);
  249. //将Template渲染至全局,以便于在子框架中调用
  250. window.Template = Template;
  251. //将Moment渲染至全局,以便于在子框架中调用
  252. window.Moment = Moment;
  253. //将Backend渲染至全局,以便于在子框架中调用
  254. window.Backend = Backend;
  255. Backend.init();
  256. return Backend;
  257. });