/*! AutoFill 2.6.0 * ©2008-2023 SpryMedia Ltd - datatables.net/license */ import jQuery from 'jquery'; import DataTable from 'datatables.net'; // Allow reassignment of the $ variable let $ = jQuery; /** * @summary AutoFill * @description Add Excel like click and drag auto-fill options to DataTables * @version 2.6.0 * @author SpryMedia Ltd (www.sprymedia.co.uk) * @copyright SpryMedia Ltd. * * This source file is free software, available under the following license: * MIT license - http://datatables.net/license/mit * * This source file is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details. * * For details please refer to: http://www.datatables.net */ var _instance = 0; /** * AutoFill provides Excel like auto-fill features for a DataTable * * @class AutoFill * @constructor * @param {object} oTD DataTables settings object * @param {object} oConfig Configuration object for AutoFill */ var AutoFill = function( dt, opts ) { if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) { throw( "Warning: AutoFill requires DataTables 1.10.8 or greater"); } // User and defaults configuration object this.c = $.extend( true, {}, DataTable.defaults.autoFill, AutoFill.defaults, opts ); /** * @namespace Settings object which contains customisable information for AutoFill instance */ this.s = { /** @type {DataTable.Api} DataTables' API instance */ dt: new DataTable.Api( dt ), /** @type {String} Unique namespace for events attached to the document */ namespace: '.autoFill'+(_instance++), /** @type {Object} Cached dimension information for use in the mouse move event handler */ scroll: {}, /** @type {integer} Interval object used for smooth scrolling */ scrollInterval: null, handle: { height: 0, width: 0 }, /** * Enabled setting * @type {Boolean} */ enabled: false }; /** * @namespace Common and useful DOM elements for the class instance */ this.dom = { closeButton: $('
×
'), /** @type {jQuery} AutoFill handle */ handle: $('
'), /** * @type {Object} Selected cells outline - Need to use 4 elements, * otherwise the mouse over if you back into the selected rectangle * will be over that element, rather than the cells! */ select: { top: $('
'), right: $('
'), bottom: $('
'), left: $('
') }, /** @type {jQuery} Fill type chooser background */ background: $('
'), /** @type {jQuery} Fill type chooser */ list: $('
'+this.s.dt.i18n('autoFill.info', '')+'
') .attr('aria-modal', true) .attr('role', 'dialog') .append('
'), /** @type {jQuery} DataTables scrolling container */ dtScroll: null, /** @type {jQuery} Offset parent element */ offsetParent: null }; /* Constructor logic */ this._constructor(); }; $.extend( AutoFill.prototype, { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Public methods (exposed via the DataTables API below) */ enabled: function () { return this.s.enabled; }, enable: function ( flag ) { var that = this; if ( flag === false ) { return this.disable(); } this.s.enabled = true; this._focusListener(); this.dom.handle.on( 'mousedown touchstart', function (e) { that._mousedown( e ); return false; } ); $(window).on('resize', function() { var handle = $('div.dt-autofill-handle'); if(handle.length > 0 && that.dom.attachedTo !== undefined) { that._attach(that.dom.attachedTo) } }) let orientationReset = function() { that.s.handle = { height: false, width: false }; $(that.dom.handle).css({ 'height': '', 'width': '' }) if(that.dom.attachedTo !== undefined) { that._attach(that.dom.attachedTo) } } $(window) .on('orientationchange', function() { setTimeout(function() { orientationReset(); setTimeout(orientationReset, 150); }, 50); }); return this; }, disable: function () { this.s.enabled = false; this._focusListenerRemove(); return this; }, /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Constructor */ /** * Initialise the RowReorder instance * * @private */ _constructor: function () { var that = this; var dt = this.s.dt; var dtScroll = $('div.dataTables_scrollBody', this.s.dt.table().container()); // Make the instance accessible to the API dt.settings()[0].autoFill = this; if ( dtScroll.length ) { this.dom.dtScroll = dtScroll; // Need to scroll container to be the offset parent if ( dtScroll.css('position') === 'static' ) { dtScroll.css( 'position', 'relative' ); } } if ( this.c.enable !== false ) { this.enable(); } dt.on( 'destroy.autoFill', function () { that._focusListenerRemove(); } ); }, /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Private methods */ /** * Display the AutoFill drag handle by appending it to a table cell. This * is the opposite of the _detach method. * * @param {node} node TD/TH cell to insert the handle into * @private */ _attach: function ( node ) { var dt = this.s.dt; var idx = dt.cell( node ).index(); var handle = this.dom.handle; var handleDim = this.s.handle; if ( ! idx || dt.columns( this.c.columns ).indexes().indexOf( idx.column ) === -1 ) { this._detach(); return; } if ( ! this.dom.offsetParent ) { // We attach to the table's offset parent this.dom.offsetParent = $( dt.table().node() ).offsetParent(); } if ( ! handleDim.height || ! handleDim.width ) { // Append to document so we can get its size. Not expecting it to // change during the life time of the page handle.appendTo( 'body' ); handleDim.height = handle.outerHeight(); handleDim.width = handle.outerWidth(); } // Might need to go through multiple offset parents var offset = this._getPosition( node, this.dom.offsetParent ); this.dom.attachedTo = node; handle .css( { top: offset.top + node.offsetHeight - handleDim.height, left: offset.left + node.offsetWidth - handleDim.width } ) .appendTo( this.dom.offsetParent ); }, /** * Determine can the fill type should be. This can be automatic, or ask the * end user. * * @param {array} cells Information about the selected cells from the key * up function * @private */ _actionSelector: function ( cells ) { var that = this; var dt = this.s.dt; var actions = AutoFill.actions; var available = []; // "Ask" each plug-in if it wants to handle this data $.each( actions, function ( key, action ) { if ( action.available( dt, cells ) ) { available.push( key ); } } ); if ( available.length === 1 && this.c.alwaysAsk === false ) { // Only one action available - enact it immediately var result = actions[ available[0] ].execute( dt, cells ); this._update( result, cells ); } else if ( available.length > 1 || this.c.alwaysAsk ) { // Multiple actions available - ask the end user what they want to do var list = this.dom.list.children('div.dt-autofill-list-items').empty(); // Add a cancel option available.push( 'cancel' ); $.each( available, function ( i, name ) { list.append( $('