More TypeScript

This commit is contained in:
Mo Bitar
2020-04-10 08:42:35 -05:00
parent e7651fe92b
commit b5b53fdc43
87 changed files with 256 additions and 2085 deletions

View File

@@ -1,20 +1,28 @@
import { WebApplication } from './../../application';
import { ApplicationEvent } from 'snjs';
export type CtrlState = Partial<Record<string, any>>
export type CtrlProps = Partial<Record<string, any>>
export class PureCtrl {
$timeout: ng.ITimeoutService
/** Passed through templates */
application?: WebApplication
props: CtrlProps = {}
state: CtrlState = {}
private unsubApp: any
private unsubState: any
private stateTimeout: any
/* @ngInject */
constructor($timeout) {
if(!$timeout) {
throw Error('$timeout must not be null');
}
constructor($timeout: ng.ITimeoutService) {
this.$timeout = $timeout;
this.props = {};
this.state = {};
/* Allow caller constructor to finish setting instance variables */
setImmediate(() => {
this.state = this.getInitialState();
});
}
$onInit() {
this.addAppEventObserver();
this.addAppStateObserver();
@@ -23,9 +31,9 @@ export class PureCtrl {
deinit() {
this.unsubApp();
this.unsubState();
this.unsubApp = null;
this.unsubState = null;
this.application = null;
this.unsubApp = undefined;
this.unsubState = undefined;
this.application = undefined;
if (this.stateTimeout) {
this.$timeout.cancel(this.stateTimeout);
}
@@ -46,8 +54,8 @@ export class PureCtrl {
return {};
}
async setState(state) {
if(!this.$timeout) {
async setState(state: CtrlState) {
if (!this.$timeout) {
return;
}
return new Promise((resolve) => {
@@ -58,7 +66,7 @@ export class PureCtrl {
});
}
initProps(props) {
initProps(props: CtrlProps) {
if (Object.keys(this.props).length > 0) {
throw 'Already init-ed props.';
}
@@ -66,23 +74,24 @@ export class PureCtrl {
}
addAppStateObserver() {
this.unsubState = this.application.getAppState().addObserver((eventName, data) => {
this.onAppStateEvent(eventName, data);
});
this.unsubState = this.application!.getAppState()
.addObserver((eventName: any, data: any) => {
this.onAppStateEvent(eventName, data);
});
}
onAppStateEvent(eventName, data) {
onAppStateEvent(eventName: any, data: any) {
/** Optional override */
}
addAppEventObserver() {
if (this.application.isStarted()) {
if (this.application!.isStarted()) {
this.onAppStart();
}
if (this.application.isLaunched()) {
if (this.application!.isLaunched()) {
this.onAppLaunch();
}
this.unsubApp = this.application.addEventObserver(async (eventName) => {
this.unsubApp = this.application!.addEventObserver(async (eventName) => {
this.onAppEvent(eventName);
if (eventName === ApplicationEvent.Started) {
await this.onAppStart();
@@ -96,7 +105,7 @@ export class PureCtrl {
});
}
onAppEvent(eventName) {
onAppEvent(eventName: ApplicationEvent) {
/** Optional override */
}

View File

@@ -1,6 +1,6 @@
import { getPlatformString } from '@/utils';
import template from '%/application-view.pug';
import { AppStateEvents } from '@/services/state';
import { AppStateEvent } from '@/services/state';
import { ApplicationEvent } from 'snjs';
import angular from 'angular';
import {
@@ -127,7 +127,7 @@ class ApplicationViewCtrl extends PureCtrl {
/** @override */
async onAppStateEvent(eventName, data) {
if (eventName === AppStateEvents.PanelResized) {
if (eventName === AppStateEvent.PanelResized) {
if (data.panel === PANEL_NAME_NOTES) {
this.notesCollapsed = data.collapsed;
}
@@ -138,7 +138,7 @@ class ApplicationViewCtrl extends PureCtrl {
if (this.notesCollapsed) { appClass += "collapsed-notes"; }
if (this.tagsCollapsed) { appClass += " collapsed-tags"; }
this.setState({ appClass });
} else if (eventName === AppStateEvents.WindowDidFocus) {
} else if (eventName === AppStateEvent.WindowDidFocus) {
if (!(await this.application.isLocked())) {
this.application.sync();
}

View File

@@ -3,14 +3,14 @@ import {
ApplicationEvent,
isPayloadSourceRetrieved,
ContentTypes,
ProtectedActions
ProtectedAction
} from 'snjs';
import find from 'lodash/find';
import { isDesktopApplication } from '@/utils';
import { KeyboardModifiers, KeyboardKeys } from '@/services/keyboardManager';
import template from '%/editor.pug';
import { PureCtrl } from '@Controllers';
import { AppStateEvents, EventSources } from '@/services/state';
import { AppStateEvent, EventSource } from '@/services/state';
import {
STRING_DELETED_NOTE,
STRING_INVALID_NOTE,
@@ -98,12 +98,12 @@ class EditorCtrl extends PureCtrl {
/** @override */
onAppStateEvent(eventName, data) {
if (eventName === AppStateEvents.NoteChanged) {
if (eventName === AppStateEvent.NoteChanged) {
this.handleNoteSelectionChange(
this.application.getAppState().getSelectedNote(),
data.previousNote
);
} else if (eventName === AppStateEvents.PreferencesChanged) {
} else if (eventName === AppStateEvent.PreferencesChanged) {
this.reloadPreferences();
}
}
@@ -496,7 +496,7 @@ class EditorCtrl extends PureCtrl {
focusEditor() {
const element = document.getElementById(ElementIds.NoteTextEditor);
if (element) {
this.lastEditorFocusEventSource = EventSources.Script;
this.lastEditorFocusEventSource = EventSource.Script;
element.focus();
}
}
@@ -568,11 +568,11 @@ class EditorCtrl extends PureCtrl {
});
};
const requiresPrivilege = await this.application.privilegesService.actionRequiresPrivilege(
ProtectedActions.DeleteNote
ProtectedAction.DeleteNote
);
if (requiresPrivilege) {
this.application.presentPrivilegesModal(
ProtectedActions.DeleteNote,
ProtectedAction.DeleteNote,
() => {
run();
}
@@ -656,7 +656,7 @@ class EditorCtrl extends PureCtrl {
/** Show privileges manager if protection is not yet set up */
this.application.privilegesService.actionHasPrivilegesConfigured(
ProtectedActions.ViewProtectedNotes
ProtectedAction.ViewProtectedNotes
).then((configured) => {
if (!configured) {
this.application.presentPrivilegesManagementModal();

View File

@@ -2,11 +2,11 @@ import { dateToLocalizedString } from '@/utils';
import {
ApplicationEvent,
TIMING_STRATEGY_FORCE_SPAWN_NEW,
ProtectedActions,
ProtectedAction,
ContentTypes
} from 'snjs';
import template from '%/footer.pug';
import { AppStateEvents, EventSources } from '@/services/state';
import { AppStateEvent, EventSource } from '@/services/state';
import {
STRING_GENERIC_SYNC_ERROR,
STRING_NEW_UPDATE_READY
@@ -96,16 +96,16 @@ class FooterCtrl extends PureCtrl {
/** @override */
onAppStateEvent(eventName, data) {
if (eventName === AppStateEvents.EditorFocused) {
if (data.eventSource === EventSources.UserInteraction) {
if (eventName === AppStateEvent.EditorFocused) {
if (data.eventSource === EventSource.UserInteraction) {
this.closeAllRooms();
this.closeAccountMenu();
}
} else if (eventName === AppStateEvents.BeganBackupDownload) {
} else if (eventName === AppStateEvent.BeganBackupDownload) {
this.backupStatus = this.application.getStatusService().addStatusFromString(
"Saving local backup..."
);
} else if (eventName === AppStateEvents.EndedBackupDownload) {
} else if (eventName === AppStateEvent.EndedBackupDownload) {
if (data.success) {
this.backupStatus = this.application.getStatusService().replaceStatusWithString(
this.backupStatus,
@@ -363,11 +363,11 @@ class FooterCtrl extends PureCtrl {
if (!room.showRoom) {
const requiresPrivilege = await this.application.privilegesService.actionRequiresPrivilege(
ProtectedActions.ManageExtensions
ProtectedAction.ManageExtensions
);
if (requiresPrivilege) {
this.application.presentPrivilegesModal(
ProtectedActions.ManageExtensions,
ProtectedAction.ManageExtensions,
run
);
} else {

View File

@@ -1,5 +1,5 @@
import template from '%/lock-screen.pug';
import { AppStateEvents } from '@/services/state';
import { AppStateEvent } from '@/services/state';
import { PureCtrl } from './abstract/pure_ctrl';
const ELEMENT_ID_PASSCODE_INPUT = 'passcode-input';
@@ -28,7 +28,7 @@ class LockScreenCtrl extends PureCtrl {
/** @override */
async onAppStateEvent(eventName, data) {
if (eventName === AppStateEvents.WindowDidFocus) {
if (eventName === AppStateEvent.WindowDidFocus) {
const input = this.passcodeInput;
if (input) {
input.focus();

View File

@@ -2,7 +2,7 @@ import angular from 'angular';
import template from '%/notes.pug';
import { ApplicationEvent, ContentTypes, removeFromArray } from 'snjs';
import { PureCtrl } from '@Controllers';
import { AppStateEvents } from '@/services/state';
import { AppStateEvent } from '@/services/state';
import { KeyboardModifiers, KeyboardKeys } from '@/services/keyboardManager';
import {
PrefKeys
@@ -89,14 +89,14 @@ class NotesCtrl extends PureCtrl {
/** @override */
onAppStateEvent(eventName, data) {
if (eventName === AppStateEvents.TagChanged) {
if (eventName === AppStateEvent.TagChanged) {
this.handleTagChange(this.application.getAppState().getSelectedTag(), data.previousTag);
} else if (eventName === AppStateEvents.NoteChanged) {
} else if (eventName === AppStateEvent.NoteChanged) {
this.handleNoteSelection(this.application.getAppState().getSelectedNote());
} else if (eventName === AppStateEvents.PreferencesChanged) {
} else if (eventName === AppStateEvent.PreferencesChanged) {
this.reloadPreferences();
this.reloadNotes();
} else if (eventName === AppStateEvents.EditorFocused) {
} else if (eventName === AppStateEvent.EditorFocused) {
this.setShowMenuFalse();
}
}
@@ -384,10 +384,10 @@ class NotesCtrl extends PureCtrl {
if (width && this.panelPuppet.ready) {
this.panelPuppet.setWidth(width);
if (this.panelPuppet.isCollapsed()) {
this.application.getAppState().panelDidResize({
name: PANEL_NAME_NOTES,
collapsed: this.panelPuppet.isCollapsed()
});
this.application.getAppState().panelDidResize(
PANEL_NAME_NOTES,
this.panelPuppet.isCollapsed()
);
}
}
}
@@ -398,10 +398,10 @@ class NotesCtrl extends PureCtrl {
newWidth
);
this.application.getPrefsService().syncUserPreferences();
this.application.getAppState().panelDidResize({
name: PANEL_NAME_NOTES,
collapsed: isCollapsed
});
this.application.getAppState().panelDidResize(
PANEL_NAME_NOTES,
isCollapsed
);
}
paginate() {

View File

@@ -6,7 +6,7 @@ import {
ComponentActions
} from 'snjs';
import template from '%/tags.pug';
import { AppStateEvents } from '@/services/state';
import { AppStateEvent } from '@/services/state';
import { PANEL_NAME_TAGS } from '@/controllers/constants';
import { PrefKeys } from '@/services/preferencesManager';
import { STRING_DELETE_TAG } from '@/strings';
@@ -94,9 +94,9 @@ class TagsPanelCtrl extends PureCtrl {
/** @override */
onAppStateEvent(eventName, data) {
if (eventName === AppStateEvents.PreferencesChanged) {
if (eventName === AppStateEvent.PreferencesChanged) {
this.loadPreferences();
} else if (eventName === AppStateEvents.TagChanged) {
} else if (eventName === AppStateEvent.TagChanged) {
this.setState({
selectedTag: this.application.getAppState().getSelectedTag()
});
@@ -146,10 +146,10 @@ class TagsPanelCtrl extends PureCtrl {
if (width) {
this.panelPuppet.setWidth(width);
if (this.panelPuppet.isCollapsed()) {
this.application.getAppState().panelDidResize({
name: PANEL_NAME_TAGS,
collapsed: this.panelPuppet.isCollapsed()
});
this.application.getAppState().panelDidResize(
PANEL_NAME_TAGS,
this.panelPuppet.isCollapsed()
);
}
}
}
@@ -160,10 +160,10 @@ class TagsPanelCtrl extends PureCtrl {
newWidth,
true
);
this.application.getAppState().panelDidResize({
name: PANEL_NAME_TAGS,
collapsed: isCollapsed
});
this.application.getAppState().panelDidResize(
PANEL_NAME_TAGS,
isCollapsed
);
}
registerComponentHandler() {