Dialog - 图1

Dialog

Dialog is a small content pane that pops up over App’s main content. Dialogs are usualy used to ask something from a user, or to notify or warn a user. Dialog, as all other modals, is part of so called “Temporary Views”.

Dialog can only be opened by using JavaScript. So lets look at related App methods to work with dialogs.

Dialog App Methods

Lets look at related App methods to work with Dialog:

app.dialog.create(parameters)- create Dialog instance

  • parameters - object. Object with dialog parameters

Method returns created Dialog’s instance

app.dialog.destroy(el)- destroy Dialog instance

  • el - HTMLElement or string (with CSS Selector) or object. Dialog element or Dialog instance to destroy.

app.dialog.get(el)- get Dialog instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Dialog element.

Method returns Dialog’s instance

app.dialog.open(el, animate)- opens Dialog

  • el - HTMLElement or string (with CSS Selector). Dialog element to open.
  • animate - boolean. Open Dialog with animation.

Method returns Dialog’s instance

app.dialog.close(el, animate)- closes Dialog

  • el - HTMLElement or string (with CSS Selector). Dialog element to close.
  • animate - boolean. Close Dialog with animation.

Method returns Dialog’s instance

Dialog Parameters

Now lets look at list of available parameters we need to create Dialog:

ParameterTypeDefaultDescription
elHTMLElementDialog element. Can be useful if you already have Dialog element in your HTML and want to create new instance using this element
backdropbooleantrueEnables Dialog backdrop (dark semi transparent layer behind)
closeByBackdropClickbooleantrueWhen enabled, dialog will be closed on backdrop click
animatebooleantrueWhether the Dialog should be opened/closed with animation or not. Can be overwritten in .open() and .close() methods
titlestringDialog title
textstringDialog inner text
contentstringCustom Dialog content that follows dialog text
buttonsarray[]Array with dialog buttons
verticalButtonsbooleanfalseEnables vertical buttons layout
destroyOnClosebooleanfalseWhen enabled will automatically destroy Dialog on close
onClickfunction(dialog, index)Callback function that will be executed after click on the Dialog button. As an arguments it received dialog instance and clicked button index number
cssClassstringAdditional css class to add
onobjectObject with events handlers. For example:
  1. var dialog = app.dialog.create({
  2. text: Hello World’,
  3. on: {
  4. opened: function () {
  5. console.log(‘Dialog opened’)
  6. }
  7. }
  8. })

Button Parameters

Each Button in buttons array must be presented as object with button parameters:

ParameterTypeDefaultDescription
textstringString with Button’s text (could be HTML string)
boldbooleanfalseEnables bold button text
colorstringButton color, one of default colors
closebooleantrueIf enabled then button click will close Dialog
cssClassstringAdditional button CSS class
keyCodesarray[]Array with keyboard keycodes that will be used to trigger button click. For example, key code 13 means that button click will be triggered on Enter key press
onClickfunction(dialog, e)Callback function that will be executed after click on this button

Dialog Methods & Properties

So to create a Dialog we have to call:

  1. var dialog = app.dialog.create({ /* parameters */ })

After that we have its initialized instance (like dialog variable in example above) with useful methods and properties:

Properties
dialog.appLink to global app instance
dialog.elDialog HTML element
dialog.$elDom7 instance with dialog HTML element
dialog.backdropElBackdrop HTML element
dialog.$backdropElDom7 instance with backdrop HTML element
dialog.paramsDialog parameters
dialog.openedBoolean prop indicating whether dialog is opened or not
Methods
dialog.open(animate)Open dialog. Where
  • animate - boolean (by default true) - defines whether it should be opened with animation
dialog.close(animate)Close dialog. Where
  • animate - boolean (by default true) - defines whether it should be closed with animation
dialog.setProgress(progress, duration)Sets dialog progress when Dialog Progress shortcut in use
  • progress - number - progressbar progress (from 0 to 100)
  • duration - number (in ms) - progressbar progress change duration
dialog.setTitle(title)Sets dialog’s title
  • title - string - new dialog title
dialog.setText(text)Sets dialog’s text
  • title - string - new dialog text
dialog.destroy()Destroy dialog
dialog.on(event, handler)Add event handler
dialog.once(event, handler)Add event handler that will be removed after it was fired
dialog.off(event, handler)Remove event handler
dialog.off(event)Remove all handlers for specified event
dialog.emit(event, …args)Fire event on instance

Dialog Events

Dialog will fire the following DOM events on dialog element and events on app and dialog instance:

DOM Events

EventTargetDescription
dialog:openDialog Element<div class=”dialog”>Event will be triggered when Dialog starts its opening animation
dialog:openedDialog Element<div class=”dialog”>Event will be triggered after Dialog completes its opening animation
dialog:closeDialog Element<div class=”dialog”>Event will be triggered when Dialog starts its closing animation
dialog:closedDialog Element<div class=”dialog”>Event will be triggered after Dialog completes its closing animation

App and Dialog Instance Events

Dialog instance emits events on both self instance and app instance. App instance events has same names prefixed with dialog.

EventArgumentsTargetDescription
opendialogdialogEvent will be triggered when Dialog starts its opening animation. As an argument event handler receives dialog instance
dialogOpendialogapp
openeddialogdialogEvent will be triggered after Dialog completes its opening animation. As an argument event handler receives dialog instance
dialogOpeneddialogapp
closedialogdialogEvent will be triggered when Dialog starts its closing animation. As an argument event handler receives dialog instance
dialogClosedialogapp
closeddialogdialogEvent will be triggered after Dialog completes its closing animation. As an argument event handler receives dialog instance
dialogCloseddialogapp
beforeDestroydialogdialogEvent will be triggered right before Dialog instance will be destroyed. As an argument event handler receives dialog instance
dialogBeforeDestroydialogapp

Dialog Shortcuts

There are a few shortcut methods which that make creating dialogs much easier.

First lets check the global app parameters which help to configure such shortcuts and also used for localization purposes.

Dialog Shortcuts Parameters

The following global dialog shortcut parameters can be passed on app initialization under dialog property:

  1. var app = new Framework7({
  2. dialog: {
  3. // set default title for all dialog shortcuts
  4. title: 'My App',
  5. // change default "OK" button text
  6. buttonOk: 'Done',
  7. ...
  8. }
  9. });
ParameterTypeDefaultDescription
titlestringDefault dialogs shortcuts title. If not specified, will be equal to app.name
buttonOkstringOKDefault “OK” button text
buttonCancelstringCancelDefault “Cancel” button text
usernamePlaceholderstringUsernameDefault username field placeholder in Login dialog
passwordPlaceholderstringPasswordDefault password field placeholder in Login & Password dialogs
preloaderTitlestringLoading…Default title for Preloader dialog
progressTitlestringLoading…Default title for Progress dialog
destroyPredefinedDialogsbooleantrueWill automatically destroy all predefined dialogs (Alert, Confirm, Prompt, etc.) on close
keyboardActionsbooleantrueEnables keyboard shortcuts (Enter and Esc) keys for predefined dialogs (Alert, Confirm, Prompt, Login, Password) “Ok” and “Cancel” buttons

Now lets look at available dialog shortcuts

Alert

To create Alert dialog we need to use the following app methods:

app.dialog.alert(text, title, callback)- create Alert Dialog and open it

  • text - string. Alert dialog text
  • title - string. Alert dialog title
  • callback - function. Optional. Callback function that will be executed after user clicks on Alert button

Method returns created Dialog’s instance

app.dialog.alert(text, callback)- create Alert Dialog with default title and open it

Method returns created Dialog’s instance

Confirm

Confirm dialog is usualy used when it is required to confirm some action. To open the Confirm modal we should also call one of the following App methods:

app.dialog.confirm(text, title, callbackOk, callbackCancel)- create Confirm Dialog and open it

  • text - string. Confirm dialog text
  • title - string. Confirm dialog title
  • callbackOk - function. Optional. Callback function that will be executed when user click “Ok” button on Confirm dialog (when user confirms action)
  • callbackCancel - function. Optional. Callback function that will be executed when user click “Cancel” button on Confirm dialog (when user dismisses action)

Method returns created Dialog’s instance

app.dialog.confirm(text, callbackOk, callbackCancel)- create Confirm Dialog with default title and open it

Method returns created Dialog’s instance

Prompt

Prompt dialog is used when it is required to get some data/answer from user. To open Prompt dialog we should also call one of the following App methods:

app.dialog.prompt(text, title, callbackOk, callbackCancel, defaultValue)- create Prompt Dialog and open it

  • text - string. Prompt dialog text
  • title - string. Prompt dialog title
  • callbackOk - function(value). Optional. Callback function that will be executed when user click “Ok” button on Prompt dialog. As an argument function receives value of text input
  • callbackCancel - function(value). Optional. Callback function that will be executed when user click “Cancel” button on Prompt dialog. As an argument function receives value of text input
  • defaultValue - string. Optional. Prompt input default value

app.dialog.prompt(text, callbackOk, callbackCancel, defaultValue)- create Prompt Dialog with default title and open it

Method returns created Dialog’s instance

Login

app.dialog.login(text, title, callbackOk, callbackCancel)- create Login Dialog and open it

  • text - string. Login dialog text
  • title - string. Login dialog title
  • callbackOk - function(username, password). Optional. Callback function that will be executed when user click “Ok” button on Login dialog. As an argument function receives username and password values
  • callbackCancel - function(username, password). Optional. Callback function that will be executed when user click “Cancel” button on Login dialog. As an argument function receives username and password values

app.dialog.login(text, callbackOk, callbackCancel)- create Login Dialog with default title and open it

Method returns created Dialog’s instance

Password

Password dialog is useful in case you need to request only the password

app.dialog.password(text, title, callbackOk, callbackCancel)- create Password Dialog and open it

  • text - string. Password dialog text
  • title - string. Password dialog title
  • callbackOk - function(password). Optional. Callback function that will be executed when user click “Ok” button on Password dialog. As an argument function receives password value
  • callbackCancel - function(password). Optional. Callback function that will be executed when user click “Cancel” button on Password dialog. As an argument function receives password value

app.dialog.password(text, callbackOk, callbackCancel)- create Password Dialog with default title and open it

Method returns created Dialog’s instance

Preloader

Preloader dialog is used to indicate some background activity (like Ajax request) and to block any user actions during this activity. To open Preloader dialog we should also call appropriate App method:

app.dialog.preloader(title, color)- create Preloader Dialog and open it

  • title - string. Optional. Preloader dialog title
  • color - string. Optional. Preloader color. One of the default colors

Method returns created Dialog’s instance

Progress

Same as Preloader dialog but with progressbar instead of preloader.

app.dialog.progress(title, progress, color)- create Progress Dialog and open it

  • title - string. Optional. Progress dialog title
  • progress - number. Optional. Progressbar progress (from 0 to 100). If no number passed then it will have infinite progressbar.
  • color - string. Optional. Progressbar color. One of default colors

Method returns created Dialog’s instance

Examples

  1. <body>
  2. ...
  3. <div class="page-content">
  4. <div class="block block-strong">
  5. <p class="row">
  6. <button class="col button open-alert">Alert</button>
  7. <button class="col button open-confirm">Confirm</button>
  8. <button class="col button open-prompt">Prompt</button>
  9. </p>
  10. <p class="row">
  11. <button class="col button open-login">Login</button>
  12. <button class="col button open-password">Password</button>
  13. </p>
  14. </div>
  15. <div class="block-title">Vertical Buttons</div>
  16. <div class="block block-strong">
  17. <p>
  18. <button class="button open-vertical">Vertical Buttons</button>
  19. </p>
  20. </div>
  21. <div class="block-title">Preloader Dialog</div>
  22. <div class="block block-strong">
  23. <p class="row">
  24. <button class="col button open-preloader">Preloader</button>
  25. <button class="col button open-preloader-custom">Custom Title</button>
  26. </p>
  27. </div>
  28. <div class="block-title">Progress Dialog</div>
  29. <div class="block block-strong">
  30. <p class="row">
  31. <button class="col button open-progress">Determined</button>
  32. <button class="col button open-progress-infinite">Infinite</button>
  33. </p>
  34. </div>
  35. </div>
  36. ...
  37. </body>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Alert
  4. $$('.open-alert').on('click', function () {
  5. app.dialog.alert('Hello');
  6. });
  7. // Confirm
  8. $$('.open-confirm').on('click', function () {
  9. app.dialog.confirm('Are you feel good today?', function () {
  10. app.dialog.alert('Great!');
  11. });
  12. });
  13. // Prompt
  14. $$('.open-prompt').on('click', function () {
  15. app.dialog.prompt('What is your name?', function (name) {
  16. app.dialog.confirm('Are you sure that your name is ' + name + '?', function () {
  17. app.dialog.alert('Ok, your name is ' + name);
  18. });
  19. });
  20. });
  21. // Login
  22. $$('.open-login').on('click', function () {
  23. app.dialog.login('Enter your username and password', function (username, password) {
  24. app.dialog.alert('Thank you!<br>Username:' + username + '<br>Password:' + password);
  25. });
  26. });
  27. // Password
  28. $$('.open-password').on('click', function () {
  29. app.dialog.password('Enter your username and password', function (password) {
  30. app.dialog.alert('Thank you!<br>Password:' + password);
  31. });
  32. });
  33. // Vertical Buttons
  34. $$('.open-vertical').on('click', function () {
  35. app.dialog.create({
  36. title: 'Vertical Buttons',
  37. text: 'Dialog with vertical buttons',
  38. buttons: [
  39. {
  40. text: 'Button 1',
  41. },
  42. {
  43. text: 'Button 2',
  44. },
  45. {
  46. text: 'Button 3',
  47. },
  48. ],
  49. verticalButtons: true,
  50. }).open();
  51. });
  52. // Preloader
  53. $$('.open-preloader').on('click', function () {
  54. app.dialog.preloader();
  55. setTimeout(function () {
  56. app.dialog.close();
  57. }, 3000);
  58. });
  59. // Preloader with custom text
  60. $$('.open-preloader-custom').on('click', function () {
  61. app.dialog.preloader('My text...');
  62. setTimeout(function () {
  63. app.dialog.close();
  64. }, 3000);
  65. });
  66. // Progress
  67. $$('.open-progress').on('click', function () {
  68. var progress = 0;
  69. var dialog = app.dialog.progress('Loading assets', progress);
  70. dialog.setText('Image 1 of 10');
  71. var interval = setInterval(function () {
  72. progress += 10;
  73. dialog.setProgress(progress);
  74. dialog.setText('Image ' + ((progress) / 10) + ' of 10');
  75. if (progress === 100) {
  76. clearInterval(interval);
  77. dialog.close();
  78. }
  79. }, 300)
  80. });
  81. // Progress Infinite
  82. $$('.open-progress-infinite').on('click', function () {
  83. app.dialog.progress();
  84. setTimeout(function () {
  85. app.dialog.close();
  86. }, 3000);
  87. });