inspector.lisp 22.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
48
49
50
51
52
53
54
;;
;; Copyright 2002, 2009 Genworks International and Genworks BV 
;;
;; 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/>.
;; 

(in-package :tasty)



(defmacro ignore-errors-with-warning (&rest body)
  (let ((values (gensym))
        (error (gensym)))
    `(let* ((,values (multiple-value-list (ignore-errors ,@body)))
            (,error (second ,values)))
       (if (and ,error (typep ,error 'error))
           (progn (warn "~a" ,error)
                  (format nil "! ERROR: ~a !" ,error))
         (apply #'values ,values)))))


;;
;; FLAG -- promote to utilities. 
;;
(defun one-line (string)
  (glisp:replace-regexp (glisp:replace-regexp string (format nil "~%") " ") "'" "\\'" ))

(defparameter *suppress-%%-messages?* t)

;;
;; FLAG -- replace with abstract associative map
;;
(defparameter *internal-keywords*
  (list :query-plist :aggregate :all-mixins :background-color 
        :base64-encoded-root-path :bashee :beziers :bounding-bbox
        :check-sanity? :color-decimal :color-hex :color-palette :cookies-received
        :cookies-to-send :dash-pattern :dom-id :fill-color-decimal
        :fixed-url-prefix :header-plist :home-page :instance-id 
        :last-visited-root-path :left-handed? :local-bbox :local-box
55
        :local-center :local-left-handed? :local-orientation :inner-html
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        :obliqueness :parent-tree :plain-url? :possible-nils 
        :quantify-box :query-toplevel :refresh-toggle :remote-id :respondent 
        :return-object :root-path-string :target :time-instantiated 
        :time-last-touched :transitory-slots :tree-root 
        :url-encoded-root-path :use-local-box? :viewable-slots 
        :viewpoints :visible-children :vrml-center))

(define-object inspector (base-html-sheet)


  :input-slots
  ((node-root-path nil :settable) 
   root-object
   tree-toplevel
   tatu-root colors-default tree-type tree-node  
   display-controls-hash
   perform-action-function click-mode 
   (relative-font-size "-1")
   
   
   (node (when (the root-object) (the root-object (follow-root-path (the node-root-path)))))

   (expand? (< (the depth) 1) :settable)
   (depth 0)
   
   )

  :computed-slots
  (   
   
   (show-settables? nil :settable)
   

   (messages-local (set-difference (the node (message-list :message-type :local))
                                   (append (the node (message-list :message-type :local 
                                                                   :category :methods))
                                           (the node (message-list :message-type :local
                                                                   :category :uncached-attributes)))))
   (messages (safe-sort
              (remove-if
               #'(lambda (keyword)
                   (or (member keyword *internal-keywords*)
                       (and *suppress-%%-messages?*
                            (let ((string (string keyword)))
                              (and
                               (> (length string) 2)
                               (or (string-equal (subseq string 0 2) "$$")
                                   (and (eql (aref string 0) #\%)
                                        (or (eql (aref string 1) #\%)
                                            (eql
                                             (aref string (1- (length string)))
                                             #\%)))))))))
               (set-difference (the :node (:message-list))
                               (append (the
                                           :node
                                         (:message-list :category :methods))
                                       (the :node (:message-list
                                                   :category
                                                   :uncached-attributes)))))
              #'string<))
   
   
   (check-links nil :settable))

  
  :hidden-objects
  ((values :type 'value-inspector 
           :depth (1+ (the depth))
           :sequence (:size (length (the messages)))
           :respondent (the respondent)
           :message (nth (the-child index) (the messages))
           :parent-node (the node)
           :pass-down (
                       tatu-root root-object
                       tree-toplevel colors-default 
                       tree-type tree-node display-controls-hash
                       perform-action-function click-mode  relative-font-size))

   
   (settables-form :type 'settables-form
                   :pass-down (show-settables? node tatu-root)))

  
  :functions
  (   
   (visit-definition-in-emacs
    (definition)
143
    (declare (ignore definition))
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    (error "Sorry! This is supposed to do 'lisp-find-definition' in emacs, but not working yet... Please send email to support@genworks.com"))
   
   #+nil
   (visit-definition-in-emacs
    (definition)
    (let ((emacs-lisp-command (format nil "(fi:lisp-find-definition ~s)" definition)))
      (print-variables emacs-lisp-command)
      #+allegro (lep::eval-in-emacs emacs-lisp-command)))
    
    
   (perform-action!
    (object)
    (the tatu-root (perform-action! object)))
   
   (message-evaluate 
    (message) 
    ;;
    ;; FLAG -- note we have to bash the view-toggle because no bashing
    ;; is actually happening when we evaluate the message, so the view
    ;; does not update by itself.
    ;;
    (the toggle-view-toggle!) 
    
    (the (set-slot! :expand? t))
    
    
    (ignore-errors-with-warning (the node (evaluate message)))
    
    #+nil
    (multiple-value-bind (value error) (ignore-errors (the node (evaluate message)))
      (let* ((error? (and error (typep error 'error))) 
             (value (if  error? (format nil "ERROR: ~a" error) value)))
                         
        (when error? (the node (set-slot! message value))))))
   
   (set-object! (object) (the (set-slot! :node-root-path (the-object object root-path))))))


(define-lens (html-format inspector)()
  
  :output-functions
185
  ((inner-html
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
    ()
    (with-cl-who ()
      (if (the node) 
          (write-the inspector-body)
        (htm "No Object Instantiated"))))
   
   
   (inspector-body
    ()
    (with-cl-who ()
      
      (:center
       ((:span :style "font-style: italic; font-weight: bold;")
        (:p "Inspecting: " 
            (esc (format nil "~s" 
                         (cons 'the (reverse (butlast (the node-root-path))))))
            (unless (butlast (the node-root-path))
              (htm " [the root object]")))
        (:p " of type:  " ((:span :style "color: blue; cursor: pointer;" 
                                  :onclick (the (gdl-ajax-call :function-key :visit-definition-in-emacs
                                                               :arguments (list (format nil "~s" (the node type)))))
                                  
                                  
                                  )
                           (esc (format nil "~s" (the node type)))))))
      
      ((:table :class "inspector-table" :border 1)
       (:tr (:td "Settables") (:td (str (the settables-form control-view))))
       
       
       (when (the settables-form show-settables?)
         (htm (:tr ((:td :colspan 2)
                    (str (the settables-form main-div))))))
       
       (let ((count -1))
         (dolist (value-inspector (list-elements (the values)))
           (let ((message (the-object value-inspector message)))
             (incf count)
             (htm (:tr
                    (:td (str (the-object value-inspector message)))
                    (:td 
                     ;;
                     ;; FLAG -- push everything to do with node into value-inspector
                     ;;
                     (let ((slot-status (if (the expand?) 
                                            (the node (slot-status message)) :unbound)))
                       (case slot-status
                         (:unbound 
                            (htm ((:span :style "color: blue; font-style: italic; cursor: pointer;"
                                         :onclick (the (gdl-ajax-call 
                                                        :function-key :message-evaluate
                                                        :arguments (list message))))
                                  "Unbound")))

                         (otherwise (str (the-object value-inspector value-display))))))))))))))))



Dave Cooper's avatar
Dave Cooper committed
244

245
246
247
248
249
250
251
252
253
254
255
(define-object value-inspector (inspector)
  
  :input-slots ((message nil)
                parent-node
                root-object
                tatu-root
                click-mode
                (value (with-error-handling () 
                         (the parent-node (evaluate (the message))))))
  
  :computed-slots 
Dave Cooper's avatar
Dave Cooper committed
256
257
258
259
  ((value-root-path (cons (the message) (the parent-node root-path)))


   (value-type (if (the 3d-vector?)
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
                   :gdl-3d-point
                 (typecase (the value)
                   (list (when (consp (the value)) :list))
                   (gdl::quantification :gdl-sequence)
                   (gdl::gdl-basis :gdl-atom))))


   (value-cardinality (case (the value-type)
                        (:list (length (the value)))
                        (:gdl-sequence (the value number-of-elements))
                        (otherwise 0)))

   (truncated-sequence (unless (zerop (the value-cardinality))
                         (with-output-to-string (ss)
                           (let ((*print-length* 3))
                             (pprint (the value) ss)))))
                   
   (expanded? nil :settable)

   (clickable? (and (not (the expanded?))
                    (or (member (the value-type)
                                (list :list :gdl-sequence :gdl-atom))
                        (and (eql (the value-type) :gdl-3d-point)
                             (member (the click-mode)
                                     (list :add-leaves
                                           :add-leaves*
                                           :add-node
                                           :draw-leaves
                                           :draw-node
                                           :delete-leaves))))))

   (3d-vector? (and (typep (the value) 'vector)
                    (= (length (the value)) 3)
                    (every #'floatp (the value))))
                   
   (value-display
    (ignore-errors-with-warning
     (let* ((value (the value))
            (gdl-object? (eql (class-of (class-of value))
                              (find-class 'gdl-class))))
Dave Cooper's avatar
Dave Cooper committed
300

301
302
303
304
305
306
307
308
309
310
311
312
313
314
       (with-cl-who-string ()
         ((:span :style (format nil "color: ~a; ~a" 
                                (if (the clickable?) "blue" "black")
                                (if (the clickable?) "cursor: pointer;" ""))
                 :onclick (case (the value-type)
                            (:gdl-atom
                             (the (gdl-ajax-call 
                                   :function-key :perform-action!
                                   :arguments (list value))))

                            (:gdl-3d-point
                             (when (the clickable?)
                               (the (gdl-ajax-call
                                     :function-key :perform-action!
Dave Cooper's avatar
Dave Cooper committed
315
316
317
318
                                     ;;:arguments (list (the point))
				     :arguments (let ((object (make-object 'gdl::root-path-container
									   :root-path (the value-root-path))))
						  (list object))))))
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

                            ((:list :gdl-sequence)
                             (unless (the expanded?)
                               (the (gdl-ajax-call
                                     ;;:bashee self
                                     :function-key :set-slot!
                                     :arguments (list :expanded? t)))))))

          (cond ((or (eql (the value-type) :list)
                     (eql (the value-type) :gdl-sequence))
                 (if (the expanded?)
                     (htm ((:table :border 1)
                           (:tr 
                            (:td ((:span :style "color: blue; cursor: pointer;"
                                         :onclick (the (gdl-ajax-call
                                                        :function-key :set-slot!
                                                        :arguments (list :expanded? nil))))
                                  "X"))
                            (:td (str
                                  (ecase (the value-type)
                                    (:list "A List")
                                    (:gdl-sequence "A GDL Sequence")))))
                           (let ((index -1))
                             (dolist (row (list-elements (the sequence-elements)))
                               (htm (:tr (:td (str (incf index)))
                                         (:td (str (the-object row value-display)))))))))
                   (htm (:pre (esc (the truncated-sequence))))))
                (gdl-object? (htm (esc (format nil "~s" value))))
Dave Cooper's avatar
Dave Cooper committed
347
348

		#+nil
349
350
                ((eql (the value-type) :gdl-3d-point)
                 (htm (esc (the point strings-for-display))))
Dave Cooper's avatar
Dave Cooper committed
351
352
353
354

		((eql (the value-type) :gdl-3d-point)
                 (htm (esc (format nil "~s" value))))

355
356
357
                (t (htm (fmt "~s" value))))))))))
    

Dave Cooper's avatar
Dave Cooper committed
358
359
360
  :objects (
	    #+nil
	    (point :type (if (the 3d-vector?) 'point 'null-object)
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
                   :center (when (the 3d-vector?) (the value))
                   :strings-for-display (format nil "~s" (the value)))

            (sequence-elements :type 'value-inspector
                               :sequence (:size (the value-cardinality))
                               :pass-down (root-object tatu-root click-mode)
                               :value (the (get-value-element (the-child index)))))

  :functions
  ((get-value-element
    (index)
    (case (the value-type)
      (:list (nth index (the value)))
      (:gdl-sequence (the value (get-member index)))))))



(define-object settables-form (base-html-sheet)
  
  :input-slots
  (show-settables? node tatu-root)
  
  
  :computed-slots
  ((settables 
    (let (settables) 
      (maphash #'(lambda(key val)(declare (ignore val)) 
                        (push key settables)) 
               (the node %settable-slots%))
      (nconc settables (the node (message-list :category :required-input-slots)))               
      (sort (remove-if
             #'(lambda (keyword)
                 (or (member keyword *internal-keywords*)
                     (and *suppress-%%-messages?*
                          (let ((string (string keyword)))
                            (or (and (> (length string) 2) 
                                     (string-equal (subseq string 0 2) "$$"))
                                (and (eql (aref string 0) #\%)
                                     (or (eql (aref string 1) #\%)
                                         (eql
                                          (aref string (1- (length string)))
                                          #\%))))))
                               
                     (let ((value (with-error-handling ()
                                    (the node (evaluate keyword)))))
                       (or (eql (class-of (class-of value)) (find-class 'gdl-class))
                           (and (consp value) 
                                (some #'(lambda(item) (eql (class-of (class-of item)) 
                                                           (find-class 'gdl-class)))
                                      value))))))
             settables) #'string<)))
   
   (respondent (the tatu-root))
   
   ;;(bashee (the node))
   
   
   (control-view (with-cl-who-string ()
                   ((:span :style "cursor: pointer; color: blue; font-style: oblique;"
                           :onclick (the (gdl-ajax-call 
                                          :bashee self
                                          :function-key :set-slot!
                                          :arguments (list :show-settables? (not (the show-settables?))))))
                    (if (the show-settables?) (htm "X") (htm "Show Settables!")))))
   
   (form-fields-hash (let ((ht (make-hash-table)))
                       (dolist (form-field (list-elements (the form-fields)) ht)
                         (setf (gethash  (the-object form-field field-name) ht) form-field)))))

  
  :hidden-objects ((reset-buttons :type 'button-form-control
                                  :sequence (:size (length (the settables)))
                                  :label "R"
                                  :onclick (the (gdl-ajax-call 
                                                 :function-key :restore-default-values! 
                                                 :arguments (list (the (form-fields (the-child index)) 
                                                                    keyword)))))
                   
                   (submit-fields-button :type 'button-form-control
                                         :label " OK "
                                         :onclick (the (gdl-ajax-call 
                                                        :form-controls (list-elements (the form-fields)))))
                   
                   (form-fields :type 'text-form-control
445
				:pseudo-inputs (keyword)
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
                                :sequence (:size (length (the settables)))
                                ;;
                                ;; FLAG -- precompute this in a slot, and allow for unbound until clicked. 
                                ;;
                                :size (length (format nil "~s" (the-child default)))
                                
                                :validation-function 
                                #'(lambda(value)
                                    (progn
                                      (the node (set-slot-if-needed! 
                                                 (the-child keyword) value :infer-types? nil))
                                      t))

                                ;;
                                ;; FLAG -- Comment this out to disable ajax submittal
                                ;;
                                :ajax-submit-on-change? t
                                
                                :domain :pass-thru
                                :keyword (nth (the-child index)(the settables))
                                :default (with-error-handling () 
                                           (the node (evaluate (the-child keyword))))
                                :prompt (the-child keyword)))
  
  :functions ((restore-default-values!
               (message-keyword)
               (the node (restore-slot-default! message-keyword))
               ;;
               ;; FLAG -- this seems to be unecessary -- why?
               ;;
               (the-object (gethash message-keyword (the form-fields-hash)) 
                           (restore-slot-default! :value)))))



(define-lens (html-format settables-form)()
  :output-functions
483
  ((inner-html
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
    ()
    (with-cl-who ()
      (when (the show-settables?) 
        (with-html-form (:cl-who? t :on-submit "return false;")
          (:table
           (mapc #'(lambda(form-control reset-button)
                     (let ((message (the-object form-control keyword)))
                       (let ((remarks (the node (message-remarks message))))
                         (htm (:tr (:td (str (the-object reset-button form-control-string))
                                        ((:td :bgcolor :yellow) 
                                         (if (null remarks) 
                                             (htm (:b (str message)))
                                           (htm ((:span :style "cursor: arrow;" 
                                                        :title (one-line (second remarks)))
                                                 (:b (str message))))))
                                        (:td (str (the-object form-control form-control-string)))
                                        ))))))
                 (list-elements (the form-fields)) (list-elements (the reset-buttons))))
          
          ;;(:p ((:input :type :submit :value "OK")))
          
          (:p (str (the submit-fields-button form-control-string)))
          
          ))))))




(define-object viewable-link (base-html-sheet)
  :input-slots (node message)
  
  :computed-slots ((strings-for-display (format nil "~a" (the message))))
  
  :functions
  ((before-present!
    ()
    (with-error-handling () (the node (evaluate (the message)))))))


(define-lens (html-format viewable-link)()
  :output-functions
  ((main-sheet () (write-the return-object main-sheet))))



(define-object switch-viewables (base-html-sheet)

  :input-slots
  (return-object)
  
  :computed-slots
  ((show-viewables? nil :settable)
   (strings-for-display (if (the show-viewables?) "X" "Uncached Slots")))
  
  :functions
  ((before-present! () (the (set-slot! :show-viewables? (not (the show-viewables?)))))))


(define-lens (html-format switch-viewables)()
  :output-functions
  ((main-sheet () (write-the return-object main-sheet))))


;;
;; Test object to get form-controls working for readable Lisp objects
;;

#+nil
(define-object sample-form (base-ajax-sheet)

  :computed-slots
  ((main-sheet-body (with-cl-who-string ()
                      (str (the item-1 html-string)))))
  
  :objects
  ((item-1 :type 'text-form-control
           :ajax-submit-on-change? t
           :domain :symbol
           :default "hey now")))