main.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. clearSearch: function () {
  34. vue.search = '';
  35. refresh();
  36. $('#inputSearch').focus();
  37. }
  38. }
  39. });
  40. Core.settings.onChanged(function (data) {
  41. if (data.title) window.document.title = data.title;
  42. if (data.backgroundUrl) window.document.body.style.backgroundImage = 'url("' + data.backgroundUrl + '")';
  43. });
  44. function hashChangeHandler() {
  45. var action = window.location.hash.split('?')[0];
  46. var params = window.location.hash.indexOf('?') > 0 ? decodeURIComponent(window.location.hash.slice(window.location.hash.indexOf('?') + 1)) : null;
  47. if (action === '#add') {
  48. vue.showThingAdd();
  49. } else if (action === '#settings') {
  50. vueSettings.show();
  51. } else if (action === '#search' && params !== null) {
  52. vue.search = params;
  53. refresh(vue.search);
  54. } else {
  55. refresh(vue.search);
  56. }
  57. }
  58. window.addEventListener('hashchange', hashChangeHandler, false);
  59. function refresh(search) {
  60. vue.busyThings = true;
  61. window.location.href = '/#search?' + (search ? encodeURIComponent(search) : '');
  62. Core.things.get(search || '', function (error) {
  63. if (error) return console.error(error);
  64. vue.things = Core.things.data;
  65. vue.busyThings = false;
  66. });
  67. }
  68. Core.things.onAdded(refresh);
  69. Core.things.onEdited(refresh);
  70. Core.things.onDeleted(refresh);
  71. // Main
  72. $.material.init();
  73. Core.settings.get(function (error) {
  74. if (error) return console.error(error);
  75. Core.tags.get(function (error, tags) {
  76. if (error) return console.error(error);
  77. vue.tags = tags;
  78. vue.busyTags = false;
  79. $('#inputSearch').focus();
  80. hashChangeHandler();
  81. });
  82. });