main.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. var Vue = require('vue'),
  3. Core = require('./core.js').Core;
  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. },
  17. methods: {
  18. showThingAdd: function () {
  19. vueThings.add.open();
  20. },
  21. showThingEdit: function (thing) {
  22. vueThings.edit.open(thing);
  23. },
  24. showThingDel: function (thing) {
  25. vueThings.del.open(thing);
  26. },
  27. doSearch: function () {
  28. window.location.href = '/#search?' + encodeURIComponent(this.search);
  29. },
  30. tagSearch: function (tag) {
  31. window.location.href = '/#search?#' + encodeURIComponent(tag.name);
  32. }
  33. }
  34. });
  35. Core.settings.onChanged(function (data) {
  36. window.document.title = data.title;
  37. window.document.body.style.backgroundImage = 'url("' + data.backgroundUrl + '")';
  38. });
  39. function hashChangeHandler() {
  40. var action = window.location.hash.split('?')[0];
  41. var params = window.location.hash.indexOf('?') > 0 ? decodeURIComponent(window.location.hash.slice(window.location.hash.indexOf('?') + 1)) : null;
  42. if (action === '#add') {
  43. vue.showThingAdd();
  44. } else if (action === '#settings') {
  45. vueSettings.show();
  46. } else if (action === '#search' && params !== null) {
  47. vue.search = params;
  48. refresh(vue.search);
  49. } else {
  50. refresh(vue.search);
  51. }
  52. }
  53. window.addEventListener('hashchange', hashChangeHandler, false);
  54. function refresh(search) {
  55. vue.busyThings = true;
  56. Core.things.get(search || '', function (error) {
  57. if (error) return console.error(error);
  58. vue.things = Core.things.data;
  59. vue.busyThings = false;
  60. });
  61. }
  62. Core.things.onAdded(refresh);
  63. Core.things.onEdited(refresh);
  64. Core.things.onDeleted(refresh);
  65. // Main
  66. $.material.init();
  67. Core.settings.get(function (error) {
  68. if (error) return console.error(error);
  69. Core.tags.get(function (error, tags) {
  70. if (error) return console.error(error);
  71. vue.tags = tags;
  72. vue.busyTags = false;
  73. $('#inputSearch').focus();
  74. hashChangeHandler();
  75. });
  76. });