Skip to content

Commit

Permalink
Templates: Web service URL button
Browse files Browse the repository at this point in the history
  • Loading branch information
heralden committed Nov 19, 2021
1 parent d3fec5c commit 3eb5ee7
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 4 deletions.
18 changes: 18 additions & 0 deletions less/components/templates.less
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@
margin-bottom: 0;
}
}

.more-actions {
.action-button {
font-weight: 400;
color: @highlight-color;
fill: @highlight-color;
}

.web-service-url-container {
padding: @spacer/4 @spacer/2;

p { padding: 0; }

input {
width: 500px;
}
}
}
}

.view-results {
Expand Down
45 changes: 43 additions & 2 deletions src/cljs/bluegenes/pages/templates/helpers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,49 @@
(distinct))
templates))

; Predicate function used to filter active constraints
;; Generate web service URL for a template query.

(def op-symbol->text
{"=" "eq"
"!=" "ne"
"<" "lt"
"<=" "le"
">" "gt"
">=" "ge"})

(defn ?encode
"Only encodes the string if it's non-nil. This is because
js/encodeURIComponent would return 'null' for nil."
[?s]
(some-> ?s js/encodeURIComponent))

(defn const->query-string [index {:keys [path op value values extraValue] :as _const}]
(let [index (inc index)] ; We start counting from 1.
(-> (str "&constraint" index "=" (?encode path)
"&op" index "=" (?encode (op-symbol->text op op))
(let [v (or value values)
as-value #(str "&value" index "=" (?encode %))]
(if (coll? v)
(apply str (map as-value v))
(as-value v)))
(when (= op "LOOKUP")
(str "&extra" index "=" (?encode extraValue))))
(str/replace "%20" "+"))))

(defn web-service-url [{:keys [root] :as _service} {:keys [name where] :as _template-query}]
(str root
"/service/template/results"
"?name=" (?encode name)
;; The constraints get matched based on their path.
;; I tested that ordering doesn't matter.
(apply str (->> where
(filter #(and (:editable %)
(not= "OFF" (:switched %))))
(map-indexed const->query-string)))
"&format=tab&size=10"))

;; Preparing template for querying.

(def not-disabled-predicate (comp (partial not= "OFF") :switched))

(defn remove-switchedoff-constraints
Expand All @@ -31,4 +73,3 @@

(def prepare-template-query
(comp clean-template-constraints remove-switchedoff-constraints))

7 changes: 7 additions & 0 deletions src/cljs/bluegenes/pages/templates/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,10 @@
:selected-template-service
(fn [db _]
(get-in db [:components :template-chooser :selected-template-service])))

(reg-sub
:template-chooser/web-service-url
:<- [:selected-template]
:<- [:active-service]
(fn [[template service]]
(template-helpers/web-service-url service template)))
40 changes: 38 additions & 2 deletions src/cljs/bluegenes/pages/templates/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[bluegenes.utils :refer [ascii-arrows ascii->svg-arrows]]
[bluegenes.pages.templates.helpers :refer [categories-from-tags]]
[bluegenes.components.top-scroll :as top-scroll]
[bluegenes.route :as route]))
[bluegenes.route :as route]
[bluegenes.components.icons :refer [icon]]))

(defn categories []
(let [categories (subscribe [:template-chooser-categories])
Expand All @@ -33,6 +34,39 @@
(def css-transition-group
(reagent/adapt-react-class js/ReactTransitionGroup.CSSTransitionGroup))

(defn web-service-url []
(let [input-ref* (atom nil)
url @(subscribe [:template-chooser/web-service-url])]
(fn []
[:span.dropdown
[:a.dropdown-toggle.action-button
{:data-toggle "dropdown"
:role "button"}
[icon "file"] "Web service URL"]
[:div.dropdown-menu
[:form {:on-submit #(.preventDefault %)}
[:div.web-service-url-container
[:p "Use the URL below to fetch the first " [:strong "10"] " records for this template from the command line or a script " [:em "(authentication needed for private templates and lists)"] " :"]
[:input.form-control
{:type "text"
:ref (fn [el]
(when el
(reset! input-ref* el)
(.focus el)
(.select el)))
:autoFocus true
:readOnly true
:value url}]
[:button.btn.btn-raised
{:on-click (fn [_]
(when-let [input-el @input-ref*]
(.focus input-el)
(.select input-el)
(try
(ocall js/document :execCommand "copy")
(catch js/Error _))))}
"Copy"]]]]])))

(defn preview-results
"Preview results of template as configured by the user or default config"
[]
Expand Down Expand Up @@ -74,7 +108,9 @@
[:button.btn.btn-default.btn-raised
{:type "button"
:on-click (fn [] (dispatch [:templates/edit-template]))}
"Edit template"])]]))
"Edit template"])]
[:div.more-actions
[web-service-url]]]))

(defn toggle []
(fn [{:keys [status on-change]}]
Expand Down
66 changes: 66 additions & 0 deletions test/cljs/bluegenes/templates_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
(ns bluegenes.templates-test
(:require [cljs.test :refer-macros [deftest is are testing]]
[bluegenes.pages.templates.helpers :as helpers]))

(comment
"Use this to get current template to add more tests."
(require '[re-frame.core :refer [subscribe]])
(select-keys @(subscribe [:selected-template]) [:name :where]))

(deftest web-service-url
(let [service {:root "https://beta.flymine.org/flymine"}]
(are [tmpl url] (= (helpers/web-service-url service tmpl) url)
{:name "Gene_transcriptionFactors",
:where [{:path "Gene.regulatoryRegions", :type "TFBindingSite"}
{:path "Gene",
:op "LOOKUP",
:code "A",
:editable true,
:switchable false,
:switched "LOCKED",
:value "dpp"}
{:path "Gene.organism.name",
:op "=",
:code "C",
:editable true,
:switchable false,
:switched "LOCKED",
:value "Drosophila melanogaster"}]}
"https://beta.flymine.org/flymine/service/template/results?name=Gene_transcriptionFactors&constraint1=Gene&op1=LOOKUP&value1=dpp&extra1=&constraint2=Gene.organism.name&op2=eq&value2=Drosophila+melanogaster&format=tab&size=10"
{:name "Tissue_Flyatlas",
:where [{:path "FlyAtlasResult.tissue.name",
:op "=",
:code "A",
:editable true,
:switchable false,
:switched "LOCKED",
:value "Ovary"}
{:path "FlyAtlasResult.affyCall",
:op "=",
:code "E",
:editable true,
:switchable true,
:switched "ON",
:value "Up"}
{:path "FlyAtlasResult.presentCall",
:op ">=",
:code "C",
:editable true,
:switchable true,
:switched "OFF",
:value "3"}
{:path "FlyAtlasResult.enrichment",
:op ">",
:code "B",
:editable true,
:switchable true,
:switched "OFF",
:value "2.0"}
{:path "FlyAtlasResult.mRNASignal",
:op ">=",
:code "D",
:editable true,
:switchable true,
:switched "OFF",
:value "100.0"}]}
"https://beta.flymine.org/flymine/service/template/results?name=Tissue_Flyatlas&constraint1=FlyAtlasResult.tissue.name&op1=eq&value1=Ovary&constraint2=FlyAtlasResult.affyCall&op2=eq&value2=Up&format=tab&size=10")))

0 comments on commit 3eb5ee7

Please sign in to comment.