@jspreadsheet/autowidth
Advanced tools
Comparing version 1.0.1 to 2.1.1
202
index.js
/** | ||
* Plugin for auto width cols | ||
* | ||
* @version 1.0.1 | ||
* @version 2.1.1 | ||
* @author Guillaume Bonnaire <contact@gbonnaire.fr> | ||
@@ -12,2 +12,5 @@ * @website https://repo.gbonnaire.fr | ||
*/ | ||
if (! jspreadsheet && typeof(require) === 'function') { | ||
var jspreadsheet = require('jspreadsheet-pro'); | ||
} | ||
@@ -18,21 +21,58 @@ ;(function (global, factory) { | ||
global.jss_autoWidth = factory(); | ||
// Compatibility Old version | ||
global.jexcel_autoWidth = global.jss_autoWidth; | ||
}(this, (function () { | ||
return (function(instance, options) { | ||
return (function(spreadsheet, options, spreadsheetConfig) { | ||
// check Version of JSS | ||
if(parseInt(jspreadsheet.version().version.split(".")[0]) < 8) { | ||
console.error("Plugin \"auto Width\" not compatible with jspreadsheet " + jspreadsheet.version().version + ", Please use an older version of this plugin compatible. Go to https://github.com/GBonnaire/jspreadsheet-plugins-and-editors/"); | ||
return {}; | ||
} | ||
// Plugin object | ||
var plugin = {}; | ||
// Set options | ||
plugin.options = Object.assign({},options); | ||
var oldValue_styleTable = ""; | ||
var columnsToResize = []; | ||
// Options | ||
var defaultOptions = { | ||
fullsizeTable: false, | ||
} | ||
// Set default value | ||
if(plugin.options==null) { | ||
plugin.options = {}; | ||
} | ||
for(var property in defaultOptions) { | ||
if (!plugin.options.hasOwnProperty(property) || plugin.options[property]==null ) { | ||
plugin.options[property] = defaultOptions[property]; | ||
} | ||
} | ||
/** | ||
* Jexcel events | ||
* jspreadsheet events | ||
*/ | ||
plugin.onevent = function(event) { | ||
if(event=="onload" || event=="onresizecolumn") { | ||
init(); | ||
plugin.onevent = function(event, worksheet) { | ||
if(event=="onresizecolumn") { | ||
run(worksheet); | ||
} | ||
} | ||
plugin.beforeinit = function(worksheet) { | ||
columnsToResize = []; | ||
for(var ite_col=0; ite_col<worksheet.options.columns.length; ite_col++) { | ||
var column = worksheet.options.columns[ite_col]; | ||
if(column.width==null || column.width=='' || column.width=='auto') { | ||
columnsToResize.push(ite_col); | ||
worksheet.options.columns[ite_col].width = 100; | ||
} | ||
} | ||
} | ||
plugin.init = function(worksheet) { | ||
run(worksheet); | ||
} | ||
/** | ||
@@ -42,8 +82,8 @@ * run calculate width of columns and apply | ||
*/ | ||
function init() { | ||
saveStyle(); | ||
setLayoutAuto(); | ||
var colsWidth = getWidthColumns(); | ||
removeLayoutAuto(); | ||
setWidthColumn(colsWidth); | ||
var run = function(worksheet) { | ||
saveStyle(worksheet); | ||
setLayoutAuto(worksheet); | ||
var colsWidth = getWidthColumns(worksheet); | ||
removeLayoutAuto(worksheet); | ||
setWidthColumn(colsWidth, worksheet); | ||
} | ||
@@ -53,6 +93,7 @@ | ||
* save old style before change | ||
* @param {object} worksheet | ||
* @returns {undefined} | ||
*/ | ||
function saveStyle() { | ||
oldValue_styleTable = instance.table.style.cssText; | ||
function saveStyle(worksheet) { | ||
oldValue_styleTable = worksheet.table.style.cssText; | ||
} | ||
@@ -62,6 +103,7 @@ | ||
* set style table layout | ||
* @param {object} worksheet | ||
* @returns {undefined} | ||
*/ | ||
function setLayoutAuto() { | ||
instance.table.style.tableLayout="auto"; | ||
function setLayoutAuto(worksheet) { | ||
worksheet.table.style.tableLayout="auto"; | ||
} | ||
@@ -71,6 +113,7 @@ | ||
* remove style table layout | ||
* @param {object} worksheet | ||
* @returns {undefined} | ||
*/ | ||
function removeLayoutAuto() { | ||
instance.table.style.cssText = oldValue_styleTable; | ||
function removeLayoutAuto(worksheet) { | ||
worksheet.table.style.cssText = oldValue_styleTable; | ||
} | ||
@@ -80,17 +123,17 @@ | ||
* get Width offset of columns | ||
* @returns {jexcel.autoWidthL#16.jexcel.autoWidthL#16#L#17.getWidthColumns.cols} | ||
* @param {object} worksheet | ||
* @returns {Array} | ||
*/ | ||
function getWidthColumns() { | ||
function getWidthColumns(worksheet) { | ||
var cols = []; | ||
var tr = instance.table.querySelector("tbody>tr"); | ||
var tr = worksheet.rows[0].element; | ||
if(tr) { | ||
var tds = tr.querySelectorAll("td"); | ||
for(var ite_td=0; ite_td<tds.length; ite_td++) { | ||
if(ite_td==0) { // Skip index | ||
for(var ite_td=0; ite_td < tr.children.length; ite_td++) { | ||
if(ite_td == 0) { // Skip index | ||
continue; | ||
} | ||
var td = tds[ite_td]; | ||
var td = tr.children[ite_td]; | ||
cols.push(td.offsetWidth); | ||
} | ||
} | ||
} | ||
return cols; | ||
@@ -102,29 +145,61 @@ } | ||
* @param {array} colsWidth | ||
* @param {object} worksheet | ||
* @returns {undefined} | ||
*/ | ||
function setWidthColumn(colsWidth) { | ||
function setWidthColumn(colsWidth, worksheet) { | ||
// Autorize changement colsWidth | ||
var editable = instance.options.editable; | ||
instance.options.editable = true; | ||
instance.ignoreEvents = true; | ||
instance.ignoreCloud = true; | ||
instance.ignoreHistory = true; | ||
instance.ignorePersistance = true; | ||
var editable = worksheet.options.editable; | ||
worksheet.options.editable = true; | ||
enableIgnoreDispatch(worksheet.parent); | ||
if(instance.options.defaultColWidth==null || instance.options.defaultColWidth=="" || instance.options.defaultColWidth=="auto") { | ||
instance.options.defaultColWidth = 50; | ||
} else if(typeof instance.options.defaultColWidth == "string") { | ||
instance.options.defaultColWidth = parseInt(instance.options.defaultColWidth); | ||
if(worksheet.options.defaultColWidth==null || worksheet.options.defaultColWidth=="" || worksheet.options.defaultColWidth=="auto") { | ||
worksheet.options.defaultColWidth = 50; | ||
} else if(typeof worksheet.options.defaultColWidth == "string") { | ||
worksheet.options.defaultColWidth = parseInt(worksheet.options.defaultColWidth); | ||
} | ||
// Parse cols | ||
for(var ite_col=0; ite_col<instance.options.columns.length; ite_col++) { | ||
var column = instance.options.columns[ite_col]; | ||
if(column.width==="auto") { | ||
// Parse cols - calculate good size | ||
var width_table = 0; | ||
var cols_size_total = 0; | ||
// Manage Option fullsizeTable | ||
if(plugin.options.fullsizeTable) { | ||
width_table = worksheet.table.parentNode.parentNode.offsetWidth - 50 - 5; | ||
cols_size_total = colsWidth.reduce(function(acc,v) { return acc+v;}); | ||
for(var ite_col=0; ite_col<worksheet.options.columns.length; ite_col++) { | ||
var column = worksheet.options.columns[ite_col]; | ||
// Exclude hidden column | ||
if(column.type == "hidden") { | ||
continue; | ||
} | ||
if(columnsToResize.indexOf(ite_col) == -1) { | ||
width_table = Math.max(0, width_table - parseFloat(column.width)); | ||
cols_size_total = Math.max(0, cols_size_total - colsWidth[ite_col]); | ||
} else if(colsWidth[ite_col]<100) { | ||
cols_size_total = Math.max(0, cols_size_total + (100-colsWidth[ite_col])); | ||
} | ||
} | ||
} | ||
width_table = width_table - 7; | ||
// Parse cols - set width | ||
for(var ite_col=0; ite_col<worksheet.options.columns.length; ite_col++) { | ||
var column = worksheet.options.columns[ite_col]; | ||
// Exclude hidden column | ||
if(column.type == "hidden") { | ||
continue; | ||
} | ||
if(columnsToResize.indexOf(ite_col) > -1) { | ||
if(colsWidth[ite_col]) { | ||
var newWidth = Math.max(instance.options.defaultColWidth, colsWidth[ite_col]); | ||
var newWidth = Math.max(worksheet.options.defaultColWidth, colsWidth[ite_col]); | ||
} else { | ||
var newWidth = instance.options.defaultColWidth; | ||
var newWidth = worksheet.options.defaultColWidth; | ||
} | ||
instance.setWidth(ite_col, newWidth); | ||
if(plugin.options.fullsizeTable && cols_size_total < width_table) { | ||
newWidth = (newWidth / cols_size_total) * width_table; | ||
} | ||
worksheet.setWidth(ite_col, newWidth); | ||
} | ||
@@ -134,17 +209,26 @@ } | ||
// Resize old value | ||
instance.ignoreEvents = false; | ||
instance.ignoreCloud = false; | ||
instance.ignoreHistory = false; | ||
instance.ignorePersistance = false; | ||
instance.options.editable = editable; | ||
disableIgnoreDispatch(worksheet.parent); | ||
worksheet.options.editable = editable; | ||
} | ||
// Set default auto width on columns without width | ||
for(var ite_col=0; ite_col<instance.options.columns.length; ite_col++) { | ||
var column = instance.options.columns[ite_col]; | ||
if(column.width==null || column.width=='') { | ||
instance.options.columns[ite_col].width="auto"; | ||
} | ||
var saveIgnore={}; | ||
function enableIgnoreDispatch(inst) { | ||
if(saveIgnore["ignoreEvents"]==null) {saveIgnore["ignoreEvents"] = inst.ignoreEvents;} | ||
inst.ignoreEvents = true; | ||
if(saveIgnore["ignoreCloud"]==null) {saveIgnore["ignoreCloud"] = inst.ignoreCloud;} | ||
//inst.ignoreCloud = true; | ||
if(saveIgnore["ignoreHistory"]==null) {saveIgnore["ignoreHistory"] = inst.ignoreHistory;} | ||
inst.ignoreHistory = true; | ||
if(saveIgnore["ignorePersistence"]==null) {saveIgnore["ignorePersistence"] = inst.ignorePersistence;} | ||
inst.ignorePersistence = true; | ||
} | ||
function disableIgnoreDispatch(inst) { | ||
inst.ignoreEvents = saveIgnore["ignoreEvents"]; | ||
//inst.ignoreCloud = saveIgnore["ignoreCloud"];; | ||
inst.ignoreHistory = saveIgnore["ignoreHistory"];; | ||
inst.ignorePersistence = saveIgnore["ignorePersistence"]; | ||
saveIgnore = {}; | ||
} | ||
return plugin; | ||
@@ -151,0 +235,0 @@ }); |
{ | ||
"name": "@jspreadsheet/autowidth", | ||
"title": "Jspreadsheet plugins: auto width columns.", | ||
"description": "Auto width columns of jexcel.", | ||
"description": "Auto width columns of jspreadsheet-pro.", | ||
"author": { | ||
"name": "Guillaume Bonnaire <contact@gbonnaire.fr>", | ||
"name": "Guillaume Bonnaire", | ||
"email": "contact@gbonnaire.fr", | ||
"url": "https://repo.gbonnaire.fr" | ||
@@ -27,4 +28,7 @@ }, | ||
], | ||
"dependencies": { | ||
"jspreadsheet-pro": "^8.1.0" | ||
}, | ||
"main": "index.js", | ||
"version": "1.0.1" | ||
"version": "2.1.1" | ||
} |
@@ -25,3 +25,3 @@ ## jSpreadsheet Plugin : auto Width | ||
- [jSpreadsheet Pro v7](https://www.jspreadsheet.com/v7) | ||
- [jSpreadsheet Pro v8](https://www.jspreadsheet.com/v8) | ||
@@ -33,6 +33,6 @@ if you have a lot a plugins, add on the top autoWidth | ||
```HTML | ||
<script src="https://jspreadsheet.com/v7/jspreadsheet.js"></script> | ||
<script src="https://jspreadsheet.com/v7/jsuites.js"></script> | ||
<link rel="stylesheet" href="https://jspreadsheet.com/v7/jsuites.css" type="text/css" /> | ||
<link rel="stylesheet" href="https://jspreadsheet.com/v7/jspreadsheet.css" type="text/css" /> | ||
<script src="https://jspreadsheet.com/v8/jspreadsheet.js"></script> | ||
<script src="https://jsuites.net/v4/jsuites.js"></script> | ||
<link rel="stylesheet" href="https://jsuites.net/v4/jsuites.css" type="text/css" /> | ||
<link rel="stylesheet" href="https://jspreadsheet.com/v8/jspreadsheet.css" type="text/css" /> | ||
@@ -61,3 +61,3 @@ <script src="/path/to/autoWidth.min.js"></script> | ||
```HTML | ||
<script src="https://cdn.jsdelivr.net/gh/GBonnaire/jspreadsheet-plugins-and-editors@latest/plugins/dist/autoWidth.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/gh/GBonnaire/jspreadsheet-plugins-and-editors@latest/plugins/JSSV8/dist/autoWidth.min.js"></script> | ||
``` | ||
@@ -64,0 +64,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11885
200
1
+ Addedjspreadsheet-pro@^8.1.0