index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. define(['jquery', 'bootstrap', 'backend', 'addtabs', 'adminlte', 'form'], function ($, undefined, Backend, undefined, AdminLTE, Form) {
  2. var Controller = {
  3. index: function () {
  4. //双击重新加载页面
  5. $(document).on("dblclick", ".sidebar-menu li > a", function (e) {
  6. $("#con_" + $(this).attr("addtabs") + " iframe").attr('src', function (i, val) {
  7. return val;
  8. });
  9. e.stopPropagation();
  10. });
  11. //修复在移除窗口时下拉框不隐藏的BUG
  12. $(window).on("blur", function () {
  13. $("[data-toggle='dropdown']").parent().removeClass("open");
  14. if ($("body").hasClass("sidebar-open")) {
  15. $(".sidebar-toggle").trigger("click");
  16. }
  17. });
  18. //快捷搜索
  19. $(".menuresult").width($("form.sidebar-form > .input-group").width());
  20. var searchResult = $(".menuresult");
  21. $("form.sidebar-form").on("blur", "input[name=q]", function () {
  22. searchResult.addClass("hide");
  23. }).on("focus", "input[name=q]", function () {
  24. if ($("a", searchResult).size() > 0) {
  25. searchResult.removeClass("hide");
  26. }
  27. }).on("keyup", "input[name=q]", function () {
  28. searchResult.html('');
  29. var val = $(this).val();
  30. var html = [];
  31. if (val != '') {
  32. $("ul.sidebar-menu li a[addtabs]:not([href^='javascript:;'])").each(function () {
  33. if ($("span:first", this).text().indexOf(val) > -1 || $(this).attr("py").indexOf(val) > -1 || $(this).attr("pinyin").indexOf(val) > -1) {
  34. html.push('<a data-url="' + $(this).attr("href") + '" href="javascript:;">' + $("span:first", this).text() + '</a>');
  35. if (html.length >= 100) {
  36. return false;
  37. }
  38. }
  39. });
  40. }
  41. $(searchResult).append(html.join(""));
  42. if (html.length > 0) {
  43. searchResult.removeClass("hide");
  44. } else {
  45. searchResult.addClass("hide");
  46. }
  47. });
  48. //快捷搜索点击事件
  49. $("form.sidebar-form").on('mousedown click', '.menuresult a[data-url]', function () {
  50. Backend.api.addtabs($(this).data("url"));
  51. });
  52. //切换左侧sidebar显示隐藏
  53. $(document).on("click fa.event.toggleitem", ".sidebar-menu li > a", function (e) {
  54. var nextul = $(this).next("ul");
  55. if (nextul.length == 0 && (!$(this).parent("li").hasClass("treeview") || ($("body").hasClass("multiplenav") && $(this).parent().parent().hasClass("sidebar-menu")))) {
  56. $(".sidebar-menu li").removeClass("active");
  57. }
  58. //当外部触发隐藏的a时,触发父辈a的事件
  59. if (!$(this).closest("ul").is(":visible")) {
  60. //如果不需要左侧的菜单栏联动可以注释下面一行即可
  61. $(this).closest("ul").prev().trigger("click");
  62. }
  63. var visible = nextul.is(":visible");
  64. if (!visible) {
  65. $(this).parents("li").addClass("active");
  66. } else {
  67. }
  68. e.stopPropagation();
  69. });
  70. //清除缓存
  71. $(document).on('click', "ul.wipecache li a,a.wipecache", function () {
  72. $.ajax({
  73. url: 'ajax/wipecache',
  74. dataType: 'json',
  75. data: {type: $(this).data("type")},
  76. cache: false,
  77. success: function (ret) {
  78. if (ret.hasOwnProperty("code")) {
  79. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  80. if (ret.code === 1) {
  81. Toastr.success(msg ? msg : __('Wipe cache completed'));
  82. } else {
  83. Toastr.error(msg ? msg : __('Wipe cache failed'));
  84. }
  85. } else {
  86. Toastr.error(__('Unknown data format'));
  87. }
  88. }, error: function () {
  89. Toastr.error(__('Network error'));
  90. }
  91. });
  92. });
  93. //全屏事件
  94. $(document).on('click', "[data-toggle='fullscreen']", function () {
  95. var doc = document.documentElement;
  96. if ($(document.body).hasClass("full-screen")) {
  97. $(document.body).removeClass("full-screen");
  98. document.exitFullscreen ? document.exitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitExitFullscreen && document.webkitExitFullscreen();
  99. } else {
  100. $(document.body).addClass("full-screen");
  101. doc.requestFullscreen ? doc.requestFullscreen() : doc.mozRequestFullScreen ? doc.mozRequestFullScreen() : doc.webkitRequestFullscreen ? doc.webkitRequestFullscreen() : doc.msRequestFullscreen && doc.msRequestFullscreen();
  102. }
  103. });
  104. var multiplenav = $("#secondnav").size() > 0 ? true : false;
  105. var firstnav = $("#firstnav .nav-addtabs");
  106. var nav = multiplenav ? $("#secondnav .nav-addtabs") : firstnav;
  107. //刷新菜单事件
  108. $(document).on('refresh', '.sidebar-menu', function () {
  109. Fast.api.ajax({
  110. url: 'index/index',
  111. data: {action: 'refreshmenu'},
  112. loading: false
  113. }, function (data) {
  114. $(".sidebar-menu li:not([data-rel='external'])").remove();
  115. $(".sidebar-menu").prepend(data.menulist);
  116. if (multiplenav) {
  117. firstnav.html(data.navlist);
  118. }
  119. $("li[role='presentation'].active a", nav).trigger('click');
  120. return false;
  121. }, function () {
  122. return false;
  123. });
  124. });
  125. if (multiplenav) {
  126. firstnav.css("overflow", "inherit");
  127. //一级菜单自适应
  128. $(window).resize(function () {
  129. var siblingsWidth = 0;
  130. firstnav.siblings().each(function () {
  131. siblingsWidth += $(this).outerWidth();
  132. });
  133. firstnav.width(firstnav.parent().width() - siblingsWidth);
  134. firstnav.refreshAddtabs();
  135. });
  136. //点击顶部第一级菜单栏
  137. firstnav.on("click", "li a", function () {
  138. $("li", firstnav).removeClass("active");
  139. $(this).closest("li").addClass("active");
  140. $(".sidebar-menu > li[pid]").addClass("hidden");
  141. if ($(this).attr("url") == "javascript:;") {
  142. var sonlist = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']");
  143. sonlist.removeClass("hidden");
  144. var sidenav;
  145. var last_id = $(this).attr("last-id");
  146. if (last_id) {
  147. sidenav = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "'] a[addtabs='" + last_id + "']");
  148. } else {
  149. sidenav = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']:first > a");
  150. }
  151. if (sidenav) {
  152. sidenav.attr("href") != "javascript:;" && sidenav.trigger('click');
  153. }
  154. } else {
  155. }
  156. });
  157. var mobilenav = $(".mobilenav");
  158. $("#firstnav .nav-addtabs li a").each(function () {
  159. mobilenav.append($(this).clone().addClass("btn btn-app"));
  160. });
  161. //点击移动端一级菜单
  162. mobilenav.on("click", "a", function () {
  163. $("a", mobilenav).removeClass("active");
  164. $(this).addClass("active");
  165. $(".sidebar-menu > li[pid]").addClass("hidden");
  166. if ($(this).attr("url") == "javascript:;") {
  167. var sonlist = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']");
  168. sonlist.removeClass("hidden");
  169. }
  170. });
  171. //点击左侧菜单栏
  172. $(document).on('click', '.sidebar-menu li a[addtabs]', function (e) {
  173. var parents = $(this).parentsUntil("ul.sidebar-menu", "li");
  174. var top = parents[parents.length - 1];
  175. var pid = $(top).attr("pid");
  176. if (pid) {
  177. var obj = $("li a[addtabs=" + pid + "]", firstnav);
  178. var last_id = obj.attr("last-id");
  179. if (!last_id || last_id != pid) {
  180. obj.attr("last-id", $(this).attr("addtabs"));
  181. if (!obj.closest("li").hasClass("active")) {
  182. obj.trigger("click");
  183. }
  184. }
  185. mobilenav.find("a").removeClass("active");
  186. mobilenav.find("a[addtabs='" + pid + "']").addClass("active");
  187. }
  188. });
  189. }
  190. //这一行需要放在点击左侧链接事件之前
  191. var addtabs = Config.referer ? localStorage.getItem("addtabs") : null;
  192. //绑定tabs事件,如果需要点击强制刷新iframe,则请将iframeForceRefresh置为true,iframeForceRefreshTable只强制刷新表格
  193. nav.addtabs({iframeHeight: "100%", iframeForceRefresh: false, iframeForceRefreshTable: true, nav: nav});
  194. if ($("ul.sidebar-menu li.active a").size() > 0) {
  195. $("ul.sidebar-menu li.active a").trigger("click");
  196. } else {
  197. if (multiplenav) {
  198. $("li:first > a", firstnav).trigger("click");
  199. } else {
  200. $("ul.sidebar-menu li a[url!='javascript:;']:first").trigger("click");
  201. }
  202. }
  203. //如果是刷新操作则直接返回刷新前的页面
  204. if (Config.referer) {
  205. if (Config.referer === $(addtabs).attr("url")) {
  206. var active = $("ul.sidebar-menu li a[addtabs=" + $(addtabs).attr("addtabs") + "]");
  207. if (multiplenav && active.size() == 0) {
  208. active = $("ul li a[addtabs='" + $(addtabs).attr("addtabs") + "']");
  209. }
  210. if (active.size() > 0) {
  211. active.trigger("click");
  212. } else {
  213. $(addtabs).appendTo(document.body).addClass("hide").trigger("click");
  214. }
  215. } else {
  216. //刷新页面后跳到到刷新前的页面
  217. Backend.api.addtabs(Config.referer);
  218. }
  219. }
  220. var my_skins = [
  221. "skin-blue",
  222. "skin-black",
  223. "skin-red",
  224. "skin-yellow",
  225. "skin-purple",
  226. "skin-green",
  227. "skin-blue-light",
  228. "skin-black-light",
  229. "skin-red-light",
  230. "skin-yellow-light",
  231. "skin-purple-light",
  232. "skin-green-light",
  233. "skin-black-blue",
  234. "skin-black-purple",
  235. "skin-black-red",
  236. "skin-black-green",
  237. "skin-black-yellow",
  238. "skin-black-pink",
  239. ];
  240. setup();
  241. function change_layout(cls) {
  242. $("body").toggleClass(cls);
  243. AdminLTE.layout.fixSidebar();
  244. //Fix the problem with right sidebar and layout boxed
  245. if (cls == "layout-boxed")
  246. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  247. if ($('body').hasClass('fixed') && cls == 'fixed') {
  248. AdminLTE.pushMenu.expandOnHover();
  249. AdminLTE.layout.activate();
  250. }
  251. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  252. AdminLTE.controlSidebar._fix($(".control-sidebar"));
  253. }
  254. function change_skin(cls) {
  255. if (!$("body").hasClass(cls)) {
  256. $("body").removeClass(my_skins.join(' ')).addClass(cls);
  257. localStorage.setItem('skin', cls);
  258. var cssfile = Config.site.cdnurl + "/assets/css/skins/" + cls + ".css";
  259. $('head').append('<link rel="stylesheet" href="' + cssfile + '" type="text/css" />');
  260. }
  261. return false;
  262. }
  263. function setup() {
  264. var tmp = localStorage.getItem('skin');
  265. if (tmp && $.inArray(tmp, my_skins) != -1)
  266. change_skin(tmp);
  267. // 皮肤切换
  268. $("[data-skin]").on('click', function (e) {
  269. if ($(this).hasClass('knob'))
  270. return;
  271. e.preventDefault();
  272. change_skin($(this).data('skin'));
  273. });
  274. // 布局切换
  275. $("[data-layout]").on('click', function () {
  276. change_layout($(this).data('layout'));
  277. });
  278. // 切换子菜单显示和菜单小图标的显示
  279. $("[data-menu]").on('click', function () {
  280. if ($(this).data("menu") == 'show-submenu') {
  281. $("ul.sidebar-menu").toggleClass("show-submenu");
  282. } else {
  283. nav.toggleClass("disable-top-badge");
  284. }
  285. });
  286. // 右侧控制栏切换
  287. $("[data-controlsidebar]").on('click', function () {
  288. change_layout($(this).data('controlsidebar'));
  289. var slide = !AdminLTE.options.controlSidebarOptions.slide;
  290. AdminLTE.options.controlSidebarOptions.slide = slide;
  291. if (!slide)
  292. $('.control-sidebar').removeClass('control-sidebar-open');
  293. });
  294. // 右侧控制栏背景切换
  295. $("[data-sidebarskin='toggle']").on('click', function () {
  296. var sidebar = $(".control-sidebar");
  297. if (sidebar.hasClass("control-sidebar-dark")) {
  298. sidebar.removeClass("control-sidebar-dark")
  299. sidebar.addClass("control-sidebar-light")
  300. } else {
  301. sidebar.removeClass("control-sidebar-light")
  302. sidebar.addClass("control-sidebar-dark")
  303. }
  304. });
  305. // 菜单栏展开或收起
  306. $("[data-enable='expandOnHover']").on('click', function () {
  307. $(this).attr('disabled', true);
  308. AdminLTE.pushMenu.expandOnHover();
  309. if (!$('body').hasClass('sidebar-collapse'))
  310. $("[data-layout='sidebar-collapse']").click();
  311. });
  312. // 重设选项
  313. if ($('body').hasClass('fixed')) {
  314. $("[data-layout='fixed']").attr('checked', 'checked');
  315. }
  316. if ($('body').hasClass('layout-boxed')) {
  317. $("[data-layout='layout-boxed']").attr('checked', 'checked');
  318. }
  319. if ($('body').hasClass('sidebar-collapse')) {
  320. $("[data-layout='sidebar-collapse']").attr('checked', 'checked');
  321. }
  322. if ($('ul.sidebar-menu').hasClass('show-submenu')) {
  323. $("[data-menu='show-submenu']").attr('checked', 'checked');
  324. }
  325. if (nav.hasClass('disable-top-badge')) {
  326. $("[data-menu='disable-top-badge']").attr('checked', 'checked');
  327. }
  328. }
  329. $(window).resize();
  330. },
  331. login: function () {
  332. var lastlogin = localStorage.getItem("lastlogin");
  333. if (lastlogin) {
  334. lastlogin = JSON.parse(lastlogin);
  335. $("#profile-img").attr("src", Backend.api.cdnurl(lastlogin.avatar));
  336. $("#profile-name").val(lastlogin.username);
  337. }
  338. //让错误提示框居中
  339. Fast.config.toastr.positionClass = "toast-top-center";
  340. //本地验证未通过时提示
  341. $("#login-form").data("validator-options", {
  342. invalid: function (form, errors) {
  343. $.each(errors, function (i, j) {
  344. Toastr.error(j);
  345. });
  346. },
  347. target: '#errtips'
  348. });
  349. //为表单绑定事件
  350. Form.api.bindevent($("#login-form"), function (data) {
  351. localStorage.setItem("lastlogin", JSON.stringify({
  352. id: data.id,
  353. username: data.username,
  354. avatar: data.avatar
  355. }));
  356. location.href = Backend.api.fixurl(data.url);
  357. }, function (data) {
  358. $("input[name=captcha]").next(".input-group-addon").find("img").trigger("click");
  359. });
  360. }
  361. };
  362. return Controller;
  363. });