Search code examples
kendo-uievent-bubblingkendo-colorpicker

How to stop event bubbling in kendo color picker widget?


I'm in developing custom Map View component with kendo-colorpicker(kendoFlatColorPicker) to change color of markers. the root element of flatcolorpicker is child element of map view wrapper element.

<div id="map-view">
    <!--some elements....-->
    <div id="custom-flat-color-picker"></div>
</div>

In kendoFlatColorPicker, user can select color in hsvRect panel by dragging mouse. There is a critical issue... When Pressing mouse button and dragging mouse on FlatColorPicker panel, the map image in Map View is also dragged.

I think that this issue came from event-bubbling. So, I try to use e.stopPropagation() method in 'mousedown' event. However, it is not working.

Other ways, I also try to customize _hsvEvets method in kendo FlatColorPicker source code through widget extends like below code.

var KEYDOWN_NS = 'keydown.kendoEditor';
var bind = kendo.bind;
var FlatColorPicker = kendo.ui.FlatColorPicker;
var extendFlatColorPicker = FlatColorPicker.extend({
    options: {
        name: 'CustomFlatColorPicker'
    },
    init: function (element, options){
        var self = this;
        FlatColorPicker.fn.init.call(self, element, options);
    },
    _hsvArea: function () {
        var that = this,
            element = that.element,
            hsvRect = element.find('.k-hsv-rectangle'),
            hsvHandle = hsvRect.find('.k-draghandle').attr('tabIndex', 0);
        function update(x, y) {
            var offset = this.offset, dx = x - offset.left, dy = y - offset.top, rw = this.width, rh = this.height;
            dx = dx < 0 ? 0 : dx > rw ? rw : dx;
            dy = dy < 0 ? 0 : dy > rh ? rh : dy;
            that._svChange(dx / rw, 1 - dy / rh);
        }

        that._hsvEvents = new kendo.UserEvents(hsvRect, {
            global: true,
            press: function (e) {
                //this.element.get(0).dispatchEvent(new Event('mousedown'));
                this.offset = kendo.getOffset(hsvRect);
                this.width = hsvRect.width();
                this.height = hsvRect.height();
                hsvHandle.focus();
                update.call(this, e.x.location, e.y.location);
            },
            start: function () {
                hsvRect.addClass('k-dragging');
                hsvHandle.focus();
            },
            move: function (e) {
                e.preventDefault();
                update.call(this, e.x.location, e.y.location);
            },
            end: function () {
                hsvRect.removeClass('k-dragging');
            }
        });
        that._hsvRect = hsvRect;
        that._hsvHandle = hsvHandle;
        // var cb = that._hsvEvents.press;
        // that._hsvEvents.press = function(e){
        //  e.stopPropagation();
        //  cb.call(this);
        // };
    }
});
kendo.ui.plugin(extendFlatColorPicker);

When user press mouse button to drag mouse, 'press' event is occured on flat color picker, however parameter 'e' has no method stopPropagation().

How can I stop bubbling from colorpicker to map view?


Solution

  • I solved this issue. I found event 'pointerdown' in event listener stack that can be found in chrome dev tool. About this event, I applied e.stopPropagation().