Browse Source

initial POS demo with save and load

Harlan Iverson 5 năm trước cách đây
commit
41d43366e8
7 tập tin đã thay đổi với 311 bổ sung0 xóa
  1. 14 0
      .gitignore
  2. 47 0
      README.md
  3. 42 0
      dev/user.clj
  4. 96 0
      project.clj
  5. 2 0
      resources/public/css/style.css
  6. 16 0
      resources/public/index.html
  7. 94 0
      src/pos_demo/core.cljs

+ 14 - 0
.gitignore

@@ -0,0 +1,14 @@
+/resources/public/js/compiled/**
+figwheel_server.log
+pom.xml
+*jar
+/lib/
+/classes/
+/out/
+/target/
+.lein-deps-sum
+.lein-repl-history
+.lein-plugins/
+.repl
+.nrepl-port
+*~

+ 47 - 0
README.md

@@ -0,0 +1,47 @@
+# pos-demo
+
+FIXME: Write a one-line description of your library/project.
+
+## Overview
+
+FIXME: Write a paragraph about the library/project and highlight its goals.
+
+## Setup
+
+To get an interactive development environment run:
+
+    lein figwheel
+
+and open your browser at [localhost:3449](http://localhost:3449/).
+This will auto compile and send all changes to the browser without the
+need to reload. After the compilation process is complete, you will
+get a Browser Connected REPL. An easy way to try it is:
+
+    (js/alert "Am I connected?")
+
+and you should see an alert in the browser window.
+
+To clean all compiled files:
+
+    lein clean
+
+To create a production build run:
+
+    lein do clean, cljsbuild once min
+
+And open your browser in `resources/public/index.html`. You will not
+get live reloading, nor a REPL. 
+
+To serve the prodction build on most machines:
+
+```
+cd resources/public
+python -m SimpleHTTPServer 3449
+```
+
+
+## License
+
+Copyright © 2014 FIXME
+
+Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

+ 42 - 0
dev/user.clj

@@ -0,0 +1,42 @@
+(ns user
+  (:require
+   [figwheel-sidecar.repl-api :as f]))
+
+;; user is a namespace that the Clojure runtime looks for and
+;; loads if its available
+
+;; You can place helper functions in here. This is great for starting
+;; and stopping your webserver and other development services
+
+;; The definitions in here will be available if you run "lein repl" or launch a
+;; Clojure repl some other way
+
+;; You have to ensure that the libraries you :require are listed in your dependencies
+
+;; Once you start down this path
+;; you will probably want to look at
+;; tools.namespace https://github.com/clojure/tools.namespace
+;; and Component https://github.com/stuartsierra/component
+
+
+(defn fig-start
+  "This starts the figwheel server and watch based auto-compiler."
+  []
+  ;; this call will only work as long as your :cljsbuild and
+  ;; :figwheel configurations are at the top level of your project.clj
+  ;; and are not spread across different lein profiles
+
+  ;; otherwise you can pass a configuration into start-figwheel! manually
+  (f/start-figwheel!))
+
+(defn fig-stop
+  "Stop the figwheel server and watch based auto-compiler."
+  []
+  (f/stop-figwheel!))
+
+;; if you are in an nREPL environment you will need to make sure you
+;; have setup piggieback for this to work
+(defn cljs-repl
+  "Launch a ClojureScript REPL that is connected to your build and host environment."
+  []
+  (f/cljs-repl))

+ 96 - 0
project.clj

@@ -0,0 +1,96 @@
+(defproject pos-demo "0.1.0-SNAPSHOT"
+  :description "FIXME: write this!"
+  :url "http://example.com/FIXME"
+  :license {:name "Eclipse Public License"
+            :url "http://www.eclipse.org/legal/epl-v10.html"}
+
+  :min-lein-version "2.9.1"
+
+  :dependencies [[org.clojure/clojure "1.10.0"]
+                 [org.clojure/clojurescript "1.10.520"]
+                 [org.clojure/core.async  "0.4.500"]
+                 [reagent "0.8.1"]]
+
+  :plugins [[lein-figwheel "0.5.19"]
+            [lein-cljsbuild "1.1.7" :exclusions [[org.clojure/clojure]]]]
+
+  :source-paths ["src"]
+
+  :cljsbuild {:builds
+              [{:id "dev"
+                :source-paths ["src"]
+
+                ;; The presence of a :figwheel configuration here
+                ;; will cause figwheel to inject the figwheel client
+                ;; into your build
+                :figwheel {:on-jsload "pos-demo.core/on-js-reload"
+                           ;; :open-urls will pop open your application
+                           ;; in the default browser once Figwheel has
+                           ;; started and compiled your application.
+                           ;; Comment this out once it no longer serves you.
+                           :open-urls ["http://localhost:3449/index.html"]}
+
+                :compiler {:main pos-demo.core
+                           :asset-path "js/compiled/out"
+                           :output-to "resources/public/js/compiled/pos_demo.js"
+                           :output-dir "resources/public/js/compiled/out"
+                           :source-map-timestamp true
+                           ;; To console.log CLJS data-structures make sure you enable devtools in Chrome
+                           ;; https://github.com/binaryage/cljs-devtools
+                           :preloads [devtools.preload]}}
+               ;; This next build is a compressed minified build for
+               ;; production. You can build this with:
+               ;; lein cljsbuild once min
+               {:id "min"
+                :source-paths ["src"]
+                :compiler {:output-to "resources/public/js/compiled/pos_demo.js"
+                           :main pos-demo.core
+                           :optimizations :advanced
+                           :pretty-print false}}]}
+
+  :figwheel {;; :http-server-root "public" ;; default and assumes "resources"
+             ;; :server-port 3449 ;; default
+             ;; :server-ip "127.0.0.1"
+
+             :css-dirs ["resources/public/css"] ;; watch and update CSS
+
+             ;; Start an nREPL server into the running figwheel process
+             ;; :nrepl-port 7888
+
+             ;; Server Ring Handler (optional)
+             ;; if you want to embed a ring handler into the figwheel http-kit
+             ;; server, this is for simple ring servers, if this
+
+             ;; doesn't work for you just run your own server :) (see lein-ring)
+
+             ;; :ring-handler hello_world.server/handler
+
+             ;; To be able to open files in your editor from the heads up display
+             ;; you will need to put a script on your path.
+             ;; that script will have to take a file path and a line number
+             ;; ie. in  ~/bin/myfile-opener
+             ;; #! /bin/sh
+             ;; emacsclient -n +$2 $1
+             ;;
+             ;; :open-file-command "myfile-opener"
+
+             ;; if you are using emacsclient you can just use
+             ;; :open-file-command "emacsclient"
+
+             ;; if you want to disable the REPL
+             ;; :repl false
+
+             ;; to configure a different figwheel logfile path
+             ;; :server-logfile "tmp/logs/figwheel-logfile.log"
+
+             ;; to pipe all the output to the repl
+             ;; :server-logfile false
+             }
+
+  :profiles {:dev {:dependencies [[binaryage/devtools "0.9.10"]
+                                  [figwheel-sidecar "0.5.19"]]
+                   ;; need to add dev source path here to get user.clj loaded
+                   :source-paths ["src" "dev"]
+                   ;; need to add the compliled assets to the :clean-targets
+                   :clean-targets ^{:protect false} ["resources/public/js/compiled"
+                                                     :target-path]}})

+ 2 - 0
resources/public/css/style.css

@@ -0,0 +1,2 @@
+/* some style */
+

+ 16 - 0
resources/public/index.html

@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <link href="css/style.css" rel="stylesheet" type="text/css">
+    <link rel="icon" href="https://clojurescript.org/images/cljs-logo-icon-32.png">
+  </head>
+  <body>
+    <div id="app">
+      <h2>Loading application. Thanks for your patience.</h2>
+      <p>Debugging information is available in the developer console.</p>
+    </div>
+    <script src="js/compiled/pos_demo.js" type="text/javascript"></script>
+  </body>
+</html>

+ 94 - 0
src/pos_demo/core.cljs

@@ -0,0 +1,94 @@
+(ns pos-demo.core
+    (:require [reagent.core :as reagent :refer [atom]]
+              [cljs.reader :refer [read-string]]))
+
+(enable-console-print!)
+
+(defonce app-state (atom {:shop-name "Hola world!"
+                          :active-order nil}))
+
+
+; model
+
+(defn current-date
+  []
+  (js/Date.))
+
+(defn new-order
+  []
+  (swap! app-state assoc :active-order {:id (str (current-date)) :requests []}))
+
+(defn abandon-order
+  []
+  (swap! app-state assoc :active-order nil))
+
+(defn save-order
+  []
+  (js/prompt "Save the text; it can be loaded, later." (pr-str (:active-order @app-state))))
+
+(defn load-order
+  []
+  (let [order-edn (js/prompt "Paste the saved text.")]
+    (when-let [order (read-string order-edn)]
+      (when (map? order)
+        (swap! app-state assoc :active-order order)))
+    ))
+
+; UI handlers
+
+(defn new-order-clicked
+  []
+  (new-order))
+  
+(defn abandon-order-clicked
+  []
+  (abandon-order))
+
+(defn save-order-clicked
+  []
+  (save-order))
+  
+(defn load-order-clicked
+  []
+  (load-order))
+
+
+; UI
+
+(defn button
+  [label on-click]
+  [:button {:on-click on-click
+            :style {:border "1px solid black"
+                   :height "100px"
+                   :width "100px"
+                   :text-align "center"
+                   :vertical-aign "middle" 
+                   :margin "3px"
+                 }}
+        label])
+
+
+
+(defn main-screen []
+  [:div
+   [:h1 (:shop-name @app-state)]
+   (when (nil? (:active-order @app-state))
+     [:span 
+       (button "New Order" new-order-clicked)
+       (button "Resume Order" load-order-clicked)])
+   (when-not (nil? (:active-order @app-state))
+     [:span
+       (button "Save Order" save-order-clicked)
+       (button "Abandon Order" abandon-order-clicked)])
+   ])
+
+
+
+(reagent/render-component [main-screen]
+                          (. js/document (getElementById "app")))
+
+(defn on-js-reload []
+  ;; optionally touch your app-state to force rerendering depending on
+  ;; your application
+  ;; (swap! app-state update-in [:__figwheel_counter] inc)
+)