title: $:/core/macros/list
tags: $:/tags/Macro

\define list-links(filter,type:"ul",subtype:"li",class:"",emptyMessage,field:"caption")
\whitespace trim
<$genesis $type=<<__type__>> class=<<__class__>>>
	<$list filter=<<__filter__>> emptyMessage=<<__emptyMessage__>>>
		<$genesis $type=<<__subtype__>>>
			<$link to={{!!title}}>
				<$let tv-wikilinks="no">
					<$transclude field=<<__field__>>>
						<$view field="title"/>
					</$transclude>
				</$let>
			</$link>
		</$genesis>
	</$list>
</$genesis>
\end

\define list-links-draggable-drop-actions()
<$action-listops $tiddler=<<targetTiddler>> $field=<<targetField>> $subfilter="+[insertbefore<actionTiddler>,<currentTiddler>]"/>
\end

\define list-links-draggable(tiddler,field:"list",emptyMessage,type:"ul",subtype:"li",class:"",itemTemplate,displayField:"caption",startactions,endactions)
\whitespace trim
<$set name="_tiddler" value="""$tiddler$""" emptyValue=<<currentTiddler>> >
	<$let field-reference={{{ [<_tiddler>] "!!" [[$field$]] +[join[]] }}}
		targetTiddler=<<_tiddler>>
		targetField="""$field$"""
	>
		<span class="tc-links-draggable-list">
			<$genesis $type=<<__type__>> class="$class$">
				<$list filter="[list<field-reference>]" emptyMessage=<<__emptyMessage__>>>
					<$droppable
						actions=<<list-links-draggable-drop-actions>>
						tag="""$subtype$"""
						enable=<<tv-enable-drag-and-drop>>
					>
						<div class="tc-droppable-placeholder"/>
						<div>
							<$transclude tiddler=<<__itemTemplate__>>>
								<$link to={{!!title}}
									startactions=<<__startactions__>>
									endactions=<<__endactions__>>
								>
									<$let tv-wikilinks="no">
										<$transclude field=<<__displayField__>>>
											<$view field="title"/>
										</$transclude>
									</$let>
								</$link>
							</$transclude>
						</div>
					</$droppable>
				</$list>
				<$tiddler tiddler="">
					<$droppable
						actions=<<list-links-draggable-drop-actions>> 
						tag="div"
						enable=<<tv-enable-drag-and-drop>>
					>
						<div class="tc-droppable-placeholder">
							{{$:/core/images/blank}}
						</div>
						<div style="height:0.5em;"/>
					</$droppable>
				</$tiddler>
			</$genesis>
		</span>
	</$let>
</$set>
\end

\define list-tagged-draggable-drop-actions(tag)
\whitespace trim
<!-- Save the current ordering of the tiddlers with this tag -->
<$set name="order" filter="[<__tag__>tagging[]]">
	<!-- Remove any list-after or list-before fields from the tiddlers with this tag -->
	<$list filter="[<__tag__>tagging[]]">
		<$action-deletefield $field="list-before"/>
		<$action-deletefield $field="list-after"/>
	</$list>
	<!-- Save the new order to the Tag Tiddler -->
	<$action-listops $tiddler=<<__tag__>> $field="list" $filter="+[enlist<order>] +[insertbefore<actionTiddler>,<currentTiddler>]"/>
	<!-- Make sure the newly added item has the right tag -->
	<!-- Removing this line makes dragging tags within the dropdown work as intended -->
	<!--<$action-listops $tiddler=<<actionTiddler>> $tags=<<__tag__>>/>-->
	<!-- Using the following 5 lines as replacement makes dragging titles from outside into the dropdown apply the tag -->
	<$list filter="[<actionTiddler>!contains:tags<__tag__>]">
		<$fieldmangler tiddler=<<actionTiddler>>>
			<$action-sendmessage $message="tm-add-tag" $param=<<__tag__>>/>
		</$fieldmangler>
	</$list>
</$set>
\end

\define list-tagged-draggable(tag,subFilter,emptyMessage,itemTemplate,elementTag:"div",storyview:"",displayField:"title",startactions,endactions)
\whitespace trim
<span class="tc-tagged-draggable-list">
	<$set name="tag" value=<<__tag__>>>
		<$list
			filter="[<__tag__>tagging[]$subFilter$]"
			emptyMessage=<<__emptyMessage__>>
			storyview=<<__storyview__>>
		>
			<$genesis $type=<<__elementTag__>> class="tc-menu-list-item">
				<$droppable
					actions="""<$macrocall $name="list-tagged-draggable-drop-actions" tag=<<__tag__>>/>"""
					enable=<<tv-enable-drag-and-drop>>
				>
					<$genesis $type=<<__elementTag__>> class="tc-droppable-placeholder"/>
					<$genesis $type=<<__elementTag__>>>
						<$transclude tiddler=<<__itemTemplate__>>>
							<$link to={{!!title}}
								startactions=<<__startactions__>>
								endactions=<<__endactions__>>
							>
								<$let tv-wikilinks="no">
									<$transclude field=<<__displayField__>>>
										<$view field="title"/>
									</$transclude>
								</$let>
							</$link>
						</$transclude>
					</$genesis>
				</$droppable>
			</$genesis>
		</$list>
		<$tiddler tiddler="">
			<$droppable
				actions="""<$macrocall $name="list-tagged-draggable-drop-actions" tag=<<__tag__>>/>"""
				enable=<<tv-enable-drag-and-drop>>
			>
				<$genesis $type=<<__elementTag__>> class="tc-droppable-placeholder"/>
				<$genesis $type=<<__elementTag__>> style="height:0.5em;"/>
			</$droppable>
		</$tiddler>
	</$set>
</span>
\end
