/* appjet:version 0.1 */
import("storage");
import("quickforms");


function get_() {    
var poll = request.params.poll
if(!poll){
    print("Choose or create your poll: ", FORM({action:"/", method:"get"},
    INPUT({text:"poll",name:"poll", value:"Example-Poll"})));
    print(DIV({style:"text-align:right"},link("listpolls", "List polls")));
    return
} 

//init
if (!storage.polls) {
    storage.polls = {}
}
if (!storage.polls[poll]) {
    storage.polls[poll] = {name: poll, choices: new StorableCollection()}
}

var choices = storage.polls[poll].choices        
print(H1("Poll: ",poll));
print(raw("<table>"))
print(TR(TH("Choice"),TH("Votes")))
if(choices.size() == 0){
    print(TR(TD("No choices in this poll")))
}
choices.forEach(
function(choice) {
  print(
    TR(
        TD(choice.name),
        TD(choice.votes),
        TD(     
        new QuickButton("+", {method: "post", action: "/add"},{id: choice.id, poll: poll }),
        new QuickButton("-", {method: "post", action: "/remove"},{id: choice.id,poll: poll})
        )
     )
    );
}
);
print(raw("</table>"))
print(raw("<BR/>"))
        
var inputform = FORM({action:"/addchoice?poll="+escape(poll), method:"post"});
inputform.push(INPUT({type: "text", name: "choice", id: "choice"}));
inputform.push(INPUT({type: "submit", value: "Add a new choice"}));
print(inputform)    

var myfooter = DIV({id:"myfooter"});
myfooter.push("Version: 20080103 - ");
myfooter.push(A({href:"http://luebken.com"},"luebken.com"));
print(myfooter);    
}

//add vote
function post_add() {
var choice = getStorable(request.params.id);        
if(choice){
  choice.votes ++;
}
var poll = request.params.poll
response.redirect("/?poll="+escape(request.params.poll))
}

//remove vote
function post_remove() {
var choice = getStorable(request.params.id);
var poll = request.params.poll        
if(choice ){
    if(choice.votes == 0) storage.polls[poll].choices.remove(choice)
    if(choice.votes > 0) choice.votes --
}
response.redirect("/?poll="+escape(poll))
}

//add choice
function post_addchoice() {
    var poll = request.params.poll
    var newchoice = request.params.choice
    storage.polls[poll].choices.add({name: newchoice, votes: 0});
    response.redirect("/?poll="+escape(poll))
}

//all polls
function get_listpolls(){
print(H1("All polls: "));
var list = UL()
for each (var poll in storage.polls) {
  list.push(LI(link("/?poll="+poll.name,poll.name)))
}
print(list)
print(BR())
}


dispatch();
/* appjet:css */
table, td
{
    border-color: #600;
    border-style: solid;
}

table
{
    border-width: 1px 1px;
    border-spacing: 0;
    border-collapse: collapse;
}

td
{
    margin: 0;
    padding: 4px;
    border-width: 1px;
    background-color: #EEE;
}

body { width: 600px; margin: 1em auto; font-size: 80%;}
.message { border-left: 1px solid gray; margin: 5px; padding: 0.25em; }
form { margin-top: 1em; }
H1 { color: #999; }
H2 { color: #999; font-size: 70%; }
#myfooter { font-size: 75%; text-align: right;}



















































































© Copyright 2007-2008 AppJet Inc.