DataTables / VisualEvent

Visually show Javascript events on a page
http://sprymedia.co.uk
1.04k stars 162 forks source link

Doesn't work with jQuery 2.0 #29

Closed DataTables closed 10 years ago

DataTables commented 11 years ago

Just as the title says

ghost commented 11 years ago

Hi Allan,

I wanted to get this working and because my "events" issues were worse than the "not working w/ jQuery v2.0", I went ahead and found a solution. I haven't fully debug it, but it seems to work as best I can tell:

    // jQuery 1.9+
    VE.parsers.push(function () {
        var version = $.fn.jquery.substr(0, 3),
            elements = [],
            count = 0;

        msos.console.debug('VE.parsers.push -> 1.9+ start, ver: ' + version);

        version = Number(version);

        if (!$ || version < 1.9) {
            msos.console.debug('VE.parsers.push -> 1.9+ done, none!');
            return [];
        }

        $('*').each(
            function () {
                count += 1;

                var html_el_data = $._data($(this).get(0)) || null,
                    i = '',
                    j = '',
                    type = '',
                    k = 0,
                    kLen = 0,
                    func = null,
                    eventAttachedNode,
                    oEvents,
                    aNodes = [],
                    sjQuery = '';

                    if (typeof html_el_data.events == 'object') {

                        eventAttachedNode = html_el_data.handle.elem || {};
                        func = null;

                    for (type in html_el_data.events) {

                        if (type !== 'live') {

                            oEvents = html_el_data.events[type] || {};

                            for (j in oEvents) {
                                aNodes = [];
                                sjQuery = "jQuery " + $.fn.jquery;

                                if (oEvents[j].selector !== undefined && oEvents[j].selector !== null) {
                                    aNodes = $(oEvents[j].selector, cache[i].handle.elem);
                                    sjQuery += " (live event)";
                                } else {
                                    aNodes.push(eventAttachedNode);
                                }

                                kLen = aNodes.length;

                                for (k = 0; k < kLen; k += 1) {
                                    elements.push(
                                        {
                                            "node": aNodes[k],
                                            "listeners": []
                                        }
                                    );

                                    if (oEvents[j].origHandler !== undefined) {
                                        func = oEvents[j].origHandler.toString();
                                    } else if (oEvents[j].handler !== undefined) {
                                        func = oEvents[j].handler.toString();
                                    } else {
                                        func = oEvents[j].toString();
                                    }

                                    /* We use jQuery for the Visual Event events... don't really want to display them */
                                    if (oEvents[j] && oEvents[j].namespace !== "VisualEvent" && func !== "0") {
                                        elements[elements.length - 1].listeners.push(
                                            {
                                                "type": type,
                                                "func": func,
                                                "removed": false,
                                                "source": sjQuery
                                            }
                                        );
                                    }
                                }

                                // Remove elements that didn't have any listeners (i.e. might be a Visual Event node)
                                if (elements[elements.length - 1].listeners.length === 0) {
                                    elements.splice(elements.length - 1, 1);
                                }
                            }
                        }
                    }
                }
            }
        );

It isn't "drop in place" for your VisualEvent.parser code, but you can probably figure it out. Thanks for the reply to my email.

DataTables commented 10 years ago

Thanks to KoalaBear84's work here https://github.com/DataTables/VisualEvent/pull/34 - it does now. I'm just about to do a build and deploy with the fix.