main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. refresh: function () {
  48. refresh();
  49. },
  50. doSearch: function () {
  51. window.location.href = '/#search?' + encodeURIComponent(this.search);
  52. },
  53. tagSearch: function (tag) {
  54. window.location.href = '/#search?#' + encodeURIComponent(tag.name);
  55. },
  56. clearSearch: function () {
  57. vue.search = '';
  58. refresh();
  59. $('#inputSearch').focus();
  60. }
  61. }
  62. });
  63. Core.settings.onChanged(function (data) {
  64. if (data.title) window.document.title = data.title;
  65. if (data.backgroundUrl) window.document.body.style.backgroundImage = 'url("' + data.backgroundUrl + '")';
  66. });
  67. function hashChangeHandler() {
  68. var action = window.location.hash.split('?')[0];
  69. var params = window.location.hash.indexOf('?') > 0 ? decodeURIComponent(window.location.hash.slice(window.location.hash.indexOf('?') + 1)) : null;
  70. if (action === '#add') {
  71. vue.showThingAdd();
  72. } else if (action === '#settings') {
  73. vueSettings.show();
  74. } else if (action === '#search' && params !== null) {
  75. vue.search = params;
  76. refresh(vue.search);
  77. } else {
  78. refresh(vue.search);
  79. }
  80. }
  81. window.addEventListener('hashchange', hashChangeHandler, false);
  82. function reset() {
  83. vue.mainView = '';
  84. vue.things = [];
  85. vue.tags = [];
  86. vue.search = '';
  87. vue.username = '';
  88. vue.password = '';
  89. Core.settings.reset();
  90. }
  91. function main() {
  92. Core.settings.get(function (error) {
  93. if (error) return console.error(error);
  94. Core.tags.get(function (error, tags) {
  95. if (error) return console.error(error);
  96. vue.tags = tags;
  97. vue.busyTags = false;
  98. vue.mainView = 'content';
  99. $('#inputSearch').focus();
  100. hashChangeHandler();
  101. });
  102. });
  103. }
  104. function refresh(search) {
  105. vue.busyThings = true;
  106. window.location.href = '/#search?' + (search ? encodeURIComponent(search) : '');
  107. Core.things.get(search || '', function (error, data) {
  108. if (error) return console.error(error);
  109. vue.things = data;
  110. vue.busyThings = false;
  111. });
  112. }
  113. Core.things.onAdded(refresh);
  114. Core.things.onEdited(refresh);
  115. Core.things.onDeleted(refresh);
  116. Core.loginFailed = vue.showLogin;
  117. Core.onLogout = function () {
  118. reset();
  119. vue.showLogin();
  120. };
  121. // Main
  122. $.material.init();
  123. main();