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
24 <!-- hidden DIV used for creating new list items -->
25 <div id="newlistitem" class="body_top" style="border: 2px outset #666; display:none; position:absolute; padding:5px; ">
26 <input type="hidden" name="newlistitem_listid" id="newlistitem_listid" value="">
27 <input type="hidden" name="newlistitem_fieldid" id="newlistitem_fieldid" value="">
28 <div id="specificForm">
30 <div style = 'text-align:center;'>
31 <div style='margin:0.5em 0 0 0;'>
32 <input type='button' name='newlistitem_submit' id='newlistitem_submit' value='<?php echo xla('Add'); ?>'>
33 <input type='button' name='newlistitem_cancel' id='newlistitem_cancel' value='<?php echo xla('Cancel'); ?>'>
39 <script language="javascript">
41 // collect the custom state widget flag
42 var stateCustomFlag = <?php echo json_encode($GLOBALS['state_custom_addlist_widget']); ?>;
44 // generic form for input box
45 var generic = "<input type='textbox' name='newlistitem_value' id='newlistitem_value' size='20' maxlength='50'>"
47 // state form for input box
49 <ul style='list-style-type:none; margin:0; padding:0;'>\
50 <li style='font-weight:bold; font-size:120%; text-align:center;'>\
51 <?php echo xlt('Enter new State'); ?></li>\
52 <li style='margin:0.5em 0 0 0; padding:0;'>\
53 <?php echo xlt('Full Name'); ?>:\
54 <input type='textbox' name='newlistitem_value' id='newlistitem_value' size='20' maxlength='50'><li>\
55 <li style='margin:0.5em 0 0 0; padding:0;'>\
56 <?php echo xlt('Abbreviation'); ?>:\
57 <input type='textbox' name='newlistitem_abbr' id='newlistitem_abbr' size='10' maxlength='50'><li>\
61 // jQuery makes life easier (sometimes)
63 $(document).ready(function(){
65 /********************************************************/
66 /************ List-box modification functions ***********/
67 /********************************************************/
69 $("#newlistitem").keypress(function(evt) { if (evt.keyCode == 13) { SaveNewListItem(this, evt); return false; }});
70 $(".addtolist").click(function(evt) { AddToList(this, evt); });
71 $("#newlistitem_submit").click(function(evt) { SaveNewListItem(this, evt); });
72 $("#newlistitem_cancel").click(function(evt) { CancelAddToList(this, evt); });
74 // display the 'new list item' DIV at the mouse position
75 var AddToList = function(btnObj, e) {
76 // capture the ID of the list and specific field being modified from the object's ID
77 $('#newlistitem_listid').val($(btnObj).attr("id").replace(/^addtolistid_/g, ""));
78 $('#newlistitem_fieldid').val($(btnObj).attr("fieldid"));
80 // REMOVE previous form
81 $("#specificForm").empty();
83 // INSERT the selected form
84 if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
85 // insert state form and clear values
86 $("#specificForm").append(state);
87 $('#newlistitem_value').val("");
88 $('#newlistitem_abbr').val("");
91 // insert generic form and clear values
92 $("#specificForm").append(generic);
93 $('#newlistitem_value').val("");
96 // make the item visible before setting its x,y location
97 $('#newlistitem').css('display', 'inline');
98 //getting height and width of the message box
99 var height = $('#newlistitem').height();
100 var width = $('#newlistitem').width();
101 //calculating offset for displaying popup message
102 leftVal=e.pageX-(width/2)+"px";
103 topVal=e.pageY-(height/2)+"px";
104 //show the DIV and set cursor focus
105 $('#newlistitem').css({left:leftVal,top:topVal}).show();
106 $('#newlistitem_value').focus();
109 // hide the add-to-list DIV and clear its textbox
110 var CancelAddToList = function(btnObj, e) {
111 $('#newlistitem').hide();
114 // save the new list item to the given list
115 var SaveNewListItem = function(btnObj, e) {
116 // VALIDATE the selected form
117 // Don't allow a number as first character
118 // Don't allow illegal characters (' and " for now) - still developing
119 // First, validate fields common to all forms
120 if ($("#newlistitem_value").val().match(/^\d/)) {
121 alert(<?php echo xlj('List items can not start with a number.'); ?>);
122 $("#newlistitem_value").focus();
125 if ($("#newlistitem_value").val().match(/[\'\"]/)) {
126 alert(<?php echo xlj('List items contains illegal character(s).'); ?>);
127 $("#newlistitem_value").focus();
130 // Second, validate form specific fields
131 if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
132 // state forms specific validation
133 if ($("#newlistitem_abbr").val().match(/^\d/)) {
134 alert(<?php echo xlj('List items can not start with a number.'); ?>);
135 $("#newlistitem_abbr").focus();
138 if ($("#newlistitem_abbr").val().match(/[\'\"]/)) {
139 alert(<?php echo xlj('List items contains illegal character(s).'); ?>);
140 $("#newlistitem_abbr").focus();
145 // PROCESS the selected form
146 if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
147 // process state form
148 // (note we have collected a title and abbreviation)
149 var listid = $("#newlistitem_listid").val();
150 var newitem = $('#newlistitem_value').val();
151 var newitem_abbr = $('#newlistitem_abbr').val();
155 // process generic form
156 // (note we have only collected the title, so will make
157 // abbreviation same as title)
158 var listid = $("#newlistitem_listid").val();
159 var newitem = $('#newlistitem_value').val();
160 var newitem_abbr = $('#newlistitem_value').val();
163 // make the AJAX call to save the new value to the specified list
164 // upon returning successfully, refresh the list box and select
166 $.getJSON("<?php echo $GLOBALS['webroot']; ?>/library/ajax/addlistitem.php",
167 {csrf_token_form: <?php echo js_escape(collectCsrfToken()); ?>,
170 newitem_abbr: newitem_abbr},
171 function(jsondata, txtresponse) {
172 if( jsondata.error == '' ){
173 //process each widget with the same list
174 $("select.addtolistclass_"+listid).each(function(){
175 var pointer = $(this);
176 var currentselected = pointer.val();
177 var listboxfield = pointer.attr("id");
178 var listbox = document.getElementById(listboxfield);
179 while (listbox.options.length > 0) { listbox.options[0] = null; }
180 $.each(jsondata.options, function () {
181 if (listboxfield == $('#newlistitem_fieldid').val()) {
182 // build select for the chosen widget field
183 listbox.options[listbox.options.length] = new Option(this.title, this.id);
184 if (this.id == newitem_abbr) {
185 pointer.val(this.id);
189 // build select for the other widgets using the same list
190 listbox.options[listbox.options.length] = new Option(this.title, this.id);
191 if (this.id == currentselected) {
192 pointer.val(currentselected);
199 $('#newlistitem').hide();
202 alert( jsondata.error );
207 } // end SaveNewListItem
209 }); // end jQuery .ready