|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 12 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 13 | +}; |
| 14 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 15 | +const Service_1 = __importDefault(require("@/services/Service")); |
| 16 | +const Workspace_1 = __importDefault(require("@/models/soukai/Workspace")); |
| 17 | +const EventBus_1 = __importDefault(require("@/utils/EventBus")); |
| 18 | +const Storage_1 = __importDefault(require("@/utils/Storage")); |
| 19 | +class Workspaces extends Service_1.default { |
| 20 | + get empty() { |
| 21 | + return !this.storage.workspaces || this.storage.workspaces.length === 0; |
| 22 | + } |
| 23 | + get active() { |
| 24 | + return this.storage.activeWorkspace; |
| 25 | + } |
| 26 | + get all() { |
| 27 | + return this.storage.workspaces; |
| 28 | + } |
| 29 | + setActive(workspace) { |
| 30 | + return __awaiter(this, void 0, void 0, function* () { |
| 31 | + this.app.$store.commit('setActiveWorkspace', workspace); |
| 32 | + if (workspace) |
| 33 | + Storage_1.default.set('activeWorkspaceId', workspace.id); |
| 34 | + else |
| 35 | + Storage_1.default.remove('activeWorkspaceId'); |
| 36 | + }); |
| 37 | + } |
| 38 | + add(workspace, activate = true) { |
| 39 | + this.app.$store.commit('addWorkspace', workspace); |
| 40 | + if (activate) |
| 41 | + this.setActive(workspace); |
| 42 | + } |
| 43 | + remove(workspace) { |
| 44 | + this.app.$store.commit('removeWorkspace', workspace); |
| 45 | + this.setActive(this.all.length > 0 ? this.all[0] : null); |
| 46 | + } |
| 47 | + get storage() { |
| 48 | + return this.app.$store.state.workspaces |
| 49 | + ? this.app.$store.state.workspaces |
| 50 | + : {}; |
| 51 | + } |
| 52 | + init() { |
| 53 | + const _super = Object.create(null, { |
| 54 | + init: { get: () => super.init } |
| 55 | + }); |
| 56 | + return __awaiter(this, void 0, void 0, function* () { |
| 57 | + yield _super.init.call(this); |
| 58 | + yield this.app.$auth.ready; |
| 59 | + if (this.app.$auth.isLoggedIn()) { |
| 60 | + yield this.load(this.app.$auth.user); |
| 61 | + } |
| 62 | + EventBus_1.default.on('login', this.load.bind(this)); |
| 63 | + EventBus_1.default.on('logout', this.unload.bind(this)); |
| 64 | + }); |
| 65 | + } |
| 66 | + registerStoreModule(store) { |
| 67 | + store.registerModule('workspaces', { |
| 68 | + state: { |
| 69 | + activeWorkspace: null, |
| 70 | + workspaces: [], |
| 71 | + }, |
| 72 | + mutations: { |
| 73 | + setActiveWorkspace(state, activeWorkspace) { |
| 74 | + state.activeWorkspace = activeWorkspace; |
| 75 | + }, |
| 76 | + setWorkspaces(state, workspaces) { |
| 77 | + state.workspaces = workspaces; |
| 78 | + }, |
| 79 | + addWorkspace(state, workspace) { |
| 80 | + state.workspaces.push(workspace); |
| 81 | + }, |
| 82 | + removeWorkspace(state, workspace) { |
| 83 | + const index = state.workspaces.findIndex(existingWorkspace => existingWorkspace === workspace); |
| 84 | + if (index !== -1) { |
| 85 | + state.workspaces.splice(index, 1); |
| 86 | + } |
| 87 | + }, |
| 88 | + }, |
| 89 | + }); |
| 90 | + } |
| 91 | + unregisterStoreModule(store) { |
| 92 | + store.unregisterModule('workspaces'); |
| 93 | + } |
| 94 | + load(user) { |
| 95 | + return __awaiter(this, void 0, void 0, function* () { |
| 96 | + const workspaces = []; |
| 97 | + const activeWorkspaceId = Storage_1.default.get('activeWorkspaceId'); |
| 98 | + const activeListId = Storage_1.default.get('activeListId'); |
| 99 | + let activeWorkspace; |
| 100 | + for (const storage of user.storages) { |
| 101 | + workspaces.push(...(yield Workspace_1.default.from(storage).all())); |
| 102 | + } |
| 103 | + activeWorkspace = workspaces.find(workspace => workspace.id === activeWorkspaceId); |
| 104 | + if (!activeWorkspace && workspaces.length > 0) |
| 105 | + activeWorkspace = workspaces[0]; |
| 106 | + if (activeWorkspace) |
| 107 | + yield this.loadWorkspace(activeWorkspace, activeListId); |
| 108 | + this.app.$store.commit('setWorkspaces', workspaces); |
| 109 | + this.app.$store.commit('setActiveWorkspace', activeWorkspace); |
| 110 | + }); |
| 111 | + } |
| 112 | + unload() { |
| 113 | + return __awaiter(this, void 0, void 0, function* () { |
| 114 | + this.app.$store.commit('setWorkspaces', []); |
| 115 | + this.app.$store.commit('setActiveWorkspace', null); |
| 116 | + Storage_1.default.remove('activeWorkspaceId'); |
| 117 | + Storage_1.default.remove('activeListId'); |
| 118 | + }); |
| 119 | + } |
| 120 | + loadWorkspace(workspace, activeListId) { |
| 121 | + return __awaiter(this, void 0, void 0, function* () { |
| 122 | + if (!workspace.isRelationLoaded('lists')) { |
| 123 | + yield workspace.loadRelation('lists'); |
| 124 | + // TODO this could be done automatically in Soukai |
| 125 | + for (const list of workspace.lists) { |
| 126 | + list.setRelationModels('workspace', workspace); |
| 127 | + } |
| 128 | + } |
| 129 | + workspace.setActiveList(workspace.lists.find(list => list.id === activeListId) || |
| 130 | + workspace.activeList); |
| 131 | + if (workspace && !workspace.activeList.isRelationLoaded('tasks')) |
| 132 | + yield workspace.activeList.loadRelation('tasks'); |
| 133 | + }); |
| 134 | + } |
| 135 | +} |
| 136 | +exports.default = Workspaces; |
0 commit comments