/**
* Kendo UI v2016.1.112 (http://www.telerik.com/kendo-ui)
* Copyright 2016 Telerik AD. All rights reserved.
*
* Kendo UI commercial licenses may be obtained at
* http://www.telerik.com/purchase/license-agreement/kendo-ui-complete
* If you do not own a commercial license, this file shall be governed by the trial license terms.
*/
(function (f, define) {
define('kendo.ooxml', ['kendo.core'], f);
}(function () {
var __meta__ = {
id: 'ooxml',
name: 'XLSX generation',
category: 'framework',
advanced: true,
depends: ['core']
};
(function ($, kendo) {
var RELS = '\r\n' + '' + '' + '' + '' + '';
var CORE = kendo.template('\r\n' + '' + '${creator}' + '${lastModifiedBy}' + '${created}' + '${modified}' + '');
var APP = kendo.template('\r\n' + '' + 'Microsoft Excel' + '0' + 'false' + '' + '' + '' + 'Worksheets' + '' + '' + '${sheets.length}' + '' + '' + '' + '' + '' + '# for (var idx = 0; idx < sheets.length; idx++) { #' + '# if (sheets[idx].options.title) { #' + '${sheets[idx].options.title}' + '# } else { #' + 'Sheet${idx+1}' + '# } #' + '# } #' + '' + '' + 'false' + 'false' + 'false' + '14.0300' + '');
var CONTENT_TYPES = kendo.template('\r\n' + '' + '' + '' + '' + '' + '' + '# for (var idx = 1; idx <= count; idx++) { #' + '' + '# } #' + '' + '' + '');
var WORKBOOK = kendo.template('\r\n' + '' + '' + '' + '' + '' + '' + '' + '# for (var idx = 0; idx < sheets.length; idx++) { #' + '# var options = sheets[idx].options; #' + '# var name = options.name || options.title #' + '# if (name) { #' + '' + '# } else { #' + '' + '# } #' + '# } #' + '' + '# if (definedNames.length) { #' + '' + ' # for (var di = 0; di < definedNames.length; di++) { #' + '' + '${definedNames[di].name}!$${definedNames[di].from}:$${definedNames[di].to}' + '' + ' # } #' + '' + '# } #' + '' + '');
var WORKSHEET = kendo.template('\r\n' + '' + '' + '' + '' + '# if (frozenRows || frozenColumns) { #' + '' + '# } #' + '' + '' + '' + '# if (columns && columns.length > 0) { #' + '' + '# for (var ci = 0; ci < columns.length; ci++) { #' + '# var column = columns[ci]; #' + '# var columnIndex = typeof column.index === "number" ? column.index + 1 : (ci + 1); #' + '# if (column.width) { #' + '' + '# } #' + '# } #' + '' + '# } #' + '' + '# for (var ri = 0; ri < data.length; ri++) { #' + '# var row = data[ri]; #' + '# var rowIndex = typeof row.index === "number" ? row.index + 1 : (ri + 1); #' + '' + '# for (var ci = 0; ci < row.data.length; ci++) { #' + '# var cell = row.data[ci];#' + '' + '# if (cell.formula != null) { #' + '${cell.formula}' + '# } #' + '# if (cell.value != null) { #' + '${cell.value}' + '# } #' + '' + '# } #' + '
' + '# } #' + '' + '# if (filter) { #' + '' + '# } #' + '# if (mergeCells.length) { #' + '' + '# for (var ci = 0; ci < mergeCells.length; ci++) { #' + '' + '# } #' + '' + '# } #' + '' + '');
var WORKBOOK_RELS = kendo.template('\r\n' + '' + '# for (var idx = 1; idx <= count; idx++) { #' + '' + '# } #' + '' + '' + '');
var SHARED_STRINGS = kendo.template('\r\n' + '' + '# for (var index in indexes) { #' + '${index.substring(1)}' + '# } #' + '');
var STYLES = kendo.template('' + '' + '' + '# for (var fi = 0; fi < formats.length; fi++) { #' + '# var format = formats[fi]; #' + '' + '# } #' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '# for (var fi = 0; fi < fonts.length; fi++) { #' + '# var font = fonts[fi]; #' + '' + '# if (font.fontSize) { #' + '' + '# } else { #' + '' + '# } #' + '# if (font.bold) { #' + '' + '# } #' + '# if (font.italic) { #' + '' + '# } #' + '# if (font.underline) { #' + '' + '# } #' + '# if (font.color) { #' + '' + '# } else { #' + '' + '# } #' + '# if (font.fontFamily) { #' + '' + '' + '# } else { #' + '' + '' + '' + '# } #' + '' + '# } #' + '' + '' + '' + '' + '# for (var fi = 0; fi < fills.length; fi++) { #' + '# var fill = fills[fi]; #' + '# if (fill.background) { #' + '' + '' + '' + '' + '' + '# } #' + '# } #' + '' + '' + '' + '# for (var bi = 0; bi < borders.length; bi++) { #' + '#= kendo.ooxml.borderTemplate(borders[bi]) #' + '# } #' + '' + '' + '' + '' + '' + '' + '# for (var si = 0; si < styles.length; si++) { #' + '# var style = styles[si]; #' + '' + '# if (style.textAlign || style.verticalAlign || style.wrap) { #' + '' + '# } #' + '' + '# } #' + '' + '' + '' + '' + '' + '' + '');
function numChar(colIndex) {
var letter = Math.floor(colIndex / 26) - 1;
return (letter >= 0 ? numChar(letter) : '') + String.fromCharCode(65 + colIndex % 26);
}
function ref(rowIndex, colIndex) {
return numChar(colIndex) + (rowIndex + 1);
}
function $ref(rowIndex, colIndex) {
return numChar(colIndex) + '$' + (rowIndex + 1);
}
function filterRowIndex(options) {
var frozenRows = options.frozenRows || (options.freezePane || {}).rowSplit || 1;
return frozenRows - 1;
}
function toWidth(px) {
return (px / 7 * 100 + 0.5) / 100;
}
function toHeight(px) {
return px * 0.75;
}
var DATE_EPOCH = new Date(1900, 0, 0);
var Worksheet = kendo.Class.extend({
init: function (options, sharedStrings, styles, borders) {
this.options = options;
this._strings = sharedStrings;
this._styles = styles;
this._borders = borders;
},
toXML: function (index) {
this._mergeCells = this.options.mergedCells || [];
this._rowsByIndex = [];
var rows = this.options.rows || [];
for (var i = 0; i < rows.length; i++) {
var ri = rows[i].index;
if (typeof ri !== 'number') {
ri = i;
}
rows[i].index = ri;
this._rowsByIndex[ri] = rows[i];
}
var data = [];
for (i = 0; i < rows.length; i++) {
data.push(this._row(rows[i], i));
}
data.sort(function (a, b) {
return a.index - b.index;
});
var filter = this.options.filter;
if (filter) {
filter = {
from: ref(filterRowIndex(this.options), filter.from),
to: ref(filterRowIndex(this.options), filter.to)
};
}
var freezePane = this.options.freezePane || {};
return WORKSHEET({
frozenColumns: this.options.frozenColumns || freezePane.colSplit,
frozenRows: this.options.frozenRows || freezePane.rowSplit,
columns: this.options.columns,
defaults: this.options.defaults || {},
data: data,
index: index,
mergeCells: this._mergeCells,
filter: filter
});
},
_row: function (row) {
var data = [];
var offset = 0;
var sheet = this;
var cellRefs = {};
$.each(row.cells, function (i, cell) {
if (!cell) {
return;
}
var cellIndex;
if (typeof cell.index === 'number') {
cellIndex = cell.index;
offset = cellIndex - i;
} else {
cellIndex = i + offset;
}
if (cell.colSpan) {
offset += cell.colSpan - 1;
}
var items = sheet._cell(cell, row.index, cellIndex);
$.each(items, function (i, cellData) {
if (cellRefs[cellData.ref]) {
return;
}
cellRefs[cellData.ref] = true;
data.push(cellData);
});
});
return {
data: data,
height: row.height,
index: row.index
};
},
_lookupString: function (value) {
var key = '$' + value;
var index = this._strings.indexes[key];
if (index !== undefined) {
value = index;
} else {
value = this._strings.indexes[key] = this._strings.uniqueCount;
this._strings.uniqueCount++;
}
this._strings.count++;
return value;
},
_lookupStyle: function (style) {
var json = kendo.stringify(style);
if (json == '{}') {
return 0;
}
var index = $.inArray(json, this._styles);
if (index < 0) {
index = this._styles.push(json) - 1;
}
return index + 1;
},
_lookupBorder: function (border) {
var json = kendo.stringify(border);
if (json == '{}') {
return;
}
var index = $.inArray(json, this._borders);
if (index < 0) {
index = this._borders.push(json) - 1;
}
return index + 1;
},
_cell: function (data, rowIndex, cellIndex) {
if (!data) {
return [];
}
var value = data.value;
var border = {};
if (data.borderLeft) {
border.left = data.borderLeft;
}
if (data.borderRight) {
border.right = data.borderRight;
}
if (data.borderTop) {
border.top = data.borderTop;
}
if (data.borderBottom) {
border.bottom = data.borderBottom;
}
border = this._lookupBorder(border);
var style = {
bold: data.bold,
color: data.color,
background: data.background,
italic: data.italic,
underline: data.underline,
fontFamily: data.fontFamily || data.fontName,
fontSize: data.fontSize,
format: data.format,
textAlign: data.textAlign || data.hAlign,
verticalAlign: data.verticalAlign || data.vAlign,
wrap: data.wrap,
borderId: border
};
var columns = this.options.columns || [];
var column = columns[cellIndex];
if (column && column.autoWidth) {
column.width = Math.max(column.width || 0, ('' + value).length);
}
var type = typeof value;
if (type === 'string') {
value = this._lookupString(value);
type = 's';
} else if (type === 'number') {
type = 'n';
} else if (type === 'boolean') {
type = 'b';
value = +value;
} else if (value && value.getTime) {
type = null;
var offset = (value.getTimezoneOffset() - DATE_EPOCH.getTimezoneOffset()) * kendo.date.MS_PER_MINUTE;
value = (value - DATE_EPOCH - offset) / kendo.date.MS_PER_DAY + 1;
if (!style.format) {
style.format = 'mm-dd-yy';
}
} else {
type = null;
value = null;
}
style = this._lookupStyle(style);
var cells = [];
var cellRef = ref(rowIndex, cellIndex);
cells.push({
value: value,
formula: data.formula,
type: type,
style: style,
ref: cellRef
});
var colSpan = data.colSpan || 1;
var rowSpan = data.rowSpan || 1;
var ci;
if (colSpan > 1 || rowSpan > 1) {
this._mergeCells.push(cellRef + ':' + ref(rowIndex + rowSpan - 1, cellIndex + colSpan - 1));
for (var ri = rowIndex + 1; ri < rowIndex + rowSpan; ri++) {
if (!this._rowsByIndex[ri]) {
this._rowsByIndex[ri] = {
index: ri,
cells: []
};
}
for (ci = cellIndex; ci < cellIndex + colSpan; ci++) {
this._rowsByIndex[ri].cells.splice(ci, 0, {});
}
}
for (ci = cellIndex + 1; ci < cellIndex + colSpan; ci++) {
cells.push({ ref: ref(rowIndex, ci) });
}
}
return cells;
}
});
var defaultFormats = {
'General': 0,
'0': 1,
'0.00': 2,
'#,##0': 3,
'#,##0.00': 4,
'0%': 9,
'0.00%': 10,
'0.00E+00': 11,
'# ?/?': 12,
'# ??/??': 13,
'mm-dd-yy': 14,
'd-mmm-yy': 15,
'd-mmm': 16,
'mmm-yy': 17,
'h:mm AM/PM': 18,
'h:mm:ss AM/PM': 19,
'h:mm': 20,
'h:mm:ss': 21,
'm/d/yy h:mm': 22,
'#,##0 ;(#,##0)': 37,
'#,##0 ;[Red](#,##0)': 38,
'#,##0.00;(#,##0.00)': 39,
'#,##0.00;[Red](#,##0.00)': 40,
'mm:ss': 45,
'[h]:mm:ss': 46,
'mmss.0': 47,
'##0.0E+0': 48,
'@': 49,
'[$-404]e/m/d': 27,
'm/d/yy': 30,
't0': 59,
't0.00': 60,
't#,##0': 61,
't#,##0.00': 62,
't0%': 67,
't0.00%': 68,
't# ?/?': 69,
't# ??/??': 70
};
function convertColor(color) {
if (color.length < 6) {
color = color.replace(/(\w)/g, function ($0, $1) {
return $1 + $1;
});
}
color = color.substring(1).toUpperCase();
if (color.length < 8) {
color = 'FF' + color;
}
return color;
}
var Workbook = kendo.Class.extend({
init: function (options) {
this.options = options || {};
this._strings = {
indexes: {},
count: 0,
uniqueCount: 0
};
this._styles = [];
this._borders = [];
this._sheets = $.map(this.options.sheets || [], $.proxy(function (options) {
options.defaults = this.options;
return new Worksheet(options, this._strings, this._styles, this._borders);
}, this));
},
toDataURL: function () {
if (typeof JSZip === 'undefined') {
throw new Error('JSZip not found. Check http://docs.telerik.com/kendo-ui/framework/excel/introduction#requirements for more details.');
}
var zip = new JSZip();
var docProps = zip.folder('docProps');
docProps.file('core.xml', CORE({
creator: this.options.creator || 'Kendo UI',
lastModifiedBy: this.options.creator || 'Kendo UI',
created: this.options.date || new Date().toJSON(),
modified: this.options.date || new Date().toJSON()
}));
var sheetCount = this._sheets.length;
docProps.file('app.xml', APP({ sheets: this._sheets }));
var rels = zip.folder('_rels');
rels.file('.rels', RELS);
var xl = zip.folder('xl');
var xlRels = xl.folder('_rels');
xlRels.file('workbook.xml.rels', WORKBOOK_RELS({ count: sheetCount }));
xl.file('workbook.xml', WORKBOOK({
sheets: this._sheets,
definedNames: $.map(this._sheets, function (sheet, index) {
var options = sheet.options;
var filter = options.filter;
if (filter && typeof filter.from !== 'undefined' && typeof filter.to !== 'undefined') {
return {
localSheetId: index,
name: options.name || options.title || 'Sheet' + (index + 1),
from: $ref(filterRowIndex(options), filter.from),
to: $ref(filterRowIndex(options), filter.to)
};
}
})
}));
var worksheets = xl.folder('worksheets');
for (var idx = 0; idx < sheetCount; idx++) {
worksheets.file(kendo.format('sheet{0}.xml', idx + 1), this._sheets[idx].toXML(idx));
}
var borders = $.map(this._borders, $.parseJSON);
var styles = $.map(this._styles, $.parseJSON);
var hasFont = function (style) {
return style.underline || style.bold || style.italic || style.color || style.fontFamily || style.fontSize;
};
var fonts = $.map(styles, function (style) {
if (style.color) {
style.color = convertColor(style.color);
}
if (hasFont(style)) {
return style;
}
});
var formats = $.map(styles, function (style) {
if (style.format && defaultFormats[style.format] === undefined) {
return style;
}
});
var fills = $.map(styles, function (style) {
if (style.background) {
style.background = convertColor(style.background);
return style;
}
});
xl.file('styles.xml', STYLES({
fonts: fonts,
fills: fills,
formats: formats,
borders: borders,
styles: $.map(styles, function (style) {
var result = {};
if (hasFont(style)) {
result.fontId = $.inArray(style, fonts) + 1;
}
if (style.background) {
result.fillId = $.inArray(style, fills) + 2;
}
result.textAlign = style.textAlign;
result.verticalAlign = style.verticalAlign;
result.wrap = style.wrap;
result.borderId = style.borderId;
if (style.format) {
if (defaultFormats[style.format] !== undefined) {
result.numFmtId = defaultFormats[style.format];
} else {
result.numFmtId = 165 + $.inArray(style, formats);
}
}
return result;
})
}));
xl.file('sharedStrings.xml', SHARED_STRINGS(this._strings));
zip.file('[Content_Types].xml', CONTENT_TYPES({ count: sheetCount }));
return 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,' + zip.generate({ compression: 'DEFLATE' });
}
});
function borderStyle(width) {
var alias = 'thin';
if (width === 2) {
alias = 'medium';
} else if (width === 3) {
alias = 'thick';
}
return alias;
}
function borderSideTemplate(name, style) {
var result = '';
if (style && style.size) {
result += '<' + name + ' style="' + borderStyle(style.size) + '">';
if (style.color) {
result += '';
}
result += '' + name + '>';
}
return result;
}
function borderTemplate(border) {
return '' + borderSideTemplate('left', border.left) + borderSideTemplate('right', border.right) + borderSideTemplate('top', border.top) + borderSideTemplate('bottom', border.bottom) + '';
}
kendo.ooxml = {
Workbook: Workbook,
Worksheet: Worksheet,
toWidth: toWidth,
toHeight: toHeight,
borderTemplate: borderTemplate
};
}(kendo.jQuery, kendo));
return kendo;
}, typeof define == 'function' && define.amd ? define : function (a1, a2, a3) {
(a3 || a2)();
}));