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