GPQAPP

Форк
0
/
dataTables.autoFill.js 
1212 строк · 29.8 Кб
1
/*! AutoFill 2.3.9
2
 * ©2008-2021 SpryMedia Ltd - datatables.net/license
3
 */
4

5
/**
6
 * @summary     AutoFill
7
 * @description Add Excel like click and drag auto-fill options to DataTables
8
 * @version     2.3.9
9
 * @file        dataTables.autoFill.js
10
 * @author      SpryMedia Ltd (www.sprymedia.co.uk)
11
 * @contact     www.sprymedia.co.uk/contact
12
 * @copyright   Copyright 2010-2021 SpryMedia Ltd.
13
 *
14
 * This source file is free software, available under the following license:
15
 *   MIT license - http://datatables.net/license/mit
16
 *
17
 * This source file is distributed in the hope that it will be useful, but
18
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19
 * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
20
 *
21
 * For details please refer to: http://www.datatables.net
22
 */
23
(function( factory ){
24
	if ( typeof define === 'function' && define.amd ) {
25
		// AMD
26
		define( ['jquery', 'datatables.net'], function ( $ ) {
27
			return factory( $, window, document );
28
		} );
29
	}
30
	else if ( typeof exports === 'object' ) {
31
		// CommonJS
32
		module.exports = function (root, $) {
33
			if ( ! root ) {
34
				root = window;
35
			}
36

37
			if ( ! $ || ! $.fn.dataTable ) {
38
				$ = require('datatables.net')(root, $).$;
39
			}
40

41
			return factory( $, root, root.document );
42
		};
43
	}
44
	else {
45
		// Browser
46
		factory( jQuery, window, document );
47
	}
48
}(function( $, window, document, undefined ) {
49
'use strict';
50
var DataTable = $.fn.dataTable;
51

52

53
var _instance = 0;
54

55
/** 
56
 * AutoFill provides Excel like auto-fill features for a DataTable
57
 *
58
 * @class AutoFill
59
 * @constructor
60
 * @param {object} oTD DataTables settings object
61
 * @param {object} oConfig Configuration object for AutoFill
62
 */
63
var AutoFill = function( dt, opts )
64
{
65
	if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) {
66
		throw( "Warning: AutoFill requires DataTables 1.10.8 or greater");
67
	}
68

69
	// User and defaults configuration object
70
	this.c = $.extend( true, {},
71
		DataTable.defaults.autoFill,
72
		AutoFill.defaults,
73
		opts
74
	);
75

76
	/**
77
	 * @namespace Settings object which contains customisable information for AutoFill instance
78
	 */
79
	this.s = {
80
		/** @type {DataTable.Api} DataTables' API instance */
81
		dt: new DataTable.Api( dt ),
82

83
		/** @type {String} Unique namespace for events attached to the document */
84
		namespace: '.autoFill'+(_instance++),
85

86
		/** @type {Object} Cached dimension information for use in the mouse move event handler */
87
		scroll: {},
88

89
		/** @type {integer} Interval object used for smooth scrolling */
90
		scrollInterval: null,
91

92
		handle: {
93
			height: 0,
94
			width: 0
95
		},
96

97
		/**
98
		 * Enabled setting
99
		 * @type {Boolean}
100
		 */
101
		enabled: false
102
	};
103

104

105
	/**
106
	 * @namespace Common and useful DOM elements for the class instance
107
	 */
108
	this.dom = {
109
		/** @type {jQuery} AutoFill handle */
110
		handle: $('<div class="dt-autofill-handle"/>'),
111

112
		/**
113
		 * @type {Object} Selected cells outline - Need to use 4 elements,
114
		 *   otherwise the mouse over if you back into the selected rectangle
115
		 *   will be over that element, rather than the cells!
116
		 */
117
		select: {
118
			top:    $('<div class="dt-autofill-select top"/>'),
119
			right:  $('<div class="dt-autofill-select right"/>'),
120
			bottom: $('<div class="dt-autofill-select bottom"/>'),
121
			left:   $('<div class="dt-autofill-select left"/>')
122
		},
123

124
		/** @type {jQuery} Fill type chooser background */
125
		background: $('<div class="dt-autofill-background"/>'),
126

127
		/** @type {jQuery} Fill type chooser */
128
		list: $('<div class="dt-autofill-list">'+this.s.dt.i18n('autoFill.info', '')+'<ul/></div>'),
129

130
		/** @type {jQuery} DataTables scrolling container */
131
		dtScroll: null,
132

133
		/** @type {jQuery} Offset parent element */
134
		offsetParent: null
135
	};
136

137

138
	/* Constructor logic */
139
	this._constructor();
140
};
141

142

143

144
$.extend( AutoFill.prototype, {
145
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
146
	 * Public methods (exposed via the DataTables API below)
147
	 */
148
	enabled: function ()
149
	{
150
		return this.s.enabled;
151
	},
152

153

154
	enable: function ( flag )
155
	{
156
		var that = this;
157

158
		if ( flag === false ) {
159
			return this.disable();
160
		}
161

162
		this.s.enabled = true;
163

164
		this._focusListener();
165

166
		this.dom.handle.on( 'mousedown', function (e) {
167
			that._mousedown( e );
168
			return false;
169
		} );
170

171
		return this;
172
	},
173

174
	disable: function ()
175
	{
176
		this.s.enabled = false;
177

178
		this._focusListenerRemove();
179

180
		return this;
181
	},
182

183

184
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
185
	 * Constructor
186
	 */
187

188
	/**
189
	 * Initialise the RowReorder instance
190
	 *
191
	 * @private
192
	 */
193
	_constructor: function ()
194
	{
195
		var that = this;
196
		var dt = this.s.dt;
197
		var dtScroll = $('div.dataTables_scrollBody', this.s.dt.table().container());
198

199
		// Make the instance accessible to the API
200
		dt.settings()[0].autoFill = this;
201

202
		if ( dtScroll.length ) {
203
			this.dom.dtScroll = dtScroll;
204

205
			// Need to scroll container to be the offset parent
206
			if ( dtScroll.css('position') === 'static' ) {
207
				dtScroll.css( 'position', 'relative' );
208
			}
209
		}
210

211
		if ( this.c.enable !== false ) {
212
			this.enable();
213
		}
214

215
		dt.on( 'destroy.autoFill', function () {
216
			that._focusListenerRemove();
217
		} );
218
	},
219

220

221
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
222
	 * Private methods
223
	 */
224

225
	/**
226
	 * Display the AutoFill drag handle by appending it to a table cell. This
227
	 * is the opposite of the _detach method.
228
	 *
229
	 * @param  {node} node TD/TH cell to insert the handle into
230
	 * @private
231
	 */
232
	_attach: function ( node )
233
	{
234
		var dt = this.s.dt;
235
		var idx = dt.cell( node ).index();
236
		var handle = this.dom.handle;
237
		var handleDim = this.s.handle;
238

239
		if ( ! idx || dt.columns( this.c.columns ).indexes().indexOf( idx.column ) === -1 ) {
240
			this._detach();
241
			return;
242
		}
243

244
		if ( ! this.dom.offsetParent ) {
245
			// We attach to the table's offset parent
246
			this.dom.offsetParent = $( dt.table().node() ).offsetParent();
247
		}
248

249
		if ( ! handleDim.height || ! handleDim.width ) {
250
			// Append to document so we can get its size. Not expecting it to
251
			// change during the life time of the page
252
			handle.appendTo( 'body' );
253
			handleDim.height = handle.outerHeight();
254
			handleDim.width = handle.outerWidth();
255
		}
256

257
		// Might need to go through multiple offset parents
258
		var offset = this._getPosition( node, this.dom.offsetParent );
259

260
		this.dom.attachedTo = node;
261
		handle
262
			.css( {
263
				top: offset.top + node.offsetHeight - handleDim.height,
264
				left: offset.left + node.offsetWidth - handleDim.width
265
			} )
266
			.appendTo( this.dom.offsetParent );
267
	},
268

269

270
	/**
271
	 * Determine can the fill type should be. This can be automatic, or ask the
272
	 * end user.
273
	 *
274
	 * @param {array} cells Information about the selected cells from the key
275
	 *     up function
276
	 * @private
277
	 */
278
	_actionSelector: function ( cells )
279
	{
280
		var that = this;
281
		var dt = this.s.dt;
282
		var actions = AutoFill.actions;
283
		var available = [];
284

285
		// "Ask" each plug-in if it wants to handle this data
286
		$.each( actions, function ( key, action ) {
287
			if ( action.available( dt, cells ) ) {
288
				available.push( key );
289
			}
290
		} );
291

292
		if ( available.length === 1 && this.c.alwaysAsk === false ) {
293
			// Only one action available - enact it immediately
294
			var result = actions[ available[0] ].execute( dt, cells );
295
			this._update( result, cells );
296
		}
297
		else if ( available.length > 1 ) {
298
			// Multiple actions available - ask the end user what they want to do
299
			var list = this.dom.list.children('ul').empty();
300

301
			// Add a cancel option
302
			available.push( 'cancel' );
303

304
			$.each( available, function ( i, name ) {
305
				list.append( $('<li/>')
306
					.append(
307
						'<div class="dt-autofill-question">'+
308
							actions[ name ].option( dt, cells )+
309
						'<div>'
310
					)
311
					.append( $('<div class="dt-autofill-button">' )
312
						.append( $('<button class="'+AutoFill.classes.btn+'">'+dt.i18n('autoFill.button', '&gt;')+'</button>')
313
							.on( 'click', function () {
314
								var result = actions[ name ].execute(
315
									dt, cells, $(this).closest('li')
316
								);
317
								that._update( result, cells );
318

319
								that.dom.background.remove();
320
								that.dom.list.remove();
321
							} )
322
						)
323
					)
324
				);
325
			} );
326

327
			this.dom.background.appendTo( 'body' );
328
			this.dom.list.appendTo( 'body' );
329

330
			this.dom.list.css( 'margin-top', this.dom.list.outerHeight()/2 * -1 );
331
		}
332
	},
333

334

335
	/**
336
	 * Remove the AutoFill handle from the document
337
	 *
338
	 * @private
339
	 */
340
	_detach: function ()
341
	{
342
		this.dom.attachedTo = null;
343
		this.dom.handle.detach();
344
	},
345

346

347
	/**
348
	 * Draw the selection outline by calculating the range between the start
349
	 * and end cells, then placing the highlighting elements to draw a rectangle
350
	 *
351
	 * @param  {node}   target End cell
352
	 * @param  {object} e      Originating event
353
	 * @private
354
	 */
355
	_drawSelection: function ( target, e )
356
	{
357
		// Calculate boundary for start cell to this one
358
		var dt = this.s.dt;
359
		var start = this.s.start;
360
		var startCell = $(this.dom.start);
361
		var end = {
362
			row: this.c.vertical ?
363
				dt.rows( { page: 'current' } ).nodes().indexOf( target.parentNode ) :
364
				start.row,
365
			column: this.c.horizontal ?
366
				$(target).index() :
367
				start.column
368
		};
369
		var colIndx = dt.column.index( 'toData', end.column );
370
		var endRow =  dt.row( ':eq('+end.row+')', { page: 'current' } ); // Workaround for M581
371
		var endCell = $( dt.cell( endRow.index(), colIndx ).node() );
372

373
		// Be sure that is a DataTables controlled cell
374
		if ( ! dt.cell( endCell ).any() ) {
375
			return;
376
		}
377

378
		// if target is not in the columns available - do nothing
379
		if ( dt.columns( this.c.columns ).indexes().indexOf( colIndx ) === -1 ) {
380
			return;
381
		}
382

383
		this.s.end = end;
384

385
		var top, bottom, left, right, height, width;
386

387
		top    = start.row    < end.row    ? startCell : endCell;
388
		bottom = start.row    < end.row    ? endCell   : startCell;
389
		left   = start.column < end.column ? startCell : endCell;
390
		right  = start.column < end.column ? endCell   : startCell;
391

392
		top    = this._getPosition( top.get(0) ).top;
393
		left   = this._getPosition( left.get(0) ).left;
394
		height = this._getPosition( bottom.get(0) ).top + bottom.outerHeight() - top;
395
		width  = this._getPosition( right.get(0) ).left + right.outerWidth() - left;
396

397
		var select = this.dom.select;
398
		select.top.css( {
399
			top: top,
400
			left: left,
401
			width: width
402
		} );
403

404
		select.left.css( {
405
			top: top,
406
			left: left,
407
			height: height
408
		} );
409

410
		select.bottom.css( {
411
			top: top + height,
412
			left: left,
413
			width: width
414
		} );
415

416
		select.right.css( {
417
			top: top,
418
			left: left + width,
419
			height: height
420
		} );
421
	},
422

423

424
	/**
425
	 * Use the Editor API to perform an update based on the new data for the
426
	 * cells
427
	 *
428
	 * @param {array} cells Information about the selected cells from the key
429
	 *     up function
430
	 * @private
431
	 */
432
	_editor: function ( cells )
433
	{
434
		var dt = this.s.dt;
435
		var editor = this.c.editor;
436

437
		if ( ! editor ) {
438
			return;
439
		}
440

441
		// Build the object structure for Editor's multi-row editing
442
		var idValues = {};
443
		var nodes = [];
444
		var fields = editor.fields();
445

446
		for ( var i=0, ien=cells.length ; i<ien ; i++ ) {
447
			for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {
448
				var cell = cells[i][j];
449

450
				// Determine the field name for the cell being edited
451
				var col = dt.settings()[0].aoColumns[ cell.index.column ];
452
				var fieldName = col.editField;
453

454
				if ( fieldName === undefined ) {
455
					var dataSrc = col.mData;
456

457
					// dataSrc is the `field.data` property, but we need to set
458
					// using the field name, so we need to translate from the
459
					// data to the name
460
					for ( var k=0, ken=fields.length ; k<ken ; k++ ) {
461
						var field = editor.field( fields[k] );
462

463
						if ( field.dataSrc() === dataSrc ) {
464
							fieldName = field.name();
465
							break;
466
						}
467
					}
468
				}
469

470
				if ( ! fieldName ) {
471
					throw 'Could not automatically determine field data. '+
472
						'Please see https://datatables.net/tn/11';
473
				}
474

475
				if ( ! idValues[ fieldName ] ) {
476
					idValues[ fieldName ] = {};
477
				}
478

479
				var id = dt.row( cell.index.row ).id();
480
				idValues[ fieldName ][ id ] = cell.set;
481

482
				// Keep a list of cells so we can activate the bubble editing
483
				// with them
484
				nodes.push( cell.index );
485
			}
486
		}
487

488
		// Perform the edit using bubble editing as it allows us to specify
489
		// the cells to be edited, rather than using full rows
490
		editor
491
			.bubble( nodes, false )
492
			.multiSet( idValues )
493
			.submit();
494
	},
495

496

497
	/**
498
	 * Emit an event on the DataTable for listeners
499
	 *
500
	 * @param  {string} name Event name
501
	 * @param  {array} args Event arguments
502
	 * @private
503
	 */
504
	_emitEvent: function ( name, args )
505
	{
506
		this.s.dt.iterator( 'table', function ( ctx, i ) {
507
			$(ctx.nTable).triggerHandler( name+'.dt', args );
508
		} );
509
	},
510

511

512
	/**
513
	 * Attach suitable listeners (based on the configuration) that will attach
514
	 * and detach the AutoFill handle in the document.
515
	 *
516
	 * @private
517
	 */
518
	_focusListener: function ()
519
	{
520
		var that = this;
521
		var dt = this.s.dt;
522
		var namespace = this.s.namespace;
523
		var focus = this.c.focus !== null ?
524
			this.c.focus :
525
			dt.init().keys || dt.settings()[0].keytable ?
526
				'focus' :
527
				'hover';
528

529
		// All event listeners attached here are removed in the `destroy`
530
		// callback in the constructor
531
		if ( focus === 'focus' ) {
532
			dt
533
				.on( 'key-focus.autoFill', function ( e, dt, cell ) {
534
					that._attach( cell.node() );
535
				} )
536
				.on( 'key-blur.autoFill', function ( e, dt, cell ) {
537
					that._detach();
538
				} );
539
		}
540
		else if ( focus === 'click' ) {
541
			$(dt.table().body()).on( 'click'+namespace, 'td, th', function (e) {
542
				that._attach( this );
543
			} );
544

545
			$(document.body).on( 'click'+namespace, function (e) {
546
				if ( ! $(e.target).parents().filter( dt.table().body() ).length ) {
547
					that._detach();
548
				}
549
			} );
550
		}
551
		else {
552
			$(dt.table().body())
553
				.on( 'mouseenter'+namespace, 'td, th', function (e) {
554
					that._attach( this );
555
				} )
556
				.on( 'mouseleave'+namespace, function (e) {
557
					if ( $(e.relatedTarget).hasClass('dt-autofill-handle') ) {
558
						return;
559
					}
560

561
					that._detach();
562
				} );
563
		}
564
	},
565

566

567
	_focusListenerRemove: function ()
568
	{
569
		var dt = this.s.dt;
570

571
		dt.off( '.autoFill' );
572
		$(dt.table().body()).off( this.s.namespace );
573
		$(document.body).off( this.s.namespace );
574
	},
575

576

577
	/**
578
	 * Get the position of a node, relative to another, including any scrolling
579
	 * offsets.
580
	 * @param  {Node}   node         Node to get the position of
581
	 * @param  {jQuery} targetParent Node to use as the parent
582
	 * @return {object}              Offset calculation
583
	 * @private
584
	 */
585
	_getPosition: function ( node, targetParent )
586
	{
587
		var
588
			currNode = node,
589
			currOffsetParent,
590
			top = 0,
591
			left = 0;
592

593
		if ( ! targetParent ) {
594
			targetParent = $( $( this.s.dt.table().node() )[0].offsetParent );
595
		}
596

597
		do {
598
			// Don't use jQuery().position() the behaviour changes between 1.x and 3.x for
599
			// tables
600
			var positionTop = currNode.offsetTop;
601
			var positionLeft = currNode.offsetLeft;
602

603
			// jQuery doesn't give a `table` as the offset parent oddly, so use DOM directly
604
			currOffsetParent = $( currNode.offsetParent );
605

606
			top += positionTop + parseInt( currOffsetParent.css('border-top-width') || 0 ) * 1;
607
			left += positionLeft + parseInt( currOffsetParent.css('border-left-width') || 0 ) * 1;
608

609
			// Emergency fall back. Shouldn't happen, but just in case!
610
			if ( currNode.nodeName.toLowerCase() === 'body' ) {
611
				break;
612
			}
613

614
			currNode = currOffsetParent.get(0); // for next loop
615
		}
616
		while ( currOffsetParent.get(0) !== targetParent.get(0) )
617

618
		return {
619
			top: top,
620
			left: left
621
		};
622
	},
623

624

625
	/**
626
	 * Start mouse drag - selects the start cell
627
	 *
628
	 * @param  {object} e Mouse down event
629
	 * @private
630
	 */
631
	_mousedown: function ( e )
632
	{
633
		var that = this;
634
		var dt = this.s.dt;
635

636
		this.dom.start = this.dom.attachedTo;
637
		this.s.start = {
638
			row: dt.rows( { page: 'current' } ).nodes().indexOf( $(this.dom.start).parent()[0] ),
639
			column: $(this.dom.start).index()
640
		};
641

642
		$(document.body)
643
			.on( 'mousemove.autoFill', function (e) {
644
				that._mousemove( e );
645
			} )
646
			.on( 'mouseup.autoFill', function (e) {
647
				that._mouseup( e );
648
			} );
649

650
		var select = this.dom.select;
651
		var offsetParent = $( dt.table().node() ).offsetParent();
652
		select.top.appendTo( offsetParent );
653
		select.left.appendTo( offsetParent );
654
		select.right.appendTo( offsetParent );
655
		select.bottom.appendTo( offsetParent );
656

657
		this._drawSelection( this.dom.start, e );
658

659
		this.dom.handle.css( 'display', 'none' );
660

661
		// Cache scrolling information so mouse move doesn't need to read.
662
		// This assumes that the window and DT scroller will not change size
663
		// during an AutoFill drag, which I think is a fair assumption
664
		var scrollWrapper = this.dom.dtScroll;
665
		this.s.scroll = {
666
			windowHeight: $(window).height(),
667
			windowWidth:  $(window).width(),
668
			dtTop:        scrollWrapper ? scrollWrapper.offset().top : null,
669
			dtLeft:       scrollWrapper ? scrollWrapper.offset().left : null,
670
			dtHeight:     scrollWrapper ? scrollWrapper.outerHeight() : null,
671
			dtWidth:      scrollWrapper ? scrollWrapper.outerWidth() : null
672
		};
673
	},
674

675

676
	/**
677
	 * Mouse drag - selects the end cell and update the selection display for
678
	 * the end user
679
	 *
680
	 * @param  {object} e Mouse move event
681
	 * @private
682
	 */
683
	_mousemove: function ( e )
684
	{	
685
		var that = this;
686
		var dt = this.s.dt;
687
		var name = e.target.nodeName.toLowerCase();
688
		if ( name !== 'td' && name !== 'th' ) {
689
			return;
690
		}
691

692
		this._drawSelection( e.target, e );
693
		this._shiftScroll( e );
694
	},
695

696

697
	/**
698
	 * End mouse drag - perform the update actions
699
	 *
700
	 * @param  {object} e Mouse up event
701
	 * @private
702
	 */
703
	_mouseup: function ( e )
704
	{
705
		$(document.body).off( '.autoFill' );
706

707
		var that = this;
708
		var dt = this.s.dt;
709
		var select = this.dom.select;
710
		select.top.remove();
711
		select.left.remove();
712
		select.right.remove();
713
		select.bottom.remove();
714

715
		this.dom.handle.css( 'display', 'block' );
716

717
		// Display complete - now do something useful with the selection!
718
		var start = this.s.start;
719
		var end = this.s.end;
720

721
		// Haven't selected multiple cells, so nothing to do
722
		if ( start.row === end.row && start.column === end.column ) {
723
			return;
724
		}
725

726
		var startDt = dt.cell( ':eq('+start.row+')', start.column+':visible', {page:'current'} );
727

728
		// If Editor is active inside this cell (inline editing) we need to wait for Editor to
729
		// submit and then we can loop back and trigger the fill.
730
		if ( $('div.DTE', startDt.node()).length ) {
731
			var editor = dt.editor();
732

733
			editor
734
				.on( 'submitSuccess.dtaf close.dtaf', function () {
735
					editor.off( '.dtaf');
736

737
					setTimeout( function () {
738
						that._mouseup( e );
739
					}, 100 );
740
				} )
741
				.on( 'submitComplete.dtaf preSubmitCancelled.dtaf close.dtaf', function () {
742
					editor.off( '.dtaf');
743
				} );
744

745
			// Make the current input submit
746
			editor.submit();
747

748
			return;
749
		}
750

751
		// Build a matrix representation of the selected rows
752
		var rows       = this._range( start.row, end.row );
753
		var columns    = this._range( start.column, end.column );
754
		var selected   = [];
755
		var dtSettings = dt.settings()[0];
756
		var dtColumns  = dtSettings.aoColumns;
757
		var enabledColumns = dt.columns( this.c.columns ).indexes();
758

759
		// Can't use Array.prototype.map as IE8 doesn't support it
760
		// Can't use $.map as jQuery flattens 2D arrays
761
		// Need to use a good old fashioned for loop
762
		for ( var rowIdx=0 ; rowIdx<rows.length ; rowIdx++ ) {
763
			selected.push(
764
				$.map( columns, function (column) {
765
					var row = dt.row( ':eq('+rows[rowIdx]+')', {page:'current'} ); // Workaround for M581
766
					var cell = dt.cell( row.index(), column+':visible' );
767
					var data = cell.data();
768
					var cellIndex = cell.index();
769
					var editField = dtColumns[ cellIndex.column ].editField;
770

771
					if ( editField !== undefined ) {
772
						data = dtSettings.oApi._fnGetObjectDataFn( editField )( dt.row( cellIndex.row ).data() );
773
					}
774

775
					if ( enabledColumns.indexOf(cellIndex.column) === -1 ) {
776
						return;
777
					}
778

779
					return {
780
						cell:  cell,
781
						data:  data,
782
						label: cell.data(),
783
						index: cellIndex
784
					};
785
				} )
786
			);
787
		}
788

789
		this._actionSelector( selected );
790
		
791
		// Stop shiftScroll
792
		clearInterval( this.s.scrollInterval );
793
		this.s.scrollInterval = null;
794
	},
795

796

797
	/**
798
	 * Create an array with a range of numbers defined by the start and end
799
	 * parameters passed in (inclusive!).
800
	 * 
801
	 * @param  {integer} start Start
802
	 * @param  {integer} end   End
803
	 * @private
804
	 */
805
	_range: function ( start, end )
806
	{
807
		var out = [];
808
		var i;
809

810
		if ( start <= end ) {
811
			for ( i=start ; i<=end ; i++ ) {
812
				out.push( i );
813
			}
814
		}
815
		else {
816
			for ( i=start ; i>=end ; i-- ) {
817
				out.push( i );
818
			}
819
		}
820

821
		return out;
822
	},
823

824

825
	/**
826
	 * Move the window and DataTables scrolling during a drag to scroll new
827
	 * content into view. This is done by proximity to the edge of the scrolling
828
	 * container of the mouse - for example near the top edge of the window
829
	 * should scroll up. This is a little complicated as there are two elements
830
	 * that can be scrolled - the window and the DataTables scrolling view port
831
	 * (if scrollX and / or scrollY is enabled).
832
	 *
833
	 * @param  {object} e Mouse move event object
834
	 * @private
835
	 */
836
	_shiftScroll: function ( e )
837
	{
838
		var that = this;
839
		var dt = this.s.dt;
840
		var scroll = this.s.scroll;
841
		var runInterval = false;
842
		var scrollSpeed = 5;
843
		var buffer = 65;
844
		var
845
			windowY = e.pageY - document.body.scrollTop,
846
			windowX = e.pageX - document.body.scrollLeft,
847
			windowVert, windowHoriz,
848
			dtVert, dtHoriz;
849

850
		// Window calculations - based on the mouse position in the window,
851
		// regardless of scrolling
852
		if ( windowY < buffer ) {
853
			windowVert = scrollSpeed * -1;
854
		}
855
		else if ( windowY > scroll.windowHeight - buffer ) {
856
			windowVert = scrollSpeed;
857
		}
858

859
		if ( windowX < buffer ) {
860
			windowHoriz = scrollSpeed * -1;
861
		}
862
		else if ( windowX > scroll.windowWidth - buffer ) {
863
			windowHoriz = scrollSpeed;
864
		}
865

866
		// DataTables scrolling calculations - based on the table's position in
867
		// the document and the mouse position on the page
868
		if ( scroll.dtTop !== null && e.pageY < scroll.dtTop + buffer ) {
869
			dtVert = scrollSpeed * -1;
870
		}
871
		else if ( scroll.dtTop !== null && e.pageY > scroll.dtTop + scroll.dtHeight - buffer ) {
872
			dtVert = scrollSpeed;
873
		}
874

875
		if ( scroll.dtLeft !== null && e.pageX < scroll.dtLeft + buffer ) {
876
			dtHoriz = scrollSpeed * -1;
877
		}
878
		else if ( scroll.dtLeft !== null && e.pageX > scroll.dtLeft + scroll.dtWidth - buffer ) {
879
			dtHoriz = scrollSpeed;
880
		}
881

882
		// This is where it gets interesting. We want to continue scrolling
883
		// without requiring a mouse move, so we need an interval to be
884
		// triggered. The interval should continue until it is no longer needed,
885
		// but it must also use the latest scroll commands (for example consider
886
		// that the mouse might move from scrolling up to scrolling left, all
887
		// with the same interval running. We use the `scroll` object to "pass"
888
		// this information to the interval. Can't use local variables as they
889
		// wouldn't be the ones that are used by an already existing interval!
890
		if ( windowVert || windowHoriz || dtVert || dtHoriz ) {
891
			scroll.windowVert = windowVert;
892
			scroll.windowHoriz = windowHoriz;
893
			scroll.dtVert = dtVert;
894
			scroll.dtHoriz = dtHoriz;
895
			runInterval = true;
896
		}
897
		else if ( this.s.scrollInterval ) {
898
			// Don't need to scroll - remove any existing timer
899
			clearInterval( this.s.scrollInterval );
900
			this.s.scrollInterval = null;
901
		}
902

903
		// If we need to run the interval to scroll and there is no existing
904
		// interval (if there is an existing one, it will continue to run)
905
		if ( ! this.s.scrollInterval && runInterval ) {
906
			this.s.scrollInterval = setInterval( function () {
907
				// Don't need to worry about setting scroll <0 or beyond the
908
				// scroll bound as the browser will just reject that.
909
				if ( scroll.windowVert ) {
910
					document.body.scrollTop += scroll.windowVert;
911
				}
912
				if ( scroll.windowHoriz ) {
913
					document.body.scrollLeft += scroll.windowHoriz;
914
				}
915

916
				// DataTables scrolling
917
				if ( scroll.dtVert || scroll.dtHoriz ) {
918
					var scroller = that.dom.dtScroll[0];
919

920
					if ( scroll.dtVert ) {
921
						scroller.scrollTop += scroll.dtVert;
922
					}
923
					if ( scroll.dtHoriz ) {
924
						scroller.scrollLeft += scroll.dtHoriz;
925
					}
926
				}
927
			}, 20 );
928
		}
929
	},
930

931

932
	/**
933
	 * Update the DataTable after the user has selected what they want to do
934
	 *
935
	 * @param  {false|undefined} result Return from the `execute` method - can
936
	 *   be false internally to do nothing. This is not documented for plug-ins
937
	 *   and is used only by the cancel option.
938
	 * @param {array} cells Information about the selected cells from the key
939
	 *     up function, argumented with the set values
940
	 * @private
941
	 */
942
	_update: function ( result, cells )
943
	{
944
		// Do nothing on `false` return from an execute function
945
		if ( result === false ) {
946
			return;
947
		}
948

949
		var dt = this.s.dt;
950
		var cell;
951
		var columns = dt.columns( this.c.columns ).indexes();
952

953
		// Potentially allow modifications to the cells matrix
954
		this._emitEvent( 'preAutoFill', [ dt, cells ] );
955

956
		this._editor( cells );
957

958
		// Automatic updates are not performed if `update` is null and the
959
		// `editor` parameter is passed in - the reason being that Editor will
960
		// update the data once submitted
961
		var update = this.c.update !== null ?
962
			this.c.update :
963
			this.c.editor ?
964
				false :
965
				true;
966

967
		if ( update ) {
968
			for ( var i=0, ien=cells.length ; i<ien ; i++ ) {
969
				for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {
970
					cell = cells[i][j];
971

972
					if ( columns.indexOf(cell.index.column) !== -1 ) {
973
						cell.cell.data( cell.set );
974
					}
975
				}
976
			}
977

978
			dt.draw(false);
979
		}
980

981
		this._emitEvent( 'autoFill', [ dt, cells ] );
982
	}
983
} );
984

985

986
/**
987
 * AutoFill actions. The options here determine how AutoFill will fill the data
988
 * in the table when the user has selected a range of cells. Please see the
989
 * documentation on the DataTables site for full details on how to create plug-
990
 * ins.
991
 *
992
 * @type {Object}
993
 */
994
AutoFill.actions = {
995
	increment: {
996
		available: function ( dt, cells ) {
997
			var d = cells[0][0].label;
998

999
			// is numeric test based on jQuery's old `isNumeric` function
1000
			return !isNaN( d - parseFloat( d ) );
1001
		},
1002

1003
		option: function ( dt, cells ) {
1004
			return dt.i18n(
1005
				'autoFill.increment',
1006
				'Increment / decrement each cell by: <input type="number" value="1">'
1007
			);
1008
		},
1009

1010
		execute: function ( dt, cells, node ) {
1011
			var value = cells[0][0].data * 1;
1012
			var increment = $('input', node).val() * 1;
1013

1014
			for ( var i=0, ien=cells.length ; i<ien ; i++ ) {
1015
				for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {
1016
					cells[i][j].set = value;
1017

1018
					value += increment;
1019
				}
1020
			}
1021
		}
1022
	},
1023

1024
	fill: {
1025
		available: function ( dt, cells ) {
1026
			return true;
1027
		},
1028

1029
		option: function ( dt, cells ) {
1030
			return dt.i18n('autoFill.fill', 'Fill all cells with <i>%d</i>', cells[0][0].label );
1031
		},
1032

1033
		execute: function ( dt, cells, node ) {
1034
			var value = cells[0][0].data;
1035

1036
			for ( var i=0, ien=cells.length ; i<ien ; i++ ) {
1037
				for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {
1038
					cells[i][j].set = value;
1039
				}
1040
			}
1041
		}
1042
	},
1043

1044
	fillHorizontal: {
1045
		available: function ( dt, cells ) {
1046
			return cells.length > 1 && cells[0].length > 1;
1047
		},
1048

1049
		option: function ( dt, cells ) {
1050
			return dt.i18n('autoFill.fillHorizontal', 'Fill cells horizontally' );
1051
		},
1052

1053
		execute: function ( dt, cells, node ) {
1054
			for ( var i=0, ien=cells.length ; i<ien ; i++ ) {
1055
				for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {
1056
					cells[i][j].set = cells[i][0].data;
1057
				}
1058
			}
1059
		}
1060
	},
1061

1062
	fillVertical: {
1063
		available: function ( dt, cells ) {
1064
			return cells.length > 1;
1065
		},
1066

1067
		option: function ( dt, cells ) {
1068
			return dt.i18n('autoFill.fillVertical', 'Fill cells vertically' );
1069
		},
1070

1071
		execute: function ( dt, cells, node ) {
1072
			for ( var i=0, ien=cells.length ; i<ien ; i++ ) {
1073
				for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {
1074
					cells[i][j].set = cells[0][j].data;
1075
				}
1076
			}
1077
		}
1078
	},
1079

1080
	// Special type that does not make itself available, but is added
1081
	// automatically by AutoFill if a multi-choice list is shown. This allows
1082
	// sensible code reuse
1083
	cancel: {
1084
		available: function () {
1085
			return false;
1086
		},
1087

1088
		option: function ( dt ) {
1089
			return dt.i18n('autoFill.cancel', 'Cancel' );
1090
		},
1091

1092
		execute: function () {
1093
			return false;
1094
		}
1095
	}
1096
};
1097

1098

1099
/**
1100
 * AutoFill version
1101
 * 
1102
 * @static
1103
 * @type      String
1104
 */
1105
AutoFill.version = '2.3.9';
1106

1107

1108
/**
1109
 * AutoFill defaults
1110
 * 
1111
 * @namespace
1112
 */
1113
AutoFill.defaults = {
1114
	/** @type {Boolean} Ask user what they want to do, even for a single option */
1115
	alwaysAsk: false,
1116

1117
	/** @type {string|null} What will trigger a focus */
1118
	focus: null, // focus, click, hover
1119

1120
	/** @type {column-selector} Columns to provide auto fill for */
1121
	columns: '', // all
1122

1123
	/** @type {Boolean} Enable AutoFill on load */
1124
	enable: true,
1125

1126
	/** @type {boolean|null} Update the cells after a drag */
1127
	update: null, // false is editor given, true otherwise
1128

1129
	/** @type {DataTable.Editor} Editor instance for automatic submission */
1130
	editor: null,
1131

1132
	/** @type {boolean} Enable vertical fill */
1133
	vertical: true,
1134

1135
	/** @type {boolean} Enable horizontal fill */
1136
	horizontal: true
1137
};
1138

1139

1140
/**
1141
 * Classes used by AutoFill that are configurable
1142
 * 
1143
 * @namespace
1144
 */
1145
AutoFill.classes = {
1146
	/** @type {String} Class used by the selection button */
1147
	btn: 'btn'
1148
};
1149

1150

1151
/*
1152
 * API
1153
 */
1154
var Api = $.fn.dataTable.Api;
1155

1156
// Doesn't do anything - Not documented
1157
Api.register( 'autoFill()', function () {
1158
	return this;
1159
} );
1160

1161
Api.register( 'autoFill().enabled()', function () {
1162
	var ctx = this.context[0];
1163

1164
	return ctx.autoFill ?
1165
		ctx.autoFill.enabled() :
1166
		false;
1167
} );
1168

1169
Api.register( 'autoFill().enable()', function ( flag ) {
1170
	return this.iterator( 'table', function ( ctx ) {
1171
		if ( ctx.autoFill ) {
1172
			ctx.autoFill.enable( flag );
1173
		}
1174
	} );
1175
} );
1176

1177
Api.register( 'autoFill().disable()', function () {
1178
	return this.iterator( 'table', function ( ctx ) {
1179
		if ( ctx.autoFill ) {
1180
			ctx.autoFill.disable();
1181
		}
1182
	} );
1183
} );
1184

1185

1186
// Attach a listener to the document which listens for DataTables initialisation
1187
// events so we can automatically initialise
1188
$(document).on( 'preInit.dt.autofill', function (e, settings, json) {
1189
	if ( e.namespace !== 'dt' ) {
1190
		return;
1191
	}
1192

1193
	var init = settings.oInit.autoFill;
1194
	var defaults = DataTable.defaults.autoFill;
1195

1196
	if ( init || defaults ) {
1197
		var opts = $.extend( {}, init, defaults );
1198

1199
		if ( init !== false ) {
1200
			new AutoFill( settings, opts  );
1201
		}
1202
	}
1203
} );
1204

1205

1206
// Alias for access
1207
DataTable.AutoFill = AutoFill;
1208
DataTable.AutoFill = AutoFill;
1209

1210

1211
return AutoFill;
1212
}));
1213

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.