ajax.lisp 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
;;
;; Copyright 2013 Genworks International
;;
;; This source file is part of the General-purpose Declarative
;; Language project (GDL).
;;
;; This source file contains free software: you can redistribute it
;; and/or modify it under the terms of the GNU Affero General Public
;; License as published by the Free Software Foundation, either
;; version 3 of the License, or (at your option) any later version.
;; 
;; This source file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Affero General Public License for more details.
;; 
;; You should have received a copy of the GNU Affero General Public
;; License along with this source file.  If not, see
;; <http://www.gnu.org/licenses/>.
;;

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
(in-package :ta2)


(defun reset-self (self)
  (if (and (typep self 'base-html-sheet)
           (not (typep self 'assembly))
           (not (the parent))
           (typep (the $$ta2) 'assembly))
      (the $$ta2)
    self))


#+nil
(defun reset-self (self)
  self)
      

(defun ta2operate (req ent)

  (let* ((query (request-query req))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
         (object (the root-object (follow-root-path 
                                   (read-safe-string 
48
                                    (base64-decode-safe (getf plist :|rootPath|)))))))
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    
    (the (perform-action! object))
    
    (respond-with-new-html-sections req ent self)))


(publish :path "/ta2operate" :function 'ta2operate)                                


(defun ta2treetoggle (req ent)

  (let ((query (request-query req)))
    (let* (
           (iid (rest (assoc "iid" query :test #'string-equal)))
           (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
           (self (reset-self self))
           (plist (gwl::assoc-list-to-plist query)))
66
      (let ((root-path (getf plist :|rootPath|)))
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        (setq root-path (base64-decode-safe root-path))
        
        (when *debug?* (print-variables self root-path))
        
        (let ((object  (the (follow-root-path (read-safe-string root-path)))))
          (the-object object toggle-state!)
          (respond-with-new-html-sections req ent self))))))

              
(publish :path "/ta2treetoggle" :function 'ta2treetoggle)                                  




(defun ta2evaluate (req ent)
    
  (let* ((query (request-query req))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
88 89
         (message (make-keyword (base64-decode-safe (getf plist :|message|)))))

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    (with-http-response (req ent :content-type "text/xml")
      (with-http-body (req ent)
        (with-html-output(*html-stream* nil )
          (:document (:html-section (:|newHTML| (str (wrap-cdata (with-output-to-string(*html-stream*)
                                                                   (with-format (html-format *html-stream*)
                                                                     (write-the inspector (message-value (make-keyword message))))))))
                                    (:|replaceId| (str (format nil "inspector_~a" message))))))))))


(publish :path "/ta2evaluate" :function 'ta2evaluate)

(defun ta2setcontrol (req ent)
  
    
  (let* ((query (request-query req))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
109 110
         (slot (make-keyword (getf plist :|control|)))
         (value (getf plist :|value|)))
111
    
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    (if (eql (read-safe-string value) 'gdl::restore-default)
        (the click-mode (restore-slot-default! slot))
      (the click-mode (set-slot-if-needed! slot value)))
    
    (respond-with-new-html-sections req ent self)))

(publish :path "/ta2setcontrol" :function 'ta2setcontrol)



(defun gdlsetslots (req ent)
    
  (let* ((query (let ((query (request-query req))) query))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
130
         (root-path (base64-decode-list (getf plist :|rootPath|)))
131
         (object (the root-object (follow-root-path root-path)))
132
         (plist (remove-plist-key (remove-plist-key plist :|iid|) :|rootPath|)))
133 134

    
135 136
    (let ((keys (read-safe-string (getf plist :|keys|)))
          (values (base64-decode-list (getf plist :|values|))))
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      
      (mapc #'(lambda(key value)
                (the-object object (set-slot-if-needed! key value :infer-types? nil))) keys values))

    
    ;;(funcall (the tatu-update-function))
    
    ;;(the viewport (toggle-view-toggle!))
    
    
    (respond-with-new-html-sections req ent self)))


(publish :path "/gdlsetslots" :function 'gdlsetslots)


(defun gdlsetcontrol (req ent)
    
  (let* ((query (request-query req))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
160
         (root-path (read-safe-string (getf plist :|rootPath|)))
161
         (object (the (follow-root-path root-path)))
162 163
         (slot (read-safe-string (getf plist :|control|)))
         (value (getf plist :|value|)))
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    
    
    (if (eql (read-safe-string value) 'gdl::restore-default)
        (the-object object (restore-slot-defaults! (ensure-list slot)))
      (the-object object (set-slot-if-needed! slot value)))

    (when (typep object 'web-drawing) (setq object (the-object object parent)))
    
    (when (typep object 'base-html-graphics-sheet) (the-object object toggle-view-toggle!))
    
    
    (respond-with-new-html-sections req ent self)))


(publish :path "/gdlsetcontrol" :function 'gdlsetcontrol)



(defun ta2setexpandmode (req ent)
    
  (let* ((query (request-query req))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
189
         (mode (make-keyword (getf plist :|mode|))))
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    (the tree (set-slot! :expand-mode mode))

    (respond-with-new-html-sections req ent self)))


(publish :path "/ta2setexpandmode" :function 'ta2setexpandmode)



(defun ta2clickbutton (req ent)
  
  (let* ((query (let ((query (request-query req))) query))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
207
         (mode (getf plist :|mode|)))
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

    
    (setq self (the click-mode))
    (setq mode (make-keyword mode))
    
    
    (case mode
      
      (:break
       #+nil
       (let ((no-trap? (member :notrap net.aserve::*debug-current*)))
         (unless no-trap? 
           (net.aserve::debug-on :notrap)
           (format t "~&Disabling AllegroServe error trapping (i.e. enabling debugging) 
  -- you can re-enable it with (net.aserve::debug-off :notrap)~%")
           (net.aserve::debug-on :notrap)))
       
       
       (the (set-slot! :click-mode mode)))

      (:clear! (funcall (the clear-viewport-function)))
              
      (:update! (let ((standalone? (not (eql (the tatu-root root) (the tatu-root root-object root)))))
                  (if standalone?
                      (progn
                        (funcall (the object-update-function))
                        (funcall (the tatu-update-function))
                        (funcall (the toggle-view-toggle-function)))
                    (let ((root (the root))
                          (tatu-root-path (the tatu-root root-path)))
                      (funcall (the object-update-function))
                      (the-object root (follow-root-path tatu-root-path) viewport toggle-view-toggle!)))))
                
      (:update-full! (let ((standalone? (not (eql (the tatu-root root) (the tatu-root root-object root)))))
                       (if standalone?
                           (progn
                             (funcall (the object-update-full-function))
                             (funcall (the tatu-update-function))
                             (funcall (the toggle-view-toggle-function)))
                         (let ((root (the root))
                               (tatu-root-path (the tatu-root root-path)))
                           (funcall (the object-update-full-function))
                           (the-object root (follow-root-path tatu-root-path) viewport toggle-view-toggle!)))))
      
      (:up-root! (funcall (the up-root-function)))

      (:reset-root! (funcall (the reset-root-function)))
      
      
      (:undo (funcall (the pop-operation-function)))
      
      (:debug (funcall (the debug-function)))
      
      (otherwise (the (set-slot! :click-mode mode))))
    
    
    
    (respond-with-new-html-sections req ent (the parent))))


(publish :path "/ta2clickbutton" :function 'ta2clickbutton)



(defun digitize-point (req ent)
    
  (let* ((query (request-query req))
         (iid (rest (assoc "iid" query :test #'string-equal)))
         (self (first (gethash (make-keyword iid) gwl:*instance-hash-table*)))
         (self (reset-self self))
         (plist (gwl::assoc-list-to-plist query))
279
         (self (the (follow-root-path (read-safe-string (getf plist :|rootPath|))))))
280 281 282 283 284
    
    ;;
    ;; FLAG -- this is internal stuff, should be handled by a simple function on the self object.
    ;;

285 286 287
    (let ((local-point (make-point (- (parse-integer (getf plist :|x|)) 
				      (half (the view-object width)))
				   (let ((y (- (the view-object length) (parse-integer (getf plist :|y|)))))
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
                                                            (- y (half (the view-object length)))))))
      (let ((digitized-point 
             (add-vectors (the view-object user-center)
                          (scalar*vector (/ (the view-object user-scale))
                                         local-point))))
        (ecase (the digitation-mode)
          
          (:measure-distance 
           (let ((new-point (the view-object main-view (model-point (make-point (get-x local-point)
                                                                                (get-y local-point)
                                                                                0))))
                 (old-point (the digitized-point)))

             (if (null old-point)
                 (progn
                   (the (set-slot! :digitized-point new-point))
                   (the (restore-slot-default! :digitized-distance)))
               (progn
                 (the (set-slot! :digitized-distance (list :total (3d-distance old-point new-point)
                                                           :x (- (get-x new-point) (get-x old-point))
                                                           :y (- (get-y new-point) (get-y old-point))
                                                           :z (- (get-z new-point) (get-z old-point)))))
                 (the (restore-slot-default! :digitized-point))))))
          
          
          (:report-point 
           (the (report-point (get-x local-point) (get-y local-point))))
          
          (:zoom-and-center
           (the view-object (set-slot! :user-center (make-point (get-x digitized-point)
                                                                (get-y digitized-point))))
           (the view-object (set-slot! :user-scale 
                                       (* (the view-object user-scale) (the zoom-factor))))))))
    
    
    (respond-with-new-html-sections req ent (the root))))


(publish :path "/digitizePoint" :function 'digitize-point)                                 



(defun wrap-cdata (string)
  (string-append "<![CDATA[" string "]]>"))

(defun respond-with-new-html-sections (req ent self)
  (let (replace-list)
    (dolist (section (the html-sections) (reverse replace-list))
336
      (let ((status (the-object section (slot-status :inner-html))))
337 338 339
        (when (eql status :unbound)
          (when *debug?* (format t "pushing html for ~a...~%" (the-object section dom-id)))
          (push (list (the-object section dom-id)
340
                      (the-object section inner-html)) replace-list))))
341 342 343 344 345 346 347 348 349 350 351 352 353
    
    
    
    (with-http-response (req ent :content-type "text/xml")
      (with-http-body (req ent)
        (with-html-output(*html-stream* nil)
          (:document
           (dolist (replace-pair replace-list)
             (htm
              (:html-section (:|replaceId| (str (first replace-pair)))
                             (:|newHTML| (str (wrap-cdata (second replace-pair)))))))))))))