<commit_before>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2015-2016 <NAME> <<EMAIL>>
;; Copyright (c) 2015-2016 <NAME> <<EMAIL>>

(ns uxbox.main.ui.workspace.sidebar.sitemap
  (:require [lentes.core :as l]
            [cuerdas.core :as str]
            [potok.core :as ptk]
            [uxbox.main.store :as st]
            [uxbox.main.refs :as refs]
            [uxbox.main.data.projects :as dp]
            [uxbox.main.data.pages :as udp]
            [uxbox.main.data.workspace :as dw]
            [uxbox.main.data.lightbox :as udl]
            [uxbox.main.ui.workspace.sidebar.sitemap-pageform]
            [uxbox.main.ui.lightbox :as lbx]
            [uxbox.builtins.icons :as i]
            [uxbox.util.mixins :as mx :include-macros true]
            [uxbox.util.i18n :refer (tr)]
            [uxbox.util.router :as r]
            [uxbox.util.data :refer (classnames)]
            [uxbox.util.dom.dnd :as dnd]
            [uxbox.util.dom :as dom]))

;; --- Refs

(defn- resolve-pages
  [state]
  (let [project (get-in state [:workspace :project])]
    (->> (vals (:pages state))
         (filter #(= project (:project %)))
         (sort-by #(get-in % [:metadata :order])))))

(def pages-ref
  (-> (l/lens resolve-pages)
      (l/derive st/state)))

;; --- Component

(mx/defcs page-item
  {:mixins [(mx/local) mx/static mx/reactive]}
  [own page total active?]
  (let [local (:rum/local own)
        classes (classnames
                 :selected active?
                 :drag-active (:dragging @local)
                 :drag-top (= :top (:over @local))
                 :drag-bottom (= :bottom (:over @local))
                 :drag-inside (= :middle (:over @local)))]
    (letfn [(on-edit [event]
              (udl/open! :page-form {:page page}))

            (on-navigate [event]
              (st/emit! (dp/go-to (:project page) (:id page))))

            (delete []
              (let [next #(st/emit! (dp/go-to (:project page)))]
                (st/emit! (udp/delete-page (:id page) next))))

            (on-delete [event]
              (dom/prevent-default event)
              (dom/stop-propagation event)
              (udl/open! :confirm {:on-accept delete}))

            (on-drag-start [event]
              (let [target (dom/event->target event)]
                (dnd/set-allowed-effect! event "move")
                (dnd/set-data! event (:id page))
                (dnd/set-image! event target 50 10)
                (swap! local assoc :dragging true)))
            (on-drag-end [event]
              (swap! local assoc :dragging false :over nil))
            (on-drop [event]
              (dom/stop-propagation event)
              (let [id (dnd/get-data event)
                    over (:over @local)]
                (case (:over @local)
                  :top (let [new-order (dec (get-in page [:metadata :order]))]
                         (st/emit! (udp/update-order id new-order))
                         (st/emit! (udp/reorder-pages))
                         (st/emit! (udp/persist-pages)))
                  :bottom (let [new-order (inc (get-in page [:metadata :order]))]
                            (st/emit! (udp/update-order id new-order))
                            (st/emit! (udp/reorder-pages))
                            (st/emit! (udp/persist-pages))))
                (swap! local assoc :dragging false :over nil)))
            (on-drag-over [event]
              (dom/prevent-default event)
              (dnd/set-drop-effect! event "move")
              (let [over (dnd/get-hover-position event false)]
                (swap! local assoc :over over)))
            (on-drag-enter [event]
              (swap! local assoc :over true))
            (on-drag-leave [event]
              (swap! local assoc :over false))]
    [:li {:class (when active? "selected")}
     [:div.element-list-body
      {:class classes
       :style {:opacity (if (:dragging @local)
                          "0.5"
                          "1")}
       :on-click on-navigate
       :on-double-click #(dom/stop-propagation %)
       :on-drag-start on-drag-start
       :on-drag-enter on-drag-enter
       :on-drag-leave on-drag-leave
       :on-drag-over on-drag-over
       :on-drag-end on-drag-end<commit_msg>Hide dragged item on sitemap sidebar widget.<commit_after>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2015-2016 <NAME> <<EMAIL>>
;; Copyright (c) 2015-2016 <NAME> <<EMAIL>>

(ns uxbox.main.ui.workspace.sidebar.sitemap
  (:require [lentes.core :as l]
            [cuerdas.core :as str]
            [potok.core :as ptk]
            [uxbox.main.store :as st]
            [uxbox.main.refs :as refs]
            [uxbox.main.data.projects :as dp]
            [uxbox.main.data.pages :as udp]
            [uxbox.main.data.workspace :as dw]
            [uxbox.main.data.lightbox :as udl]
            [uxbox.main.ui.workspace.sidebar.sitemap-pageform]
            [uxbox.main.ui.lightbox :as lbx]
            [uxbox.builtins.icons :as i]
            [uxbox.util.mixins :as mx :include-macros true]
            [uxbox.util.i18n :refer (tr)]
            [uxbox.util.router :as r]
            [uxbox.util.data :refer (classnames)]
            [uxbox.util.dom.dnd :as dnd]
            [uxbox.util.dom :as dom]))

;; --- Refs

(defn- resolve-pages
  [state]
  (let [project (get-in state [:workspace :project])]
    (->> (vals (:pages state))
         (filter #(= project (:project %)))
         (sort-by #(get-in % [:metadata :order])))))

(def pages-ref
  (-> (l/lens resolve-pages)
      (l/derive st/state)))

;; --- Component

(mx/defcs page-item
  {:mixins [(mx/local) mx/static mx/reactive]}
  [{:keys [rum/local] :as own} page total active?]
  (let [body-classes (classnames
                      :selected active?
                      :drag-active (:dragging @local)
                      :drag-top (= :top (:over @local))
                      :drag-bottom (= :bottom (:over @local))
                      :drag-inside (= :middle (:over @local)))
        li-classes (classnames
                    :selected active?
                    :hide (:dragging @local))]
    (letfn [(on-edit [event]
              (udl/open! :page-form {:page page}))

            (on-navigate [event]
              (st/emit! (dp/go-to (:project page) (:id page))))

            (delete []
              (let [next #(st/emit! (dp/go-to (:project page)))]
                (st/emit! (udp/delete-page (:id page) next))))

            (on-delete [event]
              (dom/prevent-default event)
              (dom/stop-propagation event)
              (udl/open! :confirm {:on-accept delete}))

            (on-drag-start [event]
              (let [target (dom/event->target event)]
                (dnd/set-allowed-effect! event "move")
                (dnd/set-data! event (:id page))
                (dnd/set-image! event target 50 10)
                (swap! local assoc :dragging true)))
            (on-drag-end [event]
              (swap! local assoc :dragging false :over nil))
            (on-drop [event]
              (dom/stop-propagation event)
              (let [id (dnd/get-data event)
                    over (:over @local)]
                (case (:over @local)
                  :top (let [new-order (dec (get-in page [:metadata :order]))]
                         (st/emit! (udp/update-order id new-order))
                         (st/emit! (udp/reorder-pages))
                         (st/emit! (udp/persist-pages)))
                  :bottom (let [new-order (inc (get-in page [:metadata :order]))]
                            (st/emit! (udp/update-order id new-order))
                            (st/emit! (udp/reorder-pages))
                            (st/emit! (udp/persist-pages))))
                (swap! local assoc :dragging false :over nil)))
            (on-drag-over [event]
              (dom/prevent-default event)
              (dnd/set-drop-effect! event "move")
              (let [over (dnd/get-hover-position event false)]
                (swap! local assoc :over over)))
            (on-drag-enter [event]
              (swap! local assoc :over true))
            (on-drag-leave [event]
              (swap! local assoc :over false))]
    [:li {:class li-classes}
     [:div.element-list-body
      {:class body-classes
       :style {:opacity (if (:dragging @local)
                          "0.5"
                          "1")}
       :on-click on-navigate
       :on-double-click #(dom/stop-propagation %)
       :on-drag-start on-drag-start
       :on-drag-enter on-drag-enter
       :on-drag-leave on-drag-leave
       :on-drag-over on-drag-over
       :on-drag-end on-drag-end
{'fraction_non_alphanumeric': 0.14996106352208255, 'fraction_numerical': 0.006452330626321059, 'mean_word_length': 1.5892857142857142, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '4261379', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2793, 'n_words': 698}