From df455b5de8bfa6a8ec4b67e81427a2cdfd21bba9 Mon Sep 17 00:00:00 2001 From: Chris Double Date: Sun, 1 Aug 2004 22:30:47 +0000 Subject: [PATCH] Added edit functionality and styling to todo application --- contrib/cont-responder/todo-example.factor | 171 +++++++++++++++++++-- 1 file changed, 157 insertions(+), 14 deletions(-) diff --git a/contrib/cont-responder/todo-example.factor b/contrib/cont-responder/todo-example.factor index e1b61547c4..47c2e8e601 100644 --- a/contrib/cont-responder/todo-example.factor +++ b/contrib/cont-responder/todo-example.factor @@ -42,7 +42,79 @@ USE: cont-examples USE: regexp USE: prettyprint USE: todo +USE: arithmetic +USE: logic +: todo-stylesheet ( -- string ) + #! Return the stylesheet for the todo list + <% + "table.list {" % + " text-align:center;" % + " font-family: Verdana;" % + " font-weight: normal;" % + " font-size: 11px;" % + " color: #404040;" % + " background-color: #fafafa;" % + " border: 1px #6699cc solid;" % + " border-collapse: collapse;" % + " boder-spacing: 0px;" % + "}" % + "tr.heading {" % + " border-bottom: 2px solid #6699cc;" % + " border-left: 1px solix #6699cc;" % + " background-color: #BEC8D1;" % + " text-align: left;" % + " text-indent: 0px;" % + " font-family: verdana;" % + " font-weight: bold;" % + " color: #404040;" % + "}" % + "tr.item {" % + " border-bottom: 1px solid #9cf;" % + " border-top: 0px;" % + " border-left: 1px solid #9cf;" % + " border-right: 0px;" % + " text-align: left;" % + " text-indent: 2px;" % + " font-family: verdana, sans-serif, arial;" % + " font-weight: normal;" % + " color: #404040;" % + " background-color: #fafafa;" % + "}" % + "tr.complete {" % + " border-bottom: 1px solid #9cf;" % + " border-top: 0px;" % + " border-left: 1px solid #9cf;" % + " border-right: 0px;" % + " text-align: left;" % + " text-indent: 2px;" % + " font-family: verdana, sans-serif, arial;" % + " font-weight: normal;" % + " color: #404040;" % + " background-color: #ccc;" % + "}" % + "td.lbl {" % + " font-weight: bold; text-align: right;" % + "}" % + "tr.required {" % + " background: #FCC;" % + "}" % + "input:focus {" % + " background: yellow;" % + "}" % + "textarea:focus {" % + " background: yellow;" % + "}" % + %> ; + +: todo-stylesheet-url ( -- url ) + #! Generate an URL for the stylesheet. + t [ [ drop todo-stylesheet write ] show ] register-continuation ; + +: include-todo-stylesheet ( -- ) + #! Generate HTML to include the todo stylesheet + ; + : show-stack-page ( -- ) #! Debug function to show a page containing the current call stack. [ .s ] with-string-stream chars>entities show-message-page ; @@ -54,16 +126,32 @@ USE: todo [ [ call ] ] each ] ; +: styled-row ( class list -- ) + #! Output an html TR row with each element of the list + #! being called to produce the output for each TD. + [ + [ [ call ] ] each + ] ; + : simple-input ( name -- ) #! Output a simple HTML input field which will have the #! specified name. ; +: simple-input-with-value ( name value -- ) + #! Output a simple HTML input field which will have the + #! specified name and value. + ; + : textarea-input ( name -- ) #! Output a simple HTML textarea field which will have the #! specified name. - ; -! ; + ; + +: textarea-input-with-value ( name value -- ) + #! Output a simple HTML textarea field which will have the + #! specified name and value. + ; : password-input ( name -- ) #! Output an HTML password input field which will have the @@ -186,19 +274,60 @@ USE: todo #! todo item details. [ [ - [ [ "Priority:" write ] [ "priority" simple-input ] ] row - [ [ "Description:" write ] [ "description" textarea-input ] ] row + [ + ] + [ + ] ]
[ "Priority" write ] [ "priority" simple-input ]
[ "Description" write ] [ "description" textarea-input ]
"Add" button ] form ; + +: write-edit-todo-item-form ( item url -- ) + #! Display the HTML for a form allowing editing of a + #! todo item details. + swap [ + [ + [ + [ + ] + [ + ] + ]
[ "Priority" write ] [ "priority" dup get simple-input-with-value ]
[ "Description" write ] [ "description" dup get textarea-input-with-value ]
+ "Save" button + ] form + ] bind ; +: todo-details-valid? ( priority description -- bool ) + #! Return true if a valid priority and description were entered. + str-length 0 > swap "[0-9]" re-matches and ; + : get-new-todo-item ( -- ) #! Enter a new item to the current todo list. [ - "Enter New Todo Item" [ write-new-todo-item-form ] simple-page + "Enter New Todo Item" [ include-todo-stylesheet ] [ write-new-todo-item-form ] styled-page ] show [ - "priority" get "description" get - ] bind ; + "priority" get "description" get + ] bind 2dup todo-details-valid? [ + + ] [ + 2drop + "Please ensure you enter a Priority from 0-9 and a description." show-message-page + get-new-todo-item + ] ifte ; + +: edit-item-details ( item -- ) + #! Allow editing of an existing items details. + [ + "Edit Item" [ include-todo-stylesheet ] [ write-edit-todo-item-form ] styled-page + ] show [ + "priority" get "description" get + ] bind 2dup todo-details-valid? [ + rot [ "description" set "priority" set ] bind + ] [ + drop drop + "Please ensure you enter a Priority from 0-9 and a description." show-message-page + edit-item-details + ] ifte ; : save-current-todo ( -- ) #! Save the current todo list @@ -216,22 +345,35 @@ USE: todo dup item-complete? [ "Delete" swap [ "todo" get swap delete-item save-current-todo ] lcurry1 quot-href ] [ - "Mark Completed" swap [ set-item-completed save-current-todo ] lcurry1 quot-href + "Complete" swap [ set-item-completed save-current-todo ] lcurry1 quot-href ] ifte ; +: write-edit-action ( item -- ) + #! Write out html to allow editing an item. + "Edit" swap [ edit-item-details save-current-todo ] lcurry1 quot-href ; + +: item-class ( -- string ) + #! Return the class to use for displaying the row for the + #! item. + item-complete? [ "complete" ] [ "item" ] ifte ; + : write-item-row ( -- ) #! Write the todo list item as an HTML row. - dup dup dup - [ [ item-priority write ] + dup dup dup dup + dup item-class [ + [ item-priority write ] [ item-complete? [ "Yes" ] [ "No" ] ifte write ] [ item-description write ] - [ write-mark-complete-action ] - ] row ; + [ write-mark-complete-action ] + [ write-edit-action ] + ] styled-row ; : write-item-table ( -- ) #! Write the table of items for the todo list. [ - [ [ "Priority" write ] [ "Complete?" write ] [ "Description" write ] [ "Action" write ] ] row + "heading" [ + [ "Priority" write ] [ "Complete?" write ] [ "Description" write ] [ "Action" write ] [ " " write ] + ] styled-row todo-items [ write-item-row ] each ]
; @@ -243,11 +385,12 @@ USE: todo #! Show the current todo list. [ <% "todo" get todo-username % "'s To Do list" % %> + [ include-todo-stylesheet ] [ drop "todo" get write-item-table "Add Item" [ do-add-new-item ] quot-href - ] simple-page + ] styled-page ] show drop ; : todo-example ( path -- )