/[ht-ajax]/processor-simple.lisp
ViewVC logotype

Contents of /processor-simple.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Fri Nov 14 21:17:43 2008 UTC (5 years, 5 months ago) by xlopez
File size: 4426 byte(s)
Initial commit, version 0.0.7.
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Base: 10 -*-
2 ;;;
3 ;;; Copyright (c) 2007, Ury Marshak
4 ;;; The code comes with a BSD-style license, so you can basically do
5 ;;; with it whatever you want. See the file LICENSE for details.
6 ;;;
7
8 (in-package #:ht-ajax)
9
10 (declaim #.*optimization*)
11
12 ;;
13
14
15 ;;; This is loosely based on the CL-AJAX package by Richard Newman
16 ;;; (http://www.cliki.net/cl-ajax, http://www.holygoat.co.uk/applications/cl-ajax/cl-ajax)
17 ;;; but probably does not deserve the name "port of CL-AJAX for Hunchentoot",
18 ;;; also in any case the code taken from CL-AJAX was heavily modified, so
19 ;;; the bugs are probably mine
20
21
22
23 (defclass simple-ajax-processor (ajax-processor)
24 ())
25
26
27 (defun prepare-js-simple-init-request ()
28 "
29 function init_request() {
30 // debug_alert(\"Initialising request...\");
31 var r;
32 if (window.XMLHttpRequest) { r = new XMLHttpRequest(); }
33 else {
34 try { r = new ActiveXObject(\"Msxml2.XMLHTTP\"); } catch (e) {
35 try { r = new ActiveXObject(\"Microsoft.XMLHTTP\"); } catch (ee) {
36 r = null;
37 }}}
38 if (!r) debug_alert(\"Browser couldn't make a connection object.\");
39 return r;
40 }
41 ")
42
43
44
45 (defun prepare-js-simple-ajax-preamble (server-uri)
46 "Output a string containing the call function."
47 (format nil "
48
49 function ajax_call_uri(func, callback_spec, args) {
50 var uri = '~A';
51 var i;
52 var response = null;
53 var callbacks = ajax_parse_callbacks(callback_spec);
54
55 if (uri.indexOf('?') == -1)
56 uri = uri + '?';
57 else
58 uri = uri + '&';
59
60 uri = uri + ajax_encode_args(func, args);
61
62 var re = init_request();
63
64 re.open('GET', uri, true);
65 re.onreadystatechange = function() {
66 if (re.readyState != 4) return;
67 if (((re.status>=200) && (re.status<300)) || (re.status == 304)) {
68 var data = re.responseText;
69 ajax_call_maybe_evaluate_json(callbacks[0],
70 data,
71 re.getResponseHeader('Content-Type'));
72 }
73 else {
74 if(callbacks[1]) {
75 callbacks[1](re.status + ' ' + re.statusText);
76 }
77 else {
78 debug_alert('Error for URI '+uri + ' ' + re.status + ' ' + re.statusText);
79 }
80
81 }
82 }
83 re.send(null);
84 delete re;
85 }"
86 server-uri))
87
88
89 (defmethod prepare-js-ajax-function ((processor simple-ajax-processor) fun-name js-fun-name
90 &rest rest &key method &allow-other-keys)
91 (declare (ignore processor))
92 (unless (eq method :get)
93 (error "SIMPLE-AJAX-PROCESSOR does not support methods other than GET"))
94 (apply #'prepare-js-ajax-function-definitions "ajax_call_uri" fun-name js-fun-name rest))
95
96
97
98 ;; (defun wrap-result-in-xml (result element-id)
99 ;; (no-cache)
100 ;; (format nil
101 ;; "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>
102 ;; <response>~A<result xmlns=\"http://www.w3.org/1999/xhtml\">~A</result></response>"
103 ;; (if element-id
104 ;; (concatenate 'string "<elem_id>" element-id "</elem_id>")
105 ;; "")
106 ;; result)
107 ;; )
108
109
110 ;; (defmethod handle-request ((processor simple-ajax-processor))
111 ;; (let ((ajax-xml (string-to-js-boolean (parameter "ajax-xml")))
112 ;; (ajax-elem (parameter "ajax-elem")))
113 ;; (let ((result (call-next-method)))
114 ;; (if ajax-xml
115 ;; (progn
116 ;; (setf (content-type) "text/xml")
117 ;; (wrap-result-in-xml result ajax-elem))
118 ;; result)
119 ;; ))
120 ;; )
121
122
123 (defmethod %generate-includes ((processor simple-ajax-processor))
124 "No includes for SIMPLE processor"
125 ;;
126 "")
127
128
129 (defmethod %generate-js-code ((processor simple-ajax-processor))
130 (apply #'concatenate 'string
131 (prepare-js-debug-function processor)
132 (prepare-js-ajax-encode-args)
133 (prepare-js-parse-callbacks)
134 (prepare-js-ajax-is-json)
135 (prepare-js-ajax-call-maybe-evaluate-json)
136 (prepare-js-simple-ajax-preamble (maybe-rewrite-url-for-session
137 (server-uri processor)))
138 (prepare-js-simple-init-request)
139
140 (loop
141 for fun-name being the hash-keys
142 in (exported-funcs processor)
143 collect (apply #'prepare-js-ajax-function
144 processor
145 fun-name
146 (js-function-name processor fun-name)
147 (gethash fun-name (exported-funcs processor))))
148 ))
149

  ViewVC Help
Powered by ViewVC 1.1.5