LaravelTest
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 ){24if ( typeof define === 'function' && define.amd ) {25// AMD26define( ['jquery', 'datatables.net'], function ( $ ) {27return factory( $, window, document );28} );29}30else if ( typeof exports === 'object' ) {31// CommonJS32module.exports = function (root, $) {33if ( ! root ) {34root = window;35}36
37if ( ! $ || ! $.fn.dataTable ) {38$ = require('datatables.net')(root, $).$;39}40
41return factory( $, root, root.document );42};43}44else {45// Browser46factory( jQuery, window, document );47}48}(function( $, window, document, undefined ) {49'use strict';50var DataTable = $.fn.dataTable;51
52
53var _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*/
63var AutoFill = function( dt, opts )64{
65if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) {66throw( "Warning: AutoFill requires DataTables 1.10.8 or greater");67}68
69// User and defaults configuration object70this.c = $.extend( true, {},71DataTable.defaults.autoFill,72AutoFill.defaults,73opts
74);75
76/**77* @namespace Settings object which contains customisable information for AutoFill instance
78*/
79this.s = {80/** @type {DataTable.Api} DataTables' API instance */81dt: new DataTable.Api( dt ),82
83/** @type {String} Unique namespace for events attached to the document */84namespace: '.autoFill'+(_instance++),85
86/** @type {Object} Cached dimension information for use in the mouse move event handler */87scroll: {},88
89/** @type {integer} Interval object used for smooth scrolling */90scrollInterval: null,91
92handle: {93height: 0,94width: 095},96
97/**98* Enabled setting
99* @type {Boolean}
100*/
101enabled: false102};103
104
105/**106* @namespace Common and useful DOM elements for the class instance
107*/
108this.dom = {109/** @type {jQuery} AutoFill handle */110handle: $('<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*/
117select: {118top: $('<div class="dt-autofill-select top"/>'),119right: $('<div class="dt-autofill-select right"/>'),120bottom: $('<div class="dt-autofill-select bottom"/>'),121left: $('<div class="dt-autofill-select left"/>')122},123
124/** @type {jQuery} Fill type chooser background */125background: $('<div class="dt-autofill-background"/>'),126
127/** @type {jQuery} Fill type chooser */128list: $('<div class="dt-autofill-list">'+this.s.dt.i18n('autoFill.info', '')+'<ul/></div>'),129
130/** @type {jQuery} DataTables scrolling container */131dtScroll: null,132
133/** @type {jQuery} Offset parent element */134offsetParent: null135};136
137
138/* Constructor logic */139this._constructor();140};141
142
143
144$.extend( AutoFill.prototype, {145/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *146* Public methods (exposed via the DataTables API below)
147*/
148enabled: function ()149{150return this.s.enabled;151},152
153
154enable: function ( flag )155{156var that = this;157
158if ( flag === false ) {159return this.disable();160}161
162this.s.enabled = true;163
164this._focusListener();165
166this.dom.handle.on( 'mousedown', function (e) {167that._mousedown( e );168return false;169} );170
171return this;172},173
174disable: function ()175{176this.s.enabled = false;177
178this._focusListenerRemove();179
180return this;181},182
183
184/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *185* Constructor
186*/
187
188/**189* Initialise the RowReorder instance
190*
191* @private
192*/
193_constructor: function ()194{195var that = this;196var dt = this.s.dt;197var dtScroll = $('div.dataTables_scrollBody', this.s.dt.table().container());198
199// Make the instance accessible to the API200dt.settings()[0].autoFill = this;201
202if ( dtScroll.length ) {203this.dom.dtScroll = dtScroll;204
205// Need to scroll container to be the offset parent206if ( dtScroll.css('position') === 'static' ) {207dtScroll.css( 'position', 'relative' );208}209}210
211if ( this.c.enable !== false ) {212this.enable();213}214
215dt.on( 'destroy.autoFill', function () {216that._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{234var dt = this.s.dt;235var idx = dt.cell( node ).index();236var handle = this.dom.handle;237var handleDim = this.s.handle;238
239if ( ! idx || dt.columns( this.c.columns ).indexes().indexOf( idx.column ) === -1 ) {240this._detach();241return;242}243
244if ( ! this.dom.offsetParent ) {245// We attach to the table's offset parent246this.dom.offsetParent = $( dt.table().node() ).offsetParent();247}248
249if ( ! handleDim.height || ! handleDim.width ) {250// Append to document so we can get its size. Not expecting it to251// change during the life time of the page252handle.appendTo( 'body' );253handleDim.height = handle.outerHeight();254handleDim.width = handle.outerWidth();255}256
257// Might need to go through multiple offset parents258var offset = this._getPosition( node, this.dom.offsetParent );259
260this.dom.attachedTo = node;261handle
262.css( {263top: offset.top + node.offsetHeight - handleDim.height,264left: offset.left + node.offsetWidth - handleDim.width265} )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{280var that = this;281var dt = this.s.dt;282var actions = AutoFill.actions;283var available = [];284
285// "Ask" each plug-in if it wants to handle this data286$.each( actions, function ( key, action ) {287if ( action.available( dt, cells ) ) {288available.push( key );289}290} );291
292if ( available.length === 1 && this.c.alwaysAsk === false ) {293// Only one action available - enact it immediately294var result = actions[ available[0] ].execute( dt, cells );295this._update( result, cells );296}297else if ( available.length > 1 ) {298// Multiple actions available - ask the end user what they want to do299var list = this.dom.list.children('ul').empty();300
301// Add a cancel option302available.push( 'cancel' );303
304$.each( available, function ( i, name ) {305list.append( $('<li/>')306.append(307'<div class="dt-autofill-question">'+308actions[ 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', '>')+'</button>')313.on( 'click', function () {314var result = actions[ name ].execute(315dt, cells, $(this).closest('li')316);317that._update( result, cells );318
319that.dom.background.remove();320that.dom.list.remove();321} )322)323)324);325} );326
327this.dom.background.appendTo( 'body' );328this.dom.list.appendTo( 'body' );329
330this.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{342this.dom.attachedTo = null;343this.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 one358var dt = this.s.dt;359var start = this.s.start;360var startCell = $(this.dom.start);361var end = {362row: this.c.vertical ?363dt.rows( { page: 'current' } ).nodes().indexOf( target.parentNode ) :364start.row,365column: this.c.horizontal ?366$(target).index() :367start.column368};369var colIndx = dt.column.index( 'toData', end.column );370var endRow = dt.row( ':eq('+end.row+')', { page: 'current' } ); // Workaround for M581371var endCell = $( dt.cell( endRow.index(), colIndx ).node() );372
373// Be sure that is a DataTables controlled cell374if ( ! dt.cell( endCell ).any() ) {375return;376}377
378// if target is not in the columns available - do nothing379if ( dt.columns( this.c.columns ).indexes().indexOf( colIndx ) === -1 ) {380return;381}382
383this.s.end = end;384
385var top, bottom, left, right, height, width;386
387top = start.row < end.row ? startCell : endCell;388bottom = start.row < end.row ? endCell : startCell;389left = start.column < end.column ? startCell : endCell;390right = start.column < end.column ? endCell : startCell;391
392top = this._getPosition( top.get(0) ).top;393left = this._getPosition( left.get(0) ).left;394height = this._getPosition( bottom.get(0) ).top + bottom.outerHeight() - top;395width = this._getPosition( right.get(0) ).left + right.outerWidth() - left;396
397var select = this.dom.select;398select.top.css( {399top: top,400left: left,401width: width402} );403
404select.left.css( {405top: top,406left: left,407height: height408} );409
410select.bottom.css( {411top: top + height,412left: left,413width: width414} );415
416select.right.css( {417top: top,418left: left + width,419height: height420} );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{434var dt = this.s.dt;435var editor = this.c.editor;436
437if ( ! editor ) {438return;439}440
441// Build the object structure for Editor's multi-row editing442var idValues = {};443var nodes = [];444var fields = editor.fields();445
446for ( var i=0, ien=cells.length ; i<ien ; i++ ) {447for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {448var cell = cells[i][j];449
450// Determine the field name for the cell being edited451var col = dt.settings()[0].aoColumns[ cell.index.column ];452var fieldName = col.editField;453
454if ( fieldName === undefined ) {455var dataSrc = col.mData;456
457// dataSrc is the `field.data` property, but we need to set458// using the field name, so we need to translate from the459// data to the name460for ( var k=0, ken=fields.length ; k<ken ; k++ ) {461var field = editor.field( fields[k] );462
463if ( field.dataSrc() === dataSrc ) {464fieldName = field.name();465break;466}467}468}469
470if ( ! fieldName ) {471throw 'Could not automatically determine field data. '+472'Please see https://datatables.net/tn/11';473}474
475if ( ! idValues[ fieldName ] ) {476idValues[ fieldName ] = {};477}478
479var id = dt.row( cell.index.row ).id();480idValues[ fieldName ][ id ] = cell.set;481
482// Keep a list of cells so we can activate the bubble editing483// with them484nodes.push( cell.index );485}486}487
488// Perform the edit using bubble editing as it allows us to specify489// the cells to be edited, rather than using full rows490editor
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{506this.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{520var that = this;521var dt = this.s.dt;522var namespace = this.s.namespace;523var focus = this.c.focus !== null ?524this.c.focus :525dt.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 constructor531if ( focus === 'focus' ) {532dt
533.on( 'key-focus.autoFill', function ( e, dt, cell ) {534that._attach( cell.node() );535} )536.on( 'key-blur.autoFill', function ( e, dt, cell ) {537that._detach();538} );539}540else if ( focus === 'click' ) {541$(dt.table().body()).on( 'click'+namespace, 'td, th', function (e) {542that._attach( this );543} );544
545$(document.body).on( 'click'+namespace, function (e) {546if ( ! $(e.target).parents().filter( dt.table().body() ).length ) {547that._detach();548}549} );550}551else {552$(dt.table().body())553.on( 'mouseenter'+namespace, 'td, th', function (e) {554that._attach( this );555} )556.on( 'mouseleave'+namespace, function (e) {557if ( $(e.relatedTarget).hasClass('dt-autofill-handle') ) {558return;559}560
561that._detach();562} );563}564},565
566
567_focusListenerRemove: function ()568{569var dt = this.s.dt;570
571dt.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{587var588currNode = node,589currOffsetParent,590top = 0,591left = 0;592
593if ( ! targetParent ) {594targetParent = $( $( this.s.dt.table().node() )[0].offsetParent );595}596
597do {598// Don't use jQuery().position() the behaviour changes between 1.x and 3.x for599// tables600var positionTop = currNode.offsetTop;601var positionLeft = currNode.offsetLeft;602
603// jQuery doesn't give a `table` as the offset parent oddly, so use DOM directly604currOffsetParent = $( currNode.offsetParent );605
606top += positionTop + parseInt( currOffsetParent.css('border-top-width') || 0 ) * 1;607left += positionLeft + parseInt( currOffsetParent.css('border-left-width') || 0 ) * 1;608
609// Emergency fall back. Shouldn't happen, but just in case!610if ( currNode.nodeName.toLowerCase() === 'body' ) {611break;612}613
614currNode = currOffsetParent.get(0); // for next loop615}616while ( currOffsetParent.get(0) !== targetParent.get(0) )617
618return {619top: top,620left: left621};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{633var that = this;634var dt = this.s.dt;635
636this.dom.start = this.dom.attachedTo;637this.s.start = {638row: dt.rows( { page: 'current' } ).nodes().indexOf( $(this.dom.start).parent()[0] ),639column: $(this.dom.start).index()640};641
642$(document.body)643.on( 'mousemove.autoFill', function (e) {644that._mousemove( e );645} )646.on( 'mouseup.autoFill', function (e) {647that._mouseup( e );648} );649
650var select = this.dom.select;651var offsetParent = $( dt.table().node() ).offsetParent();652select.top.appendTo( offsetParent );653select.left.appendTo( offsetParent );654select.right.appendTo( offsetParent );655select.bottom.appendTo( offsetParent );656
657this._drawSelection( this.dom.start, e );658
659this.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 size663// during an AutoFill drag, which I think is a fair assumption664var scrollWrapper = this.dom.dtScroll;665this.s.scroll = {666windowHeight: $(window).height(),667windowWidth: $(window).width(),668dtTop: scrollWrapper ? scrollWrapper.offset().top : null,669dtLeft: scrollWrapper ? scrollWrapper.offset().left : null,670dtHeight: scrollWrapper ? scrollWrapper.outerHeight() : null,671dtWidth: scrollWrapper ? scrollWrapper.outerWidth() : null672};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{685var that = this;686var dt = this.s.dt;687var name = e.target.nodeName.toLowerCase();688if ( name !== 'td' && name !== 'th' ) {689return;690}691
692this._drawSelection( e.target, e );693this._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
707var that = this;708var dt = this.s.dt;709var select = this.dom.select;710select.top.remove();711select.left.remove();712select.right.remove();713select.bottom.remove();714
715this.dom.handle.css( 'display', 'block' );716
717// Display complete - now do something useful with the selection!718var start = this.s.start;719var end = this.s.end;720
721// Haven't selected multiple cells, so nothing to do722if ( start.row === end.row && start.column === end.column ) {723return;724}725
726var 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 to729// submit and then we can loop back and trigger the fill.730if ( $('div.DTE', startDt.node()).length ) {731var editor = dt.editor();732
733editor
734.on( 'submitSuccess.dtaf close.dtaf', function () {735editor.off( '.dtaf');736
737setTimeout( function () {738that._mouseup( e );739}, 100 );740} )741.on( 'submitComplete.dtaf preSubmitCancelled.dtaf close.dtaf', function () {742editor.off( '.dtaf');743} );744
745// Make the current input submit746editor.submit();747
748return;749}750
751// Build a matrix representation of the selected rows752var rows = this._range( start.row, end.row );753var columns = this._range( start.column, end.column );754var selected = [];755var dtSettings = dt.settings()[0];756var dtColumns = dtSettings.aoColumns;757var enabledColumns = dt.columns( this.c.columns ).indexes();758
759// Can't use Array.prototype.map as IE8 doesn't support it760// Can't use $.map as jQuery flattens 2D arrays761// Need to use a good old fashioned for loop762for ( var rowIdx=0 ; rowIdx<rows.length ; rowIdx++ ) {763selected.push(764$.map( columns, function (column) {765var row = dt.row( ':eq('+rows[rowIdx]+')', {page:'current'} ); // Workaround for M581766var cell = dt.cell( row.index(), column+':visible' );767var data = cell.data();768var cellIndex = cell.index();769var editField = dtColumns[ cellIndex.column ].editField;770
771if ( editField !== undefined ) {772data = dtSettings.oApi._fnGetObjectDataFn( editField )( dt.row( cellIndex.row ).data() );773}774
775if ( enabledColumns.indexOf(cellIndex.column) === -1 ) {776return;777}778
779return {780cell: cell,781data: data,782label: cell.data(),783index: cellIndex784};785} )786);787}788
789this._actionSelector( selected );790
791// Stop shiftScroll792clearInterval( this.s.scrollInterval );793this.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{807var out = [];808var i;809
810if ( start <= end ) {811for ( i=start ; i<=end ; i++ ) {812out.push( i );813}814}815else {816for ( i=start ; i>=end ; i-- ) {817out.push( i );818}819}820
821return 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{838var that = this;839var dt = this.s.dt;840var scroll = this.s.scroll;841var runInterval = false;842var scrollSpeed = 5;843var buffer = 65;844var845windowY = e.pageY - document.body.scrollTop,846windowX = e.pageX - document.body.scrollLeft,847windowVert, windowHoriz,848dtVert, dtHoriz;849
850// Window calculations - based on the mouse position in the window,851// regardless of scrolling852if ( windowY < buffer ) {853windowVert = scrollSpeed * -1;854}855else if ( windowY > scroll.windowHeight - buffer ) {856windowVert = scrollSpeed;857}858
859if ( windowX < buffer ) {860windowHoriz = scrollSpeed * -1;861}862else if ( windowX > scroll.windowWidth - buffer ) {863windowHoriz = scrollSpeed;864}865
866// DataTables scrolling calculations - based on the table's position in867// the document and the mouse position on the page868if ( scroll.dtTop !== null && e.pageY < scroll.dtTop + buffer ) {869dtVert = scrollSpeed * -1;870}871else if ( scroll.dtTop !== null && e.pageY > scroll.dtTop + scroll.dtHeight - buffer ) {872dtVert = scrollSpeed;873}874
875if ( scroll.dtLeft !== null && e.pageX < scroll.dtLeft + buffer ) {876dtHoriz = scrollSpeed * -1;877}878else if ( scroll.dtLeft !== null && e.pageX > scroll.dtLeft + scroll.dtWidth - buffer ) {879dtHoriz = scrollSpeed;880}881
882// This is where it gets interesting. We want to continue scrolling883// without requiring a mouse move, so we need an interval to be884// triggered. The interval should continue until it is no longer needed,885// but it must also use the latest scroll commands (for example consider886// that the mouse might move from scrolling up to scrolling left, all887// with the same interval running. We use the `scroll` object to "pass"888// this information to the interval. Can't use local variables as they889// wouldn't be the ones that are used by an already existing interval!890if ( windowVert || windowHoriz || dtVert || dtHoriz ) {891scroll.windowVert = windowVert;892scroll.windowHoriz = windowHoriz;893scroll.dtVert = dtVert;894scroll.dtHoriz = dtHoriz;895runInterval = true;896}897else if ( this.s.scrollInterval ) {898// Don't need to scroll - remove any existing timer899clearInterval( this.s.scrollInterval );900this.s.scrollInterval = null;901}902
903// If we need to run the interval to scroll and there is no existing904// interval (if there is an existing one, it will continue to run)905if ( ! this.s.scrollInterval && runInterval ) {906this.s.scrollInterval = setInterval( function () {907// Don't need to worry about setting scroll <0 or beyond the908// scroll bound as the browser will just reject that.909if ( scroll.windowVert ) {910document.body.scrollTop += scroll.windowVert;911}912if ( scroll.windowHoriz ) {913document.body.scrollLeft += scroll.windowHoriz;914}915
916// DataTables scrolling917if ( scroll.dtVert || scroll.dtHoriz ) {918var scroller = that.dom.dtScroll[0];919
920if ( scroll.dtVert ) {921scroller.scrollTop += scroll.dtVert;922}923if ( scroll.dtHoriz ) {924scroller.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 function945if ( result === false ) {946return;947}948
949var dt = this.s.dt;950var cell;951var columns = dt.columns( this.c.columns ).indexes();952
953// Potentially allow modifications to the cells matrix954this._emitEvent( 'preAutoFill', [ dt, cells ] );955
956this._editor( cells );957
958// Automatic updates are not performed if `update` is null and the959// `editor` parameter is passed in - the reason being that Editor will960// update the data once submitted961var update = this.c.update !== null ?962this.c.update :963this.c.editor ?964false :965true;966
967if ( update ) {968for ( var i=0, ien=cells.length ; i<ien ; i++ ) {969for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {970cell = cells[i][j];971
972if ( columns.indexOf(cell.index.column) !== -1 ) {973cell.cell.data( cell.set );974}975}976}977
978dt.draw(false);979}980
981this._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*/
994AutoFill.actions = {995increment: {996available: function ( dt, cells ) {997var d = cells[0][0].label;998
999// is numeric test based on jQuery's old `isNumeric` function1000return !isNaN( d - parseFloat( d ) );1001},1002
1003option: function ( dt, cells ) {1004return dt.i18n(1005'autoFill.increment',1006'Increment / decrement each cell by: <input type="number" value="1">'1007);1008},1009
1010execute: function ( dt, cells, node ) {1011var value = cells[0][0].data * 1;1012var increment = $('input', node).val() * 1;1013
1014for ( var i=0, ien=cells.length ; i<ien ; i++ ) {1015for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {1016cells[i][j].set = value;1017
1018value += increment;1019}1020}1021}1022},1023
1024fill: {1025available: function ( dt, cells ) {1026return true;1027},1028
1029option: function ( dt, cells ) {1030return dt.i18n('autoFill.fill', 'Fill all cells with <i>%d</i>', cells[0][0].label );1031},1032
1033execute: function ( dt, cells, node ) {1034var value = cells[0][0].data;1035
1036for ( var i=0, ien=cells.length ; i<ien ; i++ ) {1037for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {1038cells[i][j].set = value;1039}1040}1041}1042},1043
1044fillHorizontal: {1045available: function ( dt, cells ) {1046return cells.length > 1 && cells[0].length > 1;1047},1048
1049option: function ( dt, cells ) {1050return dt.i18n('autoFill.fillHorizontal', 'Fill cells horizontally' );1051},1052
1053execute: function ( dt, cells, node ) {1054for ( var i=0, ien=cells.length ; i<ien ; i++ ) {1055for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {1056cells[i][j].set = cells[i][0].data;1057}1058}1059}1060},1061
1062fillVertical: {1063available: function ( dt, cells ) {1064return cells.length > 1;1065},1066
1067option: function ( dt, cells ) {1068return dt.i18n('autoFill.fillVertical', 'Fill cells vertically' );1069},1070
1071execute: function ( dt, cells, node ) {1072for ( var i=0, ien=cells.length ; i<ien ; i++ ) {1073for ( var j=0, jen=cells[i].length ; j<jen ; j++ ) {1074cells[i][j].set = cells[0][j].data;1075}1076}1077}1078},1079
1080// Special type that does not make itself available, but is added1081// automatically by AutoFill if a multi-choice list is shown. This allows1082// sensible code reuse1083cancel: {1084available: function () {1085return false;1086},1087
1088option: function ( dt ) {1089return dt.i18n('autoFill.cancel', 'Cancel' );1090},1091
1092execute: function () {1093return false;1094}1095}1096};1097
1098
1099/**
1100* AutoFill version
1101*
1102* @static
1103* @type String
1104*/
1105AutoFill.version = '2.3.9';1106
1107
1108/**
1109* AutoFill defaults
1110*
1111* @namespace
1112*/
1113AutoFill.defaults = {1114/** @type {Boolean} Ask user what they want to do, even for a single option */1115alwaysAsk: false,1116
1117/** @type {string|null} What will trigger a focus */1118focus: null, // focus, click, hover1119
1120/** @type {column-selector} Columns to provide auto fill for */1121columns: '', // all1122
1123/** @type {Boolean} Enable AutoFill on load */1124enable: true,1125
1126/** @type {boolean|null} Update the cells after a drag */1127update: null, // false is editor given, true otherwise1128
1129/** @type {DataTable.Editor} Editor instance for automatic submission */1130editor: null,1131
1132/** @type {boolean} Enable vertical fill */1133vertical: true,1134
1135/** @type {boolean} Enable horizontal fill */1136horizontal: true1137};1138
1139
1140/**
1141* Classes used by AutoFill that are configurable
1142*
1143* @namespace
1144*/
1145AutoFill.classes = {1146/** @type {String} Class used by the selection button */1147btn: 'btn'1148};1149
1150
1151/*
1152* API
1153*/
1154var Api = $.fn.dataTable.Api;1155
1156// Doesn't do anything - Not documented
1157Api.register( 'autoFill()', function () {1158return this;1159} );1160
1161Api.register( 'autoFill().enabled()', function () {1162var ctx = this.context[0];1163
1164return ctx.autoFill ?1165ctx.autoFill.enabled() :1166false;1167} );1168
1169Api.register( 'autoFill().enable()', function ( flag ) {1170return this.iterator( 'table', function ( ctx ) {1171if ( ctx.autoFill ) {1172ctx.autoFill.enable( flag );1173}1174} );1175} );1176
1177Api.register( 'autoFill().disable()', function () {1178return this.iterator( 'table', function ( ctx ) {1179if ( ctx.autoFill ) {1180ctx.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) {1189if ( e.namespace !== 'dt' ) {1190return;1191}1192
1193var init = settings.oInit.autoFill;1194var defaults = DataTable.defaults.autoFill;1195
1196if ( init || defaults ) {1197var opts = $.extend( {}, init, defaults );1198
1199if ( init !== false ) {1200new AutoFill( settings, opts );1201}1202}1203} );1204
1205
1206// Alias for access
1207DataTable.AutoFill = AutoFill;1208DataTable.AutoFill = AutoFill;1209
1210
1211return AutoFill;1212}));1213