2020-10-09 19:35:46 +02:00
import Plugin from '../Plugin' ;
import createViewHandle from '../utils/createViewHandle' ;
import WebviewController , { ContainerType } from '../WebviewController' ;
2020-11-13 20:48:42 +02:00
import { ButtonSpec , ViewHandle , DialogResult } from './types' ;
2022-11-17 17:36:33 +02:00
import { _ } from '../../../locale' ;
2020-10-09 19:35:46 +02:00
/ * *
2020-11-16 18:14:26 +02:00
* Allows creating and managing dialogs . A dialog is modal window that
2021-06-21 14:32:17 +02:00
* contains a webview and a row of buttons . You can update the
2020-11-16 18:14:26 +02:00
* webview using the ` setHtml ` method . Dialogs are hidden by default and
* you need to call ` open() ` to open them . Once the user clicks on a
* button , the ` open ` call will return an object indicating what button was
* clicked on .
2020-10-09 19:35:46 +02:00
*
2020-11-16 18:14:26 +02:00
* # # Retrieving form values
*
* If your HTML content included one or more forms , a ` formData ` object
* will also be included with the key / value for each form .
*
* # # Special button IDs
*
* The following buttons IDs have a special meaning :
*
* - ` ok ` , ` yes ` , ` submit ` , ` confirm ` : They are considered "submit" buttons
* - ` cancel ` , ` no ` , ` reject ` : They are considered "dismiss" buttons
*
* This information is used by the application to determine what action
* should be done when the user presses "Enter" or "Escape" within the
* dialog . If they press "Enter" , the first "submit" button will be
* automatically clicked . If they press "Escape" the first "dismiss" button
* will be automatically clicked .
*
* [ View the demo
* plugin ] ( https : //github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/dialog)
2020-10-09 19:35:46 +02:00
* /
export default class JoplinViewsDialogs {
private store : any ;
private plugin : Plugin ;
2020-11-12 21:13:28 +02:00
private implementation_ : any ;
2020-10-09 19:35:46 +02:00
2023-03-06 16:22:01 +02:00
public constructor ( implementation : any , plugin : Plugin , store : any ) {
2020-10-09 19:35:46 +02:00
this . store = store ;
this . plugin = plugin ;
this . implementation_ = implementation ;
}
2020-11-12 21:13:28 +02:00
private controller ( handle : ViewHandle ) : WebviewController {
2020-10-09 19:35:46 +02:00
return this . plugin . viewController ( handle ) as WebviewController ;
}
/ * *
* Creates a new dialog
* /
2023-03-06 16:22:01 +02:00
public async create ( id : string ) : Promise < ViewHandle > {
2020-11-13 19:09:28 +02:00
if ( ! id ) {
2021-08-05 13:02:03 +02:00
this . plugin . deprecationNotice ( '1.5' , 'Creating a view without an ID is deprecated. To fix it, change your call to `joplin.views.dialogs.create("my-unique-id")`' , true ) ;
2020-11-13 19:09:28 +02:00
id = ` ${ this . plugin . viewCount } ` ;
}
const handle = createViewHandle ( this . plugin , id ) ;
const controller = new WebviewController ( handle , this . plugin . id , this . store , this . plugin . baseDir , ContainerType . Dialog ) ;
2020-10-09 19:35:46 +02:00
this . plugin . addViewController ( controller ) ;
return handle ;
}
/ * *
* Displays a message box with OK / Cancel buttons . Returns the button index that was clicked - "0" for OK and "1" for "Cancel"
* /
2023-03-06 16:22:01 +02:00
public async showMessageBox ( message : string ) : Promise < number > {
2022-10-30 19:45:11 +02:00
return this . implementation_ . showMessageBox ( ` ${ _ ( '(In plugin: %s)' , this . plugin . manifest . name ) } \ n \ n ${ message } ` ) ;
2020-10-09 19:35:46 +02:00
}
/ * *
* Sets the dialog HTML content
* /
2023-03-06 16:22:01 +02:00
public async setHtml ( handle : ViewHandle , html : string ) {
2020-10-09 19:35:46 +02:00
return this . controller ( handle ) . html = html ;
}
2021-01-02 18:21:32 +02:00
/ * *
* Adds and loads a new JS or CSS files into the dialog .
* /
public async addScript ( handle : ViewHandle , scriptPath : string ) {
return this . controller ( handle ) . addScript ( scriptPath ) ;
}
2020-10-09 19:35:46 +02:00
/ * *
* Sets the dialog buttons .
* /
2023-03-06 16:22:01 +02:00
public async setButtons ( handle : ViewHandle , buttons : ButtonSpec [ ] ) {
2020-10-09 19:35:46 +02:00
return this . controller ( handle ) . buttons = buttons ;
}
/ * *
* Opens the dialog
* /
2023-03-06 16:22:01 +02:00
public async open ( handle : ViewHandle ) : Promise < DialogResult > {
2020-10-09 19:35:46 +02:00
return this . controller ( handle ) . open ( ) ;
}
2021-08-18 13:09:45 +02:00
/ * *
* Toggle on whether to fit the dialog size to the content or not .
* When set to false , the dialog is set to 90 vw and 80 vh
* @default true
* /
2023-03-06 16:22:01 +02:00
public async setFitToContent ( handle : ViewHandle , status : boolean ) {
2021-08-18 13:09:45 +02:00
return this . controller ( handle ) . fitToContent = status ;
}
2020-10-09 19:35:46 +02:00
}