backend.js 5.6 KB

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