chore(eslint): ESLint setup (#6708)
[openemr.git] / interface / modules / zend_modules / public / js / autosuggest / autosuggest.js
blob62c5e41b25d8ca55d581e58e25b64cba0f5d1e3f
1 /**
2  * interface/modules/zend_modules/public/js/autosuggest/autosuggest.js
3  *
4  * @package   OpenEMR
5  * @link      https://www.open-emr.org
6  * @author    Remesh Babu S <remesh@zhservices.com>
7  * @copyright Copyright (c) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
8  * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
9  */
10 /**
11  * Auto suggest
12  * autosuggest.js
13  */
15 /**
16  * Function PreventIt
17  *
18  * @param {type} evt Event
19  * @returns {undefined}
20  */
21 function PreventIt(evt) {
22   evt = (evt) ? evt : window.event;
23         var charCode = (evt.which) ? evt.which : evt.keyCode;
24         if (charCode == 38 || charCode == 40) {
25                 if (evt.preventDefault) evt.preventDefault();
26                 if (evt.stopPropagation) evt.stopPropagation();
27          }
30 /**
31    * Function lookup
32    *
33    * @param {string}  inputString   keyword
34    * @param {string}  searchType    Patient, CPT, Diagnosis etc.
35    * @param {int}     searchEleNo   more then one search box
36    * @param {string}  searchMode    Single / Multiple
37    * @returns {undefined}
38    */
39   function lookup(inputString, searchType, searchEleNo, searchMode) {
40                 if(inputString.length == 0) {
41                         $('#suggestions' + searchEleNo).hide();
42                 } else {
43       if (searchEleNo == '') {
44         searchEleNo = 0;
45       }
46                         if (searchEleNo != '' || searchEleNo > 0) {
47         $('#inputString' + searchEleNo).keyup(function() {
48           $('#page').val("");
49         });
50         $('#inputString' + searchEleNo).keydown(function() {
51           $('#page').val("");
52         });
53         var inputString       = $('#inputString' + searchEleNo).val();
54         var inputStringCheck    = $('#inputString' + searchEleNo).val();
55       } else {
56         $('#inputString').keyup(function() {
57           $('#page').val("");
58         });
59         $('#inputString').keydown(function() {
60           $('#page').val("");
61         });
62         var inputString       = $('#inputString').val();
63         var inputStringCheck    = $('#inputString').val();
64       }
66                         var leading           = '%';
67                         var trailing           = '%';
68       var page = $('#page').val();
69       /** Path settings */
70       var path = window.location;
71       var arr = path.toString().split("public");
72       var count = arr[1].split("/").length-1;
73       var newPath = './';
74       for(var i = 0; i < count; i++){
75         newPath += '../';
76       }
77       /* eslint-disable-next-line no-unused-labels */
78             dataType: "html",
79                         $.post(newPath + "public/application/index/search", {
80         queryString : inputString,
81         leading     : leading,
82         trailing    : trailing,
83         page        : page,
84         searchType  : searchType,
85         searchEleNo : searchEleNo,
86         searchMode  : searchMode,
87       }, function(data){
88         /* eslint-disable-next-line no-unused-labels */
89                                 cache: false;
90                                 if(data.length > 0) {
91           if (searchEleNo > 0) {
92             var inputStringValue = $('#inputString' + searchEleNo).val();
93           } else {
94             var inputStringValue = $('#inputString').val();
95           }
96                                         if (inputStringCheck == inputStringValue) {
97                                                 if (searchEleNo > 0) {
98               $('#suggestions' + searchEleNo).show();
99               $('#autoSuggestionsList' + searchEleNo).html(data);
100             } else {
101               $('#suggestions').show();
102               $('#autoSuggestionsList').html(data);
103             }
104             // Focus to 1st Row
105             if(document.getElementById('list_' + searchEleNo + '_1')) {
106                 $('#list_' + searchEleNo + '_1').focus();
107                 $('#list_' + searchEleNo + '_1').css("background-color","#659CD8");
108             }
109                                         }
110                                 }
111                         });
112                 }
113         }
116  * Key Board Controls
118  * @param {type} evt  Event
119  * @param {type} row  Line No.
120  * @param {type} id   ID for arrow key scroll
121  * @param {type} no   Search Element no
122  * @returns {undefined}
123  */
124 function move(evt, row, id, no) {
125         evt = (evt) ? evt : window.event;
126         var charCode = (evt.which) ? evt.which : evt.keyCode;
127   if (charCode == 38 || charCode == 40) {
128     evt.stopPropagation();
129     evt.preventDefault();
130   }
131         if (charCode == 38) {
132                 row--;
133                 if(document.getElementById(id + '_' + (row))) {
134                         if((row + 1)%2 == 1) {
135         $('#' + id + '_' + (row + 1)).css("background-color", "#fff");
136                         } else {
137         $('#' + id + '_' + (row + 1)).css("background-color", "#fff");
138                         }
139                         $('#' + id + '_' + (row)).css("background-color","#659CD8");
140                         document.getElementById(id + '_' + (row)).focus();
141                 }
142         } else if (charCode == 40) {
143                 row++;
144                 if(document.getElementById(id + '_' + (row))) {
145                         if((row - 1)%2 == 1) {
146         $('#' + id + '_' + (row - 1)).css("background-color", "#fff");
147                         } else {
148         $('#' + id + '_' + (row - 1)).css("background-color", "#fff");
149                         }
150                         $('#' + id + '_' + (row)).css("background-color","#659CD8");
151                         document.getElementById(id + '_' + (row)).focus();
152                 }
153         } else if (charCode == 13) {
154                 if (no > 0) {
155                         $('#fill' + id + '_' + (row)).trigger('click');
156                 } else {
157                         $('#fill' + id + '_' + (row)).trigger('click');
158                 }
159         }  else if (charCode == 27) {
160                 if (no > 0) {
161                         $('#closeme' + no).trigger('click');
162                 } else {
163                         $('#closeme').trigger('click');
164                 }
166         } else {
167                 if (no > 0) {
168                         document.getElementById('inputString' + no).focus();
169                 } else {
170       $('#inputString').focus();
171                 }
172         }
177  * @param {type} params
178  * @param {type} id
179  * @param {type} no
180  * @returns {undefined}
181  */
182 function nextPage(params, id, no, mode) {
183         $('#page').val(params);
184         if (no != '' || no > 0) {
185                 lookup($('#inputString' + no).val(), id, no, mode);
186         } else {
187                 lookup($('#inputString').val(), id, no, mode);
188         }
191 function previousPage(params, id, no, mode) {
192         $('#page').val(params);
193         if (no != '' || no > 0) {
194                 lookup($('#inputString' + no).val(), id, no);
195         } else {
196                 lookup($('#inputString').val(), id, no, mode);
197         }
201  * Function Fill
202  * Fill the selected value in the input box
204  * @param {string} thisValue  Name to be Filled in the box
205  * @param {string} id         Search Type 'Patient, CPT, Diagnosis etc.'
206  * @param {int}    no         search Element No more then one search box
207  * @param {string} mode       Search Mode Single / Multiple
208  * @returns {undefined}
209  */
210 function fill(thisValue, id, no, mode) {
211         //Multiple search entry is in case of feesheet.
212         /*var multiplesearch = false;
213         if(document.getElementById('multiplesearch')){
214                 multiplesearch = document.getElementById('multiplesearch').checked ;
215         }*/
216   $('#form_date').prop('disabled', true);
217   $('#form_to_date').prop('disabled', true);
218   $('.validatebox-text').prop('disabled', true);
219   var arr = thisValue.split("|");
220   if (mode == 'single') {
221     if (no == '' || no == 0) {
222       setTimeout("$('#suggestions').hide();", 100);
223       $('#inputString').val(arr[0]);
224       $('#string_id').val(arr[1]);
225       //$('#string_id').attr('type', 'text');
226       $('#string_id').get(0).type = 'text';
227     } else {
228       setTimeout("$('#suggestions" + no + "').hide();", 100);
229       $('#inputString' + no).val(arr[0]);
230       $('#string_id' + no).val(arr[1]);
231       //$('#string_id').attr('type', 'text');
232       $('#string_id' + no).get(0).type = 'text';
233     }
234   }
235   if (mode == 'multiple') {
236     if (no == '' || no == 0) {
237       //$('#inputString').val(arr[0]);
238       $('#inputString').val('').focus();
239       if ($('#string_value').val().length > 0) {
240         $('#string_value').val($('#string_value').val() + ';' + arr[0]);
241       } else {
242         $('#string_value').val(arr[0]);
243       }
244       if ($("#string_id").val().length > 0) {
245         $('#string_id').val($('#string_id').val() + ';' + arr[1]);
246       } else {
247         $("#string_id").val(arr[1]);
248       }
250       var close_id = 'close|' + arr[0] + '|' + arr[1] + '|' + no;
251       var stringValue = '<div id="'+ arr[1] + '" class="selected_item">'
252               + arr[0]
253               + '<span class="close" id="' + close_id + '">&times;</span></div>';
254       $('#selected_search').append(stringValue);
255       //$("#target").append('<div style="border:1px solid red">' + arr[0] + '</div>');
256       setTimeout("$('#suggestions').hide();", 100);
257     } else {
258       $('#inputString' + no).val('').focus();
259       if ($('#string_value' + no).val().length > 0) {
260         $('#string_value' + no).val($('#string_value' + no).val() + ';' + arr[0]);
261       } else {
262         $('#string_value' + no).val(arr[0]);
263       }
264       if ($("#string_id" + no).val().length > 0) {
265         $('#string_id' + no).val($('#string_id' + no).val() + ';' + arr[1]);
266       } else {
267         $("#string_id" + no).val(arr[1]);
268       }
270       var close_id = 'close|' + arr[0] + '|' + arr[1] + '|' + no;
271       var stringValue = '<div id="'+ arr[1] + '" class="selected_item">'
272               + arr[0]
273               + '<span class="close" id="' + close_id + '">&times;</span></div>';
274       $('#selected_search' + no).append(stringValue);
275       //$("#target").append('<div style="border:1px solid red">' + arr[0] + '</div>');
276       setTimeout("$('#suggestions" + no + "').hide();", 100);
277     }
278   }
280         $('#page').val("");
284  * Remove Selected Item
286  */
287 $(document).on('click', '.close', function(e){
288   var id = $(this).attr("id");
289   var arrId = id.split('|');
290   var no = arrId[3];
291   // Remove from hidden string values
292   if (no > 0) {
293     var stringValue = $("#string_value" + no).val();
294   } else {
295     var stringValue = $("#string_value").val();
296   }
297   var arr = stringValue.split(';');
298   var str = '';
299   for (var i = 0; i < arr.length; i++) {
300     if (arr[i] != arrId[1]) {
301       str = str + arr[i] + ';';
302     }
303   }
304   str = str.slice(0, -1);
305   if (no > 0) {
306     $("#string_value" + no).val(str);
307   } else {
308     $("#string_value").val(str);
309   }
311   // Remove hidden ids
312   if (no > 0) {
313     var stringId = $("#string_id" + no).val();
314   } else {
315     var stringId = $("#string_id").val();
316   }
317   var arr = stringId.split(';');
318   var str = '';
319   for (var i = 0; i < arr.length; i++) {
320     if (arr[i] != arrId[2]) {
321       str = str + arr[i] + ';';
322     }
323   }
324   str = str.slice(0, -1);
325   if (no > 0) {
326     $("#string_id" + no).val(str);
327   } else {
328     $("#string_id").val(str);
329   }
330   $(this).parent().remove();
334  * Clear Input Values
335  */
336 $(document).on('change click keyup', '.lookup', function(e){
337   var targetId = e.target.id;
338   var arr = targetId.split('inputString');
339   var classname =e.target.classList[1];
340   //$('#' + targetId).val("");
341   if (e.type == 'change' || (e.type == 'keyup' && e.keyCode == 8)) {
342     if (arr[1] != '') {
343         if (classname != 'multiple') {  //do not reset values if mode is multiple
344             $('#string_value' + arr[1]).val("");
345             $('#string_id' + arr[1]).val("");
346         }
347     } else {
348       $('#string_value').val("");
349       $('#string_id').val("");
350       $('#form_date').prop('disabled', false);
351       $('#form_to_date').prop('disabled', false);
352       $('.validatebox-text').prop('disabled', false);
353     }
354   }
355   if (e.type == 'click') {
356     if ($(e.target).closest('.suggestions').length === 0) {
357       $('.suggestions').hide();
358     }
359   }
363 /*$(document).on('click', function(e){
364   if ($(e.target).closest('.suggestions').length === 0) {
365     $('.suggestions').hide();
366   }
367   if(this != $(".suggestions")[0]) {
368     $(".suggestions").hide();
369   }
370   var a = $(".suggestions");
371   bodyClick(a,e);
372 });*/
375  * Close Auto suggest List
376  * On Body click
377  */
378 $(document).mouseup(function(e){
379   var a = $('.panel_content_wrapper');
380   bodyClick(a,e);
383 // Hide Pop-up Mneu on Click outside
384 function bodyClick(container,e){
385   if(!container.is(e.target) && container.has(e.target).length === 0){
386     $(container).css("display","none");
387   }