| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 'use strict';
- var Vue = require('vue'),
- Core = require('./core.js');
- require('./filter.js');
- var vueSettings = require('./settings.js');
- Vue.config.debug = true;
- var vue = new Vue({
- el: '#application',
- data: {
- tags: [],
- things: [],
- busyThings: true,
- busyTags: true,
- search: '',
- username: '',
- password: '',
- mainView: '',
- thingContent: '',
- activeThing: {}
- },
- methods: {
- giveAddFocus: function () {
- this.$els.addinput.focus();
- },
- addThing: function () {
- Core.things.add(this.thingContent, function (error) {
- if (error) return console.error(error);
- vue.thingContent = '';
- vue.refresh();
- });
- },
- showThingEdit: function (thing) {
- thing.edit = true;
- },
- saveEdit: function (thing) {
- Core.things.edit(thing, function (error) {
- if (error) return console.error(error);
- thing.edit = false;
- vue.refresh();
- });
- },
- cancelEdit: function (thing) {
- thing.edit = false;
- },
- showThingDel: function (thing) {
- this.activeThing = thing;
- $('#modalDel').openModal();
- },
- deleteThing: function () {
- if (!this.activeThing) return;
- Core.things.del(this.activeThing, function (error) {
- if (error) return console.error(error);
- vue.activeThing = null;
- vue.refresh();
- $('#modalDel').closeModal();
- });
- },
- showSettings: function () {
- vueSettings.toggleSettings();
- },
- showLogin: function () {
- this.mainView = 'login';
- setTimeout(function () {
- $('#inputUsername').focus();
- }, 0);
- },
- doLogin: function () {
- Core.session.login(this.username, this.password, function (error) {
- if (error) {
- vue.username = '';
- vue.password = '';
- $('#inputUsername').focus();
- return console.error('Login failed:', error.status ? error.status : error);
- }
- main();
- });
- },
- refresh: function () {
- refresh();
- },
- doSearch: function () {
- window.location.href = '/#search?' + encodeURIComponent(this.search);
- },
- tagSearch: function (tag) {
- window.location.href = '/#search?#' + encodeURIComponent(tag.name);
- },
- clearSearch: function () {
- vue.search = '';
- refresh();
- $('#inputSearch').focus();
- }
- }
- });
- Core.settings.onChanged(function (data) {
- if (data.title) window.document.title = data.title;
- if (data.backgroundUrl) window.document.body.style.backgroundImage = 'url("' + data.backgroundUrl + '")';
- });
- function hashChangeHandler() {
- var action = window.location.hash.split('?')[0];
- var params = window.location.hash.indexOf('?') > 0 ? decodeURIComponent(window.location.hash.slice(window.location.hash.indexOf('?') + 1)) : null;
- if (action === '#add') {
- vue.showThingAdd();
- } else if (action === '#settings') {
- vueSettings.show();
- } else if (action === '#search' && params !== null) {
- vue.search = params;
- refresh(vue.search);
- } else {
- refresh(vue.search);
- }
- }
- window.addEventListener('hashchange', hashChangeHandler, false);
- function reset() {
- vue.mainView = '';
- vue.things = [];
- vue.tags = [];
- vue.search = '';
- vue.username = '';
- vue.password = '';
- Core.settings.reset();
- }
- function main() {
- Core.settings.get(function (error) {
- if (error) return console.error(error);
- Core.tags.get(function (error, tags) {
- if (error) return console.error(error);
- vue.tags = tags;
- vue.busyTags = false;
- vue.mainView = 'content';
- window.setTimeout(function () { vue.$els.searchinput.focus(); }, 0);
- hashChangeHandler();
- });
- });
- }
- function refresh(search) {
- vue.busyThings = true;
- window.location.href = '/#search?' + (search ? encodeURIComponent(search) : '');
- Core.things.get(search || '', function (error, data) {
- if (error) return console.error(error);
- vue.things = data;
- vue.busyThings = false;
- // add global object for browser extensions
- document.getElementById('guacamoly-settings-node').textContent = JSON.stringify({
- origin: Core.origin(),
- token: Core.token(),
- title: Core.settings.data.title
- });
- });
- }
- Core.things.onAdded(refresh);
- Core.things.onEdited(refresh);
- Core.things.onDeleted(refresh);
- Core.loginFailed = vue.showLogin;
- Core.onLogout = function () {
- reset();
- vue.showLogin();
- };
- // Main
- main();
|