-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeputyRootSession.ts
664 lines (606 loc) · 20.2 KB
/
DeputyRootSession.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import DeputyCasePage, { ContributionSurveyHeading } from '../wiki/DeputyCasePage';
import DeputyCCISessionStartLink from '../ui/root/DeputyCCISessionStartLink';
import removeElement from '../util/removeElement';
import unwrapWidget from '../util/unwrapWidget';
import sectionHeadingName from '../wiki/util/sectionHeadingName';
import {
DeputyMessageEvent,
DeputySessionRequestMessage,
DeputySessionStopMessage
} from '../DeputyCommunications';
import DeputyCCISessionAddSection from '../ui/root/DeputyCCISessionAddSection';
import DeputyContributionSurveySection from '../ui/root/DeputyContributionSurveySection';
import { SessionInformation } from './DeputySession';
import { ArrayOrNot } from '../types';
import DeputyMessageWidget from '../ui/shared/DeputyMessageWidget';
import sectionHeadingId from '../wiki/util/sectionHeadingId';
import last from '../util/last';
import findNextSiblingElement from '../util/findNextSiblingElement';
/**
* The DeputyRootSession. Instantiated only when:
* (a) the page is a CCI case page, and
* (b) a session is currently active
*/
export default class DeputyRootSession {
/*
* =========================================================================
* STATIC AND SESSION-LESS FUNCTIONS
* =========================================================================
*/
/**
* Initialize interface components for *starting* a session. This includes
* the `[start CCI session]` notice at the top of each CCI page section heading.
*
* @param _casePage The current case page
*/
static async initEntryInterface( _casePage?: DeputyCasePage ): Promise<void> {
const continuing = _casePage != null;
const casePage = continuing ? _casePage : await DeputyCasePage.build();
const startLink: HTMLElement[] = [];
casePage.findContributionSurveyHeadings()
.forEach( ( heading: ContributionSurveyHeading ) => {
const link = DeputyCCISessionStartLink( heading, casePage );
startLink.push( link as HTMLElement );
heading.appendChild( link );
} );
window.deputy.comms.addEventListener( 'sessionStarted', () => {
// Re-build interface.
startLink.forEach( ( link: HTMLElement ) => {
removeElement( link );
} );
window.deputy.session.init();
}, { once: true } );
}
/**
* Shows the interface for overwriting an existing session. The provided
* action button will close the other section. This does not start a new
* session; the user must start the session on this page on their own.
*
* @param casePage The case page to continue with
*/
static async initOverwriteMessage( casePage: DeputyCasePage ): Promise<void> {
await mw.loader.using(
[ 'oojs-ui-core', 'oojs-ui.styles.icons-content' ],
() => {
const firstHeading = casePage.findFirstContributionSurveyHeading();
if ( firstHeading ) {
const stopButton = new OO.ui.ButtonWidget( {
label: mw.msg( 'deputy.session.otherActive.button' ),
flags: [ 'primary', 'destructive' ]
} );
const messageBox = DeputyMessageWidget( {
classes: [
'deputy', 'dp-cs-session-notice', 'dp-cs-session-otherActive'
],
type: 'notice',
icon: 'alert',
title: mw.msg( 'deputy.session.otherActive.head' ),
message: mw.msg( 'deputy.session.otherActive.help' ),
actions: [ stopButton ],
closable: true
} );
stopButton.on( 'click', async () => {
const session = await window.deputy.comms.sendAndWait( {
type: 'sessionStop'
} );
if ( session === null ) {
// Session did not close cleanly. Tab must be closed. Force-stop
// the session.
await window.deputy.session.clearSession();
removeElement( unwrapWidget( messageBox ) );
await window.deputy.session.init();
} else {
// Handled by session close listener.
}
} );
window.deputy.comms.addEventListener( 'sessionClosed', () => {
// Closed externally. Re-build interface.
removeElement( unwrapWidget( messageBox ) );
window.deputy.session.init();
} );
casePage.normalizeSectionHeading(
firstHeading
).insertAdjacentElement(
'beforebegin',
unwrapWidget( messageBox )
);
}
}
);
}
/**
* Shows the interface for continuing a previous session. This includes
* the `[continue CCI session]` notice at the top of each CCI page section heading
* and a single message box showing when the page was last worked on top of the
* first CCI heading found.
*
* @param casePage The case page to continue with
*/
static async initContinueInterface( casePage: DeputyCasePage ): Promise<void> {
await Promise.all( [
DeputyRootSession.initEntryInterface(),
mw.loader.using(
[ 'oojs-ui-core', 'oojs-ui.styles.icons-content' ],
() => {
const lastActiveSection =
DeputyRootSession.findFirstLastActiveSection( casePage );
const firstSection =
casePage.normalizeSectionHeading(
casePage.findFirstContributionSurveyHeading()
);
// Insert element directly into widget (not as text, or else event
// handlers will be destroyed).
const continueButton = new OO.ui.ButtonWidget( {
label: mw.msg( 'deputy.session.continue.button' ),
flags: [ 'primary', 'progressive' ]
} );
const messageBox = DeputyMessageWidget( {
classes: [
'deputy', 'dp-cs-session-notice', 'dp-cs-session-lastActive'
],
type: 'notice',
icon: 'history',
title: mw.msg(
'deputy.session.continue.head',
new Date().toLocaleString(
mw.config.get( 'wgUserLanguage' ),
{ dateStyle: 'long', timeStyle: 'medium' }
)
),
message: mw.msg(
lastActiveSection ?
'deputy.session.continue.help' :
'deputy.session.continue.help.fromStart',
lastActiveSection ?
sectionHeadingName( lastActiveSection ) :
casePage.lastActiveSections[ 0 ]
.replace( /_/g, ' ' ),
sectionHeadingName( firstSection )
),
actions: [ continueButton ],
closable: true
} );
const sessionStartListener = async () => {
removeElement( unwrapWidget( messageBox ) );
await this.initTabActiveInterface();
};
continueButton.on( 'click', () => {
removeElement( unwrapWidget( messageBox ) );
if ( lastActiveSection ) {
DeputyRootSession.continueSession( casePage );
} else {
DeputyRootSession.continueSession( casePage, [
sectionHeadingId( firstSection )
] );
}
window.deputy.comms.removeEventListener(
'sessionStarted',
sessionStartListener
);
} );
firstSection.insertAdjacentElement(
'beforebegin',
unwrapWidget( messageBox )
);
window.deputy.comms.addEventListener(
'sessionStarted',
sessionStartListener,
{ once: true }
);
}
)
] );
}
/**
* Shows the interface for an attempted Deputy execution on a different tab than
* expected. This prevents Deputy from running entirely to avoid loss of progress
* and desynchronization.
*
* @param _casePage The current case page (not the active one)
*/
static async initTabActiveInterface( _casePage?: DeputyCasePage ): Promise<void> {
const casePage = _casePage ?? await DeputyCasePage.build();
return mw.loader.using(
[ 'oojs-ui-core', 'oojs-ui.styles.icons-content' ],
() => {
const firstHeading =
casePage.findFirstContributionSurveyHeading();
if ( firstHeading ) {
const messageBox = DeputyMessageWidget( {
classes: [
'deputy', 'dp-cs-session-notice', 'dp-cs-session-tabActive'
],
type: 'notice',
title: mw.msg( 'deputy.session.tabActive.head' ),
message: mw.msg( 'deputy.session.tabActive.help' ),
closable: true
} );
casePage.normalizeSectionHeading(
firstHeading
).insertAdjacentElement(
'beforebegin',
unwrapWidget( messageBox )
);
window.deputy.comms.addEventListener(
'sessionClosed',
async () => {
removeElement( unwrapWidget( messageBox ) );
await window.deputy.session.init();
},
{ once: true }
);
}
}
);
}
/**
* Finds the first last active section that exists on the page.
* If a last active section that still exists on the page could not be found,
* `null` is returned.
*
* @param casePage The case page to use
* @return The last active session's heading element.
*/
static findFirstLastActiveSection( casePage: DeputyCasePage ): ContributionSurveyHeading {
const csHeadings = casePage.findContributionSurveyHeadings();
for ( const lastActiveSection of casePage.lastActiveSections ) {
for ( const heading of csHeadings ) {
if ( sectionHeadingId( heading ) === lastActiveSection ) {
return heading;
}
}
}
return null;
}
/**
* Starts a Deputy session.
*
* @param section
* @param _casePage
*/
static async startSession(
section: ArrayOrNot<ContributionSurveyHeading>,
_casePage?: DeputyCasePage
): Promise<void> {
const sectionIds = ( Array.isArray( section ) ? section : [ section ] ).map(
( _section ) => sectionHeadingId( _section )
);
// Save session to storage
const casePage = _casePage ?? await DeputyCasePage.build();
const session = await this.setSession( {
casePageId: casePage.pageId,
caseSections: sectionIds
} );
const rootSession =
window.deputy.session.rootSession =
new DeputyRootSession( session, casePage );
await casePage.bumpActive();
await rootSession.initSessionInterface();
}
/**
* Continue a session from a DeputyCasePage.
*
* @param casePage The case page to continue with
* @param sectionIds The section IDs to load on startup. If not provided, this will be
* taken from the cache. If provided, this overrides the cache, discarding any
* sections cached previously.
*/
static async continueSession(
casePage: DeputyCasePage,
sectionIds?: string[]
): Promise<void> {
// Save session to storage
if ( sectionIds ) {
casePage.lastActiveSections = sectionIds;
}
const session = await this.setSession( {
casePageId: casePage.pageId,
// Shallow array copy
caseSections: [ ...casePage.lastActiveSections ]
} );
const rootSession =
window.deputy.session.rootSession =
new DeputyRootSession( session, casePage );
await casePage.bumpActive();
await rootSession.initSessionInterface();
}
/**
* Sets the current active session information.
*
* @param session The session to save.
* @return SessionInformation object if successful, `null` if not.
*/
static async setSession( session: SessionInformation ): Promise<SessionInformation> {
return ( await window.deputy.storage.setKV( 'session', session ) ) ? session : null;
}
/**
* The current active session, if one exists.
*/
session: SessionInformation;
/**
* The case page that this root session is handling.
*/
casePage: DeputyCasePage;
/**
* A DiscussionTools Parser. Used for parsing comments in a streamlined way (using
* DiscussionTools) as compared to relying on an in-house parser.
*/
parser: any;
/**
* An array of active section UI elements. Populated in `initSessionInterface`.
*/
sections: DeputyContributionSurveySection[];
/**
* Responder for session requests.
*/
readonly sessionRequestResponder = this.sendSessionResponse.bind( this );
readonly sessionStopResponder = this.handleStopRequest.bind( this );
/*
* =========================================================================
* INSTANCE AND ACTIVE SESSION FUNCTIONS
* =========================================================================
*/
/**
* @param session
* @param casePage
*/
constructor( session: SessionInformation, casePage: DeputyCasePage ) {
this.session = session;
this.casePage = casePage;
}
/**
* Initialize interface components for an active session. This will always run in the
* context of a CCI case page.
*/
async initSessionInterface(): Promise<void> {
if ( window.location.search.indexOf( 'action=edit' ) !== -1 ) {
// User is editing, don't load interface.
return;
}
if ( await window.deputy.session.checkForActiveSessionTabs() ) {
// User is on another tab, don't load interface.
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-windows' ], () => {
OO.ui.alert(
mw.msg( 'deputy.session.tabActive.help' ),
{ title: mw.msg( 'deputy.session.tabActive.head' ) }
);
} );
return;
}
removeElement( this.casePage.document.querySelector( '.dp-cs-session-lastActive' ) );
this.casePage.document.querySelectorAll( '.dp-sessionStarter' )
.forEach( ( el: HTMLElement ) => {
removeElement( el );
} );
window.deputy.comms.addEventListener( 'sessionRequest', this.sessionRequestResponder );
window.deputy.comms.addEventListener( 'sessionStop', this.sessionStopResponder );
window.deputy.comms.send( { type: 'sessionStarted', caseId: this.session.casePageId } );
await new Promise<void>( ( res ) => {
mw.loader.using( [
'mediawiki.special.changeslist',
'mediawiki.interface.helpers.styles',
'mediawiki.pager.styles',
'oojs-ui-core',
'oojs-ui-windows',
'oojs-ui-widgets',
'oojs-ui.styles.icons-alerts',
'oojs-ui.styles.icons-content',
'oojs-ui.styles.icons-editing-core',
'oojs-ui.styles.icons-interactions',
'oojs-ui.styles.icons-media',
'oojs-ui.styles.icons-movement',
'ext.discussionTools.init',
'jquery.makeCollapsible'
], async ( require ) => {
// Instantiate the parser
const dt = require( 'ext.discussionTools.init' );
this.parser = new dt.Parser( dt.parserData );
document.getElementsByTagName( 'body' )[ 0 ]
.appendChild( window.deputy.windowManager.$element[ 0 ] );
// TODO: Do interface functions
this.sections = [];
const activeSectionPromises = [];
for ( const heading of this.casePage.findContributionSurveyHeadings() ) {
const headingId = sectionHeadingId( heading );
if ( this.session.caseSections.indexOf( headingId ) !== -1 ) {
activeSectionPromises.push(
this.activateSection( this.casePage, heading )
.then( v => v ? headingId : null )
);
} else {
this.addSectionOverlay( this.casePage, heading );
}
}
// Strip missing sections from caseSections.
this.session.caseSections = ( await Promise.all( activeSectionPromises ) )
.filter( v => !!v );
await DeputyRootSession.setSession( this.session );
if ( this.session.caseSections.length === 0 ) {
// No sections re-opened. All of them might have been removed or closed already.
// Close this entire session.
await this.closeSession();
}
mw.hook( 'deputy.load.cci.root' ).fire();
res();
} );
} );
}
/**
* Responds to session requests through the Deputy communicator. This prevents two
* tabs from having the same session opened.
*
* @param event
*/
sendSessionResponse(
event: DeputyMessageEvent<DeputySessionRequestMessage>
): void {
window.deputy.comms.reply(
event.data, {
type: 'sessionResponse',
caseId: this.session.casePageId,
sections: this.session.caseSections
}
);
}
/**
* Handles a session stop request.
*
* @param event
*/
async handleStopRequest(
event: DeputyMessageEvent<DeputySessionStopMessage>
): Promise<void> {
await this.closeSession();
window.deputy.comms.reply(
event.data, {
type: 'acknowledge'
}
);
}
/**
* Adds the "start working on this section" or "reload page" overlay and button
* to a given section.
*
* @param casePage
* @param heading
*/
addSectionOverlay( casePage: DeputyCasePage, heading: ContributionSurveyHeading ): void {
const normalizedHeading = casePage.normalizeSectionHeading( heading );
const section = casePage.getContributionSurveySection( normalizedHeading );
const list = section.find(
( v ) => v instanceof HTMLElement && v.tagName === 'UL'
) as HTMLUListElement;
const headingTop = window.scrollY +
normalizedHeading.getBoundingClientRect().bottom;
const sectionBottom = window.scrollY + (
findNextSiblingElement( last( section ) )?.getBoundingClientRect()?.top ??
normalizedHeading.parentElement.getBoundingClientRect().bottom
);
const overlayHeight = sectionBottom - headingTop;
if ( list != null ) {
list.style.position = 'relative';
list.appendChild( DeputyCCISessionAddSection( {
casePage, heading,
height: overlayHeight
} ) );
}
}
/**
* Closes all active session-related UI components. Done prior to closing
* a section or reloading the interface.
*/
closeSessionUI(): void {
if ( this.sections ) {
for ( const section of this.sections ) {
section.close();
}
}
this.casePage.document.querySelectorAll( '.dp-cs-section-add' )
.forEach( ( el: HTMLElement ) => removeElement( el ) );
}
/**
* Closes the current session.
*/
async closeSession(): Promise<void> {
this.closeSessionUI();
await this.casePage.saveToCache();
const oldSessionId = this.session.casePageId;
window.deputy.comms.removeEventListener(
'sessionRequest', this.sessionRequestResponder
);
await window.deputy.session.clearSession();
window.deputy.comms.send( { type: 'sessionClosed', caseId: oldSessionId } );
// Re-initialize session interface objects.
await window.deputy.session.init();
}
/**
* Activates a section. This appends the section UI, adds the section to the
* cache (if not already added), and internally stores the section for a
* graceful exit.
*
* @param casePage
* @param heading
* @return `true` if the section was activated successfully
*/
async activateSection(
casePage: DeputyCasePage,
heading: HTMLHeadingElement
): Promise<boolean> {
const el = new DeputyContributionSurveySection( casePage, heading );
if ( !( await el.prepare() ) ) {
return false;
}
const sectionId = sectionHeadingId( heading );
this.sections.push( el );
const lastActiveSession = this.session.caseSections.indexOf( sectionId );
if ( lastActiveSession === -1 ) {
this.session.caseSections.push( sectionId );
await DeputyRootSession.setSession( this.session );
}
await casePage.addActiveSection( sectionId );
if ( heading.parentElement.classList.contains( 'mw-heading' ) ) {
heading.parentElement.insertAdjacentElement( 'afterend', el.render() );
} else {
heading.insertAdjacentElement( 'afterend', el.render() );
}
await el.loadData();
mw.hook( 'deputy.load.cci.session' ).fire();
return true;
}
/**
* Closes a section. This removes the section from both the session data and from
* the case page cache.
*/
async closeSection( e0: DeputyCasePage, e1: ContributionSurveyHeading ): Promise<void>;
/**
* Closes a section. This removes the section from both the session data and from
* the case page cache.
*/
async closeSection( e0: DeputyContributionSurveySection ): Promise<void>;
/**
* Closes a section. This removes the section from both the session data and from
* the case page cache.
*
* @param e0
* @param e1
*/
async closeSection(
e0: DeputyContributionSurveySection | DeputyCasePage,
e1?: ContributionSurveyHeading,
): Promise<void> {
const el = e0 instanceof DeputyContributionSurveySection ?
e0 : null;
const casePage = e0 instanceof DeputyContributionSurveySection ?
e0.casePage : e0;
const heading = e0 instanceof DeputyContributionSurveySection ?
e0.heading : e1;
const sectionId = sectionHeadingId( heading );
const sectionListIndex = this.sections.indexOf( el );
if ( el != null && sectionListIndex !== -1 ) {
this.sections.splice( sectionListIndex, 1 );
}
const lastActiveSection = this.session.caseSections.indexOf( sectionId );
if ( lastActiveSection !== -1 ) {
this.session.caseSections.splice( lastActiveSection, 1 );
// If no sections remain, clear the session.
if ( this.session.caseSections.length === 0 ) {
await this.closeSession();
// Don't remove from casePage if there are no sections left, or
// else "continue where you left off" won't work.
} else {
await DeputyRootSession.setSession( this.session );
await casePage.removeActiveSection( sectionId );
this.addSectionOverlay( casePage, heading );
}
}
}
/**
* Restarts the section. This rebuilds *everything* from the ground up, which may
* be required when there's an edit conflict.
*/
async restartSession() {
const casePage = this.casePage;
await this.closeSession();
await window.deputy.session.DeputyRootSession.continueSession( casePage );
}
}