fix(eslint): fix no-useless-escape error (#6734)
[openemr.git] / library / js / ajax_functions_writer.js
blobc94f34af7289f66993c836a17940b9e3b2964f1c
1 // This program is free software; you can redistribute it and/or
2 // modify it under the terms of the GNU General Public License
3 // as published by the Free Software Foundation; either version 3
4 // of the License, or (at your option) any later version.
5 // modified to use activeElement for general edit sjpadgett@gmail.com 06/18/2019
6 //
8 function moveOptions_11(theSelFrom, theSelTo) {
9     document.getElementById(theSelFrom).style.color = "red";
10     document.getElementById(theSelFrom).style.fontStyle = "italic";
11     var str = document.getElementById(theSelFrom).innerHTML;
12     if (window.frames[0].document.body.innerHTML == '<br />')
13         window.frames[0].document.body.innerHTML = "";
14     var patt = /\?\?/;
15     var result = patt.test(str);
16     if (result) {
17         url = 'quest_popup.php?content=' + str;
18         window.open(url, 'quest_pop', 'width=640,height=190,menubar=no,toolbar=0,location=0, directories=0, status=0,left=400,top=375');
19         //dlgopen(url,'quest_pop', '', 640, 190);
20     } else {
21         val = str;
22         CKEDITOR.instances.textarea1.insertText(val);
23     }
26 function movePD(val, theSelTo) {
27     var textAreaContent = window.frames[0].document.body.innerHTML;
28     var textFrom = val;
29     if (textAreaContent != '')
30         textAreaContent += "  " + textFrom;
31     else
32         textAreaContent += textFrom;
33     window.frames[0].document.body.innerHTML = textAreaContent;
36 function nl2br(str) {
37     return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
40 function br2nl(str) {
41     return str.replace(/<\s*\/?br\s*[/]?>/gi, "\r\n");
44 function edit(id, ccFlag = '') {
45     let val = '';
46     if (ccFlag) {
47         // text edits
48         if (ccFlag === 'id') {
49             val = window.opener.document.getElementById(id).value;
50         } else {
51             // must be name attr.
52             val = window.opener.document.querySelector('textarea[name=' + id + ']').value;
53             if (val === null) {
54                 val = window.opener.document.querySelector("input[name=" + id + "]").value;
55             }
56         }
57         document.getElementById('textarea1').value = nl2br(val);
58         return;
59     } else {
60         val = window.opener.document.getElementById(id).value;
61     }
62     arr = val.split("|*|*|*|");
63     document.getElementById('textarea1').value = arr[0];
66 function ascii_write(asc, theSelTo) {
67     var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
68     var is_ie = navigator.userAgent.toLowerCase().indexOf('msie') > -1;
69     var is_apple = navigator.userAgent.toLowerCase().indexOf('apple') > -1;
70     if (asc == 13) {
71         if (!is_ie) {
72             var plugin = CKEDITOR.plugins.enterkey,
73                 enterBr = plugin.enterBr,
74                 editor = CKEDITOR.instances.textarea1;
75             forceMode = editor.config.forceEnterMode;
76             mode = editor.config.enterMode;
77             editor.fire('saveSnapshot');        // Save undo step.
78             enterBr(editor, mode, null, forceMode);
79             if (is_chrome || is_apple)
80                 enterBr(editor, mode, null, forceMode);
81         } else {
82             CKEDITOR.instances.textarea1.insertText('\r\n');
83         }
84     } else {
85         if (asc == 'para') {
86             var textFrom = "\r\n\r\n";
87             CKEDITOR.instances.textarea1.insertText(textFrom);
88             if (is_chrome || is_apple)
89                 CKEDITOR.instances.textarea1.insertText(textFrom);
90         } else {
91             if (asc == 32)
92                 var textFrom = "  ";
93             else
94                 var textFrom = String.fromCharCode(asc);
95             CKEDITOR.instances.textarea1.insertText(textFrom);
96         }
97     }
100 function SelectToSave(textara, ccFlag = '') {
101     let textAreaContent = '';
102     let mainForm = window.opener.document;
103     if (ccFlag) {
104         // text templates
105         let textEl = '';
106         textAreaContent = window.frames[0].document.body.innerText;
107         if (ccFlag === 'id') {
108             textEl = mainForm.getElementById(textara);
109             if (textEl === null) {
110                 alert(xl('Can not find where to insert.'));
111                 return;
112             }
113         } else {
114             textEl = mainForm.querySelector('textarea[name=' + textara + ']');
115             if (textEl === null) {
116                 // maybe input for sentence
117                 textEl = mainForm.querySelector("input[name=" + textara + "]");
118             }
119         }
120         textEl.value = jsText(br2nl(textAreaContent));
121     } else {
122         // must be html for nation note.
123         textAreaContent = window.frames[0].document.body.innerHTML;
124         if (mainForm.getElementById(textara + '_div'))
125             mainForm.getElementById(textara + '_div').innerHTML = textAreaContent;
126         if (mainForm.getElementById(textara + '_optionTD') && document.getElementById('options'))
127             mainForm.getElementById(textara + '_optionTD').innerHTML = document.getElementById('options').innerHTML;
128         if (mainForm.getElementById(textara)) {
129             mainForm.getElementById(textara).value = textAreaContent;
130             if (document.getElementById('options')) {
131                 mainForm.getElementById(textara).value += "|*|*|*|" + document.getElementById('options').innerHTML;
132             }
133         }
134     }
135     // close our dialog however insert turns out.
136     dlgclose();
139 function removeHTMLTags(strInputCode) {
140     /*
141             This line is optional, it replaces escaped brackets with real ones,
142             i.e. < is replaced with < and > is replaced with >
143     */
144     strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1) {
145         return (p1 == "lt") ? "<" : ">";
146     });
147     var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
150 function supportDragAndDrop(thedata) {
151     let tempEl = document.createElement('div'), finalEl = '';
152     tempEl.innerHTML = thedata;
153     for (let i = 0; i < tempEl.children.length; i++) {
154         let ele = tempEl.children[i], temp;
155         if (ele.id) {
156             ele.classList.add("draggable")
157             temp = document.createElement('div')
158             temp.classList.add("droppable");
159             temp.appendChild(ele.cloneNode(true));
160             finalEl += temp.outerHTML;
161         } else {
162             finalEl += ele.outerHTML;
163         }
164     }
165     return finalEl;
168 function TemplateSentence(val) {
169     if (val) {
170         document.getElementById('share').style.display = '';
171     } else {
172         document.getElementById('share').style.display = 'none';
173     }
174     $.ajax({
175         type: "POST",
176         url: "ajax_code.php",
177         dataType: "html",
178         data: {
179             templateid: val
180         },
181         success: function (thedata) {
182             //alert(thedata)
183             document.getElementById('template_sentence').innerHTML = supportDragAndDrop(thedata);
184         },
185         error: function () {
186             //alert("fail");
187         }
188     });
189     return;
192 function delete_item(id) {
193     //alert(id);
194     if (confirm("Do you really wants to delete this?")) {
195         $.ajax({
196             type: "POST",
197             url: "ajax_code.php",
198             dataType: "html",
199             data: {
200                 templateid: document.getElementById('template').value,
201                 item: id,
202                 source: "delete_item"
203             },
204             success: function (thedata) {
205                 //alert(thedata)
206                 document.getElementById('template_sentence').innerHTML = supportDragAndDrop(thedata);
207             },
208             error: function () {
209                 //alert("fail");
210             }
211         });
212         return;
213     }
214     return false;
217 function add_item() {
218     document.getElementById('new_item').style.display = '';
219     document.getElementById('item').focus();
222 function cancel_item(id) {
223     if (document.getElementById('new_item'))
224         document.getElementById('new_item').style.display = 'none';
225     if (document.getElementById('update_item' + id))
226         document.getElementById('update_item' + id).style.display = 'none';
229 function save_item() {
230     $.ajax({
231         type: "POST",
232         url: "ajax_code.php",
233         dataType: "html",
234         data: {
235             item: document.getElementById('item').value,
236             templateid: document.getElementById('template').value,
237             source: "add_item"
239         },
240         success: function (thedata) {
241             //alert(thedata)
242             document.getElementById('template_sentence').innerHTML = supportDragAndDrop(thedata);
243             cancel_item('');
244         },
245         error: function () {
246             //alert("fail");
247         }
248     });
249     return;
252 function update_item_div(id) {
253     document.getElementById('update_item' + id).style.display = '';
254     document.getElementById('update_item_txt' + id).focus();
257 function update_item(id) {
258     $.ajax({
259         type: "POST",
260         url: "ajax_code.php",
261         dataType: "html",
262         data: {
263             item: id,
264             templateid: document.getElementById('template').value,
265             content: document.getElementById('update_item_txt' + id).value,
266             source: "update_item"
268         },
269         success: function (thedata) {
270             //alert(thedata)
271             document.getElementById('template_sentence').innerHTML = supportDragAndDrop(thedata);
272             cancel_item(id);
273         },
274         error: function () {
275             //alert("fail");
276         }
277     });
278     return;