Repository navigation
Expand file tree
/
Copy pathdialog.js
More file actions
75 lines (59 loc) · 1.36 KB
/
Copy pathdialog.js
File metadata and controls
75 lines (59 loc) · 1.36 KB
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
'use strict';
/*
* Dialog
*/
class Dialog {
constructor( text ) {
const dialog = document.createElement( 'dialog' );
const form = document.createElement( 'form' );
form.method = 'dialog';
this.content = document.createElement( 'div' );
this.content.classList.add( 'content' );
const cancelBtn = document.createElement( 'button' );
cancelBtn.value = 'cancel';
cancelBtn.append( '×' );
cancelBtn.addEventListener( 'click', event => {
this.cancel();
} );
form.append( cancelBtn, this.content );
dialog.append( form );
document.body.append( dialog );
dialog.addEventListener( 'click', event => {
if ( event.target === dialog ) {
dialog.cancel();
} else {
dialog.selectText();
}
} );
Object.setPrototypeOf( Dialog.prototype, HTMLDialogElement.prototype );
Object.setPrototypeOf( dialog, this );
if ( text ) {
dialog.showModal( text );
}
return dialog;
}
show( msg ) {
this.setText( text );
super.show();
}
showModal( text ) {
this.setText( text );
super.showModal();
}
cancel() {
const cancelEvent = new Event( 'cancel' );
this.dispatchEvent( cancelEvent );
this.close();
}
setText( text ) {
if ( text ) {
if ( Array.isArray( text ) ) {
text = text.join( '\n' );
}
this.content.innerHTML = text;
}
}
selectText() {
getSelection().selectAllChildren( this.content );
}
}