index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. define(['jquery', 'bootstrap', 'backend', 'addtabs', 'adminlte', 'validator'], function ($, undefined, Backend, undefined, AdminLTE, undefined) {
  2. var Controller = {
  3. index: function () {
  4. //窗口大小改变,修正主窗体最小高度
  5. $(window).resize(function () {
  6. $(".tab-addtabs").css("height", $(".content-wrapper").height() + "px");
  7. });
  8. //双击重新加载页面
  9. $(document).on("dblclick", ".sidebar-menu li > a", function (e) {
  10. $("#tab_" + $(this).attr("addtabs") + " iframe").attr('src', function (i, val) {
  11. return val;
  12. });
  13. e.stopPropagation();
  14. });
  15. //读取FastAdmin的更新信息
  16. $.ajax({
  17. url: 'http://demo.fastadmin.net/index/index/news',
  18. type: 'post',
  19. dataType: 'jsonp',
  20. success: function (ret) {
  21. $(".notifications-menu > a span").text(ret.new > 0 ? ret.new : '');
  22. $(".notifications-menu .footer a").attr("href", ret.url);
  23. $.each(ret.newslist, function (i, j) {
  24. var item = '<li><a href="' + j.url + '" target="_blank"><i class="' + j.icon + '"></i> ' + j.title + '</a></li>';
  25. $(item).appendTo($(".notifications-menu ul.menu"));
  26. });
  27. }
  28. });
  29. //切换左侧sidebar显示隐藏
  30. $(document).on("click", ".sidebar-menu li > a", function (e) {
  31. $(".sidebar-menu li").removeClass("active");
  32. //当外部触发隐藏的a时,触发父辈a的事件
  33. if (!$(this).closest("ul").is(":visible")) {
  34. //如果不需要左侧的菜单栏联动可以注释下面一行即可
  35. $(this).closest("ul").prev().trigger("click");
  36. }
  37. var visible = $(this).next("ul").is(":visible");
  38. if (!visible) {
  39. $(this).parents("li").addClass("active");
  40. } else {
  41. }
  42. e.stopPropagation();
  43. });
  44. //清除缓存
  45. $(document).on('click', "[data-toggle='wipecache']", function () {
  46. $.ajax({
  47. url: 'ajax/wipecache',
  48. dataType: 'json',
  49. cache: false,
  50. success: function (ret) {
  51. if (ret.hasOwnProperty("code")) {
  52. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  53. if (ret.code === 1) {
  54. Toastr.success(msg ? msg : __('Wipe cache completed'));
  55. } else {
  56. Toastr.error(msg ? msg : __('Wipe cache failed'));
  57. }
  58. } else {
  59. Toastr.error(__('Unknown data format'));
  60. }
  61. }, error: function () {
  62. Toastr.error(__('Network error'));
  63. }
  64. });
  65. });
  66. //全屏事件
  67. $(document).on('click', "[data-toggle='fullscreen']", function () {
  68. var doc = document.documentElement;
  69. if ($(document.body).hasClass("full-screen")) {
  70. $(document.body).removeClass("full-screen");
  71. document.exitFullscreen ? document.exitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitExitFullscreen && document.webkitExitFullscreen();
  72. } else {
  73. $(document.body).addClass("full-screen");
  74. doc.requestFullscreen ? doc.requestFullscreen() : doc.mozRequestFullScreen ? doc.mozRequestFullScreen() : doc.webkitRequestFullscreen ? doc.webkitRequestFullscreen() : doc.msRequestFullscreen && doc.msRequestFullscreen();
  75. }
  76. });
  77. //绑定tabs事件
  78. $('#nav').addtabs({iframeHeight: "100%"});
  79. //修复iOS下iframe无法滚动的BUG
  80. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  81. $(".tab-addtabs").addClass("ios-iframe-fix");
  82. }
  83. if (location.hash.indexOf("#!") === 0) {
  84. var url = decodeURIComponent(location.hash.substring(2));
  85. //刷新页面后将左侧对应的LI展开
  86. $("ul.sidebar-menu a[href='" + url + "']").trigger("click");
  87. } else {
  88. $("ul.sidebar-menu li.active a").trigger("click");
  89. }
  90. /**
  91. * List of all the available skins
  92. *
  93. * @type Array
  94. */
  95. var my_skins = [
  96. "skin-blue",
  97. "skin-black",
  98. "skin-red",
  99. "skin-yellow",
  100. "skin-purple",
  101. "skin-green",
  102. "skin-blue-light",
  103. "skin-black-light",
  104. "skin-red-light",
  105. "skin-yellow-light",
  106. "skin-purple-light",
  107. "skin-green-light"
  108. ];
  109. setup();
  110. /**
  111. * Toggles layout classes
  112. *
  113. * @param String cls the layout class to toggle
  114. * @returns void
  115. */
  116. function change_layout(cls) {
  117. $("body").toggleClass(cls);
  118. AdminLTE.layout.fixSidebar();
  119. //Fix the problem with right sidebar and layout boxed
  120. if (cls == "layout-boxed")
  121. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  122. if ($('body').hasClass('fixed') && cls == 'fixed') {
  123. AdminLTE.pushMenu.expandOnHover();
  124. AdminLTE.layout.activate();
  125. }
  126. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  127. AdminLTE.controlSidebar._fix($(".control-sidebar"));
  128. }
  129. /**
  130. * Replaces the old skin with the new skin
  131. * @param String cls the new skin class
  132. * @returns Boolean false to prevent link's default action
  133. */
  134. function change_skin(cls) {
  135. $.each(my_skins, function (i) {
  136. $("body").removeClass(my_skins[i]);
  137. });
  138. $("body").addClass(cls);
  139. store('skin', cls);
  140. return false;
  141. }
  142. /**
  143. * Store a new settings in the browser
  144. *
  145. * @param String name Name of the setting
  146. * @param String val Value of the setting
  147. * @returns void
  148. */
  149. function store(name, val) {
  150. if (typeof (Storage) !== "undefined") {
  151. localStorage.setItem(name, val);
  152. } else {
  153. window.alert('Please use a modern browser to properly view this template!');
  154. }
  155. }
  156. /**
  157. * Get a prestored setting
  158. *
  159. * @param String name Name of of the setting
  160. * @returns String The value of the setting | null
  161. */
  162. function get(name) {
  163. if (typeof (Storage) !== "undefined") {
  164. return localStorage.getItem(name);
  165. } else {
  166. window.alert('Please use a modern browser to properly view this template!');
  167. }
  168. }
  169. /**
  170. * Retrieve default settings and apply them to the template
  171. *
  172. * @returns void
  173. */
  174. function setup() {
  175. var tmp = get('skin');
  176. if (tmp && $.inArray(tmp, my_skins))
  177. change_skin(tmp);
  178. // 皮肤切换
  179. $("[data-skin]").on('click', function (e) {
  180. if ($(this).hasClass('knob'))
  181. return;
  182. e.preventDefault();
  183. change_skin($(this).data('skin'));
  184. });
  185. // 布局切换
  186. $("[data-layout]").on('click', function () {
  187. change_layout($(this).data('layout'));
  188. });
  189. // 切换子菜单显示和菜单小图标的显示
  190. $("[data-menu]").on('click', function () {
  191. if ($(this).data("menu") == 'show-submenu') {
  192. $("ul.sidebar-menu").toggleClass("show-submenu");
  193. } else {
  194. $(".nav-addtabs").toggleClass("disable-top-badge");
  195. }
  196. });
  197. // 右侧控制栏切换
  198. $("[data-controlsidebar]").on('click', function () {
  199. change_layout($(this).data('controlsidebar'));
  200. var slide = !AdminLTE.options.controlSidebarOptions.slide;
  201. AdminLTE.options.controlSidebarOptions.slide = slide;
  202. if (!slide)
  203. $('.control-sidebar').removeClass('control-sidebar-open');
  204. });
  205. // 右侧控制栏背景切换
  206. $("[data-sidebarskin='toggle']").on('click', function () {
  207. var sidebar = $(".control-sidebar");
  208. if (sidebar.hasClass("control-sidebar-dark")) {
  209. sidebar.removeClass("control-sidebar-dark")
  210. sidebar.addClass("control-sidebar-light")
  211. } else {
  212. sidebar.removeClass("control-sidebar-light")
  213. sidebar.addClass("control-sidebar-dark")
  214. }
  215. });
  216. // 菜单栏展开或收起
  217. $("[data-enable='expandOnHover']").on('click', function () {
  218. $(this).attr('disabled', true);
  219. AdminLTE.pushMenu.expandOnHover();
  220. if (!$('body').hasClass('sidebar-collapse'))
  221. $("[data-layout='sidebar-collapse']").click();
  222. });
  223. // 重设选项
  224. if ($('body').hasClass('fixed')) {
  225. $("[data-layout='fixed']").attr('checked', 'checked');
  226. }
  227. if ($('body').hasClass('layout-boxed')) {
  228. $("[data-layout='layout-boxed']").attr('checked', 'checked');
  229. }
  230. if ($('body').hasClass('sidebar-collapse')) {
  231. $("[data-layout='sidebar-collapse']").attr('checked', 'checked');
  232. }
  233. if ($('ul.sidebar-menu').hasClass('show-submenu')) {
  234. $("[data-menu='show-submenu']").attr('checked', 'checked');
  235. }
  236. if ($('ul.nav-addtabs').hasClass('disable-top-badge')) {
  237. $("[data-menu='disable-top-badge']").attr('checked', 'checked');
  238. }
  239. }
  240. $(window).resize();
  241. },
  242. login: function () {
  243. $("#login-form").validator({
  244. timely: 2, theme: 'yellow_right_effect',
  245. fields: {
  246. username: "required",
  247. password: "required",
  248. },
  249. valid: function (form) {
  250. form.submit();
  251. }
  252. });
  253. $.ajax({
  254. url: 'ajax/dailybg',
  255. dataType: 'json',
  256. success: function (ret) {
  257. if (ret.code === 1) {
  258. $("body").css("background", "url(" + ret.data.url + ")");
  259. $("body").css("background-size", "cover");
  260. }
  261. }
  262. });
  263. }
  264. };
  265. return Controller;
  266. });