index.js 16 KB

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