main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. var Vue = require('vue'),
  3. Core = require('./core.js');
  4. require('./filter.js');
  5. var vueSettings = require('./settings.js');
  6. Vue.config.debug = true;
  7. var vue = new Vue({
  8. el: '#application',
  9. data: {
  10. tags: [],
  11. things: [],
  12. busyThings: true,
  13. busyTags: true,
  14. search: '',
  15. username: '',
  16. password: '',
  17. mainView: '',
  18. thingContent: '',
  19. activeThing: {}
  20. },
  21. methods: {
  22. giveAddFocus: function () {
  23. this.$els.addinput.focus();
  24. },
  25. addThing: function () {
  26. Core.things.add(this.thingContent, function (error) {
  27. if (error) return console.error(error);
  28. vue.thingContent = '';
  29. vue.refresh();
  30. });
  31. },
  32. showThingEdit: function (thing) {
  33. thing.edit = true;
  34. },
  35. saveEdit: function (thing) {
  36. Core.things.edit(thing, function (error) {
  37. if (error) return console.error(error);
  38. thing.edit = false;
  39. vue.refresh();
  40. });
  41. },
  42. cancelEdit: function (thing) {
  43. thing.edit = false;
  44. },
  45. showThingDel: function (thing) {
  46. this.activeThing = thing;
  47. $('#modalDel').openModal();
  48. },
  49. deleteThing: function () {
  50. if (!this.activeThing) return;
  51. Core.things.del(this.activeThing, function (error) {
  52. if (error) return console.error(error);
  53. vue.activeThing = null;
  54. vue.refresh();
  55. $('#modalDel').closeModal();
  56. });
  57. },
  58. showSettings: function () {
  59. vueSettings.toggleSettings();
  60. },
  61. showLogin: function () {
  62. this.mainView = 'login';
  63. setTimeout(function () {
  64. $('#inputUsername').focus();
  65. }, 0);
  66. },
  67. doLogin: function () {
  68. Core.session.login(this.username, this.password, function (error) {
  69. if (error) {
  70. vue.username = '';
  71. vue.password = '';
  72. $('#inputUsername').focus();
  73. return console.error('Login failed:', error.status ? error.status : error);
  74. }
  75. main();
  76. });
  77. },
  78. refresh: function () {
  79. refresh();
  80. },
  81. doSearch: function () {
  82. window.location.href = '/#search?' + encodeURIComponent(this.search);
  83. },
  84. tagSearch: function (tag) {
  85. window.location.href = '/#search?#' + encodeURIComponent(tag.name);
  86. },
  87. clearSearch: function () {
  88. vue.search = '';
  89. refresh();
  90. $('#inputSearch').focus();
  91. }
  92. }
  93. });
  94. Core.settings.onChanged(function (data) {
  95. if (data.title) window.document.title = data.title;
  96. if (data.backgroundUrl) window.document.body.style.backgroundImage = 'url("' + data.backgroundUrl + '")';
  97. });
  98. function hashChangeHandler() {
  99. var action = window.location.hash.split('?')[0];
  100. var params = window.location.hash.indexOf('?') > 0 ? decodeURIComponent(window.location.hash.slice(window.location.hash.indexOf('?') + 1)) : null;
  101. if (action === '#add') {
  102. vue.showThingAdd();
  103. } else if (action === '#settings') {
  104. vueSettings.show();
  105. } else if (action === '#search' && params !== null) {
  106. vue.search = params;
  107. refresh(vue.search);
  108. } else {
  109. refresh(vue.search);
  110. }
  111. }
  112. window.addEventListener('hashchange', hashChangeHandler, false);
  113. function reset() {
  114. vue.mainView = '';
  115. vue.things = [];
  116. vue.tags = [];
  117. vue.search = '';
  118. vue.username = '';
  119. vue.password = '';
  120. Core.settings.reset();
  121. }
  122. function main() {
  123. Core.settings.get(function (error) {
  124. if (error) return console.error(error);
  125. Core.tags.get(function (error, tags) {
  126. if (error) return console.error(error);
  127. vue.tags = tags;
  128. vue.busyTags = false;
  129. vue.mainView = 'content';
  130. window.setTimeout(function () { vue.$els.searchinput.focus(); }, 0);
  131. hashChangeHandler();
  132. });
  133. });
  134. }
  135. function refresh(search) {
  136. vue.busyThings = true;
  137. window.location.href = '/#search?' + (search ? encodeURIComponent(search) : '');
  138. Core.things.get(search || '', function (error, data) {
  139. if (error) return console.error(error);
  140. vue.things = data;
  141. vue.busyThings = false;
  142. // add global object for browser extensions
  143. document.getElementById('guacamoly-settings-node').textContent = JSON.stringify({
  144. addPostUrl: Core.url('/api/things')
  145. });
  146. });
  147. }
  148. Core.things.onAdded(refresh);
  149. Core.things.onEdited(refresh);
  150. Core.things.onDeleted(refresh);
  151. Core.loginFailed = vue.showLogin;
  152. Core.onLogout = function () {
  153. reset();
  154. vue.showLogin();
  155. };
  156. // Main
  157. main();