2006-01-23 20:05:39 -05:00
|
|
|
! Copyright (C) 2004 Chris Double.
|
2006-01-23 21:03:22 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-01-23 20:05:39 -05:00
|
|
|
!
|
2006-01-23 21:03:22 -05:00
|
|
|
! cont-responder code for display forms and anchors that use
|
|
|
|
|
! XMLHttpRequest and the 'liveUpdater.js' code.
|
2006-01-23 20:05:39 -05:00
|
|
|
IN: live-updater
|
2006-01-23 21:03:22 -05:00
|
|
|
USING: cont-responder html io kernel lists namespaces strings
|
|
|
|
|
xml ;
|
2006-01-23 20:05:39 -05:00
|
|
|
|
|
|
|
|
: get-live-updater-js ( filename -- string )
|
|
|
|
|
#! Return the liveUpdater javascript code as a string.
|
|
|
|
|
"/contrib/httpd/liveUpdater.js" <resource-stream> contents ;
|
|
|
|
|
|
|
|
|
|
: include-live-updater-js ( -- )
|
|
|
|
|
#! Write out the HTML script to include the live updater
|
|
|
|
|
#! javascript code.
|
2006-01-23 21:03:22 -05:00
|
|
|
<script "JavaScript" =language script>
|
|
|
|
|
get-live-updater-js write-html
|
2006-01-23 20:05:39 -05:00
|
|
|
</script> ;
|
|
|
|
|
|
|
|
|
|
: write-live-anchor-tag ( text -- id )
|
|
|
|
|
#! Write out the HTML for the clickable anchor. This
|
|
|
|
|
#! will have no actionable HREF assigned to it. Instead
|
|
|
|
|
#! an onclick is set via DHTML later to make it run a
|
|
|
|
|
#! quotation on the server. The randomly generated id
|
|
|
|
|
#! for the anchor is returned.
|
2006-01-23 21:03:22 -05:00
|
|
|
<a get-random-id dup =id "#" =href a> swap write </a> ;
|
2006-01-23 20:05:39 -05:00
|
|
|
|
|
|
|
|
: register-live-anchor-quot ( div-id div-quot -- kid )
|
|
|
|
|
#! Register the 'quot' with the cont-responder so
|
|
|
|
|
#! that when it is run it will produce an HTML
|
|
|
|
|
#! fragment which is the output generated by calling
|
|
|
|
|
#! 'quot'. That HTML fragment will be wrapped in a
|
|
|
|
|
#! 'div' with the given id.
|
|
|
|
|
[
|
|
|
|
|
"div-quot" set
|
|
|
|
|
"div-id" set
|
|
|
|
|
] make-hash [
|
|
|
|
|
[
|
2006-01-23 21:03:22 -05:00
|
|
|
"disable-initial-redirect?" on
|
2006-01-23 20:05:39 -05:00
|
|
|
[
|
|
|
|
|
<div "div-id" get =id div> "div-quot" get call </div>
|
|
|
|
|
] show
|
|
|
|
|
] bind
|
|
|
|
|
] cons t swap register-continuation ;
|
|
|
|
|
|
|
|
|
|
: write-live-anchor-script ( div-id div-quot anchor-id -- )
|
|
|
|
|
#! Write the javascript that will attach the onclick
|
|
|
|
|
#! event handler to the anchor with the 'anchor-id'. The
|
|
|
|
|
#! onclick, when clicked, will retrieve from the server
|
|
|
|
|
#! the HTML generated by the output of 'div-quot' wrapped
|
|
|
|
|
#! in a 'div' tag with the 'div-id'. That 'div' tag will
|
|
|
|
|
#! replace whatever HTML DOM object currently has that same
|
|
|
|
|
#! id.
|
|
|
|
|
<script "JavaScript" =language script>
|
2006-01-23 21:03:22 -05:00
|
|
|
"document.getElementById('" write-html
|
|
|
|
|
write-html
|
|
|
|
|
"').onclick=liveUpdaterUri('" write-html
|
|
|
|
|
register-live-anchor-quot id>url write-html
|
|
|
|
|
"');" write-html
|
2006-01-23 20:05:39 -05:00
|
|
|
</script> ;
|
|
|
|
|
|
|
|
|
|
: live-anchor ( id quot text -- )
|
|
|
|
|
#! Write out the HTML for an anchor that when clicked
|
|
|
|
|
#! will replace the DOM object on the current page with
|
|
|
|
|
#! the given 'id' with the result of the output of calling
|
|
|
|
|
#! 'quot'.
|
|
|
|
|
write-live-anchor-tag
|
|
|
|
|
write-live-anchor-script ;
|
|
|
|
|
|
|
|
|
|
: write-live-search-tag ( -- id )
|
|
|
|
|
#! Write out the HTML for the input box. This
|
|
|
|
|
#! will have no actionable keydown assigned to it. Instead
|
|
|
|
|
#! a keydown is set via DHTML later to make it run a
|
|
|
|
|
#! quotation on the server. The randomly generated id
|
|
|
|
|
#! for the input box is returned.
|
|
|
|
|
<input get-random-id dup =id "text" =type input/> ;
|
|
|
|
|
|
|
|
|
|
: register-live-search-quot ( div-id div-quot -- kid )
|
|
|
|
|
#! Register the 'quot' with the cont-responder so
|
|
|
|
|
#! that when it is run it will produce an HTML
|
|
|
|
|
#! fragment which is the output generated by calling
|
|
|
|
|
#! 'quot'. That HTML fragment will be wrapped in a
|
|
|
|
|
#! 'div' with the given id. The 'quot' is called with
|
|
|
|
|
#! a string on top of the stack. This is the input string
|
|
|
|
|
#! entered in the live search input box.
|
|
|
|
|
[
|
|
|
|
|
"div-quot" set
|
|
|
|
|
"div-id" set
|
|
|
|
|
] make-hash [
|
|
|
|
|
[
|
2006-01-23 21:03:22 -05:00
|
|
|
"disable-initial-redirect?" on
|
2006-01-23 20:05:39 -05:00
|
|
|
#! Retrieve the search query value from the POST parameters.
|
|
|
|
|
[ "s" get ] bind
|
|
|
|
|
[
|
|
|
|
|
#! Don't need the URL as the 'show' won't be resumed.
|
|
|
|
|
drop
|
|
|
|
|
<div "div-id" get =id div> "div-quot" get call </div>
|
|
|
|
|
] show
|
|
|
|
|
] bind
|
|
|
|
|
] cons t swap register-continuation ;
|
|
|
|
|
|
|
|
|
|
: write-live-search-script ( div-id div-quot id-id -- )
|
|
|
|
|
#! Write the javascript that will attach the keydown handler
|
|
|
|
|
#! to the input box with the give id. Whenever a keydown is
|
|
|
|
|
#! received the 'div-quot' will be executed on the server,
|
|
|
|
|
#! with the input boxes text on top of the stack. The
|
|
|
|
|
#! output of the quot will be an HTML fragment, it is wrapped in
|
|
|
|
|
#! a 'div' with the id 'div-id' and will
|
|
|
|
|
#! replace whatever HTML DOM object currently has that same
|
|
|
|
|
#! id.
|
|
|
|
|
<script "JavaScript" =language script>
|
|
|
|
|
"liveSearch('" write
|
|
|
|
|
write
|
|
|
|
|
"', '" write
|
|
|
|
|
register-live-search-quot id>url write
|
|
|
|
|
"');" write
|
|
|
|
|
</script> ;
|
|
|
|
|
|
|
|
|
|
: live-search ( div-id div-quot -- )
|
|
|
|
|
#! Write an input text field. The keydown of this
|
|
|
|
|
#! text field will run 'div-quot' on the server with
|
|
|
|
|
#! the value of the text field on the stack. The output
|
|
|
|
|
#! of div-quot will replace the HTML DOM object with the
|
|
|
|
|
#! given id.
|
|
|
|
|
write-live-search-tag
|
|
|
|
|
write-live-search-script ;
|