                    :href         (str "http://" @appshare-dev-server ":3450")}
                   "Run in own window")
 
-               (button {:className    "btn btn-default"
-                        :style       {:display "inline-block" :marginLeft "30px" :fontFamily "Ubuntu" :fontSize "1em" :marginTop "-0.3em"  }
-                        :onClick     #(reeval  (read-ui app [:app-id]))}
-                       "Save")))
+               ))
+
+
+
+            (button {:className    "btn-lg btn-default"
+                        :style       {:display "inline-block" :marginLeft "30px" :fontFamily "Ubuntu" :fontSize "1em" :marginTop "-0.3em"
+                                      :opacity  (if (= (read-ui app [:mode]) "edit")  "1.0" "0.4")
+
+                                      }
+                        :onClick     #(do
+                                        (write-ui app [:mode] "view")
+                                        (reeval  (read-ui app [:app-id])))
+                     :disabled  (if (= (read-ui app [:mode]) "edit") "" "true")
+                     }
+                       "Save")
 
             (a {:target       "appshare.co"
                 :style       {:textDecoration "underline" :float "right"  :display "inline-block" :marginRight "30px" :fontFamily "Ubuntu" :fontSize "2em" :marginTop "0.3em"}

               "AppShare on Github"))




       (div {}
            (div {:style {:display "inline-block" :width "1200" :height "800" :verticalAlign "top"}}
                 (cond
                   (or (= (read-ui app [:mode]) nil) (= (read-ui app [:mode]) "browse"))
                   (component browser-component app [])

                   (= (read-ui app [:mode]) "edit")
                   (component editor-component app [])

                   :else
                   (div {} "Nothing selected")))



       (iframe {:id "appframe" :style {:display "inline-block"} :src
                (str (cond
                      @c/debug-mode
                      "http://127.0.0.1:3449"

                      :else
                      "http://appshare.co/appshare")
                     "/devclient.html") :width "600" :height "800"}))
       ))






(defn evalapp [app-id]
  (go
;    (js/alert (str app-id))
    (let [x (remote  !getfilecontents  {:id app-id })]
      (swap! ns-counter inc)
      (js/sendcode (str (start) (:value x) (end) ))
      )))







(defn-ui-component     view-app-component   [app]
  {:on-mount
   (do  (go
          ;(remote !loadapp {:id (read-ui app [:app-id] )})
          ;(js/alert "loaded")
          ))}

  (iframe {:id "appframe" :style {:display "inline-block"}
           :src
           (str (cond
                  @c/debug-mode
                  "http://127.0.0.1:3449"

                  :else
                  "http://appshare.co/appshare")
                "/devclient.html") :width "600" :height "800"}))







(defn-ui-component     editor-component   [app]
  {:on-mount
   (do  (go
          (if  (read-ui app [:app-id])
              (let [x (remote  !getfilecontents  {:id (read-ui app [:app-id]) })]

                (js/createEditor)
                (js/populateEditor (get x :value))
                ;(reeval (read-ui app [:app-id]))
                ;(js/populateEditor (str "Loaded: " (read-ui app [:app-id])))

                ))))}


  (div {}
       (realtime select id, application_name from coils_applications where id = ? {:params [(read-ui app [:app-id])]}
                 (cond
                   (and (= (read-ui app [:submode]) "editappname") (= (<-- :id ) (read-ui app [:app-id])))
                   (input-field {:style {:marginBottom "20px" :color "black"} :placeholder  (str (<-- :application_name))}
                                app
                                (fn [new-name]
                                  (let [id (read-ui app [:app-id])]
                                    (go
                                      (sql "update  coils_applications
                                           set application_name = ?
                                           where id = ?"
                                           [new-name id ]  )
                                      ;(js/alert (str new-name ":" id))
                                      (write-ui app [:submode] nil)
                                      ))))


                   ; if we select a different app
                   :else
                   (do
                     (div {:onClick     #(go  (write-ui app [:submode] "editappname")
                                              (write-ui app [:app-id] (<-- :id))
                                              )

                           :style {:display "inline-block" :fontFamily "Ubuntu" :fontWeight "700" :fontSize "1.3em" ::marginTop "0.7em"}}
                          (str (<-- :application_name)))))
       )
       (textarea {:id "cm" :style {:display "inline-block" :width "1200" :height "800"}} "TEXT EDITOR")))



