index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. var shortcut = localStorage.getItem("shortcut");
  133. shortcut = shortcut ? JSON.parse(shortcut) : {};
  134. $.each(shortcut, function (i, j) {
  135. $("select.fastmenujump").append("<option value='" + i + "'>" + j + "</option>");
  136. });
  137. $(document).on("change", "select.fastmenujump", function () {
  138. if (!$(this).val())
  139. return;
  140. Backend.api.addtabs($(this).val(), $("option:selected", this).text());
  141. });
  142. //绑定tabs事件
  143. $('#nav').addtabs({iframeHeight: "100%"});
  144. //修复iOS下iframe无法滚动的BUG
  145. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  146. $(".tab-addtabs").addClass("ios-iframe-fix");
  147. }
  148. if (Config.referer) {
  149. //刷新页面后跳到到刷新前的页面
  150. Backend.api.addtabs(Config.referer);
  151. } else {
  152. $("ul.sidebar-menu li.active a").trigger("click");
  153. }
  154. /**
  155. * List of all the available skins
  156. *
  157. * @type Array
  158. */
  159. var my_skins = [
  160. "skin-blue",
  161. "skin-black",
  162. "skin-red",
  163. "skin-yellow",
  164. "skin-purple",
  165. "skin-green",
  166. "skin-blue-light",
  167. "skin-black-light",
  168. "skin-red-light",
  169. "skin-yellow-light",
  170. "skin-purple-light",
  171. "skin-green-light"
  172. ];
  173. setup();
  174. /**
  175. * Toggles layout classes
  176. *
  177. * @param String cls the layout class to toggle
  178. * @returns void
  179. */
  180. function change_layout(cls) {
  181. $("body").toggleClass(cls);
  182. AdminLTE.layout.fixSidebar();
  183. //Fix the problem with right sidebar and layout boxed
  184. if (cls == "layout-boxed")
  185. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  186. if ($('body').hasClass('fixed') && cls == 'fixed' && false) {
  187. AdminLTE.pushMenu.expandOnHover();
  188. AdminLTE.layout.activate();
  189. }
  190. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  191. AdminLTE.controlSidebar._fix($(".control-sidebar"));
  192. }
  193. /**
  194. * Replaces the old skin with the new skin
  195. * @param String cls the new skin class
  196. * @returns Boolean false to prevent link's default action
  197. */
  198. function change_skin(cls) {
  199. if (!$("body").hasClass(cls)) {
  200. $.each(my_skins, function (i) {
  201. $("body").removeClass(my_skins[i]);
  202. });
  203. $("body").addClass(cls);
  204. store('skin', cls);
  205. var cssfile = Backend.api.cdnurl("/assets/css/skins/" + cls + ".css");
  206. $('head').append('<link rel="stylesheet" href="' + cssfile + '" type="text/css" />');
  207. }
  208. return false;
  209. }
  210. /**
  211. * Store a new settings in the browser
  212. *
  213. * @param String name Name of the setting
  214. * @param String val Value of the setting
  215. * @returns void
  216. */
  217. function store(name, val) {
  218. if (typeof (Storage) !== "undefined") {
  219. localStorage.setItem(name, val);
  220. } else {
  221. window.alert('Please use a modern browser to properly view this template!');
  222. }
  223. }
  224. /**
  225. * Get a prestored setting
  226. *
  227. * @param String name Name of of the setting
  228. * @returns String The value of the setting | null
  229. */
  230. function get(name) {
  231. if (typeof (Storage) !== "undefined") {
  232. return localStorage.getItem(name);
  233. } else {
  234. window.alert('Please use a modern browser to properly view this template!');
  235. }
  236. }
  237. /**
  238. * Retrieve default settings and apply them to the template
  239. *
  240. * @returns void
  241. */
  242. function setup() {
  243. var tmp = get('skin');
  244. if (tmp && $.inArray(tmp, my_skins))
  245. change_skin(tmp);
  246. // 皮肤切换
  247. $("[data-skin]").on('click', function (e) {
  248. if ($(this).hasClass('knob'))
  249. return;
  250. e.preventDefault();
  251. change_skin($(this).data('skin'));
  252. });
  253. // 布局切换
  254. $("[data-layout]").on('click', function () {
  255. change_layout($(this).data('layout'));
  256. });
  257. // 切换子菜单显示和菜单小图标的显示
  258. $("[data-menu]").on('click', function () {
  259. if ($(this).data("menu") == 'show-submenu') {
  260. $("ul.sidebar-menu").toggleClass("show-submenu");
  261. } else {
  262. $(".nav-addtabs").toggleClass("disable-top-badge");
  263. }
  264. });
  265. // 右侧控制栏切换
  266. $("[data-controlsidebar]").on('click', function () {
  267. change_layout($(this).data('controlsidebar'));
  268. var slide = !AdminLTE.options.controlSidebarOptions.slide;
  269. AdminLTE.options.controlSidebarOptions.slide = slide;
  270. if (!slide)
  271. $('.control-sidebar').removeClass('control-sidebar-open');
  272. });
  273. // 右侧控制栏背景切换
  274. $("[data-sidebarskin='toggle']").on('click', function () {
  275. var sidebar = $(".control-sidebar");
  276. if (sidebar.hasClass("control-sidebar-dark")) {
  277. sidebar.removeClass("control-sidebar-dark")
  278. sidebar.addClass("control-sidebar-light")
  279. } else {
  280. sidebar.removeClass("control-sidebar-light")
  281. sidebar.addClass("control-sidebar-dark")
  282. }
  283. });
  284. // 菜单栏展开或收起
  285. $("[data-enable='expandOnHover']").on('click', function () {
  286. $(this).attr('disabled', true);
  287. AdminLTE.pushMenu.expandOnHover();
  288. if (!$('body').hasClass('sidebar-collapse'))
  289. $("[data-layout='sidebar-collapse']").click();
  290. });
  291. // 重设选项
  292. if ($('body').hasClass('fixed')) {
  293. $("[data-layout='fixed']").attr('checked', 'checked');
  294. }
  295. if ($('body').hasClass('layout-boxed')) {
  296. $("[data-layout='layout-boxed']").attr('checked', 'checked');
  297. }
  298. if ($('body').hasClass('sidebar-collapse')) {
  299. $("[data-layout='sidebar-collapse']").attr('checked', 'checked');
  300. }
  301. if ($('ul.sidebar-menu').hasClass('show-submenu')) {
  302. $("[data-menu='show-submenu']").attr('checked', 'checked');
  303. }
  304. if ($('ul.nav-addtabs').hasClass('disable-top-badge')) {
  305. $("[data-menu='disable-top-badge']").attr('checked', 'checked');
  306. }
  307. }
  308. $(window).resize();
  309. },
  310. login: function () {
  311. var lastlogin = localStorage.getItem("lastlogin");
  312. if (lastlogin) {
  313. lastlogin = JSON.parse(lastlogin);
  314. $("#profile-img").attr("src", Backend.api.cdnurl(lastlogin.avatar));
  315. $("#pd-form-username").val(lastlogin.username);
  316. }
  317. //让错误提示框居中
  318. Backend.config.toastr.positionClass = "toast-top-center";
  319. //本地验证未通过时提示
  320. $("#login-form").data("validator-options", {
  321. invalid: function (form, errors) {
  322. $.each(errors, function (i, j) {
  323. Toastr.error(j);
  324. });
  325. },
  326. target: '#errtips'
  327. });
  328. //为表单绑定事件
  329. Form.api.bindevent($("#login-form"), null, function (data) {
  330. localStorage.setItem("lastlogin", JSON.stringify({id: data.id, username: data.username, avatar: data.avatar}));
  331. location.href = Backend.api.fixurl(data.url);
  332. });
  333. }
  334. };
  335. return Controller;
  336. });