1 | /* |
||
2 | * Copyright (C) 2021 Joe Nilson <[email protected]> |
||
3 | * |
||
4 | * This program is free software: you can redistribute it and/or modify |
||
5 | * it under the terms of the GNU Lesser General Public License as |
||
6 | * published by the Free Software Foundation, either version 3 of the |
||
7 | * License, or (at your option) any later version. |
||
8 | * |
||
9 | * This program is distributed in the hope that it will be useful, |
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | * GNU Lesser General Public License for more details. |
||
13 | * You should have received a copy of the GNU Lesser General Public License |
||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
15 | */ |
||
16 | |||
17 | /** |
||
18 | * Modal Creation Functions |
||
19 | * Code based on the gist https://gist.github.com/signalpoint/e6dda6b6220a157edbd170f3d2a6250e |
||
20 | * |
||
21 | * @example |
||
22 | * Call the function executeModal with this parameters |
||
23 | * executeModal( |
||
24 | * 'completeNCFData', // This is the modalId |
||
25 | * 'Modal Title', // This is the title param |
||
26 | * '<b>Message</b>', // This is the message, can be plain text or html code. |
||
27 | * 'default', // This is the contentType, based on the content type will be showed some butons and alert indicators. |
||
28 | * 'saveFn' // If we need to process a javascript call we can set a function to execute code and process the modal. |
||
29 | * ); |
||
30 | */ |
||
31 | |||
32 | /** |
||
33 | * @param {string} modalId |
||
34 | * @param {string} title |
||
35 | * @param {string} content |
||
36 | * @param {string} contentType |
||
37 | */ |
||
38 | function executeModal(modalId, title, content, contentType = 'default', saveButtonCallback = '') |
||
39 | { |
||
40 | //Set the modal ID |
||
41 | let actualModal = getModal(modalId); |
||
42 | |||
43 | // Init the modal if it hasn't been already. |
||
44 | actualModal = (!actualModal) ? initModal(modalId) : actualModal; |
||
45 | |||
46 | //Set the modal content based on the type |
||
47 | content = setModalContentType(contentType, content); |
||
48 | |||
49 | //Choose the buttons to show based on the contentType |
||
50 | let buttons = setModalButtons(contentType, saveButtonCallback); |
||
51 | |||
52 | //Assemble all the pieces in the final modal html body |
||
53 | let html = setModalHtml(modalId, title, content, buttons); |
||
54 | |||
55 | //Put the modal content in the modal |
||
56 | setModalContent(modalId, html); |
||
57 | |||
58 | //Show the modal |
||
59 | $(actualModal).modal('show'); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @returns {HTMLElement} |
||
64 | * @param {string} modalId |
||
65 | */ |
||
66 | function getModal(modalId) |
||
67 | { |
||
68 | return document.getElementById(modalId); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param {string} contentType |
||
73 | * @param {string} content |
||
74 | * @returns {string} |
||
75 | */ |
||
76 | function setModalContentType(contentType, content) |
||
77 | { |
||
78 | if (contentType == 'default') { |
||
79 | return content; |
||
80 | } else { |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
![]() |
|||
81 | return '<div className="alert alert-' + contentType + '" role="alert"> ' + content + '</div>'; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param {string} modalId |
||
87 | * @param {string} html |
||
88 | */ |
||
89 | function setModalContent(modalId, html) |
||
90 | { |
||
91 | getModal(modalId).querySelector('.modal-content').innerHTML = html; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param {string} modalId |
||
96 | * @param {string} title |
||
97 | * @param {string} content |
||
98 | * @param {string} buttons |
||
99 | * @returns {string} |
||
100 | */ |
||
101 | function setModalHtml(modalId, title, content, buttons) |
||
102 | { |
||
103 | var html = |
||
104 | '<div class="modal-header">' + |
||
105 | '<h5 class="modal-title" id="'+modalId+'-ModalLabel">'+title+'</h5>' + |
||
106 | '<button type="button" class="close" data-dismiss="modal" aria-label="Close">' + |
||
107 | '<span aria-hidden="true">×</span>' + |
||
108 | '</button>' + |
||
109 | '</div>' + |
||
110 | '<div class="modal-body">' + |
||
111 | content + |
||
112 | '</div>' + |
||
113 | '<div class="modal-footer">' + |
||
114 | buttons + |
||
115 | '</div>'; |
||
116 | return html; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param {string} contentType |
||
121 | * @param {string} saveButtonCallback |
||
122 | * @returns {string} {string} |
||
123 | */ |
||
124 | function setModalButtons(contentType, saveButtonCallback) |
||
125 | { |
||
126 | let buttons; |
||
127 | |||
128 | const onClick = (saveButtonCallback) ? ' onClick=' + saveButtonCallback + '(this)' : ''; |
||
129 | const cancelButton = '<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>'; |
||
130 | const warningButton = '<button type="button" class="btn btn-danger" data-dismiss="modal">Cerrar</button>'; |
||
131 | const saveButton = '<button type="button" class="btn btn-primary"'+onClick+'>Guardar</button>'; |
||
132 | const pickupButton = '<button type="button" class="btn btn-primary"' +onClick+'>Usar</button>'; |
||
133 | |||
134 | switch (contentType) { |
||
135 | case 'warning': |
||
136 | buttons = warningButton; |
||
137 | break; |
||
138 | case 'pickup': |
||
139 | buttons = cancelButton + pickupButton; |
||
140 | break; |
||
141 | case 'default': |
||
142 | buttons = cancelButton + saveButton; |
||
143 | break; |
||
144 | default: |
||
145 | buttons = cancelButton + saveButton; |
||
146 | break; |
||
147 | } |
||
148 | return buttons; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | |||
153 | * @param {string} modalId |
||
154 | * @returns {HTMLDivElement} |
||
155 | */ |
||
156 | function initModal(modalId) |
||
157 | { |
||
158 | let modal = document.createElement('div'); |
||
159 | modal.classList.add('modal', 'fade'); |
||
160 | modal.setAttribute('id', modalId); |
||
161 | modal.setAttribute('tabindex', '-1'); |
||
162 | modal.setAttribute('role', 'dialog'); |
||
163 | modal.setAttribute('aria-labelledby', modalId+'-ModalLabel'); |
||
164 | modal.setAttribute('aria-hidden', 'true'); |
||
165 | modal.innerHTML = |
||
166 | '<div class="modal-dialog" role="document">' + |
||
167 | '<div class="modal-content"></div>' + |
||
168 | '</div>'; |
||
169 | document.body.appendChild(modal); |
||
170 | return modal; |
||
171 | } |