<commit_before>// Select cells with a rubberband-selection using the mouse

define([
    'base/js/namespace',
    'jquery',
    "base/js/events",
], function(IPython, $, events) {
    "use strict";

    var startX, startY;
    var tmp_cell
    var offsetY
    var isDragging = false;
    var isRubberBandEnabled = false;
    
    /*
     * Capture shift key - shift+mouse button will start rubberband selection
     *
     */
    $(document).keydown(function(event){
        if(event.keyCode==16){
            // enable the highlighter
            isRubberBandEnabled = true
        }
    })
    
    $(document).keyup(function(event){
        if(event.keyCode==16){
            // disable the highlighter
            isRubberBandEnabled = false
        }
    })

    /*
     * Start rubberband selection action
     *
     */
    $(document).mousedown(function(event){
        if(isRubberBandEnabled){
            offsetY = $('#notebook').offset().top
            tmp_cell = IPython.notebook.get_selected_index()
            startX = event.pageX;
            startY = event.pageY;
            isDragging = true;
            $("#dragmask").css(
                {
                    'left'   :    startX,
                    'top'    :    startY,
                    'width'  :    0,
                    'height' :    0
                }
     
            ).show();
            // prevent default behaviour of text selection
            return false;
        } else {
            /* clear selection */
            var ncells = IPython.notebook.ncells()
            var cells = IPython.notebook.get_cells()  
            for(var i=0; i < ncells; i++){
                delete cells[i].metadata.selected
                cells[i].element.removeClass('multiselect')
        }
    }

    });

    /*
     * Rubberband dragging operation - select cells touched by rubberband
     *
     */
    $(document).mousemove(function(event){
        if(isDragging){
     
            var left, top, width, height;
            if(event.pageX>startX){
                left = startX;
                width = event.pageX - startX;
            }
            else {
                left = event.pageX;
                width = startX - event.pageX;
            }
            if(event.pageY>startY){
                top = startY;
                height = event.pageY - startY;
            }
            else {
                top = event.pageY;
                height = startY - event.pageY;
            }

            var ncells = IPython.notebook.ncells()
            var cells = IPython.notebook.get_cells();
            for (var i=0; i<ncells; i++) { 
                var _cell = cells[i]
                if (isCellWithin(_cell,left,top,width,height) === true) {
                    _cell.element.addClass('multiselect')
                    _cell.metadata.selected = true
                } else {
                    _cell.element.removeClass('multiselect')
                    delete _cell.metadata.selected
                }
            }
            $("#dragmask").css(
                {
                    'left'   :    left,
                    'top'    :    top,
                    'width'  :    width,
                    'height' :    height
                }
            )
        }
    })

    /*
     * End rubberband dragging operation
     *
     */
    $(document).mouseup(function(event){
        if(isDragging){
            isDragging = false;
            $("#dragmask").hide();
        }
    })

    /*
     * Test if a cell touched by a certain area
     *
     * @method isCellWithin
     * @param cell
     * @param left
     * @param top
     * @param width
     * @param height
     *
     */
    var isCellWithin = function(cell,left,top,width,height){
        var cellpos = cell.element.position()
        var cellh = cell.element.height()
        var cellw = cell.element.width()
        if (cellpos.top == 0)
            return false    // not visible
        if ((cellpos.top+cellh) > top-offsetY && cellpos.top < top-offsetY+height 
            && cellpos.left+cellw > left && cellpos.left < left+width) {
            return true
        } else {
            return false
        }
    }

     /**
     * load css file and append to document
     *
     * @method load_css
     * @param name {String} filenaame of CSS file
     *
     */
    var load_css = function (name) {
        var link = document.createElement("link");
        link.type = "text/css";
        link.rel = "stylesheet";
        link.href = require.toUrl(name);
        document.getElementsByTagName("head")[0].appendChild(link);
      };

    load_css('/nbextensions/usability/rubberband/main.css');
    var rubberband_div = $('<div id="dragmask" class="highlight-drag"></div>')<commit_msg>Add scrolling to rubberband selection<commit_after>// Select cells with a rubberband-selection using the mouse
// shift + mouse drag -> select only cells currently touched by rubberband
// ctrl+shift + mouse drag -> select all cells that were touched by rubberband

define([
    'base/js/namespace',
    'jquery',
    'base/js/events',
    'base/js/keyboard',
], function(IPython, $, events, keyboard) {
    "use strict";

    var keycodes = keyboard.keycodes;

    var startX, startY;
    var offsetY
    var isDragging = false;
    var isRubberBandEnabled = false;
    var isAddSelection = false;
    var isScrolling = false
    
    /*
     * Capture shift key - shift+mouse button will start rubberband selection
     *
     */
    $(document).keydown(function(event){
        if(event.keyCode === keycodes.shift){
            isRubberBandEnabled = true
        }
        if(event.keyCode === keycodes.ctrl){
            isAddSelection = true
        }
    })
    
    $(document).keyup(function(event){
        if(event.keyCode === keycodes.shift){
            isRubberBandEnabled = false
        }
        if(event.keyCode === keycodes.ctrl){
            isAddSelection = false
        }
    })

    /*
     * Start rubberband selection action
     *
     */
    $(document).mousedown(function(event){
        if(isRubberBandEnabled){
            offsetY = $('#notebook').offset().top
            tmp_cell = IPython.notebook.get_selected_index()
            startX = event.pageX;
            startY = event.pageY;
            isDragging = true;
            $("#dragmask").css(
                {
                    'left'   :    startX,
                    'top'    :    startY,
                    'width'  :    0,
                    'height' :    0
                }
     
            ).show();
            // prevent default behaviour of text selection
            return false;
        } else {
            /* clear selection */
            var ncells = IPython.notebook.ncells()
            var cells = IPython.notebook.get_cells()  
            for(var i=0; i < ncells; i++){
                delete cells[i].metadata.selected
                cells[i].element.removeClass('multiselect')
        }
    }

    });

    /*
     * Rubberband dragging operation - select cells touched by rubberband
     *
     */
    $(document).mousemove(function(event){
        var _h = $('#notebook').height()
        if(isDragging === true){
            var left, top, width, height;
            if(event.pageX>startX){
                left = startX;
                width = event.pageX - startX;
            }
            else {
                left = event.pageX;
                width = startX - event.pageX;
            }
            if(event.pageY>startY){
                top = startY;
                height = event.pageY - startY;
            }
            else {
                top = event.pageY;
                height = startY - event.pageY;
            }
            
            var ncells = IPython.notebook.ncells()
            var cells = IPython.notebook.get_cells();
            
            
            if (event.pageY-offsetY > _h-50 && isScrolling === false) {
                var speed = event.pageY-offsetY -( _h-50)
                var n=$('#notebook')
                var scrollpos = n.scrollTop() + 50  
                var time = 20 //2*(50-speed)    
                isScrolling = true
                n.animate({scrollTop:scrollpos}, time,"linear", function() { isScrolling = false })
            }

            if ((event.pageY-offsetY) < 50 && isScrolling === false) {
                var speed = event.pageY-offsetY
                var n=$('#notebook')
                var scrollpos = n.scrollTop() - 50  
                var time = 20 //2*(50-speed)  
                isScrolling = true                
                n.animate({scrollTop:scrollpos}, time,"linear", function() { isScrolling = false })
            }

            
            for (var i=0; i<ncells; i++) { 
                var _cell = cells[i]
                if (isCellWithin(_cell,left,top,width,height) === true) {
                    _cell.element.addClass('multiselect')
                    _cell.metadata.selected = true
                } else {
                    if (isAddSelection === false) {
                        _cell.element.removeClass('multiselect')
                        delete _cell.metadata.selected
                    }
                }
            }
            $("#dragmask").css(
                {
                    'left'   :    left,
                    'top'    :    top,
                    'width'  :    width,
                    'height' :    height
                }
            )
        }
    })

    /*
     * End rubberband dragging operation
     *
     */
    $(document).mouseup(function(event){
        if(isDragging){
            isDragging = false;
            $("#dragmask").hide();
        }
    })

    /*
     * Test if a cell touched by a certain area
     *
     * @method isCellWithin
     * @param cell
     * @param left
     * @param top
     * @param width
     * @param height
     *
     */
    var isCellWithin = function(cell,left,top,width,height){
        var cellpos = cell.element.position()
        var cellh = cell.element.height()
        var cellw = cell.element.width()
        if (cellpos.top === 0)
            return false    // not visible
        if ((cellpos.top+cellh) > top-offsetY && cellpos.top < top-offsetY+height 
            && cellpos.left+cellw > left && cellpos.left < left+width) {
            return true
        } else {
            return false
        }
    }

     /**
     * load css file and append to document
     *
     * @method load_css
     * @param name {String} filenaame of CSS file
     *
     */
    var load_css = function (name) {
        var link = document.createElement("link");
        link.type = "text/css";
        link.rel = "stylesheet";
        link.href = require.toUrl(name);
        document.getElementsByTagName("head")[0].appendChild(link);
      };

    load_css('/nbextensions/usability/rubberband/main.css');
    var rubberband_div = $('<div id="dragmask" class="highlight-drag"></div>')
{'fraction_non_alphanumeric': 0.11477200327659962, 'fraction_numerical': 0.0032765996177300445, 'mean_word_length': 1.3579399141630901, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, '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': '334248', 'n_tokens_mistral': 3142, 'n_tokens_neox': 2993, 'n_words': 645}