Cross Browser detection of click vs mouse drag in JavaScript

758 views Asked by At

In jQuery Terminal I have code that bind mouse down/up/move to detect if user using click but not drag.

The code look like this:

            (function() {
                var count = 0;
                var isDragging = false;
                var $target;
                var name = 'click_' + self.id();
                self.mousedown(function(e) {
                    console.log('down');
                    isDragging = false;
                    $target = $(e.target);
                    if (e.originalEvent.button === 2) {
                        return;
                    }
                    self.oneTime(1, function() {
                        $(window).one('mousemove.terminal_' + self.id(), function() {
                            console.log('move');
                            isDragging = true;
                            count = 0;
                        });
                    });
                }).mouseup(function() {
                    console.log('up');
                    var wasDragging = isDragging;
                    isDragging = false;
                    $(window).off('mousemove.terminal_' + self.id());
                    console.log(wasDragging);
                    if (!wasDragging) {
                        if (++count === 1) {
                            if (!self.enabled() && !frozen) {
                                self.focus();
                                command_line.enable();
                                count = 0;
                            } else {
                                self.oneTime(settings.clickTimeout, name, function() {
                                    if ($target.is('.terminal') ||
                                        $target.is('.terminal-wrapper')) {
                                        var len = self.get_command().length;
                                        self.set_position(len);
                                    } else if ($target.closest('.prompt').length) {
                                        self.set_position(0);
                                    }
                                    count = 0;
                                });
                            }
                        } else {
                            self.stopTime(name);
                            count = 0;
                        }
                    }
                }).dblclick(function() {
                    count = 0;
                    self.stopTime(name);
                });
            })();

(The code also handle double clicks, don't remember why).

but there is problem on Chrome/MacOS (I'm testing it in VirtualBox but someone also reported the issue) the mousemove is triggered even that there is no mouse move and no dragging.

It seems that mousemove event (on MacOS/Chrome) is fired when it's bind after the actual move of the mouse.

Is there a better solution to detect click but not dragging that will work on MacOS as well on other OS/Browsers?

1

There are 1 answers

0
jcubic On BEST ANSWER

I think I've resolved the issue by replacing mousemove with checking if there is any text selected, using this cross-browser function

var get_selected_text = (function() {
    if (window.getSelection || document.getSelection) {
        return function() {
            var selection = (window.getSelection || document.getSelection)();
            if (selection.text) {
                return selection.text;
            } else {
                return selection.toString();
            }
        };
    } else if (document.selection && document.selection.type != "Control") {
        return function() {
            return document.selection.createRange().text;
        };
    }
    return function() {
        return '';
    };
})();