# j-Templates

> Templates must be defined in <script type='text/html' data-name='NAMEOFTEMPLATE'>Tangular template</script> element. This component supports one special template called empty that will be visible if t

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: en
Content-SHA256: e5f3357bc56d202555787c596f876f93207a56e17030a8c3f144706aba799bf1
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: components@910ed1a86f6e72b4f031d3ad0dc7844f9fc5cb8e
Review: not-recorded

- multiple templates
- drag & drop
- custom scrollbars
- context menu

__Configuration__:

- `template {String}` a path to the watching a template name
- `scrollbar {Boolean}` uses a custom scrollbar (default: `true`)
- `scrolltop {Boolean}` scrolls to top if the source is changed (default: `true`)
- `visibleY {Boolean}` shows scrollbar Y (default: `true`)
- `margin {Number}` a vertical margin (default: `0`)
- `parent {String}` jQuery selector to parent container due to `scrollbar` (default: `auto`)
- `contextmenu {String}` a path to the method `function(e)` if the user triggers context-menu
- `emptyif {String}` a condition for showing of empty content (default: `!value || !value.length`)
- `drop {String}` a path to the method `function(e, target, selel)`
	- the method is evaluated if the user drops some files, then `selel` is undefined
	- or the user drags & drops element with the `draggable` attribute (`selel` argument) into the `target` argument (target must contain `droppable` class)

Templates must be defined in `<script type="text/html" data-name="NAME_OF_TEMPLATE">Tangular template</script>` element. This component supports one special template called `empty` that will be visible if the `emptyif` option will be valid.

Use it with `<ui-component name="templates">`. When the component is not loaded yet, jComponent downloads it on first use from the address in `DEF.fallback` (the Componentator CDN by default). Author: Peter Širka. Source: the Componentator components repository, MIT license.

```html example.html
<div>
	<span data-name="list" onclick="changetemplate(this)" style="text-decoration:underline;cursor:pointer;margin-right:5px">List</span>
	<span data-name="icons" onclick="changetemplate(this)" style="text-decoration:underline;cursor:pointer">Icons</span>
</div>
<br />

<div style="width:500px;height:300px;border:1px solid #E0E0E0">
	<ui-component name="templates" path="myfiles" config="template:mytemplate;drop:template_drop">

		<script type="text/html" data-name="list">
			<div class="list padding">
				{{ foreach m in value }}
					<div draggable="true"{{ if m.isfolder }} class="droppable"{{ fi }}>{{ m.name }}</div>
				{{ end }}
			</div>
		</script>

		<script type="text/html" data-name="icons">
			<div class="icons padding">
				{{ foreach m in value }}
					<span draggable="true" class="mr5 {{ if m.isfolder }} droppable{{ fi }}"><i class="{{ m.icon }} mr5"></i>{{ m.name }}</span>
				{{ end }}
			</div>
		</script>

	</ui-component>

</div>
<br />
<div><b>Without multiple templates:</b></div>
<div style="width:500px;height:300px;border:1px solid #E0E0E0;margin-top:10px">
	<ui-component name="templates" path="myfiles">
		<script type="text/html">
			<div class="list padding">
				{{ foreach m in value }}
					<div draggable="true"{{ if m.isfolder }} class="droppable"{{ fi }}>{{ m.name }}</div>
				{{ end }}
			</div>
		</script>
	</ui-component>
</div>

<script>
	var myfiles = [{ icon: 'ti ti-folder', name: 'temp', isfolder: true }, { icon: 'ti ti-file', name: 'logo.png' }, { icon: 'ti ti-file', name: 'logo.svg' }];
	var mytemplate = 'list';

	function changetemplate(el) {
		SET('mytemplate', el.getAttribute('data-name'));
	}

	function template_drop(e, target, item) {

		console.log(e, target, item);
		// @target {jQuery element}

		if (item) {
			// @item {jQuery element}
		} else {
			var files = e.target.files || e.originalEvent.dataTransfer.files;
		}
	}

</script>
```
