<commit_before>(ns manager-web.core
  (:require-macros [cljs.core.async.macros :refer [go go-loop]]
                   [manager-web.core-macros :refer [make-grid]])
  (:require [cljs.core.async :as async :refer [<! >! put! chan alts!]]
            [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]
            [om-sync.core :refer [om-sync]]
            [om-sync.util :refer [tx-tag edn-xhr]]
            [sablono.core :as html :refer-macros [html]]
            [om-bootstrap.button :as btn]
            [om-bootstrap.random :as rnd]
            [om-bootstrap.grid :as grid]
            [om-bootstrap.panel :as panel]
            [om-bootstrap.input :as inp]
            [taoensso.sente :as sente :refer [cb-success?]]
;            [cljs.reader :as reader]
;            [goog.events :as events]
            )
  (:import
;   [goog.net XhrIo]
;   goog.net.EventType
;   [goog.events EventType]
   ))

(enable-console-print!)

(defonce app-state (atom {:text "Hello Chestnut!"
                          :view {:selected :table}
                          :data [{:value 24000 :timestamp "2015-07-01"}
                                 {:value 10000 :timestamp "2015-07-20"}]
                          :services {}
                          :server-state {:input1 {:value 123}}}))

(defonce man-app-state (atom {:services [{:id 111 :enabled false}
                                         {:id 222 :enabled true}
                                         {:id 333 :enabled true}
                                         {:id 444 :enabled false}
                                         {:id 555 :enabled true}
                                         {:id 666 :enabled true}
                                         {:id 777 :enabled false}]}))

;;
;; ==========================
;;

(let [{:keys [chsk ch-recv send-fn state]}
      (sente/make-channel-socket! "/chsk" {:type :auto})]
  (def chsk chsk)
  (def ch-chsk ch-recv)
  (def chsk-send! send-fn)
  (def chsk-state state))

(comment (go-loop [d (<! ch-chsk)]
   (println "got message!")
   (println d)
   (recur (<! ch-chsk))))

;;
;; ==========================
;;

(def sync-ch (chan))

(defn editable-text-input
  [state owner {:keys [key label placeholder help]}]
  (reify
    om/IRender
    (render [_]
      (inp/input {:type "text"
                  :value (key state)
                  :label label
                  :placeholder placeholder
                  :help help
                  :onChange (fn [e])
                  :onBlur (fn [e]
                            (let [newval (.. e -target -value)]
                              (put! sync-ch (str "a message: " newval))
                              (om/transact! state key (fn [] newval))))}))))

(defn synced-view
  [state owner {:keys [view channel] :as opts}]
  (reify
    om/IWillMount
    (will-mount [_]
      (go-loop [msg (<! channel)]
        (chsk-send! [:sync/view {:data "aaa"}]
                    10000
                    (fn [d]
                      (println "sente reply")
                      (println d)))
        (recur (<! channel))))
    om/IRender
    (render [_]
      (om/build view state {:opts opts}))))

(defn grid-view
  [state owner]
  (reify
    om/IRender
    (render [_]
      (grid/grid {}
                 (grid/row {}
                           (grid/col {:xs 12 :md 8}
                                     "11111"
                                     (om/build editable-text-input
                                               (-> state :server-state :input1)
                                               {:opts {:key :value
                                                       :label "text1"
                                                       :placeholder "placeholder1"
                                                       :help "help1"}})
                                     (om/build editable-text-input
                                               (-> state :server-state :input1)
                                               {:opts {:key :value
                                                       :label "text2"
                                                       :placeholder "placeholder2"
                                                       :help "help2"}})
                                     )
                           (grid/col {:xs 12 :md 4}
                                     "22222"
                                     (om/build synced-view
                                               (-> state :server-state :input1)
                                               {:opts {:view editable-text-input
                                                       :channel sync-ch
                                                       :key :value
                                                       :label "synced1"
                                                       :placeholder "placeholder3"
                                                       :help "help3"}})
                                     ))))))

(defn add-service
  [state id enabled]
  (om/transact! state :services #(conj % {:id id :enabled enabled})))

(defn man-actions-view
  [state owner]
  (reify
    om/IInitState
    (init-state [_]
      {:aaa 111})
    om/IRender
    (render [_]
      (btn/dropdown {:bs-style "primary"
                     :title "Add service"}
                    (btn/menu-item {:key :nginx
                                    :on-select #(add-service state 888 false)}
                                   "nginx")
                    (btn/menu-item {:key :apache
                                    :on-select #(add-service state 999 false)}
                                   "apache")
                    (btn/menu-item {:key :mysql
                                    :on-select #(add-service state 1111 false)}
                                   "mysql")
                    ))))

(defn man-service-view
  [service owner]
  (reify
    om/IRender
    (render [_]
      (panel/panel {:header (:id service)
                    :bs-style (if (:enabled service)
                                "success"
                                "warning")}
                   "service"))))

(defn man-services-view<commit_msg>added toggling enabled state of service<commit_after>(ns manager-web.core
  (:require-macros [cljs.core.async.macros :refer [go go-loop]]
                   [manager-web.core-macros :refer [make-grid]])
  (:require [cljs.core.async :as async :refer [<! >! put! chan alts!]]
            [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]
            [om-sync.core :refer [om-sync]]
            [om-sync.util :refer [tx-tag edn-xhr]]
            [sablono.core :as html :refer-macros [html]]
            [om-bootstrap.button :as btn]
            [om-bootstrap.random :as rnd]
            [om-bootstrap.grid :as grid]
            [om-bootstrap.panel :as panel]
            [om-bootstrap.input :as inp]
            [taoensso.sente :as sente :refer [cb-success?]]
;            [cljs.reader :as reader]
;            [goog.events :as events]
            )
  (:import
;   [goog.net XhrIo]
;   goog.net.EventType
;   [goog.events EventType]
   ))

(enable-console-print!)

(defonce app-state (atom {:text "Hello Chestnut!"
                          :view {:selected :table}
                          :data [{:value 24000 :timestamp "2015-07-01"}
                                 {:value 10000 :timestamp "2015-07-20"}]
                          :services {}
                          :server-state {:input1 {:value 123}}}))

(defonce man-app-state (atom {:services [{:id 111 :enabled false}
                                         {:id 222 :enabled true}
                                         {:id 333 :enabled true}
                                         {:id 444 :enabled false}
                                         {:id 555 :enabled true}
                                         {:id 666 :enabled true}
                                         {:id 777 :enabled false}]}))

;;
;; ==========================
;;

(let [{:keys [chsk ch-recv send-fn state]}
      (sente/make-channel-socket! "/chsk" {:type :auto})]
  (def chsk chsk)
  (def ch-chsk ch-recv)
  (def chsk-send! send-fn)
  (def chsk-state state))

(comment (go-loop [d (<! ch-chsk)]
   (println "got message!")
   (println d)
   (recur (<! ch-chsk))))

;;
;; ==========================
;;

(def sync-ch (chan))

(defn editable-text-input
  [state owner {:keys [key label placeholder help]}]
  (reify
    om/IRender
    (render [_]
      (inp/input {:type "text"
                  :value (key state)
                  :label label
                  :placeholder placeholder
                  :help help
                  :onChange (fn [e])
                  :onBlur (fn [e]
                            (let [newval (.. e -target -value)]
                              (put! sync-ch (str "a message: " newval))
                              (om/transact! state key (fn [] newval))))}))))

(defn synced-view
  [state owner {:keys [view channel] :as opts}]
  (reify
    om/IWillMount
    (will-mount [_]
      (go-loop [msg (<! channel)]
        (chsk-send! [:sync/view {:data "aaa"}]
                    10000
                    (fn [d]
                      (println "sente reply")
                      (println d)))
        (recur (<! channel))))
    om/IRender
    (render [_]
      (om/build view state {:opts opts}))))

(defn grid-view
  [state owner]
  (reify
    om/IRender
    (render [_]
      (grid/grid {}
                 (grid/row {}
                           (grid/col {:xs 12 :md 8}
                                     "11111"
                                     (om/build editable-text-input
                                               (-> state :server-state :input1)
                                               {:opts {:key :value
                                                       :label "text1"
                                                       :placeholder "placeholder1"
                                                       :help "help1"}})
                                     (om/build editable-text-input
                                               (-> state :server-state :input1)
                                               {:opts {:key :value
                                                       :label "text2"
                                                       :placeholder "placeholder2"
                                                       :help "help2"}})
                                     )
                           (grid/col {:xs 12 :md 4}
                                     "22222"
                                     (om/build synced-view
                                               (-> state :server-state :input1)
                                               {:opts {:view editable-text-input
                                                       :channel sync-ch
                                                       :key :value
                                                       :label "synced1"
                                                       :placeholder "placeholder3"
                                                       :help "help3"}})
                                     ))))))

(defn add-service
  [services id enabled]
  (om/transact! services :services #(conj % {:id id :enabled enabled})))

(defn toggle-enabled
  [service]
  (om/transact! service :enabled not))

(defn man-actions-view
  [state owner]
  (reify
    om/IInitState
    (init-state [_]
      {:aaa 111})
    om/IRender
    (render [_]
      (btn/dropdown {:bs-style "primary"
                     :title "Add service"}
                    (btn/menu-item {:key :nginx
                                    :on-select #(add-service state 888 false)}
                                   "nginx")
                    (btn/menu-item {:key :apache
                                    :on-select #(add-service state 999 false)}
                                   "apache")
                    (btn/menu-item {:key :mysql
                                    :on-select #(add-service state 1111 false)}
                                   "mysql")
                    ))))

(defn man-service-view
  [service owner]
  (reify
    om/IRender
    (render [_]
      (panel/panel {:header (:id service)
                    :bs-style (if (:enabled service)
                                "success"
                                "warning")
                    :on-click (fn [e]
                                (toggle-enabled service))}
                   "service"))))

(defn man-services-view
{'fraction_non_alphanumeric': 0.12221610136145432, 'fraction_numerical': 0.015267175572519083, 'mean_word_length': 0.9368998628257887, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 120, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6487606', 'n_tokens_mistral': 3391, 'n_tokens_neox': 3077, 'n_words': 796}