xieziyu / angular2-draggable

Angular directive (for version >= 2.x ) that makes the DOM element draggable and resizable
https://xieziyu.github.io/angular2-draggable/
293 stars 103 forks source link

Handle should extend to handle+child elements #28

Open innovativeclaims opened 6 years ago

innovativeclaims commented 6 years ago

The handle should really include any sub-elements of itself, so if I have a div with spans or other divs inside, those should also be part of the handle (except probably buttons).

I think this should make it behave in this way:

    // Support Mouse Events:
    @HostListener('mousedown', ['$event'])
    onMouseDown(event: any) {
        // 1. skip right click;
        // 2. if handle is set, the element can only be moved by handle (or the handle's non-button children)

        if (event.button == 2) {
            return;
        } else if (this.handle !== undefined && !this.checkChildren(event.target, this.handle)) {
            return;
        }

        this.orignal = this.getPosition(event.clientX, event.clientY);
        this.pickUp();
    }

    checkChildren(target: HTMLElement, element) {
        // Checks if the target is the element clicked, then checks each child element of element as well
        // Ignores button clicks

        // Ignore elements of type button
        if (element.tagName == 'BUTTON') return false;

        // If the target was found, return true (handle was found)
        if (element == target) return true;

        // Recursively iterate this elements children
        for (var child in element.children) {
            if (element.children.hasOwnProperty(child)) {
                if (this.checkChildren(target, element.children[child])) return true;
            }
        }

        // Handle was not found in this lineage
        // Note: return false is ignore unless it is the parent element
        return false;
    }
xieziyu commented 6 years ago

Great! Thanks for your suggestion. This issue should be fixed.

xieziyu commented 6 years ago

Added in v1.1.0