Checkbox implementation

pull/193/head
Karai Csaba 9 years ago committed by Thorsten von Eicken
parent c5c377db97
commit 225e4437c0
  1. 8
      examples/dummy-web-server.pl
  2. 68
      html/userpage.js

@ -17,6 +17,7 @@ my $userFname : shared;
my $userLname : shared; my $userLname : shared;
my $userAge : shared; my $userAge : shared;
my $userGender : shared; my $userGender : shared;
my $userNotifs : shared;
# auto-flush on socket # auto-flush on socket
@ -477,6 +478,10 @@ sub process_user_comm_user
{ {
$userGender = $http->{postArgs}{gender}; $userGender = $http->{postArgs}{gender};
} }
if( exists $http->{postArgs}{notifications} )
{
$userNotifs = $http->{postArgs}{notifications};
}
return simple_response(204, "OK"); return simple_response(204, "OK");
} }
elsif( $http->{urlArgs}{reason} eq "load" ) elsif( $http->{urlArgs}{reason} eq "load" )
@ -484,7 +489,8 @@ sub process_user_comm_user
my $r = '{"last_name": ' . jsonString($userLname) . my $r = '{"last_name": ' . jsonString($userLname) .
', "first_name": ' . jsonString($userFname) . ', "first_name": ' . jsonString($userFname) .
', "age": ' . jsonNumber($userAge) . ', "age": ' . jsonNumber($userAge) .
', "gender": ' . jsonString($userGender) . '}'; ', "gender": ' . jsonString($userGender) .
', "notifications":' . jsonString($userNotifs) . '}';
return content_response($r, $http->{url}); return content_response($r, $http->{url});
} }

@ -3,6 +3,7 @@
var loadCounter = 0; var loadCounter = 0;
var refreshRate = 0; var refreshRate = 0;
var refreshTimer; var refreshTimer;
var hiddenInputs = [];
function notifyResponse( data ) function notifyResponse( data )
{ {
@ -18,6 +19,10 @@ function notifyResponse( data )
{ {
el.checked = data[v] == el.value; el.checked = data[v] == el.value;
} }
else if( el.type == "checkbox" )
{
el.checked = data[v] == "on";
}
else else
{ {
el.value = data[v]; el.value = data[v];
@ -108,6 +113,34 @@ function refreshFormData()
} , 250); } , 250);
} }
function recalculateHiddenInputs()
{
for(var i=0; i < hiddenInputs.length; i++)
{
var hinput = hiddenInputs[i];
var name = hinput.name;
var elems = document.getElementsByName(name);
for(var j=0; j < elems.length; j++ )
{
var chk = elems[j];
var inptp = chk.type;
if( inptp == "checkbox" ) {
if( chk.checked )
{
hinput.disabled = true;
hinput.value = "on";
}
else
{
hinput.disabled = false;
hinput.value = "off";
}
}
}
}
}
document.addEventListener("DOMContentLoaded", function(){ document.addEventListener("DOMContentLoaded", function(){
// collect buttons // collect buttons
var btns = document.getElementsByTagName("button"); var btns = document.getElementsByTagName("button");
@ -141,6 +174,7 @@ document.addEventListener("DOMContentLoaded", function(){
loadCounter = 4; loadCounter = 4;
frm.onsubmit = function () { frm.onsubmit = function () {
recalculateHiddenInputs();
refreshFormData(); refreshFormData();
return true; return true;
}; };
@ -157,6 +191,40 @@ document.addEventListener("DOMContentLoaded", function(){
} }
} }
// collect checkboxes
var inputs = document.getElementsByTagName("input");
for (ndx = 0; ndx < inputs.length; ndx++) {
var inp = inputs[ndx];
if( inp.getAttribute("type") == "checkbox" )
{
var name = inp.getAttribute("name");
var hasHidden = false;
if( name != null )
{
var inpelems = document.getElementsByName(name);
for(var i=0; i < inpelems.length; i++ )
{
var inptp = inpelems[i].type;
if( inptp == "hidden" )
hasHidden = true;
}
}
if( !hasHidden )
{
var parent = inp.parentElement;
var input = document.createElement("input");
input.type = "hidden";
input.name = inp.name;
parent.appendChild(input);
hiddenInputs.push(input);
}
}
}
// load variables at first time // load variables at first time
var loadVariables = function() { var loadVariables = function() {
ajaxJson("GET", window.location.pathname + ".json?reason=load", notifyResponse, ajaxJson("GET", window.location.pathname + ".json?reason=load", notifyResponse,

Loading…
Cancel
Save