index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. SPA_RESOLVE_INIT = function ($el, $router) {
  2. let body = document.querySelector('body');
  3. body.className = 'mdui-appbar-with-toolbar mdui-theme-primary-blue mdui-theme-accent-light-blue mdui-drawer-body-left mdui-loaded';
  4. mdui.JQ($el).empty();
  5. mdui.JQ($el).append(`
  6. <header class="mdui-appbar mdui-appbar-fixed">
  7. <div class="mdui-toolbar mdui-color-theme">
  8. <span class="mdui-btn mdui-btn-icon mdui-ripple mdui-ripple-white" mdui-drawer="{target: '#main-drawer', swipe: true}">
  9. <i class="mdui-icon material-icons">menu</i>
  10. </span>
  11. <a href="#/index" class="mdui-typo-headline mdui-hidden-xs">CloudSQL Admin Panel</a>
  12. <div class="mdui-toolbar-spacer"></div>
  13. <span class="mdui-btn mdui-btn-icon mdui-ripple mdui-ripple-white">
  14. <i class="mdui-icon material-icons">exit_to_app</i>
  15. </span>
  16. </div>
  17. </header>
  18. <div class="mdui-drawer mdui-drawer-open" id="main-drawer">
  19. <div class="mdui-list" mdui-collapse="{accordion: true}" style="margin-bottom: 76px;">
  20. <li class="mdui-list-item mdui-ripple">
  21. <i class="mdui-list-item-icon mdui-icon material-icons">view_list</i>
  22. <div class="mdui-list-item-content">数据库列表</div>
  23. </li>
  24. <li class="mdui-list-item mdui-ripple">
  25. <i class="mdui-list-item-icon mdui-icon material-icons">person</i>
  26. <div class="mdui-list-item-content">用户列表</div>
  27. </li>
  28. <li class="mdui-list-item mdui-ripple">
  29. <i class="mdui-list-item-icon mdui-icon material-icons">info</i>
  30. <div class="mdui-list-item-content">关于</div>
  31. </li>
  32. </div>
  33. </div>
  34. <div class="mdui-container main-container"></div>
  35. `);
  36. //左侧菜单栏
  37. let leftMenuButton = document.querySelector('span:nth-child(1).mdui-btn.mdui-btn-icon.mdui-ripple.mdui-ripple-white'),
  38. mainDrawer = new mdui.Drawer('#main-drawer');
  39. Utils.handleEvent('click', {
  40. onElement: leftMenuButton,
  41. withCallback: function () {
  42. mainDrawer.toggle();
  43. }
  44. })
  45. let databaseListButton = document.querySelector('#main-drawer li:nth-child(1)');
  46. Utils.handleEvent('click', {
  47. onElement: databaseListButton,
  48. withCallback: function () {
  49. window.location.hash = '/index/databaseList';
  50. }
  51. })
  52. let userListButton = document.querySelector('#main-drawer li:nth-child(2)');
  53. Utils.handleEvent('click', {
  54. onElement: userListButton,
  55. withCallback: function () {
  56. window.location.hash = '/index/userList';
  57. }
  58. })
  59. //退出登录按钮
  60. let signoutButton = document.querySelector('span:nth-child(4).mdui-btn.mdui-btn-icon.mdui-ripple.mdui-ripple-white i.mdui-icon.material-icons');
  61. Utils.handleEvent('click', {
  62. onElement: signoutButton,
  63. withCallback: function () {
  64. Utils.ajaxGet('http://admin.cloudsql.1473.cn/v1/signout', function (response) {
  65. if (response.status == 'successed') {
  66. mdui.snackbar({
  67. message: response.info
  68. });
  69. window.location.hash = response.url;
  70. } else {
  71. console.log(response.info);
  72. console.log(response.error);
  73. mdui.snackbar({
  74. message: response.info
  75. });
  76. }
  77. });
  78. }
  79. })
  80. const inlineEl = document.querySelector('.mdui-container.main-container');
  81. const inlineRoute = [{
  82. path: '/index/main',
  83. fileName: 'src/components/main.js'
  84. },
  85. {
  86. path: '/index/databaseList',
  87. fileName: 'src/components/databaseList.js'
  88. },
  89. {
  90. path: '/index/userList',
  91. fileName: 'src/components/userList.js'
  92. },
  93. ]
  94. const inlineRouter = new SPARouter(inlineEl, inlineRouter, 'hash');
  95. inlineRouter.beforeEach((to, next) => {
  96. Utils.ajaxGet('http://admin.cloudsql.1473.cn/v1/index/signStatus', function (response) {
  97. if (response.status === 'signedin') {
  98. next();
  99. } else {
  100. window.location.hash = '/signin';
  101. }
  102. });
  103. });
  104. }