<commit_before>(ns dato.debug.components.core
  (:require [cljs.js :as cljs]
            [cljsjs.codemirror :as cm]
            [cljsjs.codemirror.mode.clojure :as cm-clj]
            [cljs.pprint :as pp]
            [cljs.reader :as reader]
            [clojure.string :as string]
            [dato.lib.core :as dato]
            [dato.db.utils :as dsu]
            [dato.debug.components.keyboard-listener :as keyboard]
            [garden.core :as gc :refer [css]]
            [garden.selectors :as gs]
            [garden.units :as gu :refer [px pt]]
            [goog.dom.classes :as gclass]
            [om.core :as om :include-macros true]
            [om-tools.dom :as dom])
  (:import [goog.net XhrIo]))

(def default-st
  (cljs/empty-state))

(defn get-file [lib cb]
  (let [{:keys [path macros]} lib]
    (.send XhrIo (str "/_source" "?path=" path (when macros "&macros=true"))
           (fn [e]
             (cb (js/JSON.parse (.. e -target getResponseText)))))))

(defn load [lib cb]
  (js/console.log "lib: " lib)
  (get-file lib
    (fn [file-info]
      (cb {:lang   (keyword (aget file-info "lang"))
           :source (aget file-info "src")}))))

(defn eval
  ([src]
   (eval src (fn [{:keys [error value]}]
               (js/console.log error value))))
  ([src cb]
   (binding []
     (cljs/eval-str default-st src 'dato.debug
                    {:load       load
                     :eval       cljs/js-eval
                     :ns         'dato.debug
                     :source-map true}
                    cb))))

(defn eval-expr
  ([src]
   (eval src (fn [{:keys [error value]}]
               (js/console.log error value))))
  ([src cb]
   (binding []
     (cljs/eval-str default-st src 'dato.debug
                    {:load       load
                     :eval       cljs/js-eval
                     :context    :expr
                     :ns         'dato.debug
                     :source-map true}
                    cb))))

(defn compile-expr
  ([src]
   (eval src (fn [{:keys [error value]}]
               (js/console.log error value))))
  ([src cb]
   (binding []
     (cljs/compile-str default-st src 'dato.debug
                       {:load    load
                        :eval    cljs/js-eval
                        :context :expr
                        :ns      'dato.debug}
                    cb))))

(defn state-watcher-coms [state-watchers]
  (keys (dissoc state-watchers ::count)))

(defn com-owner [state-watchers com-key]
  (get-in state-watchers [com-key :owner]))

(defn com-state [state-watchers com]
  (get-in state-watchers [com :states]))

(defn com-name [state-watchers com]
  (when com
    (get-in state-watchers [com :name])))

(def state-watchers
  (atom {::count 0}))

(defn com-key [com]
  (om/react-id com))

(defn watch-state-did-mount [f]
  (fn []
    (this-as
     this
     (swap! state-watchers
            (fn [s]
              (let [com-id (com-key this)]
                (-> s
                    (update-in [::count] inc)
                    (assoc-in [com-id :name] ((or (aget this "getDisplayName") (fn [] "Unknown"))))
                    (assoc-in [com-id :owner] this)))))
     (.call f this))))

(defn watch-state-did-update [f]
  (fn [prev-props prev-state]
    (this-as
     this
     (swap! state-watchers
            (fn [s]
              (let [com-id (com-key this)]
                (-> s
                    (assoc-in [com-id :states :dato] (dato/get-state this))
                    (assoc-in [com-id :states :om] (om/get-state this))))))
     (.call f this prev-props prev-state))))

(defn watch-state-will-unmount [f]
  (fn []
    (this-as
     this
     (swap! state-watchers
            (fn [s]
              (let [com-id (com-key this)]
                (-> s
                    (dissoc com-id)))))
     (.call f this))))

(defn wrap-watch-state-methods [methods]
  (-> methods
      (update-in [:componentDidMount] watch-state-did-mount)
      (update-in [:componentDidUpdate] watch-state-did-update)
      (update-in [:componentWillUnmount] watch-state-will-unmount)))

(def watch-state-methods
  (om/specify-state-methods!
   (-> om/pure-methods
       (wrap-watch-state-methods)
       (clj->js))))

(defn highlight-node! [node]
  (gclass/add node "dato-inspected"))

(defn unhighlight-node! [node]
  (gclass/remove node "dato-inspected"))

(def debugger-styles
  [[:.debugger-container {:position "fixed"
                          :left     (px 0)
                          :bottom   (px 0)
                          :width    "100%"
                          :height   "50%"}
    [:.dato-debugger {:background-color "#eee"
                      :height "100%"}
     [:.selected-item {:font-weight "bolder"}]
     [:.dato-selector {:cursor "pointer"}]
     [:.debugger-head
      [:button {:border-width  (px 1)
                :border-color  "#999"
                :border-style  "solid"
                :border-radius (px 2)
                :padding       (px 8)
                :margin-left   (px 4)
                :cursor        "pointer"}
       [:&.selected {:border-width  (px 2)
                     :border-radius (px 4)
                     :font-weight   "bolder"}]]]
     [:.dato-debugger-query
      [:.query-inspector {:width "80%"
                         :float "right"}]
      [:.query-listing {:width "20%"}]
      [:.code-editor {:width  "20%"
                      :height 50}]]
     [:.dato-debugger-components
      [:.component-listing {:float "right"
                            :width "50%"}]]<commit_msg>Bump the z-index on devtools<commit_after>(ns dato.debug.components.core
  (:require [cljs.js :as cljs]
            [cljsjs.codemirror :as cm]
            [cljsjs.codemirror.mode.clojure :as cm-clj]
            [cljs.pprint :as pp]
            [cljs.reader :as reader]
            [clojure.string :as string]
            [dato.lib.core :as dato]
            [dato.db.utils :as dsu]
            [dato.debug.components.keyboard-listener :as keyboard]
            [garden.core :as gc :refer [css]]
            [garden.selectors :as gs]
            [garden.units :as gu :refer [px pt]]
            [goog.dom.classes :as gclass]
            [om.core :as om :include-macros true]
            [om-tools.dom :as dom])
  (:import [goog.net XhrIo]))

(def default-st
  (cljs/empty-state))

(defn get-file [lib cb]
  (let [{:keys [path macros]} lib]
    (.send XhrIo (str "/_source" "?path=" path (when macros "&macros=true"))
           (fn [e]
             (cb (js/JSON.parse (.. e -target getResponseText)))))))

(defn load [lib cb]
  (js/console.log "lib: " lib)
  (get-file lib
    (fn [file-info]
      (cb {:lang   (keyword (aget file-info "lang"))
           :source (aget file-info "src")}))))

(defn eval
  ([src]
   (eval src (fn [{:keys [error value]}]
               (js/console.log error value))))
  ([src cb]
   (binding []
     (cljs/eval-str default-st src 'dato.debug
                    {:load       load
                     :eval       cljs/js-eval
                     :ns         'dato.debug
                     :source-map true}
                    cb))))

(defn eval-expr
  ([src]
   (eval src (fn [{:keys [error value]}]
               (js/console.log error value))))
  ([src cb]
   (binding []
     (cljs/eval-str default-st src 'dato.debug
                    {:load       load
                     :eval       cljs/js-eval
                     :context    :expr
                     :ns         'dato.debug
                     :source-map true}
                    cb))))

(defn compile-expr
  ([src]
   (eval src (fn [{:keys [error value]}]
               (js/console.log error value))))
  ([src cb]
   (binding []
     (cljs/compile-str default-st src 'dato.debug
                       {:load    load
                        :eval    cljs/js-eval
                        :context :expr
                        :ns      'dato.debug}
                    cb))))

(defn state-watcher-coms [state-watchers]
  (keys (dissoc state-watchers ::count)))

(defn com-owner [state-watchers com-key]
  (get-in state-watchers [com-key :owner]))

(defn com-state [state-watchers com]
  (get-in state-watchers [com :states]))

(defn com-name [state-watchers com]
  (when com
    (get-in state-watchers [com :name])))

(def state-watchers
  (atom {::count 0}))

(defn com-key [com]
  (om/react-id com))

(defn watch-state-did-mount [f]
  (fn []
    (this-as
     this
     (swap! state-watchers
            (fn [s]
              (let [com-id (com-key this)]
                (-> s
                    (update-in [::count] inc)
                    (assoc-in [com-id :name] ((or (aget this "getDisplayName") (fn [] "Unknown"))))
                    (assoc-in [com-id :owner] this)))))
     (.call f this))))

(defn watch-state-did-update [f]
  (fn [prev-props prev-state]
    (this-as
     this
     (swap! state-watchers
            (fn [s]
              (let [com-id (com-key this)]
                (-> s
                    (assoc-in [com-id :states :dato] (dato/get-state this))
                    (assoc-in [com-id :states :om] (om/get-state this))))))
     (.call f this prev-props prev-state))))

(defn watch-state-will-unmount [f]
  (fn []
    (this-as
     this
     (swap! state-watchers
            (fn [s]
              (let [com-id (com-key this)]
                (-> s
                    (dissoc com-id)))))
     (.call f this))))

(defn wrap-watch-state-methods [methods]
  (-> methods
      (update-in [:componentDidMount] watch-state-did-mount)
      (update-in [:componentDidUpdate] watch-state-did-update)
      (update-in [:componentWillUnmount] watch-state-will-unmount)))

(def watch-state-methods
  (om/specify-state-methods!
   (-> om/pure-methods
       (wrap-watch-state-methods)
       (clj->js))))

(defn highlight-node! [node]
  (gclass/add node "dato-inspected"))

(defn unhighlight-node! [node]
  (gclass/remove node "dato-inspected"))

(def debugger-styles
  [[:.debugger-container {:position "fixed"
                          :left     (px 0)
                          :bottom   (px 0)
                          :width    "100%"
                          :height   "50%"
                          :z-index  1501}
    [:.dato-debugger {:background-color "#eee"
                      :height "100%"}
     [:.selected-item {:font-weight "bolder"}]
     [:.dato-selector {:cursor "pointer"}]
     [:.debugger-head
      [:button {:border-width  (px 1)
                :border-color  "#999"
                :border-style  "solid"
                :border-radius (px 2)
                :padding       (px 8)
                :margin-left   (px 4)
                :cursor        "pointer"}
       [:&.selected {:border-width  (px 2)
                     :border-radius (px 4)
                     :font-weight   "bolder"}]]]
     [:.dato-debugger-query
      [:.query-inspector {:width "80%"
                         :float "right"}]
      [:.query-listing {:width "20%"}]
      [:.code-editor {:width  "20%"
                      :height 50}]]
     [:.dato-debugger-components
      [:.component-listing {:float "right"
                            :width "50%"}]]
{'fraction_non_alphanumeric': 0.15321710999281093, 'fraction_numerical': 0.005751258087706686, 'mean_word_length': 1.5065315315315315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6527592', 'n_tokens_mistral': 3806, 'n_tokens_neox': 3579, 'n_words': 865}