main.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. Core.settings.get(function (error) {
  67. if (error) return console.error(error);
  68. Core.tags.get(function (error, tags) {
  69. if (error) return console.error(error);
  70. vue.tags = tags;
  71. vue.busyTags = false;
  72. $('#inputSearch').focus();
  73. hashChangeHandler();
  74. });
  75. });