Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
 
2
---
3
layout: docs
4
title: "HTML Modals"
5
description: "A reusable handled modal component"
6
date: 2021-12-09T14:48:00+08:00
7
draft: false
8
tags:
9
- MDL-71963
10
- MDL-72928
11
- "4.0"
12
---
13
 
14
## How it works
15
 
16
The core/utility module allows different modals to be displayed automatically when interacting with the page.
17
 
18
Modals are configured using a set of specific data-attributes.
19
 
20
## Source files
21
 
22
* `lib/amd/src/utility.js` ({{< jsdoc module="core/utility" >}})
23
* `lib/templates/modal.mustache`
24
 
25
## Usage
26
The confirmation AMD module is loaded automatically, so the only thing you need to do is to add some specific data attributes
27
to the target element.
28
 
29
To display a confirmation modal.
30
{{< highlight html >}}
31
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
32
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]'>Show confirmation modal</button>
33
{{< /highlight >}}
34
 
35
To display an alert modal.
36
{{< highlight html >}}
37
<button type="button" class="btn btn-primary" data-modal="alert" data-modal-title-str='["cookiesenabled", "core"]'
38
data-modal-content-str='["cookiesenabled_help_html", "core"]'>Show alert modal</button>
39
{{< /highlight >}}
40
 
41
You can also use it on PHP, you just need to set the attributes parameter to any moodle output component that takes attributes:
42
{{< php >}}
43
echo $OUTPUT->single_button('#', get_string('delete'), 'get', [
44
    'data-modal' => 'modal',
45
    'data-modal-title-str' => json_encode(['delete', 'core']),
46
    'data-modal-content-str' => json_encode(['areyousure']),
47
    'data-modal-yes-button-str' => json_encode(['delete', 'core'])
48
]);
49
{{< / php >}}
50
 
51
## Attributes
52
 
53
<table class="table">
54
  <thead>
55
    <tr>
56
      <th style="width: 250px;">Data attribute</th>
57
      <th>Description</th>
58
    </tr>
59
  </thead>
60
  <tbody>
61
    <tr>
62
      <td>data-modal</td>
63
      <td>One of either "confirmation", or "alert".</td>
64
    </tr>
65
    <tr>
66
      <td>data-modal-title-str</td>
67
      <td>The modal title language string identifier, must be provided in JSON encoded format.</td>
68
    </tr>
69
    <tr>
70
      <td>data-modal-content-str</td>
71
      <td>The modal content or content language string identifier, must be provided in JSON encoded format.</td>
72
    </tr>
73
    <tr>
74
      <td>data-modal-yes-button-str</td>
75
      <td>
76
        The language string identifier for the "Yes" button, must be provided in JSON encoded format.
77
        Confirmation modals only.
78
      </td>
79
    </tr>
80
    <tr>
81
      <td>data-modal-toast</td>
82
      <td>
83
        If set to "true" it will display a modal toast in the end.
84
        Confirmation modals only.
85
      </td>
86
    </tr>
87
    <tr>
88
      <td>data-modal-toast-confirmation-str</td>
89
      <td>
90
        The confirmation toast language string identifier, must be provided in JSON encoded format.
91
        Confirmation modals only.
92
      </td>
93
    </tr>
94
    <tr>
95
      <td>data-modal-destination</td>
96
      <td>
97
        An url to redirect the user to.
98
        Confirmation modals only.
99
      </td>
100
    </tr>
101
  </tbody>
102
</table>
103
 
104
## Examples
105
 
106
### Basic Alert modal
107
 
108
{{< example >}}
109
<button type="button" class="btn btn-primary" data-modal="alert" data-modal-title-str='["cookiesenabled", "core"]'
110
data-modal-content-str='["cookiesenabled_help_html", "core"]'>Show alert modal</button>
111
{{< /example >}}
112
 
113
### Basic confirmation modal
114
 
115
{{< example >}}
116
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
117
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]'>Show confirmation modal</button>
118
{{< /example >}}
119
 
120
### Confirmation modal with a toast
121
 
122
{{< example >}}
123
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
124
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]' data-modal-toast="true"
125
data-modal-toast-confirmation-str='["deleteblockinprogress", "block", "Online users"]'>Show confirmation modal</button>
126
{{< /example >}}
127
 
128
### Confirmation modal with redirect
129
 
130
{{< example >}}
131
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
132
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]'
133
data-modal-destination="http://moodle.com">Show confirmation modal</button>
134
{{< /example >}}