3 * This include is just a HTML and JS *fragment* it CANNOT stand alone
5 * This file is used to include the capability to have a pop-up DIV in which
6 * a user can add a new option to a list box. Primarily this is for the
7 * demographics, history, and referral screens. This code could be used elsewhere
8 * as long as it had the required supporting HTML and JS
10 * REQUIRED to make work:
12 * - form element with the class '.addtolist'
13 * - be used in a file that already includes globals.php
16 * @link http://www.open-emr.org
17 * @author Brady Miller <brady.g.miller@gmail.com>
18 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
19 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
22 use OpenEMR\Common\Csrf\CsrfUtils;
26 <!-- hidden DIV used for creating new list items -->
27 <div id="newlistitem" class="body_top" style="border: 2px outset #666; display:none; position:absolute; padding:5px; ">
28 <input type="hidden" name="newlistitem_listid" id="newlistitem_listid" value="">
29 <input type="hidden" name="newlistitem_fieldid" id="newlistitem_fieldid" value="">
30 <div id="specificForm">
32 <div style = 'text-align:center;'>
33 <div style='margin:0.5em 0 0 0;'>
34 <input type='button' name='newlistitem_submit' id='newlistitem_submit' value='<?php echo xla('Add'); ?>'>
35 <input type='button' name='newlistitem_cancel' id='newlistitem_cancel' value='<?php echo xla('Cancel'); ?>'>
41 <script language="javascript">
43 // collect the custom state widget flag
44 var stateCustomFlag = <?php echo json_encode($GLOBALS['state_custom_addlist_widget']); ?>;
46 // generic form for input box
47 var generic = "<input type='textbox' name='newlistitem_value' id='newlistitem_value' size='20' maxlength='50'>"
49 // state form for input box
51 <ul style='list-style-type:none; margin:0; padding:0;'>\
52 <li style='font-weight:bold; font-size:120%; text-align:center;'>\
53 <?php echo xlt('Enter new State'); ?></li>\
54 <li style='margin:0.5em 0 0 0; padding:0;'>\
55 <?php echo xlt('Full Name'); ?>:\
56 <input type='textbox' name='newlistitem_value' id='newlistitem_value' size='20' maxlength='50'><li>\
57 <li style='margin:0.5em 0 0 0; padding:0;'>\
58 <?php echo xlt('Abbreviation'); ?>:\
59 <input type='textbox' name='newlistitem_abbr' id='newlistitem_abbr' size='10' maxlength='50'><li>\
63 // jQuery makes life easier (sometimes)
65 $(document).ready(function(){
67 /********************************************************/
68 /************ List-box modification functions ***********/
69 /********************************************************/
71 $("#newlistitem").on("keypress", function(evt) { if (evt.keyCode == 13) { SaveNewListItem(this, evt); return false; }});
72 $(".addtolist").on("click", function(evt) { AddToList(this, evt); });
73 $("#newlistitem_submit").on("click", function(evt) { SaveNewListItem(this, evt); });
74 $("#newlistitem_cancel").on("click", function(evt) { CancelAddToList(this, evt); });
76 // display the 'new list item' DIV at the mouse position
77 var AddToList = function(btnObj, e) {
78 // capture the ID of the list and specific field being modified from the object's ID
79 $('#newlistitem_listid').val($(btnObj).attr("id").replace(/^addtolistid_/g, ""));
80 $('#newlistitem_fieldid').val($(btnObj).attr("fieldid"));
82 // REMOVE previous form
83 $("#specificForm").empty();
85 // INSERT the selected form
86 if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
87 // insert state form and clear values
88 $("#specificForm").append(state);
89 $('#newlistitem_value').val("");
90 $('#newlistitem_abbr').val("");
93 // insert generic form and clear values
94 $("#specificForm").append(generic);
95 $('#newlistitem_value').val("");
98 // make the item visible before setting its x,y location
99 $('#newlistitem').css('display', 'inline');
100 //getting height and width of the message box
101 var height = $('#newlistitem').height();
102 var width = $('#newlistitem').width();
103 //calculating offset for displaying popup message
104 leftVal=e.pageX-(width/2)+"px";
105 topVal=e.pageY-(height/2)+"px";
106 //show the DIV and set cursor focus
107 $('#newlistitem').css({left:leftVal,top:topVal}).show();
108 $('#newlistitem_value').focus();
111 // hide the add-to-list DIV and clear its textbox
112 var CancelAddToList = function(btnObj, e) {
113 $('#newlistitem').hide();
116 // save the new list item to the given list
117 var SaveNewListItem = function(btnObj, e) {
118 // VALIDATE the selected form
119 // Don't allow a number as first character
120 // Don't allow illegal characters (' and " for now) - still developing
121 // First, validate fields common to all forms
122 if ($("#newlistitem_value").val().match(/^\d/)) {
123 alert(<?php echo xlj('List items can not start with a number.'); ?>);
124 $("#newlistitem_value").focus();
127 if ($("#newlistitem_value").val().match(/[\'\"]/)) {
128 alert(<?php echo xlj('List items contains illegal character(s).'); ?>);
129 $("#newlistitem_value").focus();
132 // Second, validate form specific fields
133 if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
134 // state forms specific validation
135 if ($("#newlistitem_abbr").val().match(/^\d/)) {
136 alert(<?php echo xlj('List items can not start with a number.'); ?>);
137 $("#newlistitem_abbr").focus();
140 if ($("#newlistitem_abbr").val().match(/[\'\"]/)) {
141 alert(<?php echo xlj('List items contains illegal character(s).'); ?>);
142 $("#newlistitem_abbr").focus();
147 // PROCESS the selected form
148 if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
149 // process state form
150 // (note we have collected a title and abbreviation)
151 var listid = $("#newlistitem_listid").val();
152 var newitem = $('#newlistitem_value').val();
153 var newitem_abbr = $('#newlistitem_abbr').val();
157 // process generic form
158 // (note we have only collected the title, so will make
159 // abbreviation same as title)
160 var listid = $("#newlistitem_listid").val();
161 var newitem = $('#newlistitem_value').val();
162 var newitem_abbr = $('#newlistitem_value').val();
165 // make the AJAX call to save the new value to the specified list
166 // upon returning successfully, refresh the list box and select
168 $.getJSON("<?php echo $GLOBALS['webroot']; ?>/library/ajax/addlistitem.php",
169 {csrf_token_form: <?php echo js_escape(CsrfUtils::collectCsrfToken()); ?>,
172 newitem_abbr: newitem_abbr},
173 function(jsondata, txtresponse) {
174 if( jsondata.error == '' ){
175 //process each widget with the same list
176 $("select.addtolistclass_"+listid).each(function(){
177 var pointer = $(this);
178 var currentselected = pointer.val();
179 var listboxfield = pointer.attr("id");
180 var listbox = document.getElementById(listboxfield);
181 while (listbox.options.length > 0) { listbox.options[0] = null; }
182 $.each(jsondata.options, function () {
183 if (listboxfield == $('#newlistitem_fieldid').val()) {
184 // build select for the chosen widget field
185 listbox.options[listbox.options.length] = new Option(this.title, this.id);
186 if (this.id == newitem_abbr) {
187 pointer.val(this.id);
191 // build select for the other widgets using the same list
192 listbox.options[listbox.options.length] = new Option(this.title, this.id);
193 if (this.id == currentselected) {
194 pointer.val(currentselected);
201 $('#newlistitem').hide();
204 alert( jsondata.error );
209 } // end SaveNewListItem
211 }); // end jQuery .ready