<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.images
  (:require [sablono.core :as html :refer-macros [html]]
            [rum.core :as rum]
            [lentes.core :as l]
            [uxbox.util.i18n :as t :refer (tr)]
            [uxbox.main.state :as st]
            [uxbox.util.rstore :as rs]
            [uxbox.main.library :as library]
            [uxbox.main.data.dashboard :as dd]
            [uxbox.main.data.lightbox :as udl]
            [uxbox.main.data.images :as di]
            [uxbox.main.ui.icons :as i]
            [uxbox.util.mixins :as mx :include-macros true]
            [uxbox.main.ui.lightbox :as lbx]
            [uxbox.main.ui.keyboard :as k]
            [uxbox.main.ui.library-bar :as ui.library-bar]
            [uxbox.main.ui.dashboard.header :refer (header)]
            [uxbox.util.lens :as ul]
            [uxbox.util.dom :as dom]))

;; --- Helpers & Constants

(defn- new-image-lightbox-render
  [own]
  (html
   [:div.lightbox-body
    [:h3 "New image"]
    [:div.row-flex
     [:div.lightbox-big-btn
      [:span.big-svg i/shapes]
      [:span.text "Go to workspace"]]
     [:div.lightbox-big-btn
      {:on-click #(dom/click (dom/get-element-by-class "upload-image-input"))}
      [:span.big-svg.upload i/exit]
      [:span.text "Upload file"]
      [:input.upload-image-input {:style {:display "none"}
                                  :type "file"
                                  :on-change #(rs/emit! (di/create-images nil (dom/get-event-files %)))}]]]
    [:a.close {:href "#"
               :on-click #(do (dom/prevent-default %)
                              (udl/close!))}
     i/close]]))

(def ^:private new-image-lightbox
  (mx/component
   {:render new-image-lightbox-render
    :name "new-image-lightbox"}))

(defmethod lbx/render-lightbox :new-image
  [_]
  (new-image-lightbox))
<commit_msg>Add the ability to import images to workspace.<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.images
  (:require [lentes.core :as l]
            [uxbox.util.i18n :as t :refer (tr)]
            [uxbox.util.rstore :as rs]
            [uxbox.util.mixins :as mx :include-macros true]
            [uxbox.util.data :as data :refer (read-string)]
            [uxbox.util.dom :as dom]
            [uxbox.main.state :as st]
            [uxbox.main.data.lightbox :as udl]
            [uxbox.main.data.images :as udi]
            [uxbox.main.data.workspace :as udw]
            [uxbox.main.data.shapes :as uds]
            [uxbox.main.ui.icons :as i]
            [uxbox.main.ui.lightbox :as lbx]))

(def ^:private collections-ref
  (-> (l/key :image-colls-by-id)
      (l/derive st/state)))

(def ^:private images-ref
  (-> (l/key :images-by-id)
      (l/derive st/state)))

(mx/defcs import-image-lightbox
  {:mixins [mx/static]}
  [own]
  (letfn [(on-upload-click [event]
            (let [input (mx/ref-node own "input")]
              (dom/click input)))
          (on-uploaded [images]
            (println "on-uploaded" images)
            (udl/close!))
          (on-files-selected [event]
            (let [files (dom/get-event-files event)]
              (rs/emit! (udi/create-images nil files on-uploaded))))
          (on-select-from-library [event]
            (dom/prevent-default event)
            (udl/open! :import-image-from-collections))
          (on-close [event]
            (dom/prevent-default event)
            (udl/close!))]
    [:div.lightbox-body
     [:h3 "New image"]
     [:div.row-flex
      [:div.lightbox-big-btn
       {:on-click on-select-from-library}
       [:span.big-svg i/image]
       [:span.text "Select from library"]]
      [:div.lightbox-big-btn
       {:on-click on-upload-click}
       [:span.big-svg.upload i/exit]
       [:span.text "Upload file"]
       [:input.upload-image-input
        {:style {:display "none"}
         :type "file"
         :ref "input"
         :on-change on-files-selected}]]]
     [:a.close {:href "#" :on-click on-close} i/close]]))

(mx/defc image-item
  {:mixins [mx/static]}
  [{:keys [thumbnail name id width height] :as image}]
  (letfn [(on-double-click [event]
            (let [shape {:type :image
                         :name name
                         :metadata {:width width
                                    :height height}
                         :image id}]
              (rs/emit! (udw/select-for-drawing shape))
              (udl/close!)))]
    [:div.library-item {:key (str id)
                        :on-double-click on-double-click}
     [:div.library-item-th
      {:style {:background-image (str "url('" thumbnail "')")}}]
     [:span name]]))

(mx/defc image-collection
  {:mixins [mx/static]}
  [images]
  [:div.library-content
   (for [image images]
     (-> (image-item image)
         (mx/with-key (str (:id image)))))])

(defn will-mount
  [own]
  (let [local (:rum/local own)]
    (rs/emit! (udi/fetch-collections))
    (rs/emit! (udi/fetch-images nil))
    (add-watch local ::key (fn [_ _ _ v]
                             (rs/emit! (udi/fetch-images (:id v)))))
    own))

(defn will-unmount
  [own]
  (let [local (:rum/local own)]
    (remove-watch local ::key)
    own))

(mx/defcs image-collections-lightbox
  {:mixins [mx/reactive (mx/local)]
   :will-mount will-mount
   :will-unmount will-unmount}
  [own]
  (let [local (:rum/local own)
        id (:id @local)
        type (:type @local :own)
        own? (= type :own)
        builtin? (= type :builtin)
        colls (mx/react collections-ref)
        colls (->> (vals colls)
                   (filter #(= type (:type %)))
                   (sort-by :name))
        id (if (and (nil? id) builtin?)
             (:id (first colls) ::no-value)
             id)
        images (mx/react images-ref)
        images (->> (vals images)
                    (filter #(= id (:collection %))))]
    (letfn [(on-close [event]
              (dom/prevent-default event)
              (udl/close!))
            (select-type [event type]
              (swap! local assoc :type type))
            (on-coll-change [event]
              (let [value (dom/event->value event)
                    value (read-string value)]
                (swap! local assoc :id value)))]
      [:div.lightbox-body.big-lightbox
       [:h3 "Import image from library"]
       [:div.import-img-library
        [:div.library-actions
         [:ul.toggle-library
          [:li.your-images {:class (when own? "current")
                            :on-click #(select-type % :own)}
           "YOUR IMAGES"]
          [:li.standard {:class (when builtin? "current")
                         :on-click #(select-type % :builtin)}
           "IMAGES STORE"]]
         [:select.input-select {:on-change on-coll-change}
          (when own?
            [:option {:value (pr-str nil)} "Storage"])
          (for [coll colls
                :let [id (:id coll)
                      name (:name coll)]]
            [:option {:key (str id) :value (pr-str id)} name])]]
        (image-collection images)]
       [:a.close {:href "#" :on-click on-close} i/close]])))

(defmethod lbx/render-lightbox :import-image
  [_]
  (import-image-lightbox))


(defmethod lbx/render-lightbox :import-image-from-collections
  [_]
  (image-collections-lightbox))

{'fraction_non_alphanumeric': 0.16090920765378194, 'fraction_numerical': 0.006035700526518557, 'mean_word_length': 1.8706229266494656, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1069446', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2578, 'n_words': 612}