<commit_before>/*!
 * dragtable - jquery ui widget to re-order table columns 
 * version 3.0
 * 
 * Copyright (c) 2010, <NAME> <<EMAIL>>
 * 12/2/2010
 * https://github.com/jebaird/dragtable
 * 
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * 
 * 
 * Forked from https://github.com/akottr/dragtable - <NAME> <EMAIL>
 * 
 *
 * 
 * 
 * quick down and and dirty on how this works
 * ###########################################
 * so when a column is selected we grab all of the cells in that row and clone append them to a semi copy of the parent table and the 
 * "real" cells get a place holder class witch is removed when the dragstop event is triggered
 * 
 * 
 * make it easy to have a button swap colums
 * 
 * 
 * Events - in order of trigger
 * start - when the user mouses down on handle or th, use in favor of display helper
 * beforechagne - called when a col will be moved
 * change - called after the col has been moved
 * stop - after the user mouses up and stops dragging
 * 
 * 
 * 
 * 
 * IE notes
 * 	ie8 in quirks mode will only drag once after that the events are lost
 * 
 */

(function($) {
  $.widget("jb.dragtable", {
  		//TODO: implement this
  		eventWidgetPrefix: 'dragtable',
		options: {
			//used to the col headers, data containted in here is used to set / get the name of the col
			dataHeader:'data-header',
			//class name that handles have 
			handle:'dragtable-drag-handle',
			//draggable items in cols, .dragtable-drag-handle has to match the handle options
			items: 'thead th:not( :has( .dragtable-drag-handle ) ), .dragtable-drag-handle',
			//if a col header as this class, cols cant be dragged past it
			boundary: 'dragtable-drag-boundary',
			//classnames that get applied to the real td, th
			placeholder: ''
			
		},
		// when a col is dragged use this to find the symantic elements, for speed
  		tableElemIndex:{  
			head: '0',
			body: '1',
			foot: '2'
		},
		tbodyRegex: /(tbody|TBODY)/,
		theadRegex: /(thead|THEAD)/,
		tfootRegex: /(tfoot|TFOOT)/,
			
		_create: function() {
			
			//console.log(this);
			//used start/end of drag
			this.startIndex = null;
			this.endIndex = null;
			//the refferences to the table cells that are getting dragged
			this.currentColumnCollection = [];
			//the refferences the postion of the first element in the currentColunmCollection position
			this.currentColumnCollectionOffset = {};
			//the div wrapping the drag display table
			this.dragDisplay = $([])

			
			var self = this,
				o = self.options,
				el = self.element;
			
			//grab the ths and the handles and bind them 
			el.delegate(o.items, 'mousedown.' + self.widgetEventPrefix, function(e){
				
				var $handle = $(this);

				//make sure we are working with a th instead of a handle
				if( $handle.hasClass( o.handle ) ){
					
					$handle = $handle.closest('th');
					//change the target to the th, so the hander can pick up the offsetleft
					e.currentTarget = $handle.closest('th')[0]
				}
				
				
			self.getCol( $handle.index() )
					.attr( 'tabindex', -1 )
	                .focus()
					.disableSelection()
					.css({
	                    top: self.currentColumnCollectionOffset.y,
	                   //using the parentOff.set makes e.pageX reletive to the parent element. This fixes the issue of the drag display not showing up under cursor on drag.
	                    left: self.currentColumnCollectionOffset.x
					})
	                .insertAfter( self.element )
				

				
				self._mousemoveHandler( e );
				//############
			});
                
		},
		
		/*
		 * e.currentTarget is used for figuring out offsetLeft
		 * getCol must be called before this is 
		 * 
		 * 
		 */
		_mousemoveHandler: function( e ){
							
				//position the drag dispaly to rel to the middle of the target co
			
				var self = this;
				
				//why is this getting stashed
				var startingOffsetX = e.pageX - this.currentColumnCollectionOffset.x;
				//TODO: make col switching relitvte to the silibing cols, not pageX
                var prevMouseX = this.currentColumnCollectionOffset.x;

					
					//get the colum count, used to contain col swap
				var colCount = self.element[ 0 ]
									.getElementsByTagName( 'thead' )[ 0 ]
									.getElementsByTagName( 'tr' )[ 0 ]
									.getElementsByTagName( 'th' )
									.length - 1;

				
                //drag the column around

               self._start( e )
               
	            $( document )
	                .bind('mousemove.' + self.widgetEventPrefix, function( e ){
                    
                	 var columnPos = self._setCurrentColumnCollectionOffset(),
						colHalfWidth = Math.floor( self.currentColumnCollection[0].clientWidth / 2 );
                   
                    
                    
                    console.log( columnPos, startingOffsetX, e.pageX)
                                        
                    //console.log( 'half width colHalfWidth ', colHalfWidth)
                    self.dragDisplay
                    	.css( 'left', ( e.pageX - startingOffsetX ) )
                    
                    if( e.pageX < prevMouseX ){
                    	//move left
							var threshold = columnPos.x;
							
							console.log( 'threshold ',threshold,  e.pageX - startingOffsetX )
							if(e.pageX < threshold ){
								self._swapCol(self.startIndex-1);
							}

						}else{
							//move right
							var threshold = columnPos.x + colHalfWidth * 2;
							//console.log('move right ', columnPos.x, threshold, e.pageX );
							//move to the right only if x is greater than threshold and the current col isn' the last one
							if(e.pageX > threshold  && colCount != self.startIndex ){
								//console.info('move right');
								self._swapCol( self.startIndex + 1 );
							}
						}
						//update mouse position
						prevMouseX = e.pageX;
			
                })
                .one( 'mouseup.' + self.widgetEventPrefix ,function(e ){

                    self._stop( e )
                    
                });
                          
		},
		
		_start: function( e ){
			
			$( document )
                	//move disableselection and cusor to default handlers of the start event
	                .disableSelection()
	                .css( 'cursor', 'move')

				
			return this._eventHelper('start',e,{
					//'draggable': $dragDisplay
				});
                
                
		},
		_stop: function( e ){

			if( this._eventHelper('stop',e,{}) == true ){
				 $( document )
			 	 .unbind( 'mousemove.' + this.widgetEventPrefix )
			 	 .enableSelection()
			 	 .css( 'cursor', 'move')<commit_msg>code clean up, renamed vars<commit_after>/*!
 * dragtable - jquery ui widget to re-order table columns 
 * version 3.0
 * 
 * Copyright (c) 2010, <NAME> <<EMAIL>>
 * 12/2/2010
 * https://github.com/jebaird/dragtable
 * 
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * 
 * 
 * Forked from https://github.com/akottr/dragtable - <NAME> <EMAIL>
 * 
 *
 * 
 * 
 * quick down and and dirty on how this works
 * ###########################################
 * so when a column is selected we grab all of the cells in that row and clone append them to a semi copy of the parent table and the 
 * "real" cells get a place holder class witch is removed when the dragstop event is triggered
 * 
 * 
 * make it easy to have a button swap colums
 * 
 * 
 * Events - in order of trigger
 * start - when the user mouses down on handle or th, use in favor of display helper
 * beforechagne - called when a col will be moved
 * change - called after the col has been moved
 * stop - after the user mouses up and stops dragging
 * 
 * 
 * 
 * 
 * IE notes
 * 	ie8 in quirks mode will only drag once after that the events are lost
 * 
 */

(function($) {
  $.widget("jb.dragtable", {
  		//TODO: implement this
  		eventWidgetPrefix: 'dragtable',
		options: {
			//used to the col headers, data containted in here is used to set / get the name of the col
			dataHeader:'data-header',
			//class name that handles have 
			handle:'dragtable-drag-handle',
			//draggable items in cols, .dragtable-drag-handle has to match the handle options
			items: 'thead th:not( :has( .dragtable-drag-handle ) ), .dragtable-drag-handle',
			//if a col header as this class, cols cant be dragged past it
			boundary: 'dragtable-drag-boundary',
			//classnames that get applied to the real td, th
			placeholder: ''
			
		},
		// when a col is dragged use this to find the symantic elements, for speed
  		tableElemIndex:{  
			head: '0',
			body: '1',
			foot: '2'
		},
		tbodyRegex: /(tbody|TBODY)/,
		theadRegex: /(thead|THEAD)/,
		tfootRegex: /(tfoot|TFOOT)/,
			
		_create: function() {
			
			//console.log(this);
			//used start/end of drag
			this.startIndex = null;
			this.endIndex = null;
			//the refferences to the table cells that are getting dragged
			this.currentColumnCollection = [];
			//the refferences the postion of the first element in the currentColunmCollection position
			this.currentColumnCollectionOffset = {};
			//the div wrapping the drag display table
			this.dragDisplay = $([])

			
			var self = this,
				o = self.options,
				el = self.element;
			
			//grab the ths and the handles and bind them 
			el.delegate(o.items, 'mousedown.' + self.widgetEventPrefix, function(e){
				
				var $handle = $(this);

				//make sure we are working with a th instead of a handle
				if( $handle.hasClass( o.handle ) ){
					
					$handle = $handle.closest('th');
					//change the target to the th, so the hander can pick up the offsetleft
					e.currentTarget = $handle.closest('th')[0]
				}
				
				
			self.getCol( $handle.index() )
					.attr( 'tabindex', -1 )
	                .focus()
					.disableSelection()
					.css({
	                    top: self.currentColumnCollectionOffset.y,
	                   //using the parentOff.set makes e.pageX reletive to the parent element. This fixes the issue of the drag display not showing up under cursor on drag.
	                    left: self.currentColumnCollectionOffset.x
					})
	                .insertAfter( self.element )
				

				
				self._mousemoveHandler( e );
				//############
			});
                
		},
		
		/*
		 * e.currentTarget is used for figuring out offsetLeft
		 * getCol must be called before this is 
		 * 
		 * 
		 */
		_mousemoveHandler: function( e ){
							
				//position the drag dispaly to rel to the middle of the target co
			
				var self = this,
					//used to position the dragdisplay against
					startingColumnOffsetX = e.pageX - this.currentColumnCollectionOffset.x,
				//TODO: make col switching relitvte to the silibing cols, not pageX
                //start from event cords
                	prevMouseX = this.currentColumnCollectionOffset.x,
					//get the colum count, used to contain col swap
					colCount = self.element[ 0 ]
									.getElementsByTagName( 'thead' )[ 0 ]
									.getElementsByTagName( 'tr' )[ 0 ]
									.getElementsByTagName( 'th' )
									.length - 1;

                //drag the column around

               self._start( e )
               
	            $( document ).bind('mousemove.' + self.widgetEventPrefix, function( e ){
	            	var columnPos = self._setCurrentColumnCollectionOffset(),
	            		colHalfWidth = Math.floor( self.currentColumnCollection[0].clientWidth / 2 );
                                        
                    //console.log( 'half width colHalfWidth ', colHalfWidth)
                    self.dragDisplay
                    	.css( 'left', ( e.pageX - startingColumnOffsetX ) )
                    
                    if( e.pageX < prevMouseX ){
                    	//move left
							var threshold = columnPos.x;
							
						//	console.log( 'threshold ',threshold,  e.pageX - startingColumnOffsetX )
							if(e.pageX < threshold ){
								self._swapCol(self.startIndex-1);
							}

						}else{
							//move right
							var threshold = columnPos.x + colHalfWidth * 2;
							//console.log('move right ', columnPos.x, threshold, e.pageX );
							//move to the right only if x is greater than threshold and the current col isn' the last one
							if(e.pageX > threshold  && colCount != self.startIndex ){
								//console.info('move right');
								self._swapCol( self.startIndex + 1 );
							}
						}
						//update mouse position
						prevMouseX = e.pageX;
			
                })
                .one( 'mouseup.' + self.widgetEventPrefix ,function(e ){
                    self._stop( e );
                });
                          
		},
		
		_start: function( e ){
			
			$( document )
                	//move disableselection and cusor to default handlers of the start event
	                .disableSelection()
	                .css( 'cursor', 'move')

				
			return this._eventHelper('start',e,{
					//'draggable': $dragDisplay
				});
                
                
		},
		_stop: function( e ){

			if( this._eventHelper('stop',e,{}) == true ){
				 $( document )
			 	 .unbind( 'mousemove.' + this.widgetEventPrefix )
			 	 .enableSelection()
			 	 .css( 'cursor', 'move')
{'fraction_non_alphanumeric': 0.11109422492401216, 'fraction_numerical': 0.00425531914893617, 'mean_word_length': 1.9416629414394277, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3071487', 'n_tokens_mistral': 4673, 'n_tokens_neox': 3655, 'n_words': 1235}