-
-
Notifications
You must be signed in to change notification settings - Fork 746
/
app.component.ts
222 lines (195 loc) · 7.51 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import {ChangeDetectionStrategy, Component, HostListener, OnDestroy, ViewChild, ViewContainerRef} from '@angular/core';
import {ProjectService} from './features/project/project.service';
import {ChromeExtensionInterfaceService} from './core/chrome-extension-interface/chrome-extension-interface.service';
import {ShortcutService} from './core-ui/shortcut/shortcut.service';
import {GlobalConfigService} from './features/config/global-config.service';
import {blendInOutAnimation} from './ui/animations/blend-in-out.ani';
import {LayoutService} from './core-ui/layout/layout.service';
import {IPC} from '../../electron/ipc-events.const';
import {SnackService} from './core/snack/snack.service';
import {IS_ELECTRON} from './app.constants';
import {SwUpdate} from '@angular/service-worker';
import {BookmarkService} from './features/bookmark/bookmark.service';
import {expandAnimation} from './ui/animations/expand.ani';
import {warpRouteAnimation} from './ui/animations/warp-route';
import {filter, map, take} from 'rxjs/operators';
import {combineLatest, Observable, Subscription} from 'rxjs';
import {Store} from '@ngrx/store';
import {fadeAnimation} from './ui/animations/fade.ani';
import {selectIsTaskDataLoaded} from './features/tasks/store/task.selectors';
import {BannerService} from './core/banner/banner.service';
import {SS_WEB_APP_INSTALL} from './core/persistence/ls-keys.const';
import {BannerId} from './core/banner/banner.model';
import {T} from './t.const';
import {TranslateService} from '@ngx-translate/core';
import {GlobalThemeService} from './core/theme/global-theme.service';
import {UiHelperService} from './features/ui-helper/ui-helper.service';
import {TaskService} from './features/tasks/task.service';
import {LanguageService} from './core/language/language.service';
import {ElectronService} from './core/electron/electron.service';
import {AttachmentService} from './features/attachment/attachment.service';
import {WorkContextService} from './features/work-context/work-context.service';
import {TagService} from './features/tag/tag.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
animations: [
blendInOutAnimation,
expandAnimation,
warpRouteAnimation,
fadeAnimation
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent implements OnDestroy {
isAllDataLoadedInitially$: Observable<boolean> = combineLatest([
this._projectService.isRelatedDataLoadedForCurrentProject$,
this._store.select(selectIsTaskDataLoaded),
]).pipe(
map(([isProjectDataLoaded, isTaskDataLoaded]) => isProjectDataLoaded && isTaskDataLoaded),
filter(isLoaded => isLoaded),
take(1),
);
@ViewChild('notesElRef', {read: ViewContainerRef}) notesElRef: ViewContainerRef;
@ViewChild('sideNavElRef', {read: ViewContainerRef}) sideNavElRef: ViewContainerRef;
isRTL: boolean;
private _subs: Subscription = new Subscription();
constructor(
private _configService: GlobalConfigService,
private _shortcutService: ShortcutService,
private _bannerService: BannerService,
private _projectService: ProjectService,
private _electronService: ElectronService,
private _snackService: SnackService,
private _chromeExtensionInterface: ChromeExtensionInterfaceService,
private _swUpdate: SwUpdate,
private _translateService: TranslateService,
private _globalThemeService: GlobalThemeService,
private _uiHelperService: UiHelperService,
private _store: Store<any>,
private _languageService: LanguageService,
private _attachmentService: AttachmentService,
private _workContextService: WorkContextService,
private _tagService: TagService,
public readonly layoutService: LayoutService,
public readonly bookmarkService: BookmarkService,
public readonly taskService: TaskService,
public projectService: ProjectService,
) {
this._subs = this._languageService.isLangRTL.subscribe((val) => {
this.isRTL = val;
document.dir = this.isRTL ? 'rtl' : 'ltr';
});
// try to avoid data-loss
if (navigator.storage && navigator.storage.persist) {
navigator.storage.persist().then(granted => {
if (granted) {
console.log('Persistent store granted');
} else {
const msg = this._translateService.instant(T.GLOBAL_SNACK.PERSISTENCE_DISALLOWED);
console.warn('Persistence not allowed');
this._snackService.open({msg});
}
});
}
// TODO we are better than this
// LOAD GLOBAL MODELS
this.taskService.load();
this._attachmentService.load();
this._projectService.load();
this._configService.load();
this._workContextService.load();
this._tagService.load();
// init theme and body class handlers
this._globalThemeService.init();
if (IS_ELECTRON) {
this._electronService.ipcRenderer.send(IPC.APP_READY);
this._initElectronErrorHandler();
this._uiHelperService.initElectron();
this._electronService.ipcRenderer.on(IPC.TRANSFER_SETTINGS_REQUESTED, () => {
this._electronService.ipcRenderer.send(IPC.TRANSFER_SETTINGS_TO_ELECTRON, this._configService.cfg);
});
} else {
// WEB VERSION
this._chromeExtensionInterface.init();
if (this._swUpdate.isEnabled) {
this._swUpdate.available.subscribe(() => {
if (confirm(this._translateService.instant(T.APP.UPDATE_WEB_APP))) {
window.location.reload();
}
});
}
window.addEventListener('beforeunload', (e) => {
if (this._configService.cfg.misc.isConfirmBeforeExit) {
e.preventDefault();
e.returnValue = '';
}
});
}
}
@HostListener('document:keydown', ['$event']) onKeyDown(ev: KeyboardEvent) {
this._shortcutService.handleKeyDown(ev);
}
// prevent page reloads on missed drops
@HostListener('document:dragover', ['$event']) onDragOver(ev: Event) {
ev.preventDefault();
}
@HostListener('document:drop', ['$event']) onDrop(ev: Event) {
ev.preventDefault();
}
@HostListener('document:paste', ['$event']) onPaste(ev: Event) {
this.bookmarkService.createFromPaste(ev);
}
@HostListener('window:beforeinstallprompt', ['$event']) onBeforeInstallPrompt(e: any) {
if (IS_ELECTRON || sessionStorage.getItem(SS_WEB_APP_INSTALL)) {
return;
}
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
this._bannerService.open({
id: BannerId.InstallWebApp,
msg: T.APP.B_INSTALL.MSG,
action: {
label: T.APP.B_INSTALL.INSTALL,
fn: () => {
e.prompt();
}
},
action2: {
label: T.APP.B_INSTALL.IGNORE,
fn: () => {
sessionStorage.setItem(SS_WEB_APP_INSTALL, 'true');
}
}
});
}
getPage(outlet) {
return outlet.activatedRouteData.page || 'one';
}
scrollToNotes() {
this.notesElRef.element.nativeElement.scrollIntoView({behavior: 'smooth'});
}
scrollToSidenav() {
this.sideNavElRef.element.nativeElement.scrollIntoView({behavior: 'smooth'});
}
ngOnDestroy() {
this._subs.unsubscribe();
}
private _initElectronErrorHandler() {
this._electronService.ipcRenderer.on(IPC.ERROR, (ev, data: {
error: any,
stack: any,
errorStr: string,
}) => {
const errMsg = (typeof data.errorStr === 'string')
? data.errorStr
: ' INVALID ERROR MSG :( ';
this._snackService.open({
msg: errMsg,
type: 'ERROR'
});
console.error(data);
});
}
}