545afc34ca79dd0f96ed32adc6c8c9ba7ddaa93d
[yaffs-website] / web / core / misc / dialog / dialog.es6.js
1 /**
2  * @file
3  * Dialog API inspired by HTML5 dialog element.
4  *
5  * @see http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#the-dialog-element
6  */
7
8 (function ($, Drupal, drupalSettings) {
9   /**
10    * Default dialog options.
11    *
12    * @type {object}
13    *
14    * @prop {bool} [autoOpen=true]
15    * @prop {string} [dialogClass='']
16    * @prop {string} [buttonClass='button']
17    * @prop {string} [buttonPrimaryClass='button--primary']
18    * @prop {function} close
19    */
20   drupalSettings.dialog = {
21     autoOpen: true,
22     dialogClass: '',
23     // Drupal-specific extensions: see dialog.jquery-ui.js.
24     buttonClass: 'button',
25     buttonPrimaryClass: 'button--primary',
26     // When using this API directly (when generating dialogs on the client
27     // side), you may want to override this method and do
28     // `jQuery(event.target).remove()` as well, to remove the dialog on
29     // closing.
30     close(event) {
31       Drupal.dialog(event.target).close();
32       Drupal.detachBehaviors(event.target, null, 'unload');
33     },
34   };
35
36   /**
37    * @typedef {object} Drupal.dialog~dialogDefinition
38    *
39    * @prop {boolean} open
40    *   Is the dialog open or not.
41    * @prop {*} returnValue
42    *   Return value of the dialog.
43    * @prop {function} show
44    *   Method to display the dialog on the page.
45    * @prop {function} showModal
46    *   Method to display the dialog as a modal on the page.
47    * @prop {function} close
48    *   Method to hide the dialog from the page.
49    */
50
51   /**
52    * Polyfill HTML5 dialog element with jQueryUI.
53    *
54    * @param {HTMLElement} element
55    *   The element that holds the dialog.
56    * @param {object} options
57    *   jQuery UI options to be passed to the dialog.
58    *
59    * @return {Drupal.dialog~dialogDefinition}
60    *   The dialog instance.
61    */
62   Drupal.dialog = function (element, options) {
63     let undef;
64     const $element = $(element);
65     const dialog = {
66       open: false,
67       returnValue: undef,
68       show() {
69         openDialog({ modal: false });
70       },
71       showModal() {
72         openDialog({ modal: true });
73       },
74       close: closeDialog,
75     };
76
77     function openDialog(settings) {
78       settings = $.extend({}, drupalSettings.dialog, options, settings);
79       // Trigger a global event to allow scripts to bind events to the dialog.
80       $(window).trigger('dialog:beforecreate', [dialog, $element, settings]);
81       $element.dialog(settings);
82       dialog.open = true;
83       $(window).trigger('dialog:aftercreate', [dialog, $element, settings]);
84     }
85
86     function closeDialog(value) {
87       $(window).trigger('dialog:beforeclose', [dialog, $element]);
88       $element.dialog('close');
89       dialog.returnValue = value;
90       dialog.open = false;
91       $(window).trigger('dialog:afterclose', [dialog, $element]);
92     }
93
94     return dialog;
95   };
96 }(jQuery, Drupal, drupalSettings));