<commit_before>(ns discuss.components.tooltip
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [cljs.core.async :refer [put! chan <!]]
            [goog.dom :as gdom]
            [goog.events :as events]
            [goog.string :refer [format]]
            [goog.string.format]
            [om.next :as om :refer-macros [defui]]
            [sablono.core :as html :refer-macros [html]]
            [discuss.components.clipboard :as clipboard]
            [discuss.config :as config]
            [discuss.translations :refer [translate]]
            [discuss.utils.common :as lib]
            [discuss.utils.logging :as log]
            [discuss.utils.views :as vlib]))

(defn- get-tooltip
  "Return DOM element of tooltip"
  []
  (gdom/getElement (lib/prefix-name "tooltip")))

(defn show
  "Show tooltip by removing a class."
  []
  (when-let [tooltip (get-tooltip)]
    (lib/add-class tooltip (lib/prefix-name "tooltip-active"))))

(defn hide
  "Hide tooltip by adding a class."
  []
  (when-let [tooltip (get-tooltip)]
    (lib/remove-class tooltip  (lib/prefix-name "tooltip-active"))))

(defn x-position
  "Center tooltip at mouse selection."
  [left twidth ewidth]
  (let [tooltip (get-tooltip)
        tooltip-parent-offset (.-offsetLeft (.-offsetParent tooltip))]
    (- (+ left js/window.scrollX (/ (- ewidth twidth) 2))
       tooltip-parent-offset)))

(defn y-position
  "Move tooltip a bit above the mouse selection."
  [top theight]
  (let [offset 5]
    (+ (- top theight offset) js/window.scrollY)))

(defn calc-position
  "Create a new tooltip at given selection. Creates a rectangle around the selection,
   which has position-properties and which are useful for positioning of the tooltip."
  [tooltip-width tooltip-height]
  (let [selection (.getSelection js/window)
        range (.getRangeAt selection 0)
        rect (.getBoundingClientRect range)
        top (.-top rect)
        left (.-left rect)
        width (.-width rect)
        positioned-top (y-position top tooltip-height)
        positioned-left (x-position left tooltip-width width)]
    (log/fine (format "Tooltip position: top %f, left %f" positioned-top positioned-left))
    [positioned-top positioned-left]))

(defn move-to-selection
  "Sets CSS position of tooltip and move it to the mouse selection."
  ([[top left]]
   (let [tooltip (get-tooltip)]
     (set! (.. tooltip -style -top) (str top "px"))
     (set! (.. tooltip -style -left) (str left "px"))
     (show)))
  ([]
   (when-let [tooltip (get-tooltip)]
     (move-to-selection (calc-position tooltip.offsetWidth tooltip.offsetHeight)))))


;;;; Include listener for tooltips
(defn- listen
  "Helper function for mouse-click events."
  [el type]
  (let [out (chan)]
    (events/listen el type (fn [e] (put! out e)))
    out))

(defn- save-selected-text
  "Get the users selection and save it."
  []
  (let [selection (str (.getSelection js/window))]
    (if (and (pos? (count selection))
             (not= selection (lib/get-selection)))
      (do (move-to-selection)
          (lib/save-selection! selection))
      (hide))))

(defn track-user-selection
  "Listen to clicks on the websites' text to store it in the app-state."
  []
  (when-let [discuss-text-dom (gdom/getElement (lib/prefix-name "text"))]
    (let [clicks (listen discuss-text-dom "click")]
      (go (while true
            (<! clicks)
            (save-selected-text))))))

;;;; Creating the view
(defui Tooltip
  Object
  (componentDidMount
   [this]
   (track-user-selection))
  (render [this]
          (html [:div#discuss-tooltip
                 [:span.pointer {:onClick (fn [] (clipboard/add-item!) (hide))}
                  (vlib/fa-icon "fa-bookmark-o")
                  (translate :common :save :space)]
                 (vlib/safe-space) " " (vlib/safe-space)

                 [:span.pointer {:onClick (fn [] (lib/change-view! :create/argument) (hide))}
                  (vlib/fa-icon "fa-comment")
                  (translate :tooltip :discuss/start :space)]

                 [:span
                  (vlib/safe-space) " " (vlib/safe-space)
                  [:span.pointer {:onClick #((lib/show-overlay) (hide))}
                   (vlib/fa-icon "fa-comments")
                   (translate :common :show-discuss :space)]]])))
(def tooltip (om/factory Tooltip))<commit_msg>Show discuss when trying to add a new argument<commit_after>(ns discuss.components.tooltip
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [cljs.core.async :refer [put! chan <!]]
            [goog.dom :as gdom]
            [goog.events :as events]
            [goog.string :refer [format]]
            [goog.string.format]
            [om.next :as om :refer-macros [defui]]
            [sablono.core :as html :refer-macros [html]]
            [discuss.components.clipboard :as clipboard]
            [discuss.config :as config]
            [discuss.translations :refer [translate]]
            [discuss.utils.common :as lib]
            [discuss.utils.logging :as log]
            [discuss.utils.views :as vlib]))

(defn- get-tooltip
  "Return DOM element of tooltip"
  []
  (gdom/getElement (lib/prefix-name "tooltip")))

(defn show
  "Show tooltip by removing a class."
  []
  (when-let [tooltip (get-tooltip)]
    (lib/add-class tooltip (lib/prefix-name "tooltip-active"))))

(defn hide
  "Hide tooltip by adding a class."
  []
  (when-let [tooltip (get-tooltip)]
    (lib/remove-class tooltip  (lib/prefix-name "tooltip-active"))))

(defn x-position
  "Center tooltip at mouse selection."
  [left twidth ewidth]
  (let [tooltip (get-tooltip)
        tooltip-parent-offset (.-offsetLeft (.-offsetParent tooltip))]
    (- (+ left js/window.scrollX (/ (- ewidth twidth) 2))
       tooltip-parent-offset)))

(defn y-position
  "Move tooltip a bit above the mouse selection."
  [top theight]
  (let [offset 5]
    (+ (- top theight offset) js/window.scrollY)))

(defn calc-position
  "Create a new tooltip at given selection. Creates a rectangle around the selection,
   which has position-properties and which are useful for positioning of the tooltip."
  [tooltip-width tooltip-height]
  (let [selection (.getSelection js/window)
        range (.getRangeAt selection 0)
        rect (.getBoundingClientRect range)
        top (.-top rect)
        left (.-left rect)
        width (.-width rect)
        positioned-top (y-position top tooltip-height)
        positioned-left (x-position left tooltip-width width)]
    (log/fine (format "Tooltip position: top %f, left %f" positioned-top positioned-left))
    [positioned-top positioned-left]))

(defn move-to-selection
  "Sets CSS position of tooltip and move it to the mouse selection."
  ([[top left]]
   (let [tooltip (get-tooltip)]
     (set! (.. tooltip -style -top) (str top "px"))
     (set! (.. tooltip -style -left) (str left "px"))
     (show)))
  ([]
   (when-let [tooltip (get-tooltip)]
     (move-to-selection (calc-position tooltip.offsetWidth tooltip.offsetHeight)))))


;;;; Include listener for tooltips
(defn- listen
  "Helper function for mouse-click events."
  [el type]
  (let [out (chan)]
    (events/listen el type (fn [e] (put! out e)))
    out))

(defn- save-selected-text
  "Get the users selection and save it."
  []
  (let [selection (str (.getSelection js/window))]
    (if (and (pos? (count selection))
             (not= selection (lib/get-selection)))
      (do (move-to-selection)
          (lib/save-selection! selection))
      (hide))))

(defn track-user-selection
  "Listen to clicks on the websites' text to store it in the app-state."
  []
  (when-let [discuss-text-dom (gdom/getElement (lib/prefix-name "text"))]
    (let [clicks (listen discuss-text-dom "click")]
      (go (while true
            (<! clicks)
            (save-selected-text))))))

;;;; Creating the view
(defui Tooltip
  Object
  (componentDidMount
   [this]
   (track-user-selection))
  (render [this]
          (html [:div#discuss-tooltip
                 [:span.pointer {:onClick #((clipboard/add-item!) (hide))}
                  (vlib/fa-icon "fa-bookmark-o")
                  (translate :common :save :space)]
                 (vlib/safe-space) " " (vlib/safe-space)

                 [:span.pointer {:onClick #((lib/change-view! :create/argument) (lib/show-overlay) (hide))}
                  (vlib/fa-icon "fa-comment")
                  (translate :tooltip :discuss/start :space)]

                 [:span
                  (vlib/safe-space) " " (vlib/safe-space)
                  [:span.pointer {:onClick #((lib/show-overlay) (hide))}
                   (vlib/fa-icon "fa-comments")
                   (translate :common :show-discuss :space)]]])))
(def tooltip (om/factory Tooltip))
{'fraction_non_alphanumeric': 0.14584290307762976, 'fraction_numerical': 0.0006890215893431327, 'mean_word_length': 2.4863891112890313, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2870031', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2691, 'n_words': 764}