smoosic 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/build/smoosic.js +167 -74
  2. package/package.json +82 -77
  3. package/release/smoosic.js +171 -78
  4. package/release/styles/menus.css +26 -0
  5. package/specs/022-text-reposition-on-resize/spec.md +1 -0
  6. package/specs/027-fix-bottom-modifier-lookup/checklists/requirements.md +38 -0
  7. package/specs/027-fix-bottom-modifier-lookup/contracts/division-lookup-contract.md +51 -0
  8. package/specs/027-fix-bottom-modifier-lookup/data-model.md +43 -0
  9. package/specs/027-fix-bottom-modifier-lookup/plan.md +77 -0
  10. package/specs/027-fix-bottom-modifier-lookup/quickstart.md +36 -0
  11. package/specs/027-fix-bottom-modifier-lookup/research.md +58 -0
  12. package/specs/027-fix-bottom-modifier-lookup/spec.md +105 -0
  13. package/specs/027-fix-bottom-modifier-lookup/tasks.md +167 -0
  14. package/specs/028-unsaved-changes-indicator/checklists/requirements.md +39 -0
  15. package/specs/028-unsaved-changes-indicator/contracts/score-change-contract.md +44 -0
  16. package/specs/028-unsaved-changes-indicator/data-model.md +64 -0
  17. package/specs/028-unsaved-changes-indicator/plan.md +96 -0
  18. package/specs/028-unsaved-changes-indicator/quickstart.md +54 -0
  19. package/specs/028-unsaved-changes-indicator/research.md +68 -0
  20. package/specs/028-unsaved-changes-indicator/spec.md +135 -0
  21. package/specs/028-unsaved-changes-indicator/tasks.md +173 -0
  22. package/specs/029-remove-promise-helpers/checklists/requirements.md +39 -0
  23. package/specs/029-remove-promise-helpers/contracts/public-api-contract.md +32 -0
  24. package/specs/029-remove-promise-helpers/data-model.md +45 -0
  25. package/specs/029-remove-promise-helpers/plan.md +86 -0
  26. package/specs/029-remove-promise-helpers/quickstart.md +53 -0
  27. package/specs/029-remove-promise-helpers/research.md +71 -0
  28. package/specs/029-remove-promise-helpers/spec.md +107 -0
  29. package/specs/029-remove-promise-helpers/tasks.md +164 -0
  30. package/specs/030-copy-paste-voices/checklists/requirements.md +39 -0
  31. package/specs/030-copy-paste-voices/contracts/voice-copy-paste-contract.md +47 -0
  32. package/specs/030-copy-paste-voices/data-model.md +63 -0
  33. package/specs/030-copy-paste-voices/plan.md +80 -0
  34. package/specs/030-copy-paste-voices/quickstart.md +50 -0
  35. package/specs/030-copy-paste-voices/research.md +80 -0
  36. package/specs/030-copy-paste-voices/spec.md +124 -0
  37. package/specs/030-copy-paste-voices/tasks.md +183 -0
  38. package/specs/031-select-voice/checklists/requirements.md +39 -0
  39. package/specs/031-select-voice/contracts/select-voice-contract.md +43 -0
  40. package/specs/031-select-voice/data-model.md +47 -0
  41. package/specs/031-select-voice/plan.md +79 -0
  42. package/specs/031-select-voice/quickstart.md +43 -0
  43. package/specs/031-select-voice/research.md +55 -0
  44. package/specs/031-select-voice/spec.md +113 -0
  45. package/specs/031-select-voice/tasks.md +171 -0
  46. package/src/application/application.ts +464 -459
  47. package/src/common/promiseHelpers.ts +2 -2
  48. package/src/render/audio/samples.ts +117 -118
  49. package/src/render/sui/pageDivisions.ts +58 -0
  50. package/src/render/sui/renderState.ts +469 -464
  51. package/src/render/sui/scoreChangeState.ts +62 -0
  52. package/src/render/sui/scoreView.ts +30 -0
  53. package/src/render/sui/scoreViewOperations.ts +2555 -2380
  54. package/src/render/sui/svgPageMap.ts +16 -17
  55. package/src/render/sui/textEdit.ts +310 -311
  56. package/src/render/sui/tracker.ts +40 -0
  57. package/src/render/vex/vxNote.ts +3 -1
  58. package/src/smo/xform/copypaste.ts +304 -4
  59. package/src/smo/xform/selections.ts +68 -0
  60. package/src/smo/xform/undo.ts +9 -0
  61. package/src/styles/menus.css +26 -0
  62. package/src/ui/buttons/ribbon.ts +3 -1
  63. package/src/ui/components/buttons/sidebar.vue +15 -6
  64. package/src/ui/components/dialogs/library.vue +3 -1
  65. package/src/ui/components/dialogs/unsavedChanges.vue +44 -0
  66. package/src/ui/dialogs/adapter.ts +181 -183
  67. package/src/ui/dialogs/addMeasure.ts +97 -97
  68. package/src/ui/dialogs/annotationVue.ts +56 -56
  69. package/src/ui/dialogs/audioSettings.ts +121 -123
  70. package/src/ui/dialogs/chordChangeVue.ts +46 -46
  71. package/src/ui/dialogs/clefChange.ts +53 -53
  72. package/src/ui/dialogs/customTuplets.ts +95 -96
  73. package/src/ui/dialogs/customTupletsVue.ts +47 -47
  74. package/src/ui/dialogs/dialog.ts +627 -620
  75. package/src/ui/dialogs/durations.ts +34 -34
  76. package/src/ui/dialogs/dynamics.ts +140 -142
  77. package/src/ui/dialogs/factory.ts +198 -198
  78. package/src/ui/dialogs/fileDialogs.ts +245 -179
  79. package/src/ui/dialogs/fonts.ts +93 -93
  80. package/src/ui/dialogs/globalLayout.ts +40 -40
  81. package/src/ui/dialogs/gracenote.ts +31 -31
  82. package/src/ui/dialogs/hairpin.ts +108 -110
  83. package/src/ui/dialogs/hairpinVue.ts +51 -51
  84. package/src/ui/dialogs/instrument.ts +111 -111
  85. package/src/ui/dialogs/keySignature.ts +175 -176
  86. package/src/ui/dialogs/keySignatureVue.ts +47 -47
  87. package/src/ui/dialogs/library.ts +207 -207
  88. package/src/ui/dialogs/libraryVue.ts +62 -48
  89. package/src/ui/dialogs/lyricVue.ts +46 -46
  90. package/src/ui/dialogs/measureFormat.ts +78 -78
  91. package/src/ui/dialogs/microtones.ts +147 -147
  92. package/src/ui/dialogs/noteHead.ts +319 -319
  93. package/src/ui/dialogs/pageLayout.ts +61 -61
  94. package/src/ui/dialogs/partInfo.ts +60 -60
  95. package/src/ui/dialogs/pedalMarking.ts +117 -117
  96. package/src/ui/dialogs/pedalMarkingVue.ts +73 -73
  97. package/src/ui/dialogs/pitch.ts +352 -352
  98. package/src/ui/dialogs/preferences.ts +40 -40
  99. package/src/ui/dialogs/rehearsalMark.ts +64 -64
  100. package/src/ui/dialogs/rehearsalMarkVue.ts +57 -57
  101. package/src/ui/dialogs/scoreId.ts +35 -36
  102. package/src/ui/dialogs/scoreView.ts +42 -42
  103. package/src/ui/dialogs/slur.ts +308 -310
  104. package/src/ui/dialogs/slurVue.ts +91 -91
  105. package/src/ui/dialogs/staffGroup.ts +112 -112
  106. package/src/ui/dialogs/tabNote.ts +84 -84
  107. package/src/ui/dialogs/tabStave.ts +182 -182
  108. package/src/ui/dialogs/tempo.ts +261 -263
  109. package/src/ui/dialogs/tempoVue.ts +73 -73
  110. package/src/ui/dialogs/textBlockVue.ts +127 -127
  111. package/src/ui/dialogs/textBracket.ts +114 -116
  112. package/src/ui/dialogs/textBracketVue.ts +56 -56
  113. package/src/ui/dialogs/tie.ts +140 -142
  114. package/src/ui/dialogs/timeSignature.ts +65 -66
  115. package/src/ui/dialogs/transposeScore.ts +39 -39
  116. package/src/ui/dialogs/unsavedChanges.ts +43 -0
  117. package/src/ui/dialogs/volta.ts +120 -122
  118. package/src/ui/dialogs/voltaVue.ts +51 -51
  119. package/src/ui/fileio/library.ts +110 -111
  120. package/src/ui/menus/edit.ts +100 -1
  121. package/src/ui/menus/file.ts +19 -3
  122. package/tests/pageDivisions.ts +156 -0
  123. package/tests/promiseHelpersUsage.ts +142 -0
  124. package/tests/scoreChangeState.ts +102 -0
  125. package/tests/selectVoice.ts +189 -0
  126. package/tests/voiceCopyPaste.ts +417 -0
@@ -1,620 +1,627 @@
1
- // [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
2
- // Copyright (c) Aaron David Newman 2021.
3
- import { SvgHelpers } from '../../render/sui/svgHelpers';
4
- import { buildDom, InputTrapper, draggable, createTopDomContainer } from '../../common/htmlHelpers';
5
- // import { SmoTranslator } from '../i18n/language';
6
- import { SmoModifier } from '../../smo/data/score';
7
- import { SvgBox } from '../../smo/data/common';
8
- import { SuiTracker } from '../../render/sui/tracker';
9
- import { SuiScoreViewOperations } from '../../render/sui/scoreViewOperations';
10
- import { CompleteNotifier } from '../common';
11
- import { BrowserEventSource } from '../eventSource';
12
- import { UndoBuffer } from '../../smo/xform/undo';
13
- import { SuiDialogNotifier, DialogDefinitionElement,
14
- SuiComponentBase, DialogDefinitionOption, SuiBaseComponentParams, SmoDynamicComponentCtor } from './components/baseComponent';
15
- import { SuiScroller } from '../../render/sui/scroller';
16
- import { SmoNote } from '../../smo/data/note';
17
- import { EventHandler } from '../eventSource';
18
- import { SmoUiConfiguration } from '../configuration';
19
- import { PromiseHelpers } from '../../common/promiseHelpers';
20
- import { createApp, ref, Ref, watch, App } from 'vue';
21
- import { SuiNavigationDom } from '../navigation';
22
- import { layoutDebug, SuiNavigation } from '../../../typedoc';
23
-
24
-
25
- declare var $: any;
26
- /**
27
- * The key/mouse event handlers take this as a parameter when we are switching control
28
- * of the input, due to a modal menu or dialog.
29
- */
30
- class closeModalPromiser {
31
- closeModalPromise: Promise<void>;
32
- closeEvent: Ref<boolean>;
33
- createUnbindPromise = async () => {
34
- return new Promise<void>((resolve) => {
35
- watch(this.closeEvent, () => {
36
- resolve();
37
- });
38
- })
39
- }
40
- constructor(closeEvent: Ref<boolean>) {
41
- this.closeEvent = closeEvent;
42
- this.closeModalPromise = this.createUnbindPromise();
43
- }
44
- }
45
- export type DialogCallback = () => Promise<void>;
46
- /**
47
- * Parameters for installing a dialog. VUE-based dialog logic.
48
- * complete is a Ref that is set to true when the dialog is finished, used to
49
- * hand off keyboard control between dialogs and menus.
50
- * app is a VUE app, appParams are the parameters to pass to the app.
51
- * we override commitCb, cancelCb, and removeCb to manage the dialog lifetime.
52
- * root is the DOM id to mount the dialog in (without the '#')
53
- * @category SuiDialog
54
- */
55
- export interface DialogInstallParams {
56
- root: string,
57
- app: any,
58
- appParams: any,
59
- dialogParams: SuiDialogParams,
60
- commitCb: DialogCallback,
61
- cancelCb: DialogCallback,
62
- removeCb?: DialogCallback
63
- };
64
- /**
65
- * The callbacks can be confusing. Dialog CB button calls this callback,
66
- * which in turn calls the user-supplied callback, and then hides the dialog.
67
- * params.commitCb is the supplied callback,
68
- * but appParams.commitCb is set to this function.
69
- * @param params
70
- */
71
- export const InstallDialog = async (params: DialogInstallParams) => {
72
- // If the dialog is raised from another dialog or menu that hasn't closed, wait for it
73
- // to close. This is required so we can keep keyboard/mouse event manager in sync.
74
- if (params.dialogParams.startPromise) {
75
- await params.dialogParams.startPromise;
76
- }
77
- const complete: Ref<boolean> = ref(false);
78
- // Track the mounted app so we can explicitly unmount it on close (mirrors
79
- // SuiMenuManager's menuApp handling). replaceVueRoot only detaches the DOM
80
- // via jQuery .empty() the next time a dialog opens -- it never calls Vue's
81
- // own unmount -- so without this, a dismissed dialog's component tree stays
82
- // alive with its lifecycle hooks (onBeforeUnmount) never firing, letting
83
- // things like the lyric/chord/text editors' debounced preview timers fire
84
- // after close and redraw a cursor marker that never gets removed.
85
- let dialogApp: App | null = null;
86
-
87
- const trapper = new InputTrapper('#vue-modal-container');
88
- trapper.trap();
89
- const commitCb = async () => {
90
- complete.value = true;
91
- await params.commitCb();
92
- trapper.close();
93
- params.dialogParams.view.navigation.hideDialogModal();
94
- dialogApp?.unmount();
95
- }
96
- const cancelCb = async () => {
97
- complete.value = true;
98
- await params.cancelCb();
99
- trapper.close();
100
- params.dialogParams.view.navigation.hideDialogModal();
101
- dialogApp?.unmount();
102
- }
103
- const removeCb = async () => {
104
- if (params.removeCb) {
105
- await params.removeCb();
106
- }
107
- trapper.close();
108
- params.dialogParams.view.navigation.hideDialogModal();
109
- complete.value = true;
110
- dialogApp?.unmount();
111
- }
112
- $('#' + params.root).addClass('modal show fade');
113
- params.appParams.commitCb = commitCb;
114
- params.appParams.cancelCb = cancelCb;
115
- if (params.removeCb) {
116
- params.appParams.removeCb = removeCb;
117
- }
118
- dialogApp = createApp(params.app as any, params.appParams);
119
- dialogApp.mount('#' + params.root);
120
-
121
- // allow a dialog to be dismissed by esc.
122
- const evKey = async (evdata: any) => {
123
- if (evdata.key === 'Escape') {
124
- cancelCb();
125
- evdata.preventDefault();
126
- }
127
- }
128
- // We take over he keyboard for the modal dialog so the user doesn't change the score
129
- // while typing into the dialog. the 'completeNotifier' takes it back when we are done
130
- params.dialogParams.completeNotifier.unbindKeyboardForModal(new closeModalPromiser(complete));
131
- params.dialogParams.eventSource.bindKeydownHandler(evKey);
132
- params.dialogParams.view.navigation.showDialogModal();
133
- /* const cb = () => { };
134
- draggable({
135
- parent: $('#' + params.root).find('.attributeModal'),
136
- handle: $('#' + params.root).find('.jsDbMove'),
137
- animateDiv: '.draganime',
138
- cb,
139
- moveParent: true
140
- });*/
141
- }
142
-
143
- /**
144
- * The JSON dialog template is a declaritive structore for the html of the dialog
145
- * and components.
146
- * @param label for the dialog itself
147
- * @param elements a series of elements that define the component
148
- * @param staticText a hash of text for the dialog and components to use
149
- * @category SuiDialog
150
- */
151
- export interface DialogDefinition {
152
- label: string,
153
- elements: DialogDefinitionElement[],
154
- staticText: Record<string, string>[]
155
- }
156
- /**
157
- * A translation of the labels in DialogDefintionElement
158
- * @param label the component label
159
- * @param id used as a key in translation tool
160
- * @param options options for dropdown and other array components
161
- * @category SuiDialog
162
- */
163
- export interface DialogTranslationElement {
164
- label: string,
165
- id: string,
166
- options?: DialogDefinitionOption[]
167
- }
168
- /**
169
- * A translation of all the strings in the dialog itself, used
170
- * when switching languages.
171
- * @param ctor the constructor for the dialog class, used to call static methods
172
- * @param label the translated label
173
- * @param dialogElements the translated component json
174
- * @param staticText translated misc text
175
- * @category SuiDialog
176
- */
177
- export interface DialogTranslation {
178
- ctor: string,
179
- label: string,
180
- dialogElements: DialogTranslationElement[],
181
- staticText: Record<string, string>
182
- }
183
-
184
- export const DialogTranslations: DialogTranslation[] = [];
185
-
186
- /**
187
- * Dialog params always contain basic information about the runtime
188
- * for modal functionality
189
- * @param ctor dialog constructor
190
- * @param id DOM id for the dialog
191
- * @param tracker to get and set selections
192
- * @param completeNotifier used to take over key/mouse control
193
- * @param startPromise used if this is called from another modal element
194
- * @param view the MVVM object to change the score
195
- * @param eventSource event source to register for additional events like mouseup
196
- * @param undoBuffer used to create undo
197
- * @category SuiDialog
198
- */
199
- export interface SuiDialogParams {
200
- ctor: string,
201
- id: string,
202
- tracker: SuiTracker,
203
- completeNotifier: CompleteNotifier,
204
- startPromise: Promise<void> | null
205
- view: SuiScoreViewOperations,
206
- eventSource: BrowserEventSource,
207
- // definition: DialogDefinition,
208
- modifier?: any,
209
- config?: SmoUiConfiguration
210
- }
211
-
212
- /**
213
- * internal interface used to create the DOM
214
- * @internal
215
- */
216
- export interface SuiDomParams {
217
- id: string,
218
- top: number,
219
- left: number,
220
- label: string
221
- }
222
- /**
223
- * DOM interface for the dialog
224
- * @internal
225
- * @param element parent element
226
- * @param trapper used to trap focus events for the dialog
227
- */
228
- export interface DialogDom {
229
- element: any,
230
- trapper: any
231
- }
232
- export const suiDialogTranslate = (dialog: DialogDefinition, ctor: string): DialogTranslation => {
233
- const elements: DialogDefinitionElement[] = dialog.elements;
234
- const output: DialogTranslationElement[] = [];
235
- elements.forEach((element: DialogDefinitionElement) => {
236
- const component: Partial<DialogTranslationElement> = {};
237
- if (element.label) {
238
- component.label = element.label ?? '';
239
- component.id = element.smoName;
240
- if (element.options) {
241
- component.options = [];
242
-
243
- element.options.forEach((option) => {
244
- component.options!.push({ value: option.value, label: option.label, css: option.css });
245
- });
246
- }
247
- output.push(component as DialogTranslationElement);
248
- }
249
- });
250
- // convert static text from an array of name/value pairs to a record for translation
251
- const staticText: Record<string, string> = {};
252
- const dialogStaticText: Record<string, string>[] = dialog.staticText;
253
- if (dialogStaticText) {
254
- dialogStaticText.forEach((st) => {
255
- const key = Object.keys(st)[0];
256
- staticText[key] = st[key];
257
- });
258
- }
259
- return { ctor, label: dialog.label, dialogElements: output, staticText };
260
- }
261
- /**
262
- * Note: Most dialogs will inherit from SuiDialogAdapter, not SuiDialogBase.
263
- * You will only want to inherit from SuiDialogBase under 2 conditions:
264
- * 1. the dialog is triviailly simple, like an alert box that makes no changes to the score, or
265
- * 2. the dialog is extremely complicated in how it interacts with the user, such that a form-based approach won't work
266
- * @category SuiDialog
267
- */
268
- export abstract class SuiDialogBase extends SuiDialogNotifier {
269
- static get displayOptions(): Record<string, string> {
270
- return {
271
- BINDCOMPONENTS: 'bindComponents', DRAGGABLE: 'makeDraggable',
272
- KEYBOARD_CAPTURE: 'captureKeyboardPromise', GLOBALPOS: 'positionGlobally',
273
- SELECTIONPOS: 'positionFromSelection', MODIFIERPOS: 'positionFromModifier',
274
- HIDEREMOVE: 'hideRemoveButton'
275
- };
276
- }
277
-
278
- static getStaticText(staticText: Record<string, string>[]) {
279
- const rv: Record<string, string> = {};
280
- staticText.forEach((st) => {
281
- const key = Object.keys(st)[0];
282
- rv[key] = st[key];
283
- });
284
- return rv;
285
- }
286
- id: string;
287
- ctor: string;
288
- debug: layoutDebug;
289
- boundKeyboard: boolean;
290
- components: SuiComponentBase[] = [];
291
- boundComponents: SuiComponentBase[] = [];
292
- cmap: Record<string, SuiComponentBase> = {};
293
- scroller: SuiScroller;
294
- closeDialogPromise: Promise<void>;
295
- label: string;
296
- staticText: Record<string, string>[] = [];
297
- startPromise: Promise<void> | null;
298
- dialogElements: DialogDefinition;
299
- eventSource: BrowserEventSource;
300
- view: SuiScoreViewOperations;
301
- completeNotifier: CompleteNotifier;
302
- modifier: any;
303
- dgDom: DialogDom;
304
- displayOptions: string[] = ['BINDCOMPONENTS', 'DRAGGABLE', 'KEYBOARD_CAPTURE', 'GLOBALPOS', 'HIDEREMOVE'];
305
- keydownHandler: EventHandler | null = null;
306
- // ### SuiDialogBase ctor
307
- // Creates the DOM element for the dialog and gets some initial elements
308
- constructor(dialogElements: DialogDefinition, parameters: SuiDialogParams) {
309
- super();
310
- this.id = parameters.id;
311
- this.boundKeyboard = false;
312
- this.scroller = parameters.view.tracker.scroller;
313
- this.label = dialogElements.label;
314
- this.eventSource = parameters.eventSource;
315
- this.view = parameters.view;
316
- this.debug = parameters.view.debug;
317
- this.completeNotifier = parameters.completeNotifier;
318
- this.modifier = parameters.modifier;
319
- this.ctor = parameters.ctor;
320
-
321
- this.closeDialogPromise = new Promise<void>((resolve) => {
322
- $('body').off('dialogDismiss').on('dialogDismiss', () => {
323
- resolve();
324
- });
325
- });
326
- this.staticText = dialogElements.staticText;
327
-
328
- // If this dialog was spawned by a menu, wait for the menu to dismiss
329
- // before continuing.
330
- // this.startPromise = parameters.closeMenuPromise;
331
- this.startPromise = parameters.startPromise;
332
-
333
- this.dialogElements = dialogElements;
334
-
335
- const left = $(this.view.navigation.scrollContainer).offset().left + $(this.view.navigation.scrollContainer).width() / 2;
336
- const top = $(this.view.navigation.scrollContainer).offset().top + $(this.view.navigation.scrollContainer).height() / 2;
337
-
338
- this.dgDom = this._constructDialog(dialogElements, {
339
- id: 'dialog-' + this.id,
340
- top,
341
- left,
342
- label: this.label
343
- });
344
-
345
- // SmoTranslator.registerDialog(this.ctor);
346
- }
347
- // ### display
348
- // make3 the modal visible. bind events and elements.
349
- display() {
350
- $('body').addClass('showAttributeDialog');
351
- this.bindComponents();
352
- this.bindElements();
353
- this.applyDisplayOptions();
354
- this.initialValue();
355
- }
356
-
357
- // ### bindElements
358
- // bing the generic controls in most dialogs.
359
- bindElements() {
360
- var dgDom = this.dgDom;
361
- $(dgDom.element).find('.ok-button').off('click').on('click', () => {
362
- this.view.groupUndo(false);
363
- this.commit();
364
- this.complete();
365
- });
366
- $(dgDom.element).find('.cancel-button').off('click').on('click', () => {
367
- this.view.groupUndo(false);
368
- if (this.modifier) {
369
- this.modifier.cancel();
370
- }
371
- this.complete();
372
- });
373
- $(dgDom.element).find('.remove-button').off('click').on('click', () => {
374
- this.view.groupUndo(false);
375
- this.complete();
376
- });
377
- }
378
- bindComponents() {
379
- this.components.forEach((component) => {
380
- component.bind();
381
- });
382
- }
383
- initialValue(){
384
- if (this.modifier === null) {
385
- return;
386
- }
387
- this.boundComponents.forEach((comp) => {
388
- (comp as any).setValue((this.modifier as any)[comp.smoName]);
389
- });
390
- }
391
- async changed() {
392
- if (this.modifier === null) {
393
- return;
394
- }
395
- this.boundComponents.forEach((comp) => {
396
- if (comp.changeFlag) {
397
- (this.modifier as any)[comp.smoName] = (comp as any).getValue();
398
- }
399
- });
400
- }
401
- getId(): string {
402
- return this.id;
403
- }
404
- getModifier() : SmoModifier | null {
405
- return this.modifier ?? null;
406
- }
407
- getEventSource() {
408
- return this.eventSource;
409
- }
410
- getStaticText() {
411
- return SuiDialogBase.getStaticText(this.staticText);
412
- }
413
- async commit(): Promise<any> {
414
- return PromiseHelpers.emptyPromise();
415
- }
416
- get closeModalPromise(): Promise<any> {
417
- return this.closeDialogPromise;
418
- }
419
- // ### position
420
- // For dialogs based on selections, tries to place the dialog near the selection and also
421
- // to scroll so the dialog is in view
422
- static position(box: SvgBox, dgDom: DialogDom, scroller: SuiScroller, navigation: SuiNavigation) {
423
- let y = (box.y + box.height) - scroller.netScroll.y;
424
- let x = 0;
425
-
426
- // TODO: adjust if db is clipped by the browser.
427
- const dge = $(dgDom.element).find('.attributeModal');
428
- const dgeHeight: number = $(dge).height();
429
- const scrollRegion = $(navigation.scrollContainer);
430
- const domRegion = $(navigation.outerContainer);
431
- const maxY: number = scrollRegion.height();
432
- const maxX: number = scrollRegion.width();
433
- const offset: any = domRegion.offset();
434
- y = y - (offset.top as number);
435
-
436
- const offsetY = dgeHeight + y > window.innerHeight ? (dgeHeight + y) - window.innerHeight : 0;
437
- y = (y < 0) ? -y : y - offsetY;
438
-
439
- y = (y > maxY || y < 0) ? maxY / 2 : y;
440
-
441
- $(dge).css('top', '' + y + 'px');
442
-
443
- x = box.x - scroller.netScroll.x;
444
- x = x - (offset.left as number);
445
- const w: number = $(dge).width();
446
- x = (x > window.innerWidth / 2) ? x - (w + 25) : x + (w + 25);
447
-
448
- x = (x < 0 || x > maxX) ? maxX / 2 : x;
449
- $(dge).css('left', '' + x + 'px');
450
- }
451
- getView() {
452
- return this.view;
453
- }
454
- applyDisplayOptions() {
455
- $('body').addClass('showAttributeDialog');
456
- this.displayOptions.forEach((option) => {
457
- (this as any)[SuiDialogBase.displayOptions[option]]();
458
- });
459
- }
460
- // ### position
461
- // Position the dialog near a selection. If the dialog is not visible due
462
- // to scrolling, make sure it is visible.
463
- position(box: SvgBox) {
464
- SuiDialogBase.position(box, this.dgDom, this.view.tracker.scroller, this.view.navigation);
465
- }
466
- hideRemoveButton() {
467
- $(this.dgDom.element).find('.remove-button').remove();
468
- }
469
- // ### positionModifier()
470
- positionFromModifier() {
471
- if (!this.modifier || this.modifier.logicalBox === null) {
472
- this.positionGlobally();
473
- return;
474
- }
475
- const pageContext = this.view.renderer.pageMap.getRendererFromModifier(this.modifier);
476
- const screenBox = this.view.renderer.pageMap.svgToClient(this.modifier.logicalBox);
477
- this.position(screenBox);
478
- }
479
- // ### positionGlobally
480
- // position the dialog box in the center of the current scroll region
481
- positionGlobally() {
482
- const box = SvgHelpers.boxPoints(250, 250, 1, 1);
483
- SuiDialogBase.position(box, this.dgDom, this.view.tracker.scroller, this.view.navigation);
484
- }
485
- // ### postionFromSelection
486
- // set initial position of dialog based on first selection
487
- positionFromSelection() {
488
- const note: SmoNote | null = this.view.tracker.selections[0].note;
489
- if (note && note.logicalBox) {
490
- const screenBox = this.view.renderer.pageMap.svgToClient(note.logicalBox);
491
- this.position(screenBox);
492
- }
493
- }
494
- // ### build the html for the dialog, based on the instance-specific components.
495
- _constructDialog(dialogElements: DialogDefinition, parameters: SuiDomParams) {
496
- createTopDomContainer('.attributeDialog');
497
- const id = parameters.id;
498
- const b = buildDom;
499
- const r = b('div').classes('attributeModal').attr('id', 'attr-modal-' + id)
500
- .css('top', parameters.top + 'px').css('left', parameters.left + 'px')
501
- .append(b('spanb').classes('draggable button').append(b('span').classes('icon icon-move jsDbMove')))
502
- .append(b('h2').classes('dialog-label').text(this.label));
503
-
504
- var ctrl = b('div').classes('smoControlContainer');
505
- dialogElements.elements.filter((de) => de.control).forEach((de) => {
506
- const classes = de.classes ? de.classes : '';
507
- const compParams: SuiBaseComponentParams = {
508
- classes, id: id + de.smoName, ...de
509
- }
510
- try {
511
- const control: SuiComponentBase = SmoDynamicComponentCtor[de.control](this, compParams);
512
- this.components.push(control);
513
- this.cmap[de.smoName + 'Ctrl'] = control;
514
- ctrl.append(control.html);
515
- } catch (exp) {
516
- console.error('bad ctor ' + de.smoName);
517
- throw(exp);
518
- }
519
- });
520
- r.append(ctrl);
521
- r.append(
522
- b('div').classes('buttonContainer').append(
523
- b('button').classes('ok-button button-left btn btn-primary').text('OK')).append(
524
- b('button').classes('cancel-button button-center btn btn-secondary').text('Cancel')).append(
525
- b('button').classes('remove-button button-right btn btn-secondary').text('Remove').append(
526
- b('span').classes('icon icon-cancel-circle'))));
527
- $('.attributeDialog').html('');
528
-
529
- $('.attributeDialog').append(r.dom());
530
-
531
- const trapper = new InputTrapper('.attributeDialog');
532
- trapper.trap();
533
- $('.attributeDialog').find('.cancel-button').focus();
534
- return {
535
- element: $('.attributeDialog'),
536
- trapper
537
- };
538
- }
539
- // ### Complete
540
- // Dialogs take over the keyboard, so release that and trigger an event
541
- // that the dialog is closing that can resolve any outstanding promises.
542
- complete() {
543
- if (this.boundKeyboard && this.keydownHandler) {
544
- this.eventSource.unbindKeydownHandler(this.keydownHandler);
545
- }
546
- $('body').removeClass('showAttributeDialog');
547
- $('body').trigger('dialogDismiss');
548
- this.dgDom.trapper.close();
549
- }
550
- // ### makeDraggable
551
- // generic code to make the dialog box draggable so it doesn't
552
- // get in front of stuff.
553
- makeDraggable() {
554
- createTopDomContainer('.draganime');
555
- const cb = () => { };
556
- draggable({
557
- parent: $(this.dgDom.element).find('.attributeModal'),
558
- handle: $(this.dgDom.element).find('.jsDbMove'),
559
- animateDiv: '.draganime',
560
- cb,
561
- moveParent: true
562
- });
563
- }
564
- // ### captureKeyboardPromise
565
- // capture keyboard events until the dialog closes,
566
- // then give control back to the current keyboard
567
- async captureKeyboardPromise() {
568
- if (!(this.startPromise)) {
569
- this.completeNotifier.unbindKeyboardForModal(this);
570
- this.bindKeyboard();
571
- return;
572
- }
573
- if (this.startPromise) {
574
- await this.startPromise;
575
- this.completeNotifier.unbindKeyboardForModal(this);
576
- this.bindKeyboard();
577
- }
578
- }
579
-
580
- // ### handleKeydown
581
- // allow a dialog to be dismissed by esc.
582
- evKey(evdata: any) {
583
- if (evdata.key === 'Escape') {
584
- $(this.dgDom.element).find('.cancel-button').click();
585
- evdata.preventDefault();
586
- }
587
- }
588
-
589
- // ### bindKeyboard
590
- // generic logic to grab keyboard elements for modal
591
- bindKeyboard() {
592
- const evkey = async (ev: any) => {
593
- this.evKey(ev);
594
- }
595
- this.boundKeyboard = true;
596
- this.keydownHandler = this.eventSource.bindKeydownHandler(evkey);
597
- }
598
- }
599
- /**
600
- * Convenience functions
601
- * @param type
602
- * @param parameters
603
- * @returns a new dialog of type T
604
- * @category SuiDialog
605
- */
606
- export function dialogConstructor<T extends SuiDialogBase>(type: { new(parameters: SuiDialogParams): T; }, parameters: SuiDialogParams ): T {
607
- return new type(parameters);
608
- }
609
- /**
610
- * Convenience function to display a dialog
611
- * @category SuiDialog
612
- * @param ctor
613
- * @param parameters
614
- * @returns
615
- */
616
- export function createAndDisplayDialog<T extends SuiDialogBase>(ctor: new (parameters: SuiDialogParams) => T, parameters: SuiDialogParams): T {
617
- const instance: T = dialogConstructor<T>(ctor, parameters);
618
- instance.display();
619
- return instance;
620
- }
1
+ // [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
2
+ // Copyright (c) Aaron David Newman 2021.
3
+ import { SvgHelpers } from '../../render/sui/svgHelpers';
4
+ import { buildDom, InputTrapper, draggable, createTopDomContainer } from '../../common/htmlHelpers';
5
+ // import { SmoTranslator } from '../i18n/language';
6
+ import { SmoModifier } from '../../smo/data/score';
7
+ import { SvgBox } from '../../smo/data/common';
8
+ import { SuiTracker } from '../../render/sui/tracker';
9
+ import { SuiScoreViewOperations } from '../../render/sui/scoreViewOperations';
10
+ import { CompleteNotifier } from '../common';
11
+ import { BrowserEventSource } from '../eventSource';
12
+ import { UndoBuffer } from '../../smo/xform/undo';
13
+ import { SuiDialogNotifier, DialogDefinitionElement,
14
+ SuiComponentBase, DialogDefinitionOption, SuiBaseComponentParams, SmoDynamicComponentCtor } from './components/baseComponent';
15
+ import { SuiScroller } from '../../render/sui/scroller';
16
+ import { SmoNote } from '../../smo/data/note';
17
+ import { EventHandler } from '../eventSource';
18
+ import { SmoUiConfiguration } from '../configuration';
19
+ import { createApp, ref, Ref, watch, App } from 'vue';
20
+ import { SuiNavigationDom } from '../navigation';
21
+ import { layoutDebug, SuiNavigation } from '../../../typedoc';
22
+
23
+
24
+ declare var $: any;
25
+ /**
26
+ * The key/mouse event handlers take this as a parameter when we are switching control
27
+ * of the input, due to a modal menu or dialog.
28
+ */
29
+ class closeModalPromiser {
30
+ closeModalPromise: Promise<void>;
31
+ closeEvent: Ref<boolean>;
32
+ createUnbindPromise = async () => {
33
+ return new Promise<void>((resolve) => {
34
+ watch(this.closeEvent, () => {
35
+ resolve();
36
+ });
37
+ })
38
+ }
39
+ constructor(closeEvent: Ref<boolean>) {
40
+ this.closeEvent = closeEvent;
41
+ this.closeModalPromise = this.createUnbindPromise();
42
+ }
43
+ }
44
+ export type DialogCallback = () => Promise<void>;
45
+ /**
46
+ * Parameters for installing a dialog. VUE-based dialog logic.
47
+ * complete is a Ref that is set to true when the dialog is finished, used to
48
+ * hand off keyboard control between dialogs and menus.
49
+ * app is a VUE app, appParams are the parameters to pass to the app.
50
+ * we override commitCb, cancelCb, and removeCb to manage the dialog lifetime.
51
+ * root is the DOM id to mount the dialog in (without the '#')
52
+ * @category SuiDialog
53
+ */
54
+ export interface DialogInstallParams {
55
+ root: string,
56
+ app: any,
57
+ appParams: any,
58
+ dialogParams: SuiDialogParams,
59
+ commitCb: DialogCallback,
60
+ cancelCb: DialogCallback,
61
+ removeCb?: DialogCallback,
62
+ /**
63
+ * Called after the dialog has been committed, closed and unmounted (not on cancel). Use this
64
+ * to do something that needs another dialog, which can't be shown until this one is gone.
65
+ */
66
+ afterCloseCb?: DialogCallback
67
+ };
68
+ /**
69
+ * The callbacks can be confusing. Dialog CB button calls this callback,
70
+ * which in turn calls the user-supplied callback, and then hides the dialog.
71
+ * params.commitCb is the supplied callback,
72
+ * but appParams.commitCb is set to this function.
73
+ * @param params
74
+ */
75
+ export const InstallDialog = async (params: DialogInstallParams) => {
76
+ // If the dialog is raised from another dialog or menu that hasn't closed, wait for it
77
+ // to close. This is required so we can keep keyboard/mouse event manager in sync.
78
+ if (params.dialogParams.startPromise) {
79
+ await params.dialogParams.startPromise;
80
+ }
81
+ const complete: Ref<boolean> = ref(false);
82
+ // Track the mounted app so we can explicitly unmount it on close (mirrors
83
+ // SuiMenuManager's menuApp handling). replaceVueRoot only detaches the DOM
84
+ // via jQuery .empty() the next time a dialog opens -- it never calls Vue's
85
+ // own unmount -- so without this, a dismissed dialog's component tree stays
86
+ // alive with its lifecycle hooks (onBeforeUnmount) never firing, letting
87
+ // things like the lyric/chord/text editors' debounced preview timers fire
88
+ // after close and redraw a cursor marker that never gets removed.
89
+ let dialogApp: App | null = null;
90
+
91
+ const trapper = new InputTrapper('#vue-modal-container');
92
+ trapper.trap();
93
+ const commitCb = async () => {
94
+ complete.value = true;
95
+ await params.commitCb();
96
+ params.dialogParams.view.groupUndo(false);
97
+ trapper.close();
98
+ params.dialogParams.view.navigation.hideDialogModal();
99
+ dialogApp?.unmount();
100
+ if (params.afterCloseCb) {
101
+ await params.afterCloseCb();
102
+ }
103
+ }
104
+ const cancelCb = async () => {
105
+ complete.value = true;
106
+ await params.cancelCb();
107
+ trapper.close();
108
+ params.dialogParams.view.navigation.hideDialogModal();
109
+ dialogApp?.unmount();
110
+ }
111
+ const removeCb = async () => {
112
+ if (params.removeCb) {
113
+ await params.removeCb();
114
+ }
115
+ trapper.close();
116
+ params.dialogParams.view.navigation.hideDialogModal();
117
+ complete.value = true;
118
+ dialogApp?.unmount();
119
+ }
120
+ $('#' + params.root).addClass('modal show fade');
121
+ params.appParams.commitCb = commitCb;
122
+ params.appParams.cancelCb = cancelCb;
123
+ if (params.removeCb) {
124
+ params.appParams.removeCb = removeCb;
125
+ }
126
+ dialogApp = createApp(params.app as any, params.appParams);
127
+ dialogApp.mount('#' + params.root);
128
+
129
+ // allow a dialog to be dismissed by esc.
130
+ const evKey = async (evdata: any) => {
131
+ if (evdata.key === 'Escape') {
132
+ cancelCb();
133
+ evdata.preventDefault();
134
+ }
135
+ }
136
+ // We take over he keyboard for the modal dialog so the user doesn't change the score
137
+ // while typing into the dialog. the 'completeNotifier' takes it back when we are done
138
+ params.dialogParams.completeNotifier.unbindKeyboardForModal(new closeModalPromiser(complete));
139
+ params.dialogParams.eventSource.bindKeydownHandler(evKey);
140
+ params.dialogParams.view.navigation.showDialogModal();
141
+ /* const cb = () => { };
142
+ draggable({
143
+ parent: $('#' + params.root).find('.attributeModal'),
144
+ handle: $('#' + params.root).find('.jsDbMove'),
145
+ animateDiv: '.draganime',
146
+ cb,
147
+ moveParent: true
148
+ });*/
149
+ }
150
+
151
+ /**
152
+ * The JSON dialog template is a declaritive structore for the html of the dialog
153
+ * and components.
154
+ * @param label for the dialog itself
155
+ * @param elements a series of elements that define the component
156
+ * @param staticText a hash of text for the dialog and components to use
157
+ * @category SuiDialog
158
+ */
159
+ export interface DialogDefinition {
160
+ label: string,
161
+ elements: DialogDefinitionElement[],
162
+ staticText: Record<string, string>[]
163
+ }
164
+ /**
165
+ * A translation of the labels in DialogDefintionElement
166
+ * @param label the component label
167
+ * @param id used as a key in translation tool
168
+ * @param options options for dropdown and other array components
169
+ * @category SuiDialog
170
+ */
171
+ export interface DialogTranslationElement {
172
+ label: string,
173
+ id: string,
174
+ options?: DialogDefinitionOption[]
175
+ }
176
+ /**
177
+ * A translation of all the strings in the dialog itself, used
178
+ * when switching languages.
179
+ * @param ctor the constructor for the dialog class, used to call static methods
180
+ * @param label the translated label
181
+ * @param dialogElements the translated component json
182
+ * @param staticText translated misc text
183
+ * @category SuiDialog
184
+ */
185
+ export interface DialogTranslation {
186
+ ctor: string,
187
+ label: string,
188
+ dialogElements: DialogTranslationElement[],
189
+ staticText: Record<string, string>
190
+ }
191
+
192
+ export const DialogTranslations: DialogTranslation[] = [];
193
+
194
+ /**
195
+ * Dialog params always contain basic information about the runtime
196
+ * for modal functionality
197
+ * @param ctor dialog constructor
198
+ * @param id DOM id for the dialog
199
+ * @param tracker to get and set selections
200
+ * @param completeNotifier used to take over key/mouse control
201
+ * @param startPromise used if this is called from another modal element
202
+ * @param view the MVVM object to change the score
203
+ * @param eventSource event source to register for additional events like mouseup
204
+ * @param undoBuffer used to create undo
205
+ * @category SuiDialog
206
+ */
207
+ export interface SuiDialogParams {
208
+ ctor: string,
209
+ id: string,
210
+ tracker: SuiTracker,
211
+ completeNotifier: CompleteNotifier,
212
+ startPromise: Promise<void> | null
213
+ view: SuiScoreViewOperations,
214
+ eventSource: BrowserEventSource,
215
+ // definition: DialogDefinition,
216
+ modifier?: any,
217
+ config?: SmoUiConfiguration
218
+ }
219
+
220
+ /**
221
+ * internal interface used to create the DOM
222
+ * @internal
223
+ */
224
+ export interface SuiDomParams {
225
+ id: string,
226
+ top: number,
227
+ left: number,
228
+ label: string
229
+ }
230
+ /**
231
+ * DOM interface for the dialog
232
+ * @internal
233
+ * @param element parent element
234
+ * @param trapper used to trap focus events for the dialog
235
+ */
236
+ export interface DialogDom {
237
+ element: any,
238
+ trapper: any
239
+ }
240
+ export const suiDialogTranslate = (dialog: DialogDefinition, ctor: string): DialogTranslation => {
241
+ const elements: DialogDefinitionElement[] = dialog.elements;
242
+ const output: DialogTranslationElement[] = [];
243
+ elements.forEach((element: DialogDefinitionElement) => {
244
+ const component: Partial<DialogTranslationElement> = {};
245
+ if (element.label) {
246
+ component.label = element.label ?? '';
247
+ component.id = element.smoName;
248
+ if (element.options) {
249
+ component.options = [];
250
+
251
+ element.options.forEach((option) => {
252
+ component.options!.push({ value: option.value, label: option.label, css: option.css });
253
+ });
254
+ }
255
+ output.push(component as DialogTranslationElement);
256
+ }
257
+ });
258
+ // convert static text from an array of name/value pairs to a record for translation
259
+ const staticText: Record<string, string> = {};
260
+ const dialogStaticText: Record<string, string>[] = dialog.staticText;
261
+ if (dialogStaticText) {
262
+ dialogStaticText.forEach((st) => {
263
+ const key = Object.keys(st)[0];
264
+ staticText[key] = st[key];
265
+ });
266
+ }
267
+ return { ctor, label: dialog.label, dialogElements: output, staticText };
268
+ }
269
+ /**
270
+ * Note: Most dialogs will inherit from SuiDialogAdapter, not SuiDialogBase.
271
+ * You will only want to inherit from SuiDialogBase under 2 conditions:
272
+ * 1. the dialog is triviailly simple, like an alert box that makes no changes to the score, or
273
+ * 2. the dialog is extremely complicated in how it interacts with the user, such that a form-based approach won't work
274
+ * @category SuiDialog
275
+ */
276
+ export abstract class SuiDialogBase extends SuiDialogNotifier {
277
+ static get displayOptions(): Record<string, string> {
278
+ return {
279
+ BINDCOMPONENTS: 'bindComponents', DRAGGABLE: 'makeDraggable',
280
+ KEYBOARD_CAPTURE: 'captureKeyboardPromise', GLOBALPOS: 'positionGlobally',
281
+ SELECTIONPOS: 'positionFromSelection', MODIFIERPOS: 'positionFromModifier',
282
+ HIDEREMOVE: 'hideRemoveButton'
283
+ };
284
+ }
285
+
286
+ static getStaticText(staticText: Record<string, string>[]) {
287
+ const rv: Record<string, string> = {};
288
+ staticText.forEach((st) => {
289
+ const key = Object.keys(st)[0];
290
+ rv[key] = st[key];
291
+ });
292
+ return rv;
293
+ }
294
+ id: string;
295
+ ctor: string;
296
+ debug: layoutDebug;
297
+ boundKeyboard: boolean;
298
+ components: SuiComponentBase[] = [];
299
+ boundComponents: SuiComponentBase[] = [];
300
+ cmap: Record<string, SuiComponentBase> = {};
301
+ scroller: SuiScroller;
302
+ closeDialogPromise: Promise<void>;
303
+ label: string;
304
+ staticText: Record<string, string>[] = [];
305
+ startPromise: Promise<void> | null;
306
+ dialogElements: DialogDefinition;
307
+ eventSource: BrowserEventSource;
308
+ view: SuiScoreViewOperations;
309
+ completeNotifier: CompleteNotifier;
310
+ modifier: any;
311
+ dgDom: DialogDom;
312
+ displayOptions: string[] = ['BINDCOMPONENTS', 'DRAGGABLE', 'KEYBOARD_CAPTURE', 'GLOBALPOS', 'HIDEREMOVE'];
313
+ keydownHandler: EventHandler | null = null;
314
+ // ### SuiDialogBase ctor
315
+ // Creates the DOM element for the dialog and gets some initial elements
316
+ constructor(dialogElements: DialogDefinition, parameters: SuiDialogParams) {
317
+ super();
318
+ this.id = parameters.id;
319
+ this.boundKeyboard = false;
320
+ this.scroller = parameters.view.tracker.scroller;
321
+ this.label = dialogElements.label;
322
+ this.eventSource = parameters.eventSource;
323
+ this.view = parameters.view;
324
+ this.debug = parameters.view.debug;
325
+ this.completeNotifier = parameters.completeNotifier;
326
+ this.modifier = parameters.modifier;
327
+ this.ctor = parameters.ctor;
328
+
329
+ this.closeDialogPromise = new Promise<void>((resolve) => {
330
+ $('body').off('dialogDismiss').on('dialogDismiss', () => {
331
+ resolve();
332
+ });
333
+ });
334
+ this.staticText = dialogElements.staticText;
335
+
336
+ // If this dialog was spawned by a menu, wait for the menu to dismiss
337
+ // before continuing.
338
+ // this.startPromise = parameters.closeMenuPromise;
339
+ this.startPromise = parameters.startPromise;
340
+
341
+ this.dialogElements = dialogElements;
342
+
343
+ const left = $(this.view.navigation.scrollContainer).offset().left + $(this.view.navigation.scrollContainer).width() / 2;
344
+ const top = $(this.view.navigation.scrollContainer).offset().top + $(this.view.navigation.scrollContainer).height() / 2;
345
+
346
+ this.dgDom = this._constructDialog(dialogElements, {
347
+ id: 'dialog-' + this.id,
348
+ top,
349
+ left,
350
+ label: this.label
351
+ });
352
+
353
+ // SmoTranslator.registerDialog(this.ctor);
354
+ }
355
+ // ### display
356
+ // make3 the modal visible. bind events and elements.
357
+ display() {
358
+ $('body').addClass('showAttributeDialog');
359
+ this.bindComponents();
360
+ this.bindElements();
361
+ this.applyDisplayOptions();
362
+ this.initialValue();
363
+ }
364
+
365
+ // ### bindElements
366
+ // bing the generic controls in most dialogs.
367
+ bindElements() {
368
+ var dgDom = this.dgDom;
369
+ $(dgDom.element).find('.ok-button').off('click').on('click', () => {
370
+ this.view.groupUndo(false);
371
+ this.commit();
372
+ this.complete();
373
+ });
374
+ $(dgDom.element).find('.cancel-button').off('click').on('click', () => {
375
+ this.view.groupUndo(false);
376
+ if (this.modifier) {
377
+ this.modifier.cancel();
378
+ }
379
+ this.complete();
380
+ });
381
+ $(dgDom.element).find('.remove-button').off('click').on('click', () => {
382
+ this.view.groupUndo(false);
383
+ this.complete();
384
+ });
385
+ }
386
+ bindComponents() {
387
+ this.components.forEach((component) => {
388
+ component.bind();
389
+ });
390
+ }
391
+ initialValue(){
392
+ if (this.modifier === null) {
393
+ return;
394
+ }
395
+ this.boundComponents.forEach((comp) => {
396
+ (comp as any).setValue((this.modifier as any)[comp.smoName]);
397
+ });
398
+ }
399
+ async changed() {
400
+ if (this.modifier === null) {
401
+ return;
402
+ }
403
+ this.boundComponents.forEach((comp) => {
404
+ if (comp.changeFlag) {
405
+ (this.modifier as any)[comp.smoName] = (comp as any).getValue();
406
+ }
407
+ });
408
+ }
409
+ getId(): string {
410
+ return this.id;
411
+ }
412
+ getModifier() : SmoModifier | null {
413
+ return this.modifier ?? null;
414
+ }
415
+ getEventSource() {
416
+ return this.eventSource;
417
+ }
418
+ getStaticText() {
419
+ return SuiDialogBase.getStaticText(this.staticText);
420
+ }
421
+ async commit(): Promise<any> {
422
+ }
423
+ get closeModalPromise(): Promise<any> {
424
+ return this.closeDialogPromise;
425
+ }
426
+ // ### position
427
+ // For dialogs based on selections, tries to place the dialog near the selection and also
428
+ // to scroll so the dialog is in view
429
+ static position(box: SvgBox, dgDom: DialogDom, scroller: SuiScroller, navigation: SuiNavigation) {
430
+ let y = (box.y + box.height) - scroller.netScroll.y;
431
+ let x = 0;
432
+
433
+ // TODO: adjust if db is clipped by the browser.
434
+ const dge = $(dgDom.element).find('.attributeModal');
435
+ const dgeHeight: number = $(dge).height();
436
+ const scrollRegion = $(navigation.scrollContainer);
437
+ const domRegion = $(navigation.outerContainer);
438
+ const maxY: number = scrollRegion.height();
439
+ const maxX: number = scrollRegion.width();
440
+ const offset: any = domRegion.offset();
441
+ y = y - (offset.top as number);
442
+
443
+ const offsetY = dgeHeight + y > window.innerHeight ? (dgeHeight + y) - window.innerHeight : 0;
444
+ y = (y < 0) ? -y : y - offsetY;
445
+
446
+ y = (y > maxY || y < 0) ? maxY / 2 : y;
447
+
448
+ $(dge).css('top', '' + y + 'px');
449
+
450
+ x = box.x - scroller.netScroll.x;
451
+ x = x - (offset.left as number);
452
+ const w: number = $(dge).width();
453
+ x = (x > window.innerWidth / 2) ? x - (w + 25) : x + (w + 25);
454
+
455
+ x = (x < 0 || x > maxX) ? maxX / 2 : x;
456
+ $(dge).css('left', '' + x + 'px');
457
+ }
458
+ getView() {
459
+ return this.view;
460
+ }
461
+ applyDisplayOptions() {
462
+ $('body').addClass('showAttributeDialog');
463
+ this.displayOptions.forEach((option) => {
464
+ (this as any)[SuiDialogBase.displayOptions[option]]();
465
+ });
466
+ }
467
+ // ### position
468
+ // Position the dialog near a selection. If the dialog is not visible due
469
+ // to scrolling, make sure it is visible.
470
+ position(box: SvgBox) {
471
+ SuiDialogBase.position(box, this.dgDom, this.view.tracker.scroller, this.view.navigation);
472
+ }
473
+ hideRemoveButton() {
474
+ $(this.dgDom.element).find('.remove-button').remove();
475
+ }
476
+ // ### positionModifier()
477
+ positionFromModifier() {
478
+ if (!this.modifier || this.modifier.logicalBox === null) {
479
+ this.positionGlobally();
480
+ return;
481
+ }
482
+ const pageContext = this.view.renderer.pageMap.getRendererFromModifier(this.modifier);
483
+ const screenBox = this.view.renderer.pageMap.svgToClient(this.modifier.logicalBox);
484
+ this.position(screenBox);
485
+ }
486
+ // ### positionGlobally
487
+ // position the dialog box in the center of the current scroll region
488
+ positionGlobally() {
489
+ const box = SvgHelpers.boxPoints(250, 250, 1, 1);
490
+ SuiDialogBase.position(box, this.dgDom, this.view.tracker.scroller, this.view.navigation);
491
+ }
492
+ // ### postionFromSelection
493
+ // set initial position of dialog based on first selection
494
+ positionFromSelection() {
495
+ const note: SmoNote | null = this.view.tracker.selections[0].note;
496
+ if (note && note.logicalBox) {
497
+ const screenBox = this.view.renderer.pageMap.svgToClient(note.logicalBox);
498
+ this.position(screenBox);
499
+ }
500
+ }
501
+ // ### build the html for the dialog, based on the instance-specific components.
502
+ _constructDialog(dialogElements: DialogDefinition, parameters: SuiDomParams) {
503
+ createTopDomContainer('.attributeDialog');
504
+ const id = parameters.id;
505
+ const b = buildDom;
506
+ const r = b('div').classes('attributeModal').attr('id', 'attr-modal-' + id)
507
+ .css('top', parameters.top + 'px').css('left', parameters.left + 'px')
508
+ .append(b('spanb').classes('draggable button').append(b('span').classes('icon icon-move jsDbMove')))
509
+ .append(b('h2').classes('dialog-label').text(this.label));
510
+
511
+ var ctrl = b('div').classes('smoControlContainer');
512
+ dialogElements.elements.filter((de) => de.control).forEach((de) => {
513
+ const classes = de.classes ? de.classes : '';
514
+ const compParams: SuiBaseComponentParams = {
515
+ classes, id: id + de.smoName, ...de
516
+ }
517
+ try {
518
+ const control: SuiComponentBase = SmoDynamicComponentCtor[de.control](this, compParams);
519
+ this.components.push(control);
520
+ this.cmap[de.smoName + 'Ctrl'] = control;
521
+ ctrl.append(control.html);
522
+ } catch (exp) {
523
+ console.error('bad ctor ' + de.smoName);
524
+ throw(exp);
525
+ }
526
+ });
527
+ r.append(ctrl);
528
+ r.append(
529
+ b('div').classes('buttonContainer').append(
530
+ b('button').classes('ok-button button-left btn btn-primary').text('OK')).append(
531
+ b('button').classes('cancel-button button-center btn btn-secondary').text('Cancel')).append(
532
+ b('button').classes('remove-button button-right btn btn-secondary').text('Remove').append(
533
+ b('span').classes('icon icon-cancel-circle'))));
534
+ $('.attributeDialog').html('');
535
+
536
+ $('.attributeDialog').append(r.dom());
537
+
538
+ const trapper = new InputTrapper('.attributeDialog');
539
+ trapper.trap();
540
+ $('.attributeDialog').find('.cancel-button').focus();
541
+ return {
542
+ element: $('.attributeDialog'),
543
+ trapper
544
+ };
545
+ }
546
+ // ### Complete
547
+ // Dialogs take over the keyboard, so release that and trigger an event
548
+ // that the dialog is closing that can resolve any outstanding promises.
549
+ complete() {
550
+ if (this.boundKeyboard && this.keydownHandler) {
551
+ this.eventSource.unbindKeydownHandler(this.keydownHandler);
552
+ }
553
+ $('body').removeClass('showAttributeDialog');
554
+ $('body').trigger('dialogDismiss');
555
+ this.dgDom.trapper.close();
556
+ }
557
+ // ### makeDraggable
558
+ // generic code to make the dialog box draggable so it doesn't
559
+ // get in front of stuff.
560
+ makeDraggable() {
561
+ createTopDomContainer('.draganime');
562
+ const cb = () => { };
563
+ draggable({
564
+ parent: $(this.dgDom.element).find('.attributeModal'),
565
+ handle: $(this.dgDom.element).find('.jsDbMove'),
566
+ animateDiv: '.draganime',
567
+ cb,
568
+ moveParent: true
569
+ });
570
+ }
571
+ // ### captureKeyboardPromise
572
+ // capture keyboard events until the dialog closes,
573
+ // then give control back to the current keyboard
574
+ async captureKeyboardPromise() {
575
+ if (!(this.startPromise)) {
576
+ this.completeNotifier.unbindKeyboardForModal(this);
577
+ this.bindKeyboard();
578
+ return;
579
+ }
580
+ if (this.startPromise) {
581
+ await this.startPromise;
582
+ this.completeNotifier.unbindKeyboardForModal(this);
583
+ this.bindKeyboard();
584
+ }
585
+ }
586
+
587
+ // ### handleKeydown
588
+ // allow a dialog to be dismissed by esc.
589
+ evKey(evdata: any) {
590
+ if (evdata.key === 'Escape') {
591
+ $(this.dgDom.element).find('.cancel-button').click();
592
+ evdata.preventDefault();
593
+ }
594
+ }
595
+
596
+ // ### bindKeyboard
597
+ // generic logic to grab keyboard elements for modal
598
+ bindKeyboard() {
599
+ const evkey = async (ev: any) => {
600
+ this.evKey(ev);
601
+ }
602
+ this.boundKeyboard = true;
603
+ this.keydownHandler = this.eventSource.bindKeydownHandler(evkey);
604
+ }
605
+ }
606
+ /**
607
+ * Convenience functions
608
+ * @param type
609
+ * @param parameters
610
+ * @returns a new dialog of type T
611
+ * @category SuiDialog
612
+ */
613
+ export function dialogConstructor<T extends SuiDialogBase>(type: { new(parameters: SuiDialogParams): T; }, parameters: SuiDialogParams ): T {
614
+ return new type(parameters);
615
+ }
616
+ /**
617
+ * Convenience function to display a dialog
618
+ * @category SuiDialog
619
+ * @param ctor
620
+ * @param parameters
621
+ * @returns
622
+ */
623
+ export function createAndDisplayDialog<T extends SuiDialogBase>(ctor: new (parameters: SuiDialogParams) => T, parameters: SuiDialogParams): T {
624
+ const instance: T = dialogConstructor<T>(ctor, parameters);
625
+ instance.display();
626
+ return instance;
627
+ }