2003-04-16 08:30:57 -04:00
|
|
|
;;; SUrflets' input fields
|
|
|
|
;;; Copyright 2002, 2003 Andreas Bernauer
|
2003-05-22 09:55:03 -04:00
|
|
|
;;; With additions from Eric Knauel (2003)
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Functions to create simple input fields
|
|
|
|
|
|
|
|
;; The interface for input-fields does not prescribe what the type of
|
|
|
|
;; attributes has to be. We choose a record here.
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define-record-type field-attributes :field-attributes
|
|
|
|
(make-field-attributes default others)
|
|
|
|
field-attributes?
|
|
|
|
(default field-attributes-default set-field-attributes-default!)
|
|
|
|
(others field-attributes-others set-field-attributes-others!))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;; A simple input-field is a prototype for other input-fields.
|
|
|
|
;; REPORTED-TYPE is the type of the input-field in HTML, TYPE the
|
|
|
|
;; internal referenced type and TRANSFORMER the function that
|
|
|
|
;; translates the HTTP-string of the request into a scheme value.
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (simple-field-maker reported-type type default-pred transformer)
|
2003-04-16 08:30:57 -04:00
|
|
|
(lambda maybe-further-attributes
|
|
|
|
(let ((name (generate-input-field-name type)))
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((default "" default-pred)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-04-16 08:30:57 -04:00
|
|
|
(make-input-field name type
|
|
|
|
transformer
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
2003-04-16 08:30:57 -04:00
|
|
|
(and default `(value ,default))
|
|
|
|
(sxml-attribute-attributes attributes))
|
2003-07-08 17:22:06 -04:00
|
|
|
(simple-html-tree-maker reported-type))))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (simple-html-tree-maker reported-type)
|
2003-04-16 08:30:57 -04:00
|
|
|
(lambda (input-field)
|
|
|
|
(let ((attributes (input-field-attributes input-field)))
|
|
|
|
`(input (@ (type ,reported-type)
|
|
|
|
(name ,(input-field-name input-field))
|
2003-07-08 17:22:06 -04:00
|
|
|
,(field-attributes-default attributes)
|
|
|
|
,(field-attributes-others attributes))))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-13 16:08:28 -04:00
|
|
|
(define (make-simple-default-setter default-pred?)
|
2003-04-16 08:30:57 -04:00
|
|
|
(lambda (input-field value)
|
|
|
|
(if (default-pred? value)
|
2003-07-08 17:22:06 -04:00
|
|
|
(set-field-attributes-default!
|
2003-04-16 08:30:57 -04:00
|
|
|
(input-field-attributes input-field)
|
2003-07-13 16:08:28 -04:00
|
|
|
`(value ,value))
|
2003-04-16 08:30:57 -04:00
|
|
|
(error "Default value must be a number or a string or a symbol."
|
|
|
|
value))
|
|
|
|
(touch-input-field! input-field)))
|
|
|
|
|
|
|
|
(define (string-or-symbol? thing)
|
|
|
|
(or (string? thing) (symbol? thing)))
|
|
|
|
(define simple-default? string-or-symbol?)
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define set-simple-field-default!
|
2003-07-13 16:08:28 -04:00
|
|
|
(make-simple-default-setter simple-default?))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Text input field
|
2003-07-08 17:22:06 -04:00
|
|
|
(define make-text-field
|
|
|
|
(simple-field-maker "text" "text" simple-default? identity))
|
|
|
|
(define set-text-field-value! set-simple-field-default!)
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Number input field
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (number-field-default? value)
|
2003-04-16 08:30:57 -04:00
|
|
|
(or (number? value)
|
|
|
|
(simple-default? value)))
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (number-field-transformer string)
|
2003-04-16 08:30:57 -04:00
|
|
|
(or (string->number string)
|
|
|
|
(error "wrong type")))
|
2003-07-08 17:22:06 -04:00
|
|
|
(define make-number-field
|
|
|
|
(simple-field-maker "text" "number"
|
|
|
|
number-field-default? number-field-transformer))
|
|
|
|
(define set-number-field-value!
|
2003-07-13 16:08:28 -04:00
|
|
|
(make-simple-default-setter number-field-default?))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; hidden input-field
|
2003-07-13 16:08:28 -04:00
|
|
|
;; The programmer should supply a default value for this input-field
|
|
|
|
;; as it is hidden.
|
2003-07-08 17:22:06 -04:00
|
|
|
(define make-hidden-field
|
2003-07-15 10:05:22 -04:00
|
|
|
(simple-field-maker "hidden" "hidden"
|
2003-07-13 16:08:28 -04:00
|
|
|
simple-default? identity))
|
2003-07-08 17:22:06 -04:00
|
|
|
(define set-hidden-field-value! set-simple-field-default!)
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Password input field
|
2003-07-08 17:22:06 -04:00
|
|
|
(define make-password-field
|
|
|
|
(simple-field-maker "password" "password"
|
|
|
|
simple-default? identity))
|
|
|
|
(define set-password-field-value! set-simple-field-default!)
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;; That's it for simple input fields.
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Textarea input field
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-textarea . maybe-further-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((default-text "" simple-default?)
|
|
|
|
(rows 5 number?)
|
|
|
|
(cols 20 number?)
|
|
|
|
(readonly #f boolean?)
|
|
|
|
(attributes '() sxml-attribute?))
|
|
|
|
(let ((name (generate-input-field-name "textarea"))
|
|
|
|
(all-attributes `((cols ,cols)
|
|
|
|
(rows ,rows)
|
|
|
|
,@(if readonly '(readonly) '())
|
|
|
|
,@(sxml-attribute-attributes attributes))))
|
|
|
|
(make-input-field
|
|
|
|
name "textarea"
|
|
|
|
identity
|
|
|
|
(make-field-attributes (and default-text)
|
|
|
|
all-attributes)
|
|
|
|
make-textarea-html-tree))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-textarea-html-tree textarea)
|
|
|
|
(let ((attributes (input-field-attributes textarea)))
|
2003-04-16 08:30:57 -04:00
|
|
|
`(textarea (@ (type "textarea")
|
2003-07-08 17:22:06 -04:00
|
|
|
(name ,(input-field-name textarea))
|
|
|
|
,(field-attributes-others attributes))
|
|
|
|
,(field-attributes-default attributes))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-13 16:08:28 -04:00
|
|
|
(define (set-textarea-value! textarea value)
|
|
|
|
(if (simple-default? value)
|
|
|
|
(set-field-attributes-default!
|
|
|
|
(input-field-attributes textarea)
|
|
|
|
value)
|
|
|
|
(error "Default value must be a string or a symbol." value))
|
|
|
|
(touch-input-field! textarea))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;
|
2003-07-08 17:22:06 -04:00
|
|
|
;;; Select input field
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
;(make-select '("this" "that" "those") '(@ ((id "sushi"))))
|
|
|
|
;(make-select '("this" ("that" '(@ (selected))) "those"))
|
2003-04-16 08:30:57 -04:00
|
|
|
;; dropdown: (size 1)
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
;;; A select input field shows a list of options that can be
|
|
|
|
;;; selected. For this purpose, we introduce an select-option record,
|
2003-04-16 08:30:57 -04:00
|
|
|
;;; that contains all the information for each option. This is
|
2003-05-22 09:32:49 -04:00
|
|
|
;;; justified by the fact, that the options list is seperated in HTML,
|
|
|
|
;;; too. The TAG is the string that is displayed in the website, the
|
|
|
|
;;; VALUE is the value that is returned by input-field-value, if this
|
|
|
|
;;; option was selected. TAG is assumed to be unique by some functions
|
|
|
|
;;; (e.g. select and unselect) SELECTED? tells us, if this option is
|
|
|
|
;;; preselected.
|
2003-07-08 17:22:06 -04:00
|
|
|
(define-record-type select-option :select-option
|
|
|
|
(really-make-select-option tag value selected? attributes)
|
|
|
|
select-option?
|
|
|
|
(tag select-option-tag)
|
|
|
|
(value select-option-value)
|
|
|
|
(selected? select-option-selected? really-set-select-option-selected?!)
|
|
|
|
(attributes select-option-attributes set-select-option-attributes!))
|
|
|
|
|
2003-07-13 16:08:28 -04:00
|
|
|
;; No check of attributes as this is done by calling function. (This
|
|
|
|
;; function isn't exported.
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-select-option tag value selected? attributes)
|
2003-04-16 08:30:57 -04:00
|
|
|
(if (string? tag)
|
2003-07-08 17:22:06 -04:00
|
|
|
(really-make-select-option tag value selected?
|
2003-04-16 08:30:57 -04:00
|
|
|
(sxml-attribute-attributes attributes))
|
2003-07-08 17:22:06 -04:00
|
|
|
(error "Select-option's tag must be a string." tag)))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;; Constructor for valued select input-field option.
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-annotated-select-option tag value . maybe-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-attributes
|
|
|
|
((selected? #f boolean?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-select-option tag value selected? attributes)))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;; Constructor for a simple select input-field option (not annotated).
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-simple-select-option tag . maybe-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-attributes
|
|
|
|
((selected? #f boolean?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-select-option tag tag selected? attributes)))
|
|
|
|
|
|
|
|
(define-record-discloser :select-option
|
|
|
|
(lambda (select-option)
|
|
|
|
(list 'select-option
|
|
|
|
(select-option-tag select-option)
|
|
|
|
(select-option-value select-option)
|
|
|
|
(select-option-selected? select-option)
|
|
|
|
(select-option-attributes select-option)
|
2003-04-16 08:30:57 -04:00
|
|
|
)))
|
|
|
|
|
|
|
|
;; Selecting / Unselecting of an option in an select input-field,
|
|
|
|
;; chosen by tag.
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (select-select-option! tag select)
|
|
|
|
(set-select-option-selected?! tag select #t))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (unselect-select-option! tag select)
|
|
|
|
(set-select-option-selected?! tag select #f))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (set-select-option-selected?! tag select selected?)
|
|
|
|
(let ((options (field-attributes-default
|
|
|
|
(input-field-attributes select))))
|
2003-05-22 09:32:49 -04:00
|
|
|
(if (number? tag) ; is tag an index?
|
2003-07-08 17:22:06 -04:00
|
|
|
(really-set-select-option-selected?! (list-ref options tag)
|
|
|
|
selected?)
|
2003-04-16 08:30:57 -04:00
|
|
|
(let lp ((options options))
|
|
|
|
(if (null? options)
|
2003-07-08 17:22:06 -04:00
|
|
|
(error "No such option" tag select)
|
|
|
|
(if (tag=select-option? tag (car options))
|
|
|
|
(really-set-select-option-selected?! (car options)
|
|
|
|
selected?)
|
2003-04-16 08:30:57 -04:00
|
|
|
(lp (cdr options))))))
|
2003-07-08 17:22:06 -04:00
|
|
|
(touch-input-field! select)))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
;; Find select-option in a list by its tag.
|
|
|
|
(define (tag=select-option? tag select-option)
|
|
|
|
(string=? tag (select-option-tag select-option)))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (find-select-option tag select-options)
|
|
|
|
(cond ((member/srfi-1 tag select-options tag=select-option?)
|
2003-05-22 09:32:49 -04:00
|
|
|
=> car)
|
|
|
|
;; MEMBER/SRFI-1 returns the sublist that starts with the
|
|
|
|
;; searched element.
|
2003-04-16 08:30:57 -04:00
|
|
|
(else #f)))
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (find-select-option-value tag select-options)
|
|
|
|
(cond ((find-select-option tag select-options)
|
|
|
|
=> select-option-value)
|
2003-04-16 08:30:57 -04:00
|
|
|
(else #f)))
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (add-select-option! select select-option)
|
|
|
|
(let ((attributes (input-field-attributes select)))
|
|
|
|
(set-field-attributes-default!
|
2003-05-22 09:32:49 -04:00
|
|
|
attributes
|
2003-07-08 17:22:06 -04:00
|
|
|
(cons select-option
|
|
|
|
(field-attributes-default attributes)))
|
|
|
|
(touch-input-field! select)))
|
|
|
|
|
|
|
|
(define (delete-select-option! select select-option)
|
|
|
|
(let* ((attributes (input-field-attributes select))
|
|
|
|
(select-options (field-attributes-default attributes)))
|
|
|
|
(if (select-option? select-option)
|
|
|
|
(set-field-attributes-default!
|
2003-05-22 09:32:49 -04:00
|
|
|
attributes
|
2003-07-08 17:22:06 -04:00
|
|
|
(delete select-option select-options))
|
|
|
|
(let ((tag select-option))
|
|
|
|
(set-field-attributes-default!
|
2003-05-22 09:32:49 -04:00
|
|
|
attributes
|
2003-07-08 17:22:06 -04:00
|
|
|
(delete tag select-options tag=select-option?))))
|
|
|
|
(touch-input-field! select)))
|
2003-05-22 09:32:49 -04:00
|
|
|
|
2003-04-16 08:30:57 -04:00
|
|
|
;; To be compatible with previous versions of MAKE-SELECT-INPUT-FIELD,
|
|
|
|
;; we accept also a simple list as an option-list. New programs should
|
2003-07-08 17:22:06 -04:00
|
|
|
;; use select-options-list (easily createable with
|
|
|
|
;; (map make-simple-select-option option-list))
|
2003-07-13 16:08:28 -04:00
|
|
|
(define (simple-options select-options)
|
2003-07-08 17:22:06 -04:00
|
|
|
(if (and (list? select-options)
|
|
|
|
(every select-option? select-options))
|
|
|
|
select-options
|
|
|
|
(map make-simple-select-option select-options)))
|
|
|
|
|
2003-08-01 13:36:18 -04:00
|
|
|
(define (make-select select-options . maybe-further-attributes)
|
2003-07-13 16:08:28 -04:00
|
|
|
(let ((real-select-options (simple-options select-options)))
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((multiple? #f boolean?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-04-16 08:30:57 -04:00
|
|
|
(let ((name (generate-input-field-name "select")))
|
|
|
|
(if multiple?
|
2003-08-01 13:41:11 -04:00
|
|
|
(make-multiple-select name real-select-options attributes)
|
|
|
|
(make-single-select name real-select-options
|
2003-07-08 17:22:06 -04:00
|
|
|
attributes))))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-14 13:33:37 -04:00
|
|
|
;; deprecated: Does not introduce further functionality.
|
|
|
|
(define make-annotated-select make-select)
|
|
|
|
|
2003-04-16 08:30:57 -04:00
|
|
|
;; internal
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-multiple-select name select-options attributes)
|
2003-04-16 08:30:57 -04:00
|
|
|
(make-multi-input-field name "mult-select"
|
2003-07-15 10:39:55 -04:00
|
|
|
multiple-select-transformer
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
|
|
|
select-options
|
2003-07-09 13:18:57 -04:00
|
|
|
`((multiple)
|
|
|
|
,@(sxml-attribute-attributes attributes)))
|
2003-07-08 17:22:06 -04:00
|
|
|
make-select-html-tree))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;; internal
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-single-select name select-options attributes)
|
2003-04-16 08:30:57 -04:00
|
|
|
(make-input-field name "select"
|
2003-05-22 09:32:49 -04:00
|
|
|
(lambda (tag)
|
2003-07-08 17:22:06 -04:00
|
|
|
(cond ((find-select-option-value tag select-options)
|
2003-05-22 09:32:49 -04:00
|
|
|
=> identity)
|
|
|
|
(else (error "no such option." tag))))
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
|
|
|
select-options
|
2003-04-16 08:30:57 -04:00
|
|
|
(sxml-attribute-attributes attributes))
|
2003-07-08 17:22:06 -04:00
|
|
|
make-select-html-tree))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-15 10:39:55 -04:00
|
|
|
(define (multiple-select-transformer select bindings)
|
2003-07-08 17:22:06 -04:00
|
|
|
(let ((name (input-field-name select))
|
|
|
|
(select-options (field-attributes-default
|
|
|
|
(input-field-attributes select))))
|
2003-05-22 09:32:49 -04:00
|
|
|
(let* ((my-bindings (filter (lambda (binding)
|
2003-07-08 17:22:06 -04:00
|
|
|
(equal? (car binding) name))
|
|
|
|
bindings))
|
2003-05-22 09:32:49 -04:00
|
|
|
(tags (map cdr my-bindings)))
|
|
|
|
(filter-map (lambda (tag)
|
2003-07-08 17:22:06 -04:00
|
|
|
(find-select-option-value tag select-options))
|
2003-04-16 08:30:57 -04:00
|
|
|
tags))))
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-select-html-tree select)
|
|
|
|
(let ((attributes (input-field-attributes select)))
|
|
|
|
`(select (@ (name ,(input-field-name select))
|
|
|
|
,(field-attributes-others attributes))
|
2003-04-16 08:30:57 -04:00
|
|
|
#\newline
|
2003-07-08 17:22:06 -04:00
|
|
|
,@(make-select-options-html-tree
|
|
|
|
(field-attributes-default attributes)))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-select-options-html-tree select-options)
|
|
|
|
(map (lambda (select-option)
|
|
|
|
`(option (@ ,(and (select-option-selected? select-option) '(selected))
|
|
|
|
,(select-option-attributes select-option))
|
|
|
|
,(select-option-tag select-option)))
|
|
|
|
select-options))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; radio input-fields
|
|
|
|
;; Because grouped radio input-fields must use the same name, we
|
|
|
|
;; cannot just return one radio input-field object, but we must
|
|
|
|
;; generate several ones with the same name.
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-radio-group)
|
2003-04-16 08:30:57 -04:00
|
|
|
(let ((name (generate-input-field-name "radio")))
|
|
|
|
(lambda (value-string . maybe-further-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((checked? #f boolean?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-04-16 08:30:57 -04:00
|
|
|
(make-input-field name "radio"
|
|
|
|
identity
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
2003-07-09 13:18:57 -04:00
|
|
|
(and checked? '(checked))
|
|
|
|
`((value ,value-string)
|
|
|
|
,@(sxml-attribute-attributes attributes)))
|
2003-07-08 17:22:06 -04:00
|
|
|
radio-html-tree-maker)))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-annotated-radio-group)
|
2003-04-16 08:30:57 -04:00
|
|
|
(let* ((name (generate-input-field-name "radio"))
|
|
|
|
(value-table (make-integer-table))
|
2003-07-08 17:22:06 -04:00
|
|
|
(transformer (make-radio-transformer value-table)))
|
2003-04-16 08:30:57 -04:00
|
|
|
(lambda (value . maybe-further-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((checked? #f boolean?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-04-16 08:30:57 -04:00
|
|
|
(let ((number (generate-unique-number)))
|
|
|
|
(table-set! value-table number value)
|
|
|
|
(make-input-field name "radio"
|
|
|
|
transformer
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
2003-07-09 13:18:57 -04:00
|
|
|
(and checked? '(checked))
|
|
|
|
`((value ,(number->string number))
|
|
|
|
,@(sxml-attribute-attributes attributes)))
|
2003-07-08 17:22:06 -04:00
|
|
|
radio-html-tree-maker))))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-radios values . maybe-further-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((attributes '() sxml-attribute?))
|
2003-07-08 17:22:06 -04:00
|
|
|
(let ((radio-gen (make-annotated-radio-group)))
|
2003-04-16 08:30:57 -04:00
|
|
|
(map (lambda (value)
|
|
|
|
(if attributes
|
|
|
|
(radio-gen value attributes)
|
|
|
|
(radio-gen value)))
|
|
|
|
values))))
|
|
|
|
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-radio-transformer value-table)
|
2003-04-16 08:30:57 -04:00
|
|
|
(lambda (tag)
|
|
|
|
(cond
|
|
|
|
((string->number tag) =>
|
|
|
|
(lambda (number)
|
|
|
|
(let ((value (table-ref value-table number)))
|
|
|
|
(if value
|
|
|
|
value
|
|
|
|
(error "Unknown tag number for radio input-field" tag)))))
|
|
|
|
(else
|
|
|
|
(error "Unknown tag number for radio input-field" tag)))))
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (radio-html-tree-maker radio)
|
|
|
|
(let* ((attributes (input-field-attributes radio)))
|
2003-04-16 08:30:57 -04:00
|
|
|
`(input (@ ((type "radio")
|
2003-07-08 17:22:06 -04:00
|
|
|
(name ,(input-field-name radio))
|
|
|
|
,(field-attributes-default attributes)
|
|
|
|
,(field-attributes-others attributes))))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
(define (set-input-field-checked?! input-field checked?)
|
|
|
|
(let ((attributes (input-field-attributes input-field)))
|
2003-07-08 17:22:06 -04:00
|
|
|
(set-field-attributes-default!
|
2003-04-16 08:30:57 -04:00
|
|
|
attributes
|
|
|
|
(if checked? '(checked) #f))
|
|
|
|
(touch-input-field! input-field)))
|
2003-07-08 17:22:06 -04:00
|
|
|
|
|
|
|
(define set-radio-checked?! set-input-field-checked?!)
|
|
|
|
(define (check-radio! radio) (set-radio-checked?! radio #t))
|
|
|
|
(define (uncheck-radio! radio) (set-radio-checked?! radio #f))
|
2003-04-16 08:30:57 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; checkbox input-field
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-checkbox . maybe-further-attributes)
|
|
|
|
(really-make-checkbox #t
|
|
|
|
checkbox-transformer
|
|
|
|
maybe-further-attributes))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-annotated-checkbox value . maybe-further-attributes)
|
|
|
|
(really-make-checkbox value
|
|
|
|
(make-checkbox-transformer value)
|
|
|
|
maybe-further-attributes))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (really-make-checkbox value transformer attributes)
|
2003-04-16 08:30:57 -04:00
|
|
|
(let ((name (generate-input-field-name "checkbox")))
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals attributes
|
|
|
|
((checked? #f boolean?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-04-16 08:30:57 -04:00
|
|
|
(make-input-field name "checkbox"
|
|
|
|
transformer
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
2003-07-09 13:18:57 -04:00
|
|
|
(and checked? '(checked))
|
2003-04-16 08:30:57 -04:00
|
|
|
(sxml-attribute-attributes attributes))
|
2003-07-08 17:22:06 -04:00
|
|
|
checkbox-html-tree-maker))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
(define (make-checkbox-transformer value)
|
|
|
|
(lambda (tag)
|
|
|
|
(if (string=? tag "on")
|
|
|
|
value
|
|
|
|
#f)))
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define checkbox-transformer (make-checkbox-transformer #t))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (checkbox-html-tree-maker checkbox)
|
|
|
|
(let ((attributes (input-field-attributes checkbox)))
|
2003-04-16 08:30:57 -04:00
|
|
|
`(input (@ ((type "checkbox")
|
2003-07-08 17:22:06 -04:00
|
|
|
(name ,(input-field-name checkbox))
|
|
|
|
,(field-attributes-default attributes)
|
|
|
|
,(field-attributes-others attributes))))))
|
|
|
|
|
|
|
|
(define set-checkbox-checked?! set-input-field-checked?!)
|
|
|
|
(define (check-checkbox! checkbox) (set-checkbox-checked?! checkbox #t))
|
|
|
|
(define (uncheck-checkbox! checkbox) (set-checkbox-checked?! checkbox #f))
|
|
|
|
|
|
|
|
|
2003-04-16 08:30:57 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; button input-fields
|
|
|
|
(define (make-button type name button-caption attributes)
|
|
|
|
(make-input-field name type
|
|
|
|
identity
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
2003-04-16 08:30:57 -04:00
|
|
|
(and button-caption `(value ,button-caption))
|
|
|
|
(sxml-attribute-attributes attributes))
|
|
|
|
make-button-html-tree))
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-button-html-tree button)
|
|
|
|
(let ((attributes (input-field-attributes button)))
|
|
|
|
`(input (@ (type ,(input-field-type button))
|
|
|
|
(name ,(input-field-name button))
|
|
|
|
,(field-attributes-default attributes)
|
|
|
|
,(field-attributes-others attributes)))))
|
2003-04-16 08:30:57 -04:00
|
|
|
|
|
|
|
(define (make-submit-button . maybe-further-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((button-caption #f string?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-04-16 08:30:57 -04:00
|
|
|
(make-button "submit" (generate-input-field-name "submit")
|
|
|
|
button-caption attributes)))
|
|
|
|
|
|
|
|
(define (make-reset-button . maybe-further-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((button-caption #f string?)
|
|
|
|
(attributes '() sxml-attribute?))
|
2003-04-16 08:30:57 -04:00
|
|
|
(make-button "reset" (generate-input-field-name "reset")
|
|
|
|
button-caption attributes)))
|
|
|
|
|
2003-05-22 09:55:03 -04:00
|
|
|
;; Image buttons cannot be simple buttons, as the browser does not
|
|
|
|
;; send their simple name, but the coordinates where the user clicked
|
|
|
|
;; into. Thanks to Eric Knauel for reporting this bug.
|
2003-05-22 09:32:49 -04:00
|
|
|
(define (make-image-button image-source . maybe-further-attributes)
|
2003-07-09 13:18:57 -04:00
|
|
|
(let-optionals maybe-further-attributes
|
|
|
|
((attributes '() sxml-attribute?))
|
2003-05-22 09:32:49 -04:00
|
|
|
(make-multi-input-field (generate-input-field-name "imgbtn")
|
|
|
|
"image"
|
|
|
|
image-button-transformer
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-field-attributes
|
2003-05-22 09:32:49 -04:00
|
|
|
`(src ,image-source)
|
|
|
|
(sxml-attribute-attributes attributes))
|
|
|
|
make-button-html-tree)))
|
|
|
|
|
2003-05-22 09:55:03 -04:00
|
|
|
;; The following two functions are from Eric Knauel's fix for the
|
|
|
|
;; image-button bug:
|
2003-05-22 09:32:49 -04:00
|
|
|
(define (image-button-transformer image-button bindings)
|
|
|
|
(let ((x (find-image-button-coordinate image-button bindings ".x"))
|
|
|
|
(y (find-image-button-coordinate image-button bindings ".y")))
|
2003-05-23 04:05:03 -04:00
|
|
|
(let ((x-number (string->number x))
|
|
|
|
(y-number (string->number y)))
|
2003-07-08 17:22:06 -04:00
|
|
|
(and x y
|
|
|
|
(if (and x-number y-number)
|
|
|
|
(cons x-number y-number)
|
|
|
|
(error "Image button coordinates aren't numbers. " x y))))))
|
2003-05-22 09:32:49 -04:00
|
|
|
|
|
|
|
(define (find-image-button-coordinate image-button bindings suffix)
|
|
|
|
(let* ((name (input-field-name image-button)))
|
|
|
|
(cond
|
|
|
|
((assoc (string-append name suffix) bindings)
|
2003-07-14 05:10:36 -04:00
|
|
|
=> cdr)
|
2003-05-22 09:32:49 -04:00
|
|
|
(else #f))))
|
2003-07-08 17:22:06 -04:00
|
|
|
|
2003-08-01 14:00:51 -04:00
|
|
|
;;EOF
|