/[ht-ajax]/utils.lisp
ViewVC logotype

Contents of /utils.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: 5086 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 ;; Common functions
15 ;;
16
17 (defun make-safe-js-name (function-name)
18 "Primitive function to try to turn rich lisp names into suitable
19 for Javascript"
20 (loop for c across "-<>"
21 do (setf function-name (substitute #\_ c function-name))
22 finally (return function-name)))
23
24
25 (defun prepare-js-debug-function (processor)
26 "Output the debugging function."
27 (concatenate 'string "
28 function debug_alert(text) {"
29 (when (js-debug processor)
30 " alert(\"HT-AJAX: \" + text);" )
31 "}
32 "))
33
34
35 ;; (defun js-string-to-boolean (str)
36 ;; (when str
37 ;; (not (or (string= str "")
38 ;; (string= str "null")
39 ;; (string= str "false")
40 ;; (string= str "0"))
41 ;; )))
42
43
44
45 ;; (defun entity-escape (s)
46 ;; (setf s (regex-replace-all "(?s)&" s "&amp;"))
47 ;; (setf s (regex-replace-all "(?s)<" s "&lt;"))
48 ;; (setf s (regex-replace-all "(?s)>" s "&gt;"))
49 ;; )
50
51
52
53 (defun prepare-js-collect-varargs-to-array (num-standard-args &optional (array-name "args"))
54 (let ((start_args (symbol-name (gensym)))
55 (end_args (symbol-name (gensym)))
56 (i (symbol-name (gensym))))
57 (concatenate 'string
58 "
59 var " start_args " = " (princ-to-string num-standard-args) ";
60 var " end_args " = arguments.length;
61
62 var " array-name " = new Array();
63 var " i " = " start_args ";
64 for (var " i "=" start_args "; " i " < " end_args "; ++" i ")
65 " array-name ".push(arguments[" i "]);
66 " )))
67
68
69 (defun prepare-js-ajax-encode-args ()
70 ;;
71 "
72 function ajax_encode_args(func, args) {
73 var res = 'ajax-fun=' + encodeURIComponent(func);
74 var i;
75 if (args)
76 for (i = 0; i < args.length; ++i) {
77 res = res + '&ajax-arg' + i + '=' + encodeURIComponent(args[i]);
78 }
79 res = res + '&ajax-num-args=' + args.length;
80
81 res = res + '&ajax-xml=false';
82
83 return res;
84 }
85 ")
86
87
88 (defun wrap-js-in-script-tags (js)
89 (concatenate 'string
90 "
91 <script type=\"text/javascript\">
92 //<![CDATA[
93 "
94 js
95 "
96 //]]>
97 </script>
98 " ))
99
100
101
102 (defun prepare-js-file-include (js-file-uri)
103 (concatenate 'string
104 "<script src=\"" js-file-uri "\" type=\"text/javascript\"></script>"))
105
106
107 (defun prepare-js-parse-callbacks ()
108 "Create a Javascript function that receives a specification for
109 callbacks and returns an array of two functions, the first is the
110 success callback and the second is the error callback. The callback
111 specification may be:
112 Function. It is assumed to be the success callback, the error callback
113 is assumed to be null
114 Array. Returned as is, i.e. it should be [success_callback, error_callback]
115 Object. If the object has a success property it is used as success callback.
116 The error property if present becomes the error callback."
117 ;;
118 "
119 function ajax_parse_callbacks(obj) {
120 if (typeof obj === 'function') {
121 return [obj, null];
122 }
123 if (typeof obj === 'object' && typeof obj.length === 'number') {
124 // array
125 return obj;
126 }
127 var error_callback = null;
128 var success_callback = null;
129 if (obj.error !== undefined) {
130 error_callback = obj.error;
131 }
132 if (obj.success !== undefined) {
133 success_callback = obj.success;
134 }
135
136 return [success_callback, error_callback];
137 }
138 ")
139
140
141 (defun json-content-type ()
142 "Official IANA http://www.iana.org/assignments/media-types/application/"
143 ;;
144 "application/json")
145
146
147 (defun prepare-js-ajax-is-json ()
148 (concatenate 'string
149 "
150 function ajax_trim_CR(s) {
151 if (s.charCodeAt(s.length-1)==13) {
152 s = s.substring(0,s.length-1)
153 }
154 return s;
155 }
156
157 function ajax_is_json(content_type) {
158 content_type = ajax_trim_CR(content_type); // YUI under IE needs this
159 return (content_type == '" (json-content-type) "');
160 }
161 "))
162
163
164 (defun prepare-js-ajax-call-maybe-evaluate-json ()
165 (concatenate 'string
166 "
167 function ajax_call_maybe_evaluate_json(callback, data, content_type) {
168 if (ajax_is_json(content_type)) {
169 try {
170 data = eval('(' + data + ')');
171 }
172 catch (e) {
173 debug_alert(e.message);
174 }
175 }
176 callback(data);
177 }
178 "))
179
180
181 (defun prepare-js-ajax-function-definitions(request-func fun-name js-fun-name &key method &allow-other-keys)
182 "Output a string containing the appropriate Javascript for accessing fun-name
183 on server-uri."
184 (concatenate 'string
185 "
186 function " js-fun-name "_callback(callback)
187 {
188 " (prepare-js-collect-varargs-to-array 1 "args") "
189 " request-func "('" fun-name "', callback, args);
190 }
191 "
192 "
193 function " js-fun-name "_set_element(elem_id)
194 {
195 " (prepare-js-collect-varargs-to-array 1 "args") "
196
197 var elem = document.getElementById(elem_id);
198 if (!elem) {
199 debug_alert('!elem');
200 }
201
202 " request-func "('" fun-name "', function(res) {elem.innerHTML=res;} , args);
203 }
204 "))
205
206

  ViewVC Help
Powered by ViewVC 1.1.5