main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. var Vue = require('vue'),
  3. Core = require('./core.js');
  4. require('./filter.js');
  5. var vueThings = require('./thing.js');
  6. var vueSettings = require('./settings.js');
  7. // Vue.config.debug = true;
  8. var vue = new Vue({
  9. el: '#application',
  10. data: {
  11. tags: [],
  12. things: [],
  13. busyThings: true,
  14. busyTags: true,
  15. search: '',
  16. username: '',
  17. password: '',
  18. mainView: ''
  19. },
  20. methods: {
  21. showThingAdd: function () {
  22. vueThings.add.open();
  23. },
  24. showThingEdit: function (thing) {
  25. vueThings.edit.open(thing);
  26. },
  27. showThingDel: function (thing) {
  28. vueThings.del.open(thing);
  29. },
  30. showLogin: function () {
  31. this.mainView = 'login';
  32. setTimeout(function () {
  33. $('#inputUsername').focus();
  34. }, 0);
  35. },
  36. doLogin: function () {
  37. Core.session.login(this.username, this.password, function (error) {
  38. if (error) {
  39. vue.username = '';
  40. vue.password = '';
  41. $('#inputUsername').focus();
  42. return console.error('Login failed:', error.status ? error.status : error);
  43. }
  44. main();
  45. });
  46. },
  47. doSearch: function () {
  48. window.location.href = '/#search?' + encodeURIComponent(this.search);
  49. },
  50. tagSearch: function (tag) {
  51. window.location.href = '/#search?#' + encodeURIComponent(tag.name);
  52. },
  53. clearSearch: function () {
  54. vue.search = '';
  55. refresh();
  56. $('#inputSearch').focus();
  57. }
  58. }
  59. });
  60. Core.settings.onChanged(function (data) {
  61. if (data.title) window.document.title = data.title;
  62. if (data.backgroundUrl) window.document.body.style.backgroundImage = 'url("' + data.backgroundUrl + '")';
  63. });
  64. function hashChangeHandler() {
  65. var action = window.location.hash.split('?')[0];
  66. var params = window.location.hash.indexOf('?') > 0 ? decodeURIComponent(window.location.hash.slice(window.location.hash.indexOf('?') + 1)) : null;
  67. if (action === '#add') {
  68. vue.showThingAdd();
  69. } else if (action === '#settings') {
  70. vueSettings.show();
  71. } else if (action === '#search' && params !== null) {
  72. vue.search = params;
  73. refresh(vue.search);
  74. } else {
  75. refresh(vue.search);
  76. }
  77. }
  78. window.addEventListener('hashchange', hashChangeHandler, false);
  79. function reset() {
  80. vue.mainView = '';
  81. vue.things = [];
  82. vue.tags = [];
  83. vue.search = '';
  84. vue.username = '';
  85. vue.password = '';
  86. Core.settings.reset();
  87. }
  88. function main() {
  89. Core.settings.get(function (error) {
  90. if (error) return console.error(error);
  91. Core.tags.get(function (error, tags) {
  92. if (error) return console.error(error);
  93. vue.tags = tags;
  94. vue.busyTags = false;
  95. vue.mainView = 'content';
  96. $('#inputSearch').focus();
  97. hashChangeHandler();
  98. });
  99. });
  100. }
  101. function refresh(search) {
  102. vue.busyThings = true;
  103. window.location.href = '/#search?' + (search ? encodeURIComponent(search) : '');
  104. Core.things.get(search || '', function (error, data) {
  105. if (error) return console.error(error);
  106. vue.things = data;
  107. vue.busyThings = false;
  108. });
  109. }
  110. Core.things.onAdded(refresh);
  111. Core.things.onEdited(refresh);
  112. Core.things.onDeleted(refresh);
  113. Core.loginFailed = vue.showLogin;
  114. Core.onLogout = function () {
  115. reset();
  116. vue.showLogin();
  117. };
  118. // Main
  119. $.material.init();
  120. main();