2022.2

Table Of Contents
});
function getCameraIndex(){
return $("input.camera-dyn").length;
}
function addCameraWidget(cameraID, value) {
if(typeof value == 'undefined') {
value = '';
}
var html = '<label>Camera' +
var html = '<label>Camera' +
'<div id="' + cameraID + '" role="cotg.PhotoWidget">' +
'<div class="panel" role="control-wrapper" style="position:relative;">' +
'<img role="photo" src="">' +
'<input role="photo-data" class="camera-dyn" name="' + cameraID + '" type="hidden" value="' + value + '">' +
'</div>' +
'<button class="small" role="take-button" type="button">Take now</button>' +
'<button class="small" role="pick-button" type="button">Library</button>' +
'<button class="small" role="clear-button" type="button">Clear</button>' +
'</div></label>';
$('#cameras').append(html); // add the camera object to the DOM
$('#' + cameraID).cotgPhotoWidget(); // init COTG widget
}
Saving and restoring custom data and widgets
TheCaptureOnTheGo(COTG)appstoresthevaluesoftheinputfieldstothelocalrepositoryofthe
appuponclosingtheForm.OnreopeningtheFormtheoriginalinputfieldsandtheirvaluesare
restored.However,dynamicallyaddedwidgets(see"DynamicallyaddingCOTGwidgets"onpage548)
don'tgetrestored.Thisneedstobehandledincode.Thistopicexplainshowtodothat.
Addingeventlisteners
IntheprocessofclosingandopeningaFormtwoimportanteventsaretriggeredbytheCOTGlibrary:
l
olcotgsavestate.Thiseventismeantforcustomscriptstosaveinformationwhentheuser
closesorsubmitstheForm.Itoccursafterthestateofallstaticinputfieldshasbeensaved.
l
olcotgrestorestate.ThiseventallowscustomscriptstodosomethingwhentheFormis
reopened.Itoccursafterallstaticinputshavebeenrestored.
Tolatchontotheseevents,youhavetoregisteraolcotgsavestatelistenerandaolcot-
grestorestatelistener,respectively,onthewindowobject.Thewindowobjectrepresentsawin-
dowcontainingaDOM(documentobjectmodel),inthiscasetheCOTGForm.
ThefunctioncallbackonitsaddEventListenermethodallowsyoutoimplementcustomcodeaspart
ofthesaveandrestoreprocesses.
(FormoreinformationabouttheaddEventListenermethodseethedocumentation:Mozillaand
w3schools.)
Thefollowingcoderegisterstheolcotgsavestatelisteneronthewindowobject.
window.addEventListener('olcotgsavestate', function(event) {
/* code to save custom data */
});
Page 551