Commit 9c7cea98 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[iframe] improve searx support

Related to #179
parent 3d865bb1
......@@ -11,14 +11,15 @@ import Data.Nullable (Nullable)
import Data.String (toLower)
import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\))
import Gargantext.Components.Forest.Tree.Node.Action.Search.Types (DataField(..), Search, isIsTex_Advanced)
import Gargantext.Prelude (discard, identity, pure, unit, ($), (<>), (==), class Show, show)
import Gargantext.Utils.Reactix as R2
import Reactix as R
import Reactix.DOM.HTML as H
import URI.Extra.QueryPairs as NQP
import URI.Query as Query
import Gargantext.Components.Forest.Tree.Node.Action.Search.Types (DataField(..), Search, isIsTex_Advanced)
import Gargantext.Prelude (discard, identity, pure, unit, ($), (<>), (==), class Show, show)
import Gargantext.Utils.Reactix as R2
--------------------
data FrameSource = Istex | Searx
......@@ -30,59 +31,93 @@ instance showFrameSource :: Show FrameSource where
--------------------
-- | Iframes
searchIframes :: R.State Search
-> R.Ref (Nullable DOM.Element)
-> R.Element
searchIframes search@(search' /\ _) iframeRef =
if isIsTex_Advanced search'.datafield
then divIframe Istex search iframeRef
else
if Just Web == search'.datafield
then divIframe Searx search iframeRef
else H.div {} []
divIframe :: FrameSource
-> R.State Search
-> R.Ref (Nullable DOM.Element)
-> R.Element
divIframe frameSource search@(search' /\ _) iframeRef =
H.div { className: "frame-search panel panel-default" }
[ iframeWith (frameUrl frameSource)search iframeRef ]
frameUrl :: FrameSource -> String
frameUrl frameSource = frameUrl' (toLower $ show frameSource)
type SearchIFramesProps = (
iframeRef :: R.Ref (Nullable DOM.Element)
, search :: R.State Search
)
searchIframes :: Record SearchIFramesProps -> R.Element
searchIframes props = R.createElement searchIframesCpt props []
searchIframesCpt :: R.Component SearchIFramesProps
searchIframesCpt = R.hooksComponent "G.C.F.T.N.A.S.F.searchIframes" cpt
where
frameUrl' s = "https://" <> s <> ".frame.gargantext.org"
iframeWith :: String
-> R.State Search
-> R.Ref (Nullable DOM.Element)
-> R.Element
iframeWith url (search /\ setSearch) iframeRef =
H.iframe { src: isTexTermUrl search.term
, width: "100%"
, height: "100%"
, ref: iframeRef
, on: { load: \_ -> do
addEventListener window "message" (changeSearchOnMessage url)
R2.postMessage iframeRef search.term
}
} []
cpt { iframeRef, search: search@(search' /\ _) } _ = do
pure $ if isIsTex_Advanced search'.datafield
then divIframe { frameSource: Istex, iframeRef, search }
else
if Just Web == search'.datafield
then divIframe { frameSource: Searx, iframeRef, search }
else H.div {} []
type IFrameProps = (
frameSource :: FrameSource
, iframeRef :: R.Ref (Nullable DOM.Element)
, search :: R.State Search
)
divIframe :: Record IFrameProps -> R.Element
divIframe props = R.createElement divIframeCpt props []
divIframeCpt :: R.Component IFrameProps
divIframeCpt = R.hooksComponent "G.C.F.T.N.A.S.F.divIframe" cpt
where
changeSearchOnMessage :: String -> Callback MessageEvent
changeSearchOnMessage url' =
callback $ \m -> if R2.getMessageOrigin m == url'
then do
let {url'', term} = R2.getMessageData m
setSearch $ _ {url = url'', term = term}
else
pure unit
isTexTermUrl term = url <> query
cpt { frameSource, iframeRef, search: search@(search' /\ _) } _ = do
pure $ H.div { className: "frame-search panel panel-default" }
[ iframeWith { frameSource, iframeRef, search } ]
frameUrl :: FrameSource -> String
frameUrl Istex = "https://istex.frame.gargantext.org"
frameUrl Searx = "http://192.168.1.4:8080"
iframeWith :: Record IFrameProps -> R.Element
iframeWith props = R.createElement iframeWithCpt props []
iframeWithCpt :: R.Component IFrameProps
iframeWithCpt = R.hooksComponent "G.C.F.T.N.A.S.F.iframeWith" cpt
where
cpt { frameSource, iframeRef, search: (search /\ setSearch) } _ =
pure $ H.iframe { src: src frameSource search.term
, width: "100%"
, height: "100%"
, ref: iframeRef
, on: { load: \_ -> do
addEventListener window "message" (changeSearchOnMessage url)
R2.postMessage iframeRef search.term
}
} []
where
query = Query.print $ NQP.print identity identity qp
url :: String
url = frameUrl frameSource
changeSearchOnMessage :: String -> Callback MessageEvent
changeSearchOnMessage url' =
callback $ \m -> if R2.getMessageOrigin m == url' then do
let {url'', term} = R2.getMessageData m
setSearch $ _ {url = url'', term = term}
else
pure unit
isTexTermUrl :: String -> String
isTexTermUrl term = url <> query
where
query = Query.print $ NQP.print identity identity qp
qp = NQP.QueryPairs [ Tuple (NQP.keyFromString "query")
(Just (NQP.valueFromString term))
]
qp = NQP.QueryPairs [ Tuple (NQP.keyFromString "query")
(Just (NQP.valueFromString term))
]
searxTermUrl :: String -> String
searxTermUrl term = url <> query
where
query = Query.print $ NQP.print identity identity qp
qp = NQP.QueryPairs [ Tuple (NQP.keyFromString "q")
(Just $ NQP.valueFromString term)
]
src :: FrameSource -> String -> String
src Istex term = isTexTermUrl term
src Searx term = searxTermUrl term
......@@ -23,7 +23,7 @@ searchBar :: Record Props -> R.Element
searchBar props = R.createElement searchBarCpt props []
searchBarCpt :: R.Component Props
searchBarCpt = R.hooksComponent "G.C.Node.SearchBar.searchBar" cpt
searchBarCpt = R.hooksComponent "G.C.F.T.N.A.S.SB.searchBar" cpt
where
cpt {langs, onSearch, search: search@(s /\ _), session} _ = do
--onSearchChange session s
......
......@@ -47,7 +47,7 @@ searchField p = R.createElement searchFieldComponent p []
--searchFieldComponent :: R.Memo Props
--searchFieldComponent = R.memo (R.hooksComponent "SearchField" cpt) eqProps
searchFieldComponent :: R.Component Props
searchFieldComponent = R.hooksComponent "G.C.S.SearchField" cpt
searchFieldComponent = R.hooksComponent "G.C.F.T.N.A.S.SF.searchField" cpt
where
cpt props@{onSearch, search: search@(s /\ _)} _ = do
iframeRef <- R.useRef null
......@@ -75,7 +75,7 @@ searchFieldComponent = R.hooksComponent "G.C.S.SearchField" cpt
then componentCNRS search
else H.div {} []
, H.div {} [ searchIframes search iframeRef ]
, H.div {} [ searchIframes { iframeRef, search } ]
, if needsLang s.datafield
then langNav search props.langs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment