index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. $.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 fa.event.toggleitem", ".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 (Config.referer) {
  134. //刷新页面后跳到到刷新前的页面
  135. Backend.api.addtabs(Config.referer);
  136. } else {
  137. $("ul.sidebar-menu li.active a").trigger("click");
  138. }
  139. /**
  140. * List of all the available skins
  141. *
  142. * @type Array
  143. */
  144. var my_skins = [
  145. "skin-blue",
  146. "skin-black",
  147. "skin-red",
  148. "skin-yellow",
  149. "skin-purple",
  150. "skin-green",
  151. "skin-blue-light",
  152. "skin-black-light",
  153. "skin-red-light",
  154. "skin-yellow-light",
  155. "skin-purple-light",
  156. "skin-green-light"
  157. ];
  158. setup();
  159. /**
  160. * Toggles layout classes
  161. *
  162. * @param String cls the layout class to toggle
  163. * @returns void
  164. */
  165. function change_layout(cls) {
  166. $("body").toggleClass(cls);
  167. AdminLTE.layout.fixSidebar();
  168. //Fix the problem with right sidebar and layout boxed
  169. if (cls == "layout-boxed")
  170. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  171. if ($('body').hasClass('fixed') && cls == 'fixed') {
  172. AdminLTE.pushMenu.expandOnHover();
  173. AdminLTE.layout.activate();
  174. }
  175. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  176. AdminLTE.controlSidebar._fix($(".control-sidebar"));
  177. }
  178. /**
  179. * Replaces the old skin with the new skin
  180. * @param String cls the new skin class
  181. * @returns Boolean false to prevent link's default action
  182. */
  183. function change_skin(cls) {
  184. if (!$("body").hasClass(cls)) {
  185. $.each(my_skins, function (i) {
  186. $("body").removeClass(my_skins[i]);
  187. });
  188. $("body").addClass(cls);
  189. store('skin', cls);
  190. var cssfile = Backend.api.cdnurl("/assets/css/skins/" + cls + ".css");
  191. $('head').append('<link rel="stylesheet" href="' + cssfile + '" type="text/css" />');
  192. }
  193. return false;
  194. }
  195. /**
  196. * Store a new settings in the browser
  197. *
  198. * @param String name Name of the setting
  199. * @param String val Value of the setting
  200. * @returns void
  201. */
  202. function store(name, val) {
  203. if (typeof (Storage) !== "undefined") {
  204. localStorage.setItem(name, val);
  205. } else {
  206. window.alert('Please use a modern browser to properly view this template!');
  207. }
  208. }
  209. /**
  210. * Get a prestored setting
  211. *
  212. * @param String name Name of of the setting
  213. * @returns String The value of the setting | null
  214. */
  215. function get(name) {
  216. if (typeof (Storage) !== "undefined") {
  217. return localStorage.getItem(name);
  218. } else {
  219. window.alert('Please use a modern browser to properly view this template!');
  220. }
  221. }
  222. /**
  223. * Retrieve default settings and apply them to the template
  224. *
  225. * @returns void
  226. */
  227. function setup() {
  228. var tmp = get('skin');
  229. if (tmp && $.inArray(tmp, my_skins))
  230. change_skin(tmp);
  231. // 皮肤切换
  232. $("[data-skin]").on('click', function (e) {
  233. if ($(this).hasClass('knob'))
  234. return;
  235. e.preventDefault();
  236. change_skin($(this).data('skin'));
  237. });
  238. // 布局切换
  239. $("[data-layout]").on('click', function () {
  240. change_layout($(this).data('layout'));
  241. });
  242. // 切换子菜单显示和菜单小图标的显示
  243. $("[data-menu]").on('click', function () {
  244. if ($(this).data("menu") == 'show-submenu') {
  245. $("ul.sidebar-menu").toggleClass("show-submenu");
  246. } else {
  247. $(".nav-addtabs").toggleClass("disable-top-badge");
  248. }
  249. });
  250. // 右侧控制栏切换
  251. $("[data-controlsidebar]").on('click', function () {
  252. change_layout($(this).data('controlsidebar'));
  253. var slide = !AdminLTE.options.controlSidebarOptions.slide;
  254. AdminLTE.options.controlSidebarOptions.slide = slide;
  255. if (!slide)
  256. $('.control-sidebar').removeClass('control-sidebar-open');
  257. });
  258. // 右侧控制栏背景切换
  259. $("[data-sidebarskin='toggle']").on('click', function () {
  260. var sidebar = $(".control-sidebar");
  261. if (sidebar.hasClass("control-sidebar-dark")) {
  262. sidebar.removeClass("control-sidebar-dark")
  263. sidebar.addClass("control-sidebar-light")
  264. } else {
  265. sidebar.removeClass("control-sidebar-light")
  266. sidebar.addClass("control-sidebar-dark")
  267. }
  268. });
  269. // 菜单栏展开或收起
  270. $("[data-enable='expandOnHover']").on('click', function () {
  271. $(this).attr('disabled', true);
  272. AdminLTE.pushMenu.expandOnHover();
  273. if (!$('body').hasClass('sidebar-collapse'))
  274. $("[data-layout='sidebar-collapse']").click();
  275. });
  276. // 重设选项
  277. if ($('body').hasClass('fixed')) {
  278. $("[data-layout='fixed']").attr('checked', 'checked');
  279. }
  280. if ($('body').hasClass('layout-boxed')) {
  281. $("[data-layout='layout-boxed']").attr('checked', 'checked');
  282. }
  283. if ($('body').hasClass('sidebar-collapse')) {
  284. $("[data-layout='sidebar-collapse']").attr('checked', 'checked');
  285. }
  286. if ($('ul.sidebar-menu').hasClass('show-submenu')) {
  287. $("[data-menu='show-submenu']").attr('checked', 'checked');
  288. }
  289. if ($('ul.nav-addtabs').hasClass('disable-top-badge')) {
  290. $("[data-menu='disable-top-badge']").attr('checked', 'checked');
  291. }
  292. }
  293. $(window).resize();
  294. },
  295. login: function () {
  296. Form.api.bindevent($("#login-form"), null, function (data) {
  297. location.href = Backend.api.fixurl(data.url);
  298. });
  299. }
  300. };
  301. return Controller;
  302. });