2 // Copyright (C) 2014-2017 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This is the place to put JavaScript functions that are needed to support
10 // options.inc.php. Include this in the <head> section of relevant modules.
11 // It's a .php module so that translation can be supported.
13 <script type
="text/javascript">
15 // JavaScript support for date types when the A or B edit option is used.
16 // Called to recompute displayed age dynamically when the corresponding date is
17 // changed. Must generate the same age formats as the oeFormatAge() function.
19 function updateAgeString(fieldid
, asof
, format
, description
) {
20 var datefld
= document
.getElementById('form_' + fieldid
);
23 var date1
= new Date(datefld
.value
);
24 var date2
= asof ?
new Date(asof
) : new Date();
27 var msecs
= date2
.getTime() - date1
.getTime();
28 var days
= Math
.round(msecs
/ (24 * 60 * 60 * 1000));
29 var weeks
= Math
.floor(days
/ 7);
31 if (description
== '') description
= '<?php echo xls('Gest age
') ?>';
32 age
= description +
' ' +
33 weeks +
(weeks
== 1 ?
' <?php echo xls('week
') ?>' : ' <?php echo xls('weeks
') ?>') +
' ' +
34 days +
(days
== 1 ?
' <?php echo xls('day
') ?>' : ' <?php echo xls('days
') ?>');
38 var dayDiff
= date2
.getDate() - date1
.getDate();
39 var monthDiff
= date2
.getMonth() - date1
.getMonth();
40 var yearDiff
= date2
.getFullYear() - date1
.getFullYear();
41 var ageInMonths
= yearDiff
* 12 + monthDiff
;
42 if (dayDiff
< 0) --ageInMonths
;
43 if (format
== 1 ||
(format
== 0 && ageInMonths
>= 24)) {
45 if (monthDiff
< 0 ||
(monthDiff
== 0 && dayDiff
< 0)) --age
;
49 age
= '' + ageInMonths
;
51 age
= age +
' ' +
(ageInMonths
== 1 ?
'<?php echo xls('month
') ?>' : '<?php echo xls('months
') ?>');
54 if (description
== '') description
= '<?php echo xls('Age
') ?>';
55 if (age
!= '') age
= description +
' ' + age
;
57 document
.getElementById('span_' + fieldid
).innerHTML
= age
;
60 // Function to show or hide form fields (and their labels) depending on "skip conditions"
61 // defined in the layout.
63 var cskerror
= false; // to avoid repeating error messages
64 function checkSkipConditions() {
65 var myerror
= cskerror
;
68 for (var i
= 0; i
< skipArray
.length
; ++i
) {
69 var target
= skipArray
[i
].target
;
70 var id
= skipArray
[i
].id
;
71 var itemid
= skipArray
[i
].itemid
;
72 var operator
= skipArray
[i
].operator
;
73 var value
= skipArray
[i
].value
;
75 var action
= skipArray
[i
].action
;
78 if (itemid
) tofind +
= '[' + itemid +
']';
79 // Some different source IDs are possible depending on the data type.
80 var srcelem
= document
.getElementById('check_' + tofind
);
81 var radio_id
='form_' + tofind +
'[' + value +
']';
82 if(typeof document
.getElementById(radio_id
)!=="undefined"){
83 srcelem
= document
.getElementById(radio_id
);
88 if (srcelem
== null) srcelem
= document
.getElementById('radio_' + tofind
);
89 if (srcelem
== null) srcelem
= document
.getElementById('form_' + tofind
) ;
90 if (srcelem
== null) srcelem
= document
.getElementById('text_' + tofind
);
92 if (srcelem
== null) {
93 if (!cskerror
) alert('<?php echo xls('Cannot find a skip source field
for'); ?> "' + tofind +
'"');
98 var condition
= false;
99 var is_multiple
= false;
102 for (var k
= 0; k
< document
.getElementsByName('form_' + tofind
).length
; k++
){
103 if (document
.getElementsByName('form_' + tofind
)[k
].checked
){
104 elem_val
= document
.getElementsByName('form_' + tofind
)[k
].value
;
107 }else if( typeof srcelem
.options
!=="undefined" && srcelem
.type
== 'select-one' ){
108 elem_val
=srcelem
.options
[srcelem
.selectedIndex
].value
;
110 }else if( srcelem
.type
== 'select-multiple' ) {
111 elem_val
= new Array();
113 for (var k
= 0; k
< srcelem
.length
; k++
) {
114 if (srcelem
.options
[k
].selected
) {
115 if( elem_val
.indexOf(srcelem
.options
[k
].value
)<0) {
116 elem_val
.push(srcelem
.options
[k
].value
);
121 elem_val
=srcelem
.value
;
123 if(elem_val
== null) {
124 elem_val
= srcelem
.getAttribute("data-value");
125 if( elem_val
!== null && elem_val
.indexOf("|") !== -1 ) {
126 elem_val
= elem_val
.split("|");
130 if(elem_val
== null) elem_val
= srcelem
.innerText
;
132 //this is a feature fix for the multiple select list option
133 //collect all the multiselect control values:
137 condition
= (-1 !== elem_val
.indexOf(value
));break;
139 condition
= (-1 == elem_val
.indexOf(value
)); break;
141 condition
= srcelem
.checked
; break; // doesn't make sense?
143 condition
= !srcelem
.checked
; break;
147 if (operator
== 'eq') condition
= elem_val
== value
; else
148 if (operator
== 'ne') condition
= elem_val
!= value
; else
149 if (operator
== 'se') condition
= srcelem
.checked
; else
150 if (operator
== 'ns') condition
= !srcelem
.checked
;
153 // Logic to accumulate multiple conditions for the same target.
154 // alert('target = ' + target + ' prevandor = ' + prevandor + ' prevcond = ' + prevcond); // debugging
155 if (prevandor
== 'and') condition
= condition
&& prevcond
; else
156 if (prevandor
== 'or' ) condition
= condition || prevcond
;
157 prevandor
= skipArray
[i
].andor
;
158 prevcond
= condition
;
160 if (j
< skipArray
.length
&& skipArray
[j
].target
== target
) continue;
162 // At this point condition indicates the target should be hidden or have its value set.
164 if (action
== 'skip') {
165 var trgelem1
= document
.getElementById('label_id_' + target
);
166 var trgelem2
= document
.getElementById('value_id_text_' + target
);
167 if (trgelem2
== null) {
168 trgelem2
= document
.getElementById('value_id_' + target
);
171 if (trgelem1
== null && trgelem2
== null) {
172 var trgelem1
= document
.getElementById('label_' + target
);
173 var trgelem2
= document
.getElementById('text_' + target
);
174 if(trgelem2
== null){
175 trgelem2
= document
.getElementById('form_' + target
);
177 if (trgelem1
== null && trgelem2
== null) {
178 if (!cskerror
) alert('<?php echo xls('Cannot find a skip target field
for'); ?> "' + target +
'"');
184 // Find the target row and count its cells, accounting for colspans.
185 var trgrow
= trgelem1 ? trgelem1
.parentNode
: trgelem2
.parentNode
;
187 for (var itmp
= 0; itmp
< trgrow
.cells
.length
; ++itmp
) {
188 rowcells +
= trgrow
.cells
[itmp
].colSpan
;
191 // If the item occupies a whole row then undisplay its row, otherwise hide its cells.
193 if (trgelem1
) colspan +
= trgelem1
.colSpan
;
194 if (trgelem2
) colspan +
= trgelem2
.colSpan
;
195 if (colspan
< rowcells
) {
196 if (trgelem1
) trgelem1
.style
.visibility
= condition ?
'hidden' : 'visible';
197 if (trgelem2
) trgelem2
.style
.visibility
= condition ?
'hidden' : 'visible';
200 trgrow
.style
.display
= condition ?
'none' : '';
203 else if (condition
) { // action starts with "value="
204 var trgelem
= document
.getElementById('form_' + target
);
205 if (trgelem
== null) {
206 if (!cskerror
) alert('Cannot find a value target field "' + trgelem +
'"');
210 var action_value
= action
.substring(6);
211 if (trgelem
.type
== 'checkbox') {
212 trgelem
.checked
= !(action_value
== '0' || action_value
== '');
215 trgelem
.value
= action_value
;
219 // If any errors, all show in the first pass and none in subsequent passes.
220 cskerror
= cskerror || myerror
;
223 ///////////////////////////////////////////////////////////////////////
224 // Image canvas support starts here.
225 ///////////////////////////////////////////////////////////////////////
227 var lbfCanvases
= {}; // contains the LC instance for each canvas.
229 // Initialize the drawing widget.
230 // canid is the id of the div that will contain the canvas, and the image
231 // element used for initialization should have an id of canid + '_img'.
233 function lbfCanvasSetup(canid
, canWidth
, canHeight
) {
235 "stroke" : "<?php echo xls('stroke'); ?>",
236 "fill" : "<?php echo xls('fill'); ?>",
237 "bg" : "<?php echo xls('bg{{image canvas label}}'); ?>",
238 "Clear" : "<?php echo xls('Clear'); ?>",
239 // The following are tooltip translations, however they do not work due to
240 // a bug in LiterallyCanvas 0.4.13. We'll leave them here pending a fix.
241 "Eraser" : "<?php echo xls('Eraser'); ?>",
242 "Pencil" : "<?php echo xls('Pencil'); ?>",
243 "Line" : "<?php echo xls('Line'); ?>",
244 "Rectangle" : "<?php echo xls('Rectangle'); ?>",
245 "Ellipse" : "<?php echo xls('Ellipse'); ?>",
246 "Text" : "<?php echo xls('Text'); ?>",
247 "Polygon" : "<?php echo xls('Polygon'); ?>",
248 "Pan" : "<?php echo xls('Pan'); ?>",
249 "Eyedropper": "<?php echo xls('Eyedropper'); ?>",
250 "Undo" : "<?php echo xls('Undo'); ?>",
251 "Redo" : "<?php echo xls('Redo'); ?>",
252 "Zoom out" : "<?php echo xls('Zoom out'); ?>",
253 "Zoom in" : "<?php echo xls('Zoom in'); ?>",
255 var tmpImage
= document
.getElementById(canid +
'_img');
256 var shape
= LC
.createShape('Image', {x
: 0, y
: 0, image
: tmpImage
});
257 var lc
= LC
.init(document
.getElementById(canid
), {
258 imageSize
: {width
: canWidth
, height
: canHeight
},
259 strokeWidths
: [1, 2, 3, 5, 8, 12],
260 defaultStrokeWidth
: 2,
261 backgroundShapes
: [shape
],
262 imageURLPrefix
: '<?php echo $GLOBALS['assets_static_relative
'] ?>/literallycanvas-0-4-13/img'
264 if (canHeight
> 261) {
265 // TBD: Do something to make the widget bigger?
266 // Look for some help with this in the next LC release.
268 // lc.saveShape(shape); // alternative to the above backgroundShapes
269 lbfCanvases
[canid
] = lc
;
272 // This returns a standard "Data URL" string representing the image data.
273 // It will typically be a few kilobytes. Here's a truncated example:
274 // data:image/png;base64,iVBORw0K ...
276 function lbfCanvasGetData(canid
) {
277 return lbfCanvases
[canid
].getImage().toDataURL();