<commit_before>define([
    'hr/dom',
    'hr/utils',
    'views/commands/menu'
], function ($, _, MenuView) {

    var ContextMenu = {
        lastTimeOpened: 0,
        origin: null,

        /*
         * Clear context menus
         */
        clear: function() {
            $(".ui-context-menu").removeClass("ui-context-menu");
            $("#ui-context-menu").remove();
        },

        /*
         *  Generate menu
         */
        generateMenu: function(menuItems) {
            // Handle dynamic menu
            if (_.isFunction(menuItems)) menuItems = menuItems();

            var menu = new MenuView();
            menu.collection.add(menuItems);
            menu.$el.appendTo($("body"));
            menu.on("action", function() {
                ContextMenu.clear();
            })
            menu.render();
            return menu;
        },

        /*
         *  Create a new context menu
         */
        open: function(menuItems, pos) {
            ContextMenu.clear();
            ContextMenu.lastTimeOpened = Date.now();

            var menu = ContextMenu.generateMenu(menuItems);

            var w = menu.$el.width();
            var h = menu.$el.height();

            var windowW = $(window).width();
            var windowH = $(window).height();

            if ((pos.left+w) > windowW) {
                pos.left = pos.left - w;
                menu.$el.addClass("submenus-right");
            }
            if ((pos.top+h) > windowH) {
                pos.top = pos.top - h;
                menu.$el.addClass("submenus-top");
            }

            menu.$el.css(_.extend({
                'position': "fixed",
                'z-index': 100
            }, pos));
            menu.$el.attr("id", "ui-context-menu");
            menu.open();
        },

        /*
         *  Add a context menu to an element
         *
         */
        add: function(el, menu, options) {
            var $el = $(el);

            options = _.defaults({}, options, {
                // Menu accessible in textinput
                'textinput': false
            });

            var handler = function(e) {
                ContextMenu.origin = e.type;
                var target = e.target || e.srcElement;

                // Ignore contextmenu on textinput
                if (!options.textinput && 
                (target.tagName == 'INPUT' || target.tagName == 'SELECT' || target.tagName == 'TEXTAREA' || target.isContentEditable)) {
                    return;
                }
                
                var x = e.pageX || e.originalEvent.touches[0].pageX;
                var y = e.pageY || e.originalEvent.touches[0].pageY;

                ContextMenu.open(menu, {
                    'left': x,
                    'top': y
                });

                $el.addClass("ui-context-menu");
                return false;
            }

            $el.on("contextmenu", handler);
            //if (navigator.userAgent.match(/iPad/i) != null) 
                $el.taphold(handler);
        }
    };

    // Click on the page: clse context menu
    $(document).on("click", function (e) {
        if (ContextMenu.lastTimeOpened > (Date.now() - 600) && ContextMenu.origin != "contextmenu") return;
        ContextMenu.clear();
    });

    // Open new contextmenu: close other context menu
    $(document).on("contextmenu", function() {<commit_msg>Disable taphold for contextmenu on non-ipad<commit_after>define([
    'hr/dom',
    'hr/utils',
    'views/commands/menu'
], function ($, _, MenuView) {

    var ContextMenu = {
        lastTimeOpened: 0,
        origin: null,

        /*
         * Clear context menus
         */
        clear: function() {
            $(".ui-context-menu").removeClass("ui-context-menu");
            $("#ui-context-menu").remove();
        },

        /*
         *  Generate menu
         */
        generateMenu: function(menuItems) {
            // Handle dynamic menu
            if (_.isFunction(menuItems)) menuItems = menuItems();

            var menu = new MenuView();
            menu.collection.add(menuItems);
            menu.$el.appendTo($("body"));
            menu.on("action", function() {
                ContextMenu.clear();
            })
            menu.render();
            return menu;
        },

        /*
         *  Create a new context menu
         */
        open: function(menuItems, pos) {
            ContextMenu.clear();
            ContextMenu.lastTimeOpened = Date.now();

            var menu = ContextMenu.generateMenu(menuItems);

            var w = menu.$el.width();
            var h = menu.$el.height();

            var windowW = $(window).width();
            var windowH = $(window).height();

            if ((pos.left+w) > windowW) {
                pos.left = pos.left - w;
                menu.$el.addClass("submenus-right");
            }
            if ((pos.top+h) > windowH) {
                pos.top = pos.top - h;
                menu.$el.addClass("submenus-top");
            }

            menu.$el.css(_.extend({
                'position': "fixed",
                'z-index': 100
            }, pos));
            menu.$el.attr("id", "ui-context-menu");
            menu.open();
        },

        /*
         *  Add a context menu to an element
         *
         */
        add: function(el, menu, options) {
            var $el = $(el);

            options = _.defaults({}, options, {
                // Menu accessible in textinput
                'textinput': false
            });

            var handler = function(e) {
                ContextMenu.origin = e.type;
                var target = e.target || e.srcElement;

                // Ignore contextmenu on textinput
                if (!options.textinput && 
                (target.tagName == 'INPUT' || target.tagName == 'SELECT' || target.tagName == 'TEXTAREA' || target.isContentEditable)) {
                    return;
                }
                
                var x = e.pageX || e.originalEvent.touches[0].pageX;
                var y = e.pageY || e.originalEvent.touches[0].pageY;

                ContextMenu.open(menu, {
                    'left': x,
                    'top': y
                });

                $el.addClass("ui-context-menu");
                return false;
            }

            $el.on("contextmenu", handler);
            if (navigator.userAgent.match(/iPad/i) != null) $el.taphold(handler);
        }
    };

    // Click on the page: clse context menu
    $(document).on("click", function (e) {
        if (ContextMenu.lastTimeOpened > (Date.now() - 600) && ContextMenu.origin != "contextmenu") return;
        ContextMenu.clear();
    });

    // Open new contextmenu: close other context menu
    $(document).on("contextmenu", function() {
{'fraction_non_alphanumeric': 0.14147201410312912, 'fraction_numerical': 0.002644336712208021, 'mean_word_length': 1.3109300746775288, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2104329', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1795, 'n_words': 377}