quick minor path updates (#1968)
[openemr.git] / library / options.js.php
blob0a9d9612f1ab97f20e3468e0845b1fdef39825b1
1 <?php
2 /**
3 * This is the place to put JavaScript functions that are needed to support
4 * options.inc.php. Include this in the <head> section of relevant modules.
5 * It's a .php module so that translation can be supported.
7 * @package OpenEMR
8 * @link http://www.open-emr.org
9 * @author Rod Roark <rod@sunsetsystems.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2014-2017 Rod Roark <rod@sunsetsystems.com>
12 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 <script type="text/javascript">
18 // JavaScript support for date types when the A or B edit option is used.
19 // Called to recompute displayed age dynamically when the corresponding date is
20 // changed. Must generate the same age formats as the oeFormatAge() function.
22 function updateAgeString(fieldid, asof, format, description) {
23 var datefld = document.getElementById('form_' + fieldid);
24 var f = datefld.form;
25 var age = '';
26 var date1 = new Date(datefld.value);
27 var date2 = asof ? new Date(asof) : new Date();
28 if (format == 3) {
29 // Gestational age.
30 var msecs = date2.getTime() - date1.getTime();
31 var days = Math.round(msecs / (24 * 60 * 60 * 1000));
32 var weeks = Math.floor(days / 7);
33 days = days % 7;
34 if (description == '') description = <?php echo xlj('Gest age') ?>;
35 age = description + ' ' +
36 weeks + (weeks == 1 ? ' ' + <?php echo xlj('week') ?> : ' ' + <?php echo xlj('weeks') ?>) + ' ' +
37 days + (days == 1 ? ' ' + <?php echo xlj('day') ?> : ' ' + <?php echo xlj('days') ?>);
39 else {
40 // Years or months.
41 var dayDiff = date2.getDate() - date1.getDate();
42 var monthDiff = date2.getMonth() - date1.getMonth();
43 var yearDiff = date2.getFullYear() - date1.getFullYear();
44 var ageInMonths = yearDiff * 12 + monthDiff;
45 if (dayDiff < 0) --ageInMonths;
46 if (format == 1 || (format == 0 && ageInMonths >= 24)) {
47 age = yearDiff;
48 if (monthDiff < 0 || (monthDiff == 0 && dayDiff < 0)) --age;
49 age = '' + age;
51 else {
52 age = '' + ageInMonths;
53 if (format == 0) {
54 age = age + ' ' + (ageInMonths == 1 ? <?php echo xlj('month') ?> : <?php echo xlj('months') ?>);
57 if (description == '') description = <?php echo xlj('Age') ?>;
58 if (age != '') age = description + ' ' + age;
60 document.getElementById('span_' + fieldid).innerHTML = age;
63 // Function to show or hide form fields (and their labels) depending on "skip conditions"
64 // defined in the layout.
66 var cskerror = false; // to avoid repeating error messages
67 function checkSkipConditions() {
68 var myerror = cskerror;
69 var prevandor = '';
70 var prevcond = false;
71 for (var i = 0; i < skipArray.length; ++i) {
72 var target = skipArray[i].target;
73 var id = skipArray[i].id;
74 var itemid = skipArray[i].itemid;
75 var operator = skipArray[i].operator;
76 var value = skipArray[i].value;
77 var is_radio = false;
78 var action = skipArray[i].action;
79 var tofind = id;
81 if (itemid) tofind += '[' + itemid + ']';
82 // Some different source IDs are possible depending on the data type.
83 var srcelem = document.getElementById('check_' + tofind);
84 var radio_id='form_' + tofind + '[' + value + ']';
85 if(typeof document.getElementById(radio_id)!=="undefined"){
86 srcelem = document.getElementById(radio_id);
87 if(srcelem != null){
88 is_radio = true;
91 if (srcelem == null) srcelem = document.getElementById('radio_' + tofind);
92 if (srcelem == null) srcelem = document.getElementById('form_' + tofind) ;
93 if (srcelem == null) srcelem = document.getElementById('text_' + tofind);
95 if (srcelem == null) {
96 if (!cskerror) alert(<?php echo xlj('Cannot find a skip source field for'); ?> + ' "' + tofind + '"');
97 myerror = true;
98 continue;
101 var condition = false;
102 var is_multiple = false;
103 var elem_val;
104 if ( is_radio){
105 for (var k = 0; k < document.getElementsByName('form_' + tofind).length; k++){
106 if (document.getElementsByName('form_' + tofind)[k].checked){
107 elem_val= document.getElementsByName('form_' + tofind)[k].value;
110 }else if( typeof srcelem.options!=="undefined" && srcelem.type == 'select-one' ){
111 elem_val=srcelem.options[srcelem.selectedIndex].value;
113 }else if( srcelem.type == 'select-multiple' ) {
114 elem_val = new Array();
115 is_multiple = true;
116 for (var k = 0; k < srcelem.length; k++) {
117 if (srcelem.options[k].selected) {
118 if( elem_val.indexOf(srcelem.options[k].value)<0) {
119 elem_val.push(srcelem.options[k].value);
123 } else {
124 elem_val=srcelem.value;
126 if(elem_val == null) {
127 elem_val = srcelem.getAttribute("data-value");
128 if( elem_val !== null && elem_val.indexOf("|") !== -1 ) {
129 elem_val = elem_val.split("|");
130 is_multiple = true;
133 if(elem_val == null) elem_val = srcelem.innerText;
135 //this is a feature fix for the multiple select list option
136 //collect all the multiselect control values:
137 if( is_multiple ) {
138 switch(operator) {
139 case 'eq':
140 condition = (-1 !== elem_val.indexOf(value));break;
141 case 'ne':
142 condition = (-1 == elem_val.indexOf(value)); break;
143 case 'se':
144 condition = srcelem.checked ; break; // doesn't make sense?
145 case 'ns':
146 condition = !srcelem.checked; break;
149 } else {
150 if (operator == 'eq') condition = elem_val == value; else
151 if (operator == 'ne') condition = elem_val != value; else
152 if (operator == 'se') condition = srcelem.checked ; else
153 if (operator == 'ns') condition = !srcelem.checked;
156 // Logic to accumulate multiple conditions for the same target.
157 // alert('target = ' + target + ' prevandor = ' + prevandor + ' prevcond = ' + prevcond); // debugging
158 if (prevandor == 'and') condition = condition && prevcond; else
159 if (prevandor == 'or' ) condition = condition || prevcond;
160 prevandor = skipArray[i].andor;
161 prevcond = condition;
162 var j = i + 1;
163 if (j < skipArray.length && skipArray[j].target == target) continue;
165 // At this point condition indicates the target should be hidden or have its value set.
167 if (action == 'skip') {
168 var trgelem1 = document.getElementById('label_id_' + target);
169 var trgelem2 = document.getElementById('value_id_text_' + target);
170 if (trgelem2 == null) {
171 trgelem2 = document.getElementById('value_id_' + target);
174 if (trgelem1 == null && trgelem2 == null) {
175 var trgelem1 = document.getElementById('label_' + target);
176 var trgelem2 = document.getElementById('text_' + target);
177 if(trgelem2 == null){
178 trgelem2 = document.getElementById('form_' + target);
180 if (trgelem1 == null && trgelem2 == null) {
181 if (!cskerror) alert(<?php echo xlj('Cannot find a skip target field for'); ?> + ' "' + target + '"');
182 myerror = true;
183 continue;
187 // Find the target row and count its cells, accounting for colspans.
188 var trgrow = trgelem1 ? trgelem1.parentNode : trgelem2.parentNode;
189 var rowcells = 0;
190 for (var itmp = 0; itmp < trgrow.cells.length; ++itmp) {
191 rowcells += trgrow.cells[itmp].colSpan;
194 // If the item occupies a whole row then undisplay its row, otherwise hide its cells.
195 var colspan = 0;
196 if (trgelem1) colspan += trgelem1.colSpan;
197 if (trgelem2) colspan += trgelem2.colSpan;
198 if (colspan < rowcells) {
199 if (trgelem1) trgelem1.style.visibility = condition ? 'hidden' : 'visible';
200 if (trgelem2) trgelem2.style.visibility = condition ? 'hidden' : 'visible';
202 else {
203 trgrow.style.display = condition ? 'none' : '';
206 else if (condition) { // action starts with "value="
207 var trgelem = document.getElementById('form_' + target);
208 if (trgelem == null) {
209 if (!cskerror) alert('Cannot find a value target field "' + trgelem + '"');
210 myerror = true;
211 continue;
213 var action_value = action.substring(6);
214 if (trgelem.type == 'checkbox') {
215 trgelem.checked = !(action_value == '0' || action_value == '');
217 else {
218 trgelem.value = action_value;
222 // If any errors, all show in the first pass and none in subsequent passes.
223 cskerror = cskerror || myerror;
226 ///////////////////////////////////////////////////////////////////////
227 // Image canvas support starts here.
228 ///////////////////////////////////////////////////////////////////////
230 var lbfCanvases = {}; // contains the LC instance for each canvas.
232 // Initialize the drawing widget.
233 // canid is the id of the div that will contain the canvas, and the image
234 // element used for initialization should have an id of canid + '_img'.
236 function lbfCanvasSetup(canid, canWidth, canHeight) {
237 LC.localize({
238 "stroke" : <?php echo xlj('stroke'); ?>,
239 "fill" : <?php echo xlj('fill'); ?>,
240 "bg" : <?php echo xlj('bg{{image canvas label}}'); ?>,
241 "Clear" : <?php echo xlj('Clear'); ?>,
242 // The following are tooltip translations, however they do not work due to
243 // a bug in LiterallyCanvas 0.4.13. We'll leave them here pending a fix.
244 "Eraser" : <?php echo xlj('Eraser'); ?>,
245 "Pencil" : <?php echo xlj('Pencil'); ?>,
246 "Line" : <?php echo xlj('Line'); ?>,
247 "Rectangle" : <?php echo xlj('Rectangle'); ?>,
248 "Ellipse" : <?php echo xlj('Ellipse'); ?>,
249 "Text" : <?php echo xlj('Text'); ?>,
250 "Polygon" : <?php echo xlj('Polygon'); ?>,
251 "Pan" : <?php echo xlj('Pan'); ?>,
252 "Eyedropper": <?php echo xlj('Eyedropper'); ?>,
253 "Undo" : <?php echo xlj('Undo'); ?>,
254 "Redo" : <?php echo xlj('Redo'); ?>,
255 "Zoom out" : <?php echo xlj('Zoom out'); ?>,
256 "Zoom in" : <?php echo xlj('Zoom in'); ?>,
258 var tmpImage = document.getElementById(canid + '_img');
259 var shape = LC.createShape('Image', {x: 0, y: 0, image: tmpImage});
260 var lc = LC.init(document.getElementById(canid), {
261 imageSize: {width: canWidth, height: canHeight},
262 strokeWidths: [1, 2, 3, 5, 8, 12],
263 defaultStrokeWidth: 2,
264 backgroundShapes: [shape],
265 imageURLPrefix: '<?php echo $GLOBALS['assets_static_relative'] ?>/literallycanvas/img'
267 if (canHeight > 261) {
268 // TBD: Do something to make the widget bigger?
269 // Look for some help with this in the next LC release.
271 // lc.saveShape(shape); // alternative to the above backgroundShapes
272 lbfCanvases[canid] = lc;
275 // This returns a standard "Data URL" string representing the image data.
276 // It will typically be a few kilobytes. Here's a truncated example:
277 // data:image/png;base64,iVBORw0K ...
279 function lbfCanvasGetData(canid) {
280 return lbfCanvases[canid].getImage().toDataURL();
283 </script>