tags: $:/tags/ViewToolbar
title: $:/plugins/felixhayashi/tiddlymap/misc/quickConnectButton
description: {{$:/language/Buttons/TiddlyMap/Hint}}
caption: {{$:/plugins/felixhayashi/tiddlymap/icon}} {{$:/language/Buttons/TiddlyMap/Caption}}

\define buttonClass() $(tv-config-toolbar-class)$ $(additional-classes)$

\define nonExistentItem()
<<item>> <span style="color: #9E9E9E">(will be created)</span>
\end

\define noConnectionsMsg()
<tr><td colspan="4">//No connections found!//</td></tr>
\end

\define normalSearchFilter()
[!is[system]!has[draft.of]search:title{$:/temp/quickConnectSearch}sortcs[title]limit[50]]
\end

\define regexSearchFilter()
[!is[system]!has[draft.of]regexp{$:/temp/quickConnectSearch}sortcs[title]limit[50]]
\end

\define showButton(state)
<$button set="$:/temp/tmap/state/popup/quickConnect"
         setTo="$state$" tooltip={{$:/language/Buttons/TiddlyMap/Hint}} 
         aria-label={{$:/language/Buttons/TiddlyMap/Caption}}
         class=<<buttonClass>>>
<$list filter="[<tv-config-toolbar-icons>prefix[yes]]">{{$:/plugins/felixhayashi/tiddlymap/icon}}</$list>
<$list filter="[<tv-config-toolbar-text>prefix[yes]]">
<span class="tc-btn-text"><$text text={{$:/language/Buttons/TiddlyMap/Caption}}/></span>
</$list>
</$button>
\end

\define searchResults()
<td>
  <$button tooltip="Create incoming edge">
    <<tmap "option" "misc.arrows.in">>
    <$action-sendmessage $message="tmap:tm-create-edge"
                         from=<<item>>
                         to=<<currentTiddler>>
                         force="true"
                         label={{$:/temp/quickConnectSearch/type}}
                         view={{$:/temp/quickConnectSearch/view}} />
  </$button>
</td>
<td>
  <$button tooltip="Create outgoing edge">
    <<tmap "option" "misc.arrows.out">>
    <$action-sendmessage $message="tmap:tm-create-edge"
                         from=<<currentTiddler>>
                         to=<<item>>
                         force="true"
                         label={{$:/temp/quickConnectSearch/type}}
                         view={{$:/temp/quickConnectSearch/view}} />
  </$button>
</td>
<td>
  <$list
      filter="[<item>is[tiddler]]"
      emptyMessage=<<nonExistentItem>>>
    <$view tiddler=<<item>> field="title" />
  </$list>
</td>
\end

\define hidePopup()
<$macrocall $name="showButton" state=<<qualify>> />
\end

\define showPopup()
<$set name="additional-classes" value="tmap-active-button">
  <$macrocall $name="showButton" state="" />
</$set>
<$set
    name="searchFilter"
    filter="[field:title[$:/state/tmap/tid-toolbar]has[re-filter]]"
    value=<<regexSearchFilter>>
    emptyValue=<<normalSearchFilter>>>
<div class="tmap-quick-connect tc-reveal tc-popup">
  <div class="tc-drop-down">
    <div class="title">Create connection</div>
    <table class="tmap-quick-connect-search-bar">
      <tr>
        <td>Type:</td>
        <td>
          <$edit-text
              tiddler="$:/temp/quickConnectSearch/type"
              field="text"
              type="text"
              tag="input"
              default="" />
          <$select tiddler="$:/temp/quickConnectSearch/type" default="">
            <option></option>
            <$list filter=<<tmap "option" "selector.allEdgeTypesById">>>
              <option>{{!!title}}</option>
            </$list>
          </$select>
        </td>
      </tr>
      <tr>
        <td>Search:</td>
        <td>
          <$edit-text tiddler="$:/temp/quickConnectSearch" type="text" tag="input" default=""></$edit-text>
          <$checkbox
              tiddler="$:/state/tmap/tid-toolbar"
              field="re-filter"
              checked="1"
              unchecked=""
              default=""> regexp
          </$checkbox>
<!--
          <small>(<$count filter=<<searchFilter>> /> results)</small>
-->
        </td>
      </tr>
      </table>
      <table class="tmap-create-connection-table">
      <tr>
        <td colspan="2">
          <table class="tmap-very-small-list">
            <$list
                filter=<<searchFilter>>
                variable="item">
            <tr><<searchResults>></tr>
            </$list>
            <tr>
            <$list filter="[{$:/temp/quickConnectSearch}regexp[.+]] -[is[tiddler]]" variable="item">
              <<searchResults>>
            </$list>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    <div class="title">Existing Connections</div>
    <div class="tmap-quick-connect-existing-bar">
      <$select
          tiddler="$:/state/tmap/tid-toolbar"
          field="direction"
          default="both">
          <option value="both">both</option>
          <option value="in">incoming</option>
          <option value="out">outgoing</option>
      </$select>
      <$checkbox
          tiddler="$:/state/tmap/tid-toolbar"
          field="filter.links"
          checked="-[[tw-body:link]]"
          unchecked=""
          default=""> hide links
      </$checkbox>
    </div>
    <table class="tmap-connection-table">
<!--
    <tr>
      <th></th>
      <th>Tiddler</th>
      <th>Type</th>
      <th></th>
    </tr>
-->
    
    <$tmap-connections
        filter=<<tmap mergeFields "$:/state/tmap/tid-toolbar" "filter.">>
        direction={{$:/state/tmap/tid-toolbar!!direction}}
        emptyMessage=<<noConnectionsMsg>>>
      <tr>
        <td title=<<direction>>><<directionSymbol>></td>
        <td><$link to=<<neighbour>>><$view field="title" /></$link></td>
        <td><<edge.type>></td>
        <td>
          <$button
              tooltip="Delete this connection"
              class="tc-btn-invisible">{{$:/core/images/close-button}}
            <$action-sendmessage $message="tmap:tm-remove-edge"
                id=<<edge.id>>
                from=<<edge.from>>
                to=<<edge.to>>
                type=<<edge.type>> />
          </$button>
        </td>
      </tr>
    </$tmap-connections>
    </table>   
  </div>
</div>
</$set>
\end

<$list filter="[all[current]is[tiddler]]"><$list filter="[{$:/temp/tmap/state/popup/quickConnect}prefix<qualify>]" variable="item" emptyMessage=<<hidePopup>>><<showPopup>></$list></$list>