# Drag and drop directive

The `sfDragDrop` is a directive that adds drag-and-drop functionality to an `HTML` element. Using this directive you can drag one or several files over a specified `HTML` element. When a drop event occurs a delegate is called. The `sfDragDrop` directive is defined in a module with the same name: `sfDragDrop`.

## Attributes

The `sfDragDrop` directive exposes the following attributes:

 <table>
<colgroup><col><col></colgroup>
<tbody>
<tr>
<td>

<strong>Attribute</strong>

</td>
<td>

<strong>Description</strong>

</td>
</tr>
<tr>
<td>

<code>sf-template-html</code>

</td>
<td>

Passes the markup that is included at the beginning of the directive's host element

</td>
</tr>
<tr>
<td>

<code>sf-data-transfer-callback</code>

</td>
<td>

Passes a delegate that is called when a drop occurs. The <code>dataTransferObject</code> is resolved as an argument when calling the delegate. For more information, see [DataTransfer Object](https://msdn.microsoft.com/en-us/library/ie/ms535861%28v=vs.85%29.aspx).

</td>
</tr>
</tbody>
</table>

## CSS classes

The `sfDragDrop` directive adds the following CSS classes to the `HTML` host element:

 <table>
<colgroup><col><col></colgroup>
<tbody>
<tr>
<td>

<strong>CSS class</strong>

</td>
<td>

<strong>Action</strong>

</td>
</tr>
<tr>
<td>

<code>sf-Drag-start</code><br>

</td>
<td>

Added when dragging is in progress

</td>
</tr>
<tr>
<td>

<code>sf-Drag-over</code><br>

</td>
<td>

Added when dragging is in progress
over the element</td>

</tr>
</tbody>
</table>

## Add the drag and drop directive

The following example demonstrates how to add a drag-and-drop directive in a widget designer's view.

To enable AngularJs to link the `sfDragDrop` directive in your custom designer view, you must load the script of the directive and add a dependency to the module.\
Perform the following:

1. Sitefinity CMS automatically registers the scripts you need and, if no other designer view with explicitly set priority exists, Sitefinity CMS sets your designer view priority 1. In case you need to have full control over the scripts that are loaded or you want to set custom priority, you can alternatively create your own `DesignerView.YourView.json` file. If you have a `JSON` file that matches the convention (even if empty), this automatic scripts registration will not occur. In your `DesignerView.YourView.json` file, add a scripts array.\
   Use the following code:

   ```JSON
   {
     "priority": 1,
     "components" : ["sf-drag-drop"]
   }
   ```
2. In your `designerview-yourview.js` file, place the following code snippet:

   ```JavaScript
   (function () {
       var designerModule = angular.module('designer');

       designerModule.requires.push('sfDragDrop');

       designerModule.controller('YourViewCtrl', ['$scope', function ($scope) {

           $scope.dataTransferDropped = function (dataTransferObject) {

               if (dataTransferObject.files && dataTransferObject.files[0]) {
                   alert(dataTransferObject.files[0].name);
               }
           };
       }]);
   })();
   ```

   The code above is called when a drop event occurs. As a result, an alert displays the name of the first file that was dropped in the `HTML` element hosting the `sfDragDrop` directive.
3. In your `DesignerView.YourView.cshtml` file, place the following tag where you want to render the `sfDragDrop` directive:
   ```HTML
   <div sf-drag-drop sf-data-transfer-callback="dataTransferDropped(dataTransferObject)"></div>
   ```
