# all files / Core/Components/DropFile/ DropFileDirective.js

25% Statements 9/36
100% Branches 0/0
0% Functions 0/8
25% Lines 9/36

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67

    1

    1

    1

    1

    1

    1

    1

    1

    1
     'use strict';

    // Utilities:
    var _ = require('lodash');

    // Module:
    var Core = require('../../Core');

    var DropFileDirective = function () {
        return {
            restrict: 'A',

            scope: {
                onDrop: '&',
                dropDirectory: '='
            },

            link: link
        };

        function link ($scope, $element) {
            var element = _.first($element);

            element.addEventListener('dragover', dragover);
            element.addEventListener('dragenter', dragenter);
            element.addEventListener('dragleave', dragleave);
            element.addEventListener('drop', _.partial(drop, $scope));
        }

        function dragover (event) {
            event.dataTransfer.dropEffect = 'move';
            event.preventDefault();
            event.stopPropagation();
            Array.prototype.forEach.call(document.querySelectorAll('.dragover'), function (element) {
                element.classList.remove('dragover');
            });
            this.classList.add('dragover');
            return false;
        }

        function dragenter () {
            Array.prototype.forEach.call(document.querySelectorAll('.dragover'), function (element) {
                element.classList.remove('dragover');
            });
            this.classList.add('dragover');
            return false;
        }

        function dragleave () {
            this.classList.remove('dragover');
            return false;
        }

        function drop ($scope, event) {
            event.preventDefault();
            event.stopPropagation();
            this.classList.remove('dragover');
            var file = JSON.parse(event.dataTransfer.getData('file'));
            var directory = $scope.dropDirectory;
            var dropHandler = $scope.onDrop();
            dropHandler(file, directory);
            return false;
        }
    };

    Core.directive('tractorDropFile', DropFileDirective);