backend.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = $(".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. },
  66. init: function () {
  67. //公共代码
  68. //配置Toastr的参数
  69. Toastr.options.positionClass = Config.controllername === 'index' ? "toast-top-right-index" : "toast-top-right";
  70. //点击包含.btn-dialog的元素时弹出dialog
  71. $(document).on('click', '.btn-dialog,.dialogit', function (e) {
  72. e.preventDefault();
  73. var options = $(this).data();
  74. options = options ? options : {};
  75. Backend.api.open(Backend.api.fixurl($(this).attr('href')), $(this).attr('title'), options);
  76. });
  77. //点击包含.btn-addtabs的元素时事件
  78. $(document).on('click', '.btn-addtabs,.addtabsit', function (e) {
  79. e.preventDefault();
  80. Backend.api.addtabs($(this).attr("href"), $(this).attr("title"));
  81. });
  82. //点击包含.btn-ajax的元素时事件
  83. $(document).on('click', '.btn-ajax,.ajaxit', function (e) {
  84. e.preventDefault();
  85. var options = $(this).data();
  86. if (typeof options.url === 'undefined' && $(this).attr("href")) {
  87. options.url = $(this).attr("href");
  88. }
  89. Backend.api.ajax(options);
  90. });
  91. //修复含有fixed-footer类的body边距
  92. if ($(".fixed-footer").size() > 0) {
  93. $(document.body).css("padding-bottom", $(".fixed-footer").height());
  94. }
  95. }
  96. };
  97. Backend.api = $.extend(Fast.api, Backend.api);
  98. //将Moment渲染至全局,以便于在子框架中调用
  99. window.Moment = Moment;
  100. //将Backend渲染至全局,以便于在子框架中调用
  101. window.Backend = Backend;
  102. Backend.init();
  103. return Backend;
  104. });