index.js 15 KB

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