index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. define(['jquery', 'bootstrap', 'backend', 'addtabs', 'adminlte', 'form'], function ($, undefined, Backend, undefined, AdminLTE, Form) {
  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. var s = document.createElement("script");
  17. s.type = "text/javascript";
  18. s.src = "https://hm.baidu.com/hm.js?58347d769d009bcf6074e9a0ab7ba05e";
  19. $("head").append(s);
  20. //读取FastAdmin的更新信息
  21. $.ajax({
  22. url: 'http://demo.fastadmin.net/index/index/news',
  23. type: 'post',
  24. dataType: 'jsonp',
  25. success: function (ret) {
  26. $(".notifications-menu > a span").text(ret.new > 0 ? ret.new : '');
  27. $(".notifications-menu .footer a").attr("href", ret.url);
  28. $.each(ret.newslist, function (i, j) {
  29. var item = '<li><a href="' + j.url + '" target="_blank"><i class="' + j.icon + '"></i> ' + j.title + '</a></li>';
  30. $(item).appendTo($(".notifications-menu ul.menu"));
  31. });
  32. }
  33. });
  34. //读取FastAdmin的Commits信息
  35. $.ajax({
  36. url: 'https://api.github.com/repos/karsonzhang/fastadmin/commits?state=open&per_page=10&page=1&sort=updated',
  37. type: 'get',
  38. dataType: 'jsonp',
  39. success: function (ret) {
  40. $(".github-commits > a span").text(ret.data.length);
  41. $(".github-commits .footer a").attr("href", "https://github.com/karsonzhang/fastadmin/commits/master");
  42. var dateDiff = function (hisTime, nowTime) {
  43. if (!arguments.length)
  44. return '';
  45. var arg = arguments,
  46. now = arg[1] ? arg[1] : new Date().getTime(),
  47. diffValue = now - arg[0],
  48. result = '',
  49. minute = 1000 * 60,
  50. hour = minute * 60,
  51. day = hour * 24,
  52. halfamonth = day * 15,
  53. month = day * 30,
  54. year = month * 12,
  55. _year = diffValue / year,
  56. _month = diffValue / month,
  57. _week = diffValue / (7 * day),
  58. _day = diffValue / day,
  59. _hour = diffValue / hour,
  60. _min = diffValue / minute;
  61. if (_year >= 1)
  62. result = parseInt(_year) + "年前";
  63. else if (_month >= 1)
  64. result = parseInt(_month) + "个月前";
  65. else if (_week >= 1)
  66. result = parseInt(_week) + "周前";
  67. else if (_day >= 1)
  68. result = parseInt(_day) + "天前";
  69. else if (_hour >= 1)
  70. result = parseInt(_hour) + "个小时前";
  71. else if (_min >= 1)
  72. result = parseInt(_min) + "分钟前";
  73. else
  74. result = "刚刚";
  75. return result;
  76. };
  77. $.each(ret.data, function (i, j) {
  78. var author = j.author ? j.author : {html_url: "https://github.com/karsonzhang", avatar_url: "/assets/img/avatar.png", login: "Anonymous"};
  79. var item = '<li><a href="' + j.html_url + '"><div class="pull-left"><img src="' + author.avatar_url + '" class="img-circle" alt="' + author.login + '"></div><h4>' + author.login + '<small><i class="fa fa-clock-o"></i> ' + dateDiff(new Date(j.commit.committer.date).getTime()) + '</small></h4><p>' + j.commit.message + '</p></a></li>';
  80. $(item).appendTo($(".github-commits ul.menu"));
  81. });
  82. }
  83. });
  84. //切换左侧sidebar显示隐藏
  85. $(document).on("click fa.event.toggleitem", ".sidebar-menu li > a", function (e) {
  86. $(".sidebar-menu li").removeClass("active");
  87. //当外部触发隐藏的a时,触发父辈a的事件
  88. if (!$(this).closest("ul").is(":visible")) {
  89. //如果不需要左侧的菜单栏联动可以注释下面一行即可
  90. $(this).closest("ul").prev().trigger("click");
  91. }
  92. var visible = $(this).next("ul").is(":visible");
  93. if (!visible) {
  94. $(this).parents("li").addClass("active");
  95. } else {
  96. }
  97. e.stopPropagation();
  98. });
  99. //清除缓存
  100. $(document).on('click', "[data-toggle='wipecache']", function () {
  101. $.ajax({
  102. url: 'ajax/wipecache',
  103. dataType: 'json',
  104. cache: false,
  105. success: function (ret) {
  106. if (ret.hasOwnProperty("code")) {
  107. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  108. if (ret.code === 1) {
  109. Toastr.success(msg ? msg : __('Wipe cache completed'));
  110. } else {
  111. Toastr.error(msg ? msg : __('Wipe cache failed'));
  112. }
  113. } else {
  114. Toastr.error(__('Unknown data format'));
  115. }
  116. }, error: function () {
  117. Toastr.error(__('Network error'));
  118. }
  119. });
  120. });
  121. //全屏事件
  122. $(document).on('click', "[data-toggle='fullscreen']", function () {
  123. var doc = document.documentElement;
  124. if ($(document.body).hasClass("full-screen")) {
  125. $(document.body).removeClass("full-screen");
  126. document.exitFullscreen ? document.exitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitExitFullscreen && document.webkitExitFullscreen();
  127. } else {
  128. $(document.body).addClass("full-screen");
  129. doc.requestFullscreen ? doc.requestFullscreen() : doc.mozRequestFullScreen ? doc.mozRequestFullScreen() : doc.webkitRequestFullscreen ? doc.webkitRequestFullscreen() : doc.msRequestFullscreen && doc.msRequestFullscreen();
  130. }
  131. });
  132. //绑定tabs事件
  133. $('#nav').addtabs({iframeHeight: "100%"});
  134. //修复iOS下iframe无法滚动的BUG
  135. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  136. $(".tab-addtabs").addClass("ios-iframe-fix");
  137. }
  138. if (Config.referer) {
  139. //刷新页面后跳到到刷新前的页面
  140. Backend.api.addtabs(Config.referer);
  141. } else {
  142. $("ul.sidebar-menu li.active a").trigger("click");
  143. }
  144. /**
  145. * List of all the available skins
  146. *
  147. * @type Array
  148. */
  149. var my_skins = [
  150. "skin-blue",
  151. "skin-black",
  152. "skin-red",
  153. "skin-yellow",
  154. "skin-purple",
  155. "skin-green",
  156. "skin-blue-light",
  157. "skin-black-light",
  158. "skin-red-light",
  159. "skin-yellow-light",
  160. "skin-purple-light",
  161. "skin-green-light"
  162. ];
  163. setup();
  164. /**
  165. * Toggles layout classes
  166. *
  167. * @param String cls the layout class to toggle
  168. * @returns void
  169. */
  170. function change_layout(cls) {
  171. $("body").toggleClass(cls);
  172. AdminLTE.layout.fixSidebar();
  173. //Fix the problem with right sidebar and layout boxed
  174. if (cls == "layout-boxed")
  175. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  176. if ($('body').hasClass('fixed') && cls == 'fixed') {
  177. AdminLTE.pushMenu.expandOnHover();
  178. AdminLTE.layout.activate();
  179. }
  180. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  181. AdminLTE.controlSidebar._fix($(".control-sidebar"));
  182. }
  183. /**
  184. * Replaces the old skin with the new skin
  185. * @param String cls the new skin class
  186. * @returns Boolean false to prevent link's default action
  187. */
  188. function change_skin(cls) {
  189. if (!$("body").hasClass(cls)) {
  190. $.each(my_skins, function (i) {
  191. $("body").removeClass(my_skins[i]);
  192. });
  193. $("body").addClass(cls);
  194. store('skin', cls);
  195. var cssfile = Backend.api.cdnurl("/assets/css/skins/" + cls + ".css");
  196. $('head').append('<link rel="stylesheet" href="' + cssfile + '" type="text/css" />');
  197. }
  198. return false;
  199. }
  200. /**
  201. * Store a new settings in the browser
  202. *
  203. * @param String name Name of the setting
  204. * @param String val Value of the setting
  205. * @returns void
  206. */
  207. function store(name, val) {
  208. if (typeof (Storage) !== "undefined") {
  209. localStorage.setItem(name, val);
  210. } else {
  211. window.alert('Please use a modern browser to properly view this template!');
  212. }
  213. }
  214. /**
  215. * Get a prestored setting
  216. *
  217. * @param String name Name of of the setting
  218. * @returns String The value of the setting | null
  219. */
  220. function get(name) {
  221. if (typeof (Storage) !== "undefined") {
  222. return localStorage.getItem(name);
  223. } else {
  224. window.alert('Please use a modern browser to properly view this template!');
  225. }
  226. }
  227. /**
  228. * Retrieve default settings and apply them to the template
  229. *
  230. * @returns void
  231. */
  232. function setup() {
  233. var tmp = get('skin');
  234. if (tmp && $.inArray(tmp, my_skins))
  235. change_skin(tmp);
  236. // 皮肤切换
  237. $("[data-skin]").on('click', function (e) {
  238. if ($(this).hasClass('knob'))
  239. return;
  240. e.preventDefault();
  241. change_skin($(this).data('skin'));
  242. });
  243. // 布局切换
  244. $("[data-layout]").on('click', function () {
  245. change_layout($(this).data('layout'));
  246. });
  247. // 切换子菜单显示和菜单小图标的显示
  248. $("[data-menu]").on('click', function () {
  249. if ($(this).data("menu") == 'show-submenu') {
  250. $("ul.sidebar-menu").toggleClass("show-submenu");
  251. } else {
  252. $(".nav-addtabs").toggleClass("disable-top-badge");
  253. }
  254. });
  255. // 右侧控制栏切换
  256. $("[data-controlsidebar]").on('click', function () {
  257. change_layout($(this).data('controlsidebar'));
  258. var slide = !AdminLTE.options.controlSidebarOptions.slide;
  259. AdminLTE.options.controlSidebarOptions.slide = slide;
  260. if (!slide)
  261. $('.control-sidebar').removeClass('control-sidebar-open');
  262. });
  263. // 右侧控制栏背景切换
  264. $("[data-sidebarskin='toggle']").on('click', function () {
  265. var sidebar = $(".control-sidebar");
  266. if (sidebar.hasClass("control-sidebar-dark")) {
  267. sidebar.removeClass("control-sidebar-dark")
  268. sidebar.addClass("control-sidebar-light")
  269. } else {
  270. sidebar.removeClass("control-sidebar-light")
  271. sidebar.addClass("control-sidebar-dark")
  272. }
  273. });
  274. // 菜单栏展开或收起
  275. $("[data-enable='expandOnHover']").on('click', function () {
  276. $(this).attr('disabled', true);
  277. AdminLTE.pushMenu.expandOnHover();
  278. if (!$('body').hasClass('sidebar-collapse'))
  279. $("[data-layout='sidebar-collapse']").click();
  280. });
  281. // 重设选项
  282. if ($('body').hasClass('fixed')) {
  283. $("[data-layout='fixed']").attr('checked', 'checked');
  284. }
  285. if ($('body').hasClass('layout-boxed')) {
  286. $("[data-layout='layout-boxed']").attr('checked', 'checked');
  287. }
  288. if ($('body').hasClass('sidebar-collapse')) {
  289. $("[data-layout='sidebar-collapse']").attr('checked', 'checked');
  290. }
  291. if ($('ul.sidebar-menu').hasClass('show-submenu')) {
  292. $("[data-menu='show-submenu']").attr('checked', 'checked');
  293. }
  294. if ($('ul.nav-addtabs').hasClass('disable-top-badge')) {
  295. $("[data-menu='disable-top-badge']").attr('checked', 'checked');
  296. }
  297. }
  298. $(window).resize();
  299. },
  300. login: function () {
  301. Form.api.bindevent($("#login-form"), null, function (data) {
  302. location.href = Backend.api.fixurl(data.url);
  303. });
  304. }
  305. };
  306. return Controller;
  307. });