index.js 18 KB

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