Skip to content
This repository has been archived by the owner on Jul 25, 2021. It is now read-only.

Commit

Permalink
format-type constraint, constants, and xls fallback when xlsx is not …
Browse files Browse the repository at this point in the history
…available
  • Loading branch information
clarketm committed May 5, 2017
1 parent a844047 commit 782fd7a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 38 deletions.
76 changes: 58 additions & 18 deletions dist/js/tableexport.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
}(this, function ($, Blob, saveAs, XLSX) {
'use strict';
// TODO: update typings (def file)
// TODO: update TS `definition` file
/**
* TableExport main library constructor
* @param selectors {jQuery} jQuery selector(s)
Expand Down Expand Up @@ -90,12 +90,26 @@
};
})();

var formatMap = {};
for (var type in _type) {
formatMap[type] = 0;
}

settings.formats.forEach(
function (key) {
XLSX && !isMobile && key === 'xls' ? key = 'xlsm' : false;
!XLSX || isMobile && key === 'xlsx' ? key = null : false;
key && context.setExportData(self.exporters[key].call(self, context));
var before = key;
(XLSX && !isMobile) && key === _type.xls
? key = _type.xlsm
: false;

(!XLSX || isMobile) && key === _type.xlsx
? key = _type.xls
: false;

if (!formatMap[key]) {
context.setExportData(self.exporters[key].call(self, context));
formatMap[key]++;
}
}
);
});
Expand Down Expand Up @@ -288,12 +302,12 @@
t: self.getType(val.className)
};
}).filter(function (val) {
return val;
return typeof val !== 'undefined';
});
}).map(function (val) {
return val && [].concat.apply([], val);
}).filter(function (val) {
return val;
return typeof val !== 'undefined';
}),
dataObject = TableExport.prototype.escapeHtml(
JSON.stringify({
Expand All @@ -304,7 +318,7 @@
})),
myContent = TableExport.prototype.xlsx.buttonContent,
myClass = TableExport.prototype.xlsx.defaultClass,
hashKey = _hashCode({uuid: context.uuid, type: 'xlsx'}),
hashKey = _hashCode({uuid: context.uuid, type: _type.xlsx}),
exportButton = settings.exportButtons && TableExport.prototype.createObjButton(
hashKey,
dataObject,
Expand Down Expand Up @@ -363,12 +377,12 @@
t: self.getType(val.className)
};
}).filter(function (val) {
return val;
return typeof val !== 'undefined';
});
}).map(function (val) {
return val && [].concat.apply([], val);
}).filter(function (val) {
return val;
return typeof val !== 'undefined';
}),
dataObject = TableExport.prototype.escapeHtml(
JSON.stringify({
Expand All @@ -379,7 +393,7 @@
})),
myContent = TableExport.prototype.xls.buttonContent,
myClass = TableExport.prototype.xls.defaultClass,
hashKey = _hashCode({uuid: context.uuid, type: 'xls'}),
hashKey = _hashCode({uuid: context.uuid, type: _type.xls}),
exportButton = settings.exportButtons && TableExport.prototype.createObjButton(
hashKey,
dataObject,
Expand Down Expand Up @@ -407,9 +421,12 @@
if (_hasClass(val, settings.emptyCSS)) {
return ' '
}

return val.textContent;
}).filter(function (val) {
return typeof val !== 'undefined';
}).join(colD);
}).filter(function (val) {
return typeof val !== 'undefined';
}).join(self.rowDel),
dataObject = TableExport.prototype.escapeHtml(
JSON.stringify({
Expand All @@ -420,7 +437,7 @@
})),
myContent = TableExport.prototype.xls.buttonContent,
myClass = TableExport.prototype.xls.defaultClass,
hashKey = _hashCode({uuid: context.uuid, type: 'xls'}),
hashKey = _hashCode({uuid: context.uuid, type: _type.xls}),
exportButton = settings.exportButtons && TableExport.prototype.createObjButton(
hashKey,
dataObject,
Expand Down Expand Up @@ -450,7 +467,11 @@
return ' '
}
return '"' + settings.formatValue(val.textContent.replace(/"/g, '""')) + '"';
}).filter(function (val) {
return typeof val !== 'undefined';
}).join(colD);
}).filter(function (val) {
return typeof val !== 'undefined';
}).join(self.rowDel),
dataObject = TableExport.prototype.escapeHtml(
JSON.stringify({
Expand All @@ -461,7 +482,7 @@
})),
myContent = TableExport.prototype.csv.buttonContent,
myClass = TableExport.prototype.csv.defaultClass,
hashKey = _hashCode({uuid: context.uuid, type: 'csv'}),
hashKey = _hashCode({uuid: context.uuid, type: _type.csv}),
exportButton = settings.exportButtons && TableExport.prototype.createObjButton(
hashKey,
dataObject,
Expand All @@ -473,7 +494,6 @@
_store.getInstance().setItem(hashKey, dataObject, true);
return hashKey;
},
// TODO: bug with `txt` ignoreRows and ignoreCols
txt: function (context) {
var self = this;
var settings = self.settings;
Expand All @@ -491,7 +511,11 @@
return ' '
}
return settings.formatValue(val.textContent);
}).filter(function (val) {
return typeof val !== 'undefined';
}).join(colD);
}).filter(function (val) {
return typeof val !== 'undefined';
}).join(self.rowDel),
dataObject = TableExport.prototype.escapeHtml(
JSON.stringify({
Expand All @@ -502,7 +526,7 @@
})),
myContent = TableExport.prototype.txt.buttonContent,
myClass = TableExport.prototype.txt.defaultClass,
hashKey = _hashCode({uuid: context.uuid, type: 'txt'}),
hashKey = _hashCode({uuid: context.uuid, type: _type.txt}),
exportButton = settings.exportButtons && TableExport.prototype.createObjButton(
hashKey,
dataObject,
Expand Down Expand Up @@ -787,6 +811,17 @@
};
})();

var _type = (function () {
return {
xlsx: 'xlsx',
xlsm: 'xlsm',
xls: 'xls',
csv: 'csv',
txt: 'txt'
};
})();


var _hashCode = (function () {
var hash = 0, i, char;

Expand Down Expand Up @@ -827,8 +862,14 @@
}

function _nodesArray(els) {
if (!(els instanceof NodeList) && (!$ || !(els instanceof $))) return [].concat(els);
return [].slice.call(els)
var result;
try {
result = [].slice.call(els);
if (!result.length) throw Error();
} catch (e) {
result = [].concat(els);
}
return result;
}

function _hasClass(el, cls) {
Expand Down Expand Up @@ -866,7 +907,6 @@

// alias the TableExport prototype
for (var prop in TableExport.prototype) {
// TODO: check compat
$.fn.tableExport[prop] = TableExport.prototype[prop];
}
}
Expand Down
Loading

0 comments on commit 782fd7a

Please sign in to comment.