Added AJAX support
[clinton/lisp-on-lines.git] / src / mewa / slot-presentations.lisp
index 4e69bd8..1454c53 100644 (file)
@@ -1,13 +1,35 @@
 (in-package :it.bese.ucw)
 
 (defun multiple-value-funcall->list (function &rest args)
+  "The function to be called by m-v-bf"
                   (multiple-value-call #'list (apply function args)))
 
 (defmacro multiple-value-bindf (vars form &body body)
+  "Like M-V-B, only it works in actions. form must be a function call"
                   `(destructuring-bind ,vars 
                     (multiple-value-funcall->list #',(car form) ,@(cdr form))
                     ,@body))
 
+
+;;;; ** Textarea Slot Presentation
+;;;;  This should really be in UCW.
+
+(defslot-presentation text-slot-presentation ()
+  ((rows :initarg :rows :accessor rows :initform nil)
+   (columns :initarg :columns :accessor columns :initform nil)
+   (html-contentp :initarg :escape-html-p :accessor escape-html-p :initform nil))
+  (:type-name text))
+
+(defmethod present-slot ((slot text-slot-presentation) instance)
+  (if (editablep slot)
+      (<ucw:textarea :accessor (presentation-slot-value slot instance)
+                    :rows (rows slot)
+                    :cols (columns slot))
+      (if (escape-html-p slot)
+         (<:as-is (presentation-slot-value slot instance))
+         (<:as-html (presentation-slot-value slot instance)))))
+
+
 (defcomponent mewa-slot-presentation ()
   ((slot-name :accessor slot-name 
              :initarg :slot-name 
@@ -18,7 +40,8 @@
                     :initform nil
                     :documentation 
                     "When nil, the instance is syncronised with the database. 
-When T, only the default value for primary keys and the joins are updated."))
+When T, only the default value for primary keys and the joins are updated.")
+   (show-label-p :accessor show-label-p :initarg :show-label-p :initform t))
   (:documentation "The superclass of all Mewa slot presentations"))
 
 ;;;; this has to be in the eval when i would think
@@ -44,7 +67,6 @@ When T, only the default value for primary keys and the joins are updated."))
   integer
   currency)
 
-
 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
        ()
        (:type-name clsql-sys:wall-time))
@@ -82,21 +104,16 @@ When T, only the default value for primary keys and the joins are updated."))
 
 (defslot-presentation  mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
   ((foreign-instance :accessor foreign-instance)
-   (linkedp :accessor linkedp :initarg :linkedp :initform t))
+   (linkedp :accessor linkedp :initarg :linkedp :initform t)
+   (creator :accessor creator :initarg :creator :initform :editor))
   (:type-name relation))
 
-(defun get-fkey-data (instance slot-name)
-  "ugly workaround b/c UCW does not like M-V-B"
-  (multiple-value-bind (finstance foreign-slot-name)
-      (meta-model:explode-foreign-key instance slot-name)
-    (cons finstance foreign-slot-name)))
-
 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
   (multiple-value-bindf (finstance foreign-slot-name)
       (meta-model:explode-foreign-key instance (slot-name slot))
     (let ((new-instance
             (call-component 
-             (parent slot) 
+             (parent slot)
              (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
                                 'mewa::mewa-presentation-search)
                             :search-presentation
@@ -106,17 +123,30 @@ When T, only the default value for primary keys and the joins are updated."))
                             (mewa:make-presentation finstance 
                                                     :type :listing)))))
       (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
-      (meta-model:sync-instance instance :fill-gaps-only t))))
+      (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
 
-(defaction create-record ((slot mewa-relation-slot-presentation) instance)
+(defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
   (multiple-value-bindf (finstance foreign-slot-name)
       (meta-model:explode-foreign-key instance (slot-name slot))
     (let ((new-instance
            (call-component
             (parent slot) 
-            (mewa:make-presentation finstance :type :editor))))
+            (mewa:make-presentation finstance :type (creator self)))))
+      
+      ;;;; TODO: this next bit is due to a bad design decision. 
+      ;;;; Components should always have (ok) return self, but somewhere 
+      ;;;; i've made in return (instance self) sometimes, and this
+      ;;;; bahaviour is totatlly fucked.
+      
+     (when (typep new-instance 'mewa::mewa)
+       (setf new-instance (instance new-instance)))
+
+      ;;;; sorry about that, and now back t our regular program.
+      
+      (meta-model:sync-instance new-instance)
       (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
       (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
+      
 
 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
  ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
@@ -147,40 +177,117 @@ When T, only the default value for primary keys and the joins are updated."))
   (meta-model:sync-instance (instance (parent self))))
 
 
-(defmethod  present-slot :around ((slot foreign-key-slot-presentation) instance)
-  (setf (foreign-instance slot) (when (presentation-slot-value slot instance) (meta-model:explode-foreign-key instance (slot-name slot))))
-  (flet ((render () (call-next-method)))
-    (cond 
-      ((editablep slot)
-       (render)
-       (<ucw:submit :action  (search-records slot instance) :value "Search" :style "display:inline")
-       (<ucw:submit :action  (create-record slot instance) :value "Add New" :style "display:inline"))
-      ((linkedp slot)
-       (<ucw:a :action (view-instance slot (foreign-instance slot)) 
-              (render)))
-      (t       
-       (render)))))
+
+(defmethod present-slot :before ((slot foreign-key-slot-presentation) instance)
+  ())
+
+
+(defmethod  present-slot :around ((slot foreign-key-slot-presentation) instance)  
+  (setf (foreign-instance slot) 
+       (when (presentation-slot-value slot instance) 
+         (meta-model:explode-foreign-key instance (slot-name slot))))
+  (flet ((render () (when (foreign-instance slot)(call-next-method))))
+    (if (slot-boundp slot 'place)
+        (cond 
+          ((editablep slot)
+           (render)
+           (<ucw:submit :action  (search-records slot instance) :value "Search" :style "display:inline")
+           (<ucw:submit :action  (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
+          ((linkedp slot)
+           (<ucw:a :action (view-instance slot (foreign-instance slot)) 
+                   (render)))
+          (t       
+           (render)))
+       ;; presentation is used only for rendering
+        (render))))
+
+
+;;;; * AJAX stuff 
+
+;;;; TODO: This search stuff should probably me refactored elsewhere
+
+(defmethod find-slots-of-type (model &key (type 'string)
+                             (types '((string)) types-supplied-p))
+  "returns a list of slots matching TYPE, or matching any of TYPES"
+  (let (ty)
+    (if types-supplied-p 
+       (setf ty types)
+       (setf ty (list type)))
+    (remove nil (mapcar #'(lambda (st) (when (member (second st) ty)
+                                        (first st)))
+            (lisp-on-lines::list-slot-types model)))))
+
+(defslot-presentation ajax-foreign-key-slot-presentation (foreign-key-slot-presentation)
+  ((search-slots :accessor search-slots :initarg :search-slots :initform nil)
+   (live-search 
+     :accessor live-search
+     :component (lisp-on-lines:auto-complete
+                :values-generator
+                (lambda (value)
+                  (when (< 0 (length value))
+                    (limited-word-search 'person '(first-name last-name company-name) (list value))))
+
+                :render (lambda (x)
+                          (<:as-html (if (> (length (last-name x)) 0)
+                                        (strcat (last-name x) ", ")
+                                     " ")
+                                     (first-name x)" " (company-name x)))
+                :as-value (lambda (x) x)
+                :submit-on-click-p nil)))
+  (:type-name ajax-foreign-key))
+
+
+(defmethod shared-initialize :after ((slot ajax-foreign-key-slot-presentation) slots &rest args)
+  ;; If no search-slots than use the any slots of type string
+  (unless (search-slots slot)
+    (setf (search-slots slot) t)
+  (let ((l (live-search slot)))
+    (setf (lisp-on-lines::values-generator l) t))))
+         
+
+(defmethod  present-slot :around ((slot ajax-foreign-key-slot-presentation) instance)  
+  (setf (foreign-instance slot) 
+       (when (presentation-slot-value slot instance) 
+         (meta-model:explode-foreign-key instance (slot-name slot))))
+  (flet ((render () (when (foreign-instance slot)(call-next-method))))
+    (if (slot-boundp slot 'place)
+        (cond 
+          ((editablep slot)
+          
+           (<ucw:submit :action  (search-records slot instance) :value "find" :style "display:inline"))
+          ((linkedp slot)
+           (<ucw:a :action (view-instance slot (foreign-instance slot)) 
+                   (render)))
+          (t       
+           (render)))
+       ;; presentation is used only for rendering
+        (render))))
 
 ;;;; HAS MANY 
 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
   ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
   (:type-name has-many))
 
-
 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
+  ;; if the instance is not stored we must make sure to mark it stored now!
+  (unless (mewa::instance-is-stored-p instance)
+    (setf (mewa::modifiedp (parent self)) t))
+  ;; sync up the instance
   (mewa:ensure-instance-sync (parent slot))
+
   (multiple-value-bindf (class home foreign) 
       (meta-model:explode-has-many instance (slot-name slot))
     (let ((new (make-instance class)))
       (setf (slot-value new foreign) (slot-value instance home))
       (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
-      (call-component (parent slot) (mewa:make-presentation new :type :editor))
+      (call-component (parent slot) (mewa:make-presentation new :type (creator slot)))
       (meta-model:sync-instance instance))))
 
 (defmethod present-slot ((slot has-many-slot-presentation) instance)
-  (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot))
-  (let ((i (get-foreign-instances slot instance))
-       (linkedp (linkedp slot)))
+  (when (slot-boundp slot 'place)
+    (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
+  (let ((i (get-foreign-instances slot instance)))
+       
     (<:ul 
      (dolist (s i)
        (let ((s s))
@@ -219,13 +326,16 @@ When T, only the default value for primary keys and the joins are updated."))
 
 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
   ;;(<:as-html "isance: " instance)
-  (<ucw:a :action (list-prev slot) (<:as-html "<<"))
-  (let ((self (parent slot)))
-    (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
-           (<:as-html  (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
-  (<ucw:a :action (list-next slot) (<:as-html ">>"))
-  (call-next-method)
-  (<:as-html "total :" (len slot))) 
+  (if (slot-boundp slot 'place)
+      (progn
+        (<ucw:a :action (list-prev slot) (<:as-html "<<"))
+        (let ((self (parent slot)))
+          (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
+               (<:as-html  (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
+        (<ucw:a :action (list-next slot) (<:as-html ">>"))
+        (call-next-method)
+        (<:as-html "total :" (len slot)))
+      (call-next-method)))
 
 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
   (let ((f (call-next-method)))
@@ -256,3 +366,10 @@ When T, only the default value for primary keys and the joins are updated."))
            (setf (instance (presentation slot)) (presentation-slot-value slot instance))
            (present (presentation slot)))
          (<:as-html "--"))))
+
+
+
+
+
+
+