Adding todo webapp

db4
Slava Pestov 2008-04-15 06:35:06 -05:00
parent 318be5fdf8
commit c0206b3165
8 changed files with 304 additions and 0 deletions

View File

@ -0,0 +1,26 @@
<?xml version='1.0' ?>
<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
<t:title>Edit Item</t:title>
<t:form action="edit">
<t:edit component="id" />
<table>
<tr><th class="field-label">Summary: </th><td><t:edit component="summary" /></td></tr>
<tr><th class="field-label">Priority: </th><td><t:edit component="priority" /></td></tr>
<tr><th class="field-label big-field-label">Description:</th><td><t:edit component="description" /></td></tr>
</table>
<input type="SUBMIT" value="Done" />
</t:form>
<t:a href="view" query="id">View</t:a>
|
<t:form action="delete" class="inline">
<t:edit component="id" />
<button type="submit" class="link-button link">Delete</button>
</t:form>
</t:chloe>

View File

@ -0,0 +1,45 @@
<?xml version='1.0' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
<head>
<t:write-title />
<t:style>
body, button {
font:9pt "Lucida Grande", "Lucida Sans Unicode", verdana, geneva, sans-serif;
color:#444;
}
a, .link {
color: #222;
border-bottom:1px dotted #666;
text-decoration:none;
}
a:hover, .link:hover {
border-bottom:1px solid #66a;
}
.error { color: #a00; }
.field-label {
text-align: right;
}
</t:style>
<t:write-style />
</head>
<body>
<t:call-next-template />
</body>
</t:chloe>
</html>

View File

@ -0,0 +1,12 @@
<?xml version='1.0' ?>
<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
<t:title>My Todo List</t:title>
<table class="todo-list">
<tr><th>Summary</th><th>Priority</th><th>View</th><th>Edit</th></tr>
<t:view component="list" />
</table>
</t:chloe>

View File

@ -0,0 +1,20 @@
<?xml version='1.0' ?>
<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
<tr>
<td>
<t:view component="summary" />
</td>
<td>
<t:view component="priority" />
</td>
<td>
<t:a href="view" query="id">View</t:a>
</td>
<td>
<t:a href="edit" query="id">Edit</t:a>
</td>
</tr>
</t:chloe>

View File

@ -0,0 +1,41 @@
.big-field-label {
vertical-align: top;
}
.description {
border: 1px dashed #ccc;
background-color: #f5f5f5;
padding: 5px;
font-size: 150%;
color: #000000;3
}
.link-button {
padding: 0px;
background: none;
border: none;
}
.navbar {
background-color: #eeeeee;
padding: 5px;
border: 1px solid #ccc;
}
.inline {
display: inline;
}
pre {
font-size: 75%;
}
.todo-list {
border-style: none;
}
.todo-list td, .todo-list th {
border-width: 1px;
padding: 2px;
border-style: solid;
}

111
extra/webapps/todo/todo.factor Executable file
View File

@ -0,0 +1,111 @@
! Copyright (c) 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel locals sequences
db db.types db.tuples
http.server.components http.server.components.farkup
http.server.forms http.server.templating.chloe
http.server.boilerplate http.server.crud http.server.auth
http.server.actions http.server.db
http.server ;
IN: todo
TUPLE: todo uid id priority summary description ;
todo "TODO"
{
{ "uid" "UID" { VARCHAR 256 } +not-null+ }
{ "id" "ID" +native-id+ }
{ "priority" "PRIORITY" INTEGER +not-null+ }
{ "summary" "SUMMARY" { VARCHAR 256 } +not-null+ }
{ "description" "DESCRIPTION" { VARCHAR 256 } }
} define-persistent
: init-todo-table todo ensure-table ;
: <todo> ( id -- todo )
todo new
swap >>id
uid >>uid ;
: todo-template ( name -- template )
"resource:extra/webapps/todo/" swap ".xml" 3append <chloe> ;
: <todo-form> ( -- form )
"todo" <form>
"view-todo" todo-template >>view-template
"edit-todo" todo-template >>edit-template
"todo-summary" todo-template >>summary-template
"id" <integer>
hidden >>renderer
add-field
"summary" <string>
t >>required
add-field
"priority" <integer>
t >>required
0 >>default
0 >>min-value
10 >>max-value
add-field
"description" <farkup>
add-field ;
: <todo-list-form> ( -- form )
"todo-list" <form>
"todo-list" todo-template >>view-template
"list" <todo-form> <list>
add-field ;
TUPLE: todo-responder < dispatcher ;
:: <todo-responder> ( -- responder )
[let | todo-form [ <todo-form> ]
list-form [ <todo-list-form> ]
ctor [ [ <todo> ] ] |
todo-responder new-dispatcher
list-form ctor <list-action> "list" add-main-responder
todo-form ctor <view-action> "view" add-responder
todo-form ctor "view" <edit-action> "edit" add-responder
ctor "list" <delete-action> "delete" add-responder
<boilerplate>
"todo" todo-template >>template
] ;
! What follows below is somewhat akin to a 'deployment descriptor'
! for the todo application. The <todo-responder> can be integrated
! into an existing web app that provides session management and
! login facilities, or <todo-app> can be used to run a
! self-contained todo instance.
USING: namespaces io.files io.sockets
db.sqlite smtp
http.server.sessions
http.server.auth.login
http.server.auth.providers.db
http.server.sessions.storage.db ;
: test-db "todo.db" resource-path sqlite-db ;
: <todo-app> ( -- responder )
<todo-app>
<login>
users-in-db >>users
allow-registration
allow-password-recovery
allow-edit-profile
<boilerplate>
"page" todo-template >>template
<url-sessions>
sessions-in-db >>sessions
test-db <db-persistence> ;
: init-todo ( -- )
"factorcode.org" 25 <inet> smtp-server set-global
"todo@factorcode.org" lost-password-from set-global
test-db [
init-todo-table
init-users-table
init-sessions-table
] with-db
<todo-app> main-responder set-global ;

View File

@ -0,0 +1,26 @@
<?xml version='1.0' ?>
<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
<t:style include="resource:extra/webapps/todo/todo.css" />
<t:style include="resource:extra/xmode/code2html/stylesheet.css" />
<div class="navbar">
<t:a href="list">List Items</t:a>
| <t:a href="edit">New Items</t:a>
<t:if code="http.server.auth.login:allow-edit-profile?">
| <t:a href="edit-profile">Edit Profile</t:a>
</t:if>
<t:form action="logout" class="inline">
| <button type="submit" class="link-button link">Logout</button>
</t:form>
</div>
<h1><t:write-title /></h1>
<t:call-next-template />
</t:chloe>

View File

@ -0,0 +1,23 @@
<?xml version='1.0' ?>
<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
<t:title>View Item</t:title>
<table>
<tr><th class="field-label">Summary: </th><td><t:view component="summary" /></td></tr>
<tr><th class="field-label">Priority: </th><td><t:view component="priority" /></td></tr>
</table>
<div class="description">
<t:view component="description" />
</div>
<t:a href="edit" query="id">Edit</t:a>
|
<t:form action="delete" class="inline">
<t:edit component="id" />
<button class="link-button link">Delete</button>
</t:form>
</t:chloe>