Linkify alert annotations (#946)

* Make links in annotations clickable

* Update test script and bindata.go

* Add target = _blank
This commit is contained in:
Andrey Kuzmin 2017-08-13 19:48:36 +02:00 committed by Max Inden
parent 09bc5dd8e5
commit 250bd35c97
6 changed files with 87 additions and 5 deletions

View File

@ -18,7 +18,8 @@ alerts1='[
},
"annotations": {
"info": "The disk sda2 is running full",
"summary": "please check the instance example1"
"summary": "please check the instance example1",
"runbook": "the following link http://test-url should be clickable"
}
},
{

View File

@ -1,4 +1,4 @@
module Utils.String exposing (capitalizeFirst)
module Utils.String exposing (capitalizeFirst, linkify)
import String
import Char
@ -12,3 +12,46 @@ capitalizeFirst string =
Just ( char, rest ) ->
String.cons (Char.toUpper char) rest
linkify : String -> List (Result String String)
linkify string =
List.reverse (linkifyHelp (String.words string) [])
linkifyHelp : List String -> List (Result String String) -> List (Result String String)
linkifyHelp words linkified =
case words of
[] ->
linkified
word :: restWords ->
if isUrl word then
case linkified of
(Err lastWord) :: restLinkified ->
-- append space to last word
linkifyHelp restWords (Ok word :: Err (lastWord ++ " ") :: restLinkified)
(Ok lastWord) :: restLinkified ->
-- insert space between two links
linkifyHelp restWords (Ok word :: Err " " :: linkified)
_ ->
linkifyHelp restWords (Ok word :: linkified)
else
case linkified of
(Err lastWord) :: restLinkified ->
-- concatenate with last word
linkifyHelp restWords (Err (lastWord ++ " " ++ word) :: restLinkified)
(Ok lastWord) :: restLinkified ->
-- insert space after the link
linkifyHelp restWords (Err (" " ++ word) :: linkified)
_ ->
linkifyHelp restWords (Err word :: linkified)
isUrl : String -> Bool
isUrl =
flip String.startsWith >> (flip List.any) [ "http://", "https://" ]

View File

@ -33,6 +33,20 @@ labelButton maybeMsg labelText =
[ span [ class "text-muted" ] [ text labelText ] ]
linkifyText : String -> List (Html msg)
linkifyText str =
List.map
(\result ->
case result of
Ok link ->
a [ href link, target "_blank" ] [ text link ]
Err txt ->
text txt
)
(Utils.String.linkify str)
iconButtonMsg : String -> String -> msg -> Html msg
iconButtonMsg classString icon msg =
a [ class classString, onClick msg ]

View File

@ -9,6 +9,7 @@ import Utils.Date
import Views.FilterBar.Types as FilterBarTypes
import Views.AlertList.Types exposing (AlertListMsg(MsgForFilterBar, SetActive))
import Utils.Filter
import Utils.Views
view : List ( String, String ) -> Maybe String -> Alert -> Html Msg
@ -82,7 +83,7 @@ annotation : ( String, String ) -> Html Msg
annotation ( key, value ) =
tr []
[ th [ class "text-nowrap" ] [ text (key ++ ":") ]
, td [ class "w-100" ] [ text value ]
, td [ class "w-100" ] (Utils.Views.linkifyText value)
]

View File

@ -0,0 +1,23 @@
module StringUtils exposing (testLinkify)
import Utils.String exposing (linkify)
import Test exposing (..)
import Expect
testLinkify : Test
testLinkify =
describe "linkify"
[ test "should linkify a url in the middle" <|
\() ->
Expect.equal (linkify "word1 http://url word2")
[ Err "word1 ", Ok "http://url", Err " word2" ]
, test "should linkify a url in the beginning" <|
\() ->
Expect.equal (linkify "http://url word1 word2")
[ Ok "http://url", Err " word1 word2" ]
, test "should linkify a url in the end" <|
\() ->
Expect.equal (linkify "word1 word2 http://url")
[ Err "word1 word2 ", Ok "http://url" ]
]

File diff suppressed because one or more lines are too long