@refinitiv-ui/efx-grid 6.0.45 → 6.0.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,774 @@
1
+ import { Dom } from "../../tr-grid-util/es6/Dom.js";
2
+ import { injectCss, prettifyCss } from "../../tr-grid-util/es6/Util.js";
3
+ import { Table } from "../../tr-grid-util/es6/Table.js";
4
+
5
+ import { PrintTrait } from "./PrintTrait.js";
6
+ import { SectionWriter } from "./SectionWriter.js";
7
+
8
+
9
+ /** @private
10
+ * @type {Node}
11
+ */
12
+ var _dummyNode = null;
13
+
14
+
15
+ /** TODO: Move this logic to PrintTrait
16
+ * @private
17
+ * @param {Object=} options
18
+ * @return {!Object}
19
+ */
20
+ var _getPageSize = function (options) {
21
+ var pageWidth = 0;
22
+ var pageHeight = 0;
23
+
24
+ if (options) {
25
+ if (options["pageWidth"]) {
26
+ pageWidth = options["pageWidth"];
27
+ }
28
+ if (options["pageHeight"]) {
29
+ pageHeight = options["pageHeight"];
30
+ }
31
+ }
32
+ if (!pageWidth) {
33
+ pageWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
34
+ }
35
+ if (!pageHeight) {
36
+ pageHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
37
+ }
38
+
39
+ // TODO: Subtract page margins
40
+ pageWidth -= 2; // HACK: Prevent chrome from populating table incorrectly
41
+ pageHeight -= 10; // HACK: Prevent chrome from populating table incorrectly
42
+
43
+ return {
44
+ "pageWidth": pageWidth,
45
+ "pageHeight": pageHeight
46
+ };
47
+ };
48
+
49
+ /** @private
50
+ * @param {Node} nodeA
51
+ * @param {Node} nodeB
52
+ */
53
+ var swapNode = function (nodeA, nodeB) {
54
+ if (!nodeA || !nodeB || !nodeA.parentNode || !nodeB.parentNode) {
55
+ Dom.removeParent(nodeA);
56
+ Dom.removeParent(nodeB);
57
+ return;
58
+ }
59
+
60
+ if (!_dummyNode) {
61
+ _dummyNode = document.createElement("span");
62
+ }
63
+
64
+ nodeA.parentNode.replaceChild(_dummyNode, nodeA);
65
+ nodeB.parentNode.replaceChild(nodeA, nodeB);
66
+ _dummyNode.parentNode.replaceChild(nodeB, _dummyNode);
67
+ };
68
+ /** @private
69
+ * @param {Node} fromNode
70
+ * @param {Node} toNode
71
+ */
72
+ var copyNode = function (fromNode, toNode) {
73
+ if (fromNode && toNode) {
74
+ var i, attrs;
75
+ if (toNode.hasAttributes()) {
76
+ attrs = toNode.attributes;
77
+ for (i = attrs.length; --i >= 0;) {
78
+ toNode.removeAttribute(attrs[i].name);
79
+ }
80
+ }
81
+
82
+ if (fromNode.hasAttributes()) {
83
+ attrs = fromNode.attributes;
84
+ for (i = attrs.length; --i >= 0;) {
85
+ toNode.setAttribute(attrs[i].name, attrs[i].value);
86
+ }
87
+ }
88
+
89
+ Dom.removeChildren(toNode);
90
+
91
+ var chdr = fromNode.childNodes;
92
+ var childCount = chdr.length;
93
+ for (i = 0; i < childCount; ++i) {
94
+ toNode.appendChild(chdr[i].cloneNode(true));
95
+ }
96
+ }
97
+ };
98
+
99
+ /** @private
100
+ * @param {Table|SubTable} tbl
101
+ * @param {number} colIndex
102
+ * @param {string} alignment
103
+ */
104
+ var _setColumnAlignment = function (tbl, colIndex, alignment) {
105
+ if (!alignment || alignment === "default") {
106
+ return;
107
+ }
108
+ var cells = tbl.getCellsInColumn(colIndex);
109
+ if (!cells) return;
110
+ for (var i = cells.length; --i >= 0;) {
111
+ var cell = cells[i];
112
+ cell.classList.add("tr-align-" + alignment);
113
+ }
114
+ };
115
+
116
+ /** @private
117
+ * @param {*} grid grid element, currently supports atlas-blotter, emerald-grid, tr.CompositeGrid, rt.Grid and Core
118
+ * @return {Object} core grid
119
+ */
120
+ var _getCoreGrid = function (grid) {
121
+ var core = null;
122
+ try {
123
+ if (grid.api) { // emerald-grid or atlas-blotter
124
+ core = grid.api.getCoreGrid();
125
+ } else if (grid.getCoreGrid) { // tr.CompositeGrid or rt.Grid
126
+ core = grid.getCoreGrid();
127
+ } else if (grid.getPlugin) { // coreGrid
128
+ core = grid;
129
+ }
130
+ // TODO: support react/angular wrapper
131
+ } catch (err) {
132
+ console.log("Cannot print the given object");
133
+ }
134
+ return core;
135
+ };
136
+
137
+ /** @namespaces
138
+ */
139
+ var GridPrinter = {};
140
+ /** @private
141
+ * @type {string}
142
+ */
143
+ GridPrinter._styles = ""; // Static variable
144
+ /** @type {PrintTrait}
145
+ * @private
146
+ */
147
+ GridPrinter._printTrait = null;
148
+ /** @type {Element}
149
+ * @private
150
+ */
151
+ GridPrinter._blankPage = null;
152
+ /** @type {tr.Grid}
153
+ * @private
154
+ */
155
+ GridPrinter._grid = null;
156
+ /** @type {boolean}
157
+ * @private
158
+ */
159
+ GridPrinter._isObserving = false;
160
+ /** @type {!Object}
161
+ * @private
162
+ */
163
+ GridPrinter._options = {};
164
+
165
+ /** @public
166
+ * @param {GridPrinter.Options} options
167
+ */
168
+ GridPrinter.setPrintOptions = function (options) {
169
+ if (options) {
170
+ GridPrinter._options = options;
171
+ } else {
172
+ GridPrinter._options = {};
173
+ }
174
+ };
175
+
176
+ /** @public
177
+ * @param {HTMLIFrameElement=} iFrameElement If not specified, current window is used instead. Specify null to un-observe existing window object.
178
+ */
179
+ GridPrinter.observe = function (iFrameElement) {
180
+ var pt = GridPrinter._getPrintTrait();
181
+ pt.observe(iFrameElement);
182
+ GridPrinter._isObserving = pt.isObserving();
183
+ };
184
+ /** @public
185
+ */
186
+ GridPrinter.unobserve = function () {
187
+ GridPrinter._getPrintTrait().unobserve();
188
+ };
189
+ /** @public
190
+ * @param {boolean=} bool
191
+ */
192
+ GridPrinter.enableDebugMode = function (bool) {
193
+ PrintTrait.DEBUG = bool !== false;
194
+ };
195
+
196
+ /** @public
197
+ * @param {tr.Grid} grid
198
+ * @param {Object=} options
199
+ * @return {!Object}
200
+ */
201
+ GridPrinter.getPreFlightInfo = function (grid, options) {
202
+ var pfInfo = options || {};
203
+ if (!pfInfo.pageCount) {
204
+ pfInfo.pageCount = 0;
205
+ }
206
+ if (!grid) {
207
+ return pfInfo;
208
+ }
209
+
210
+ var colCount = grid.getColumnCount();
211
+ if (!colCount) {
212
+ return pfInfo;
213
+ }
214
+ pfInfo._calculated = false;
215
+ if (GridPrinter._options["pageWidth"]) {
216
+ options.pageWidth = GridPrinter._options["pageWidth"];
217
+ }
218
+ if (GridPrinter._options["pageHeight"]) {
219
+ options.pageHeight = GridPrinter._options["pageHeight"];
220
+ }
221
+
222
+ var pageSize = _getPageSize(options);
223
+ var pageWidth = pfInfo.pageWidth = pageSize.pageWidth;
224
+ var pageHeight = pfInfo.pageHeight = pageSize.pageHeight;
225
+
226
+ // Find primary column that will exist in every page
227
+ var c;
228
+ var primaryColIndex = 0;
229
+ var identifierCol = GridPrinter._options["identifierField"] || GridPrinter._options["primaryColumn"];
230
+ // if(typeof identifierCol === "string") {
231
+ // for(c = 0; c < colCount; ++c) {
232
+ // if() {
233
+ // }
234
+ // }
235
+ // } else
236
+ if (typeof identifierCol === "number") {
237
+ primaryColIndex = identifierCol;
238
+ }
239
+
240
+ var primaryCol = pfInfo.primaryColumn = GridPrinter._newColInfo(grid, primaryColIndex);
241
+
242
+ // Collect properties from all visible columns
243
+ // Create a column set that fit the page width
244
+ // Create multiple column sets for single grid
245
+ var colSets = []; // Each set will fit a single page
246
+ var colSet, colInfo;
247
+ // var defaultMinWidth = 50; // WARNING: Hardcoded value
248
+ var availSpace = 0;
249
+ for (c = 0; c < colCount; ++c) {
250
+ if (c == primaryCol.index) {
251
+ continue;
252
+ }
253
+
254
+ var colVisible = grid.isColumnVisible(c);
255
+ if (!colVisible) {
256
+ continue;
257
+ }
258
+ colInfo = GridPrinter._newColInfo(grid, c);
259
+
260
+ availSpace -= colInfo.width;
261
+ if (availSpace <= 0 || !colSets.length) {
262
+ colSet = {
263
+ columns: []
264
+ };
265
+ colSet.columns.push(primaryCol); // Primary column must exist as the first column on each page
266
+ availSpace = pageWidth - primaryCol.width;
267
+ availSpace -= colInfo.width;
268
+
269
+ colSets.push(colSet);
270
+ }
271
+ colSet.columns.push(colInfo);
272
+ }
273
+
274
+
275
+ var colSetCount = colSets.length;
276
+ if (!colSetCount) {
277
+ colSetCount = 1;
278
+ colSets.push([primaryCol]);
279
+ }
280
+ pfInfo.columnSets = colSets;
281
+
282
+ // Resolve scalable columns and width
283
+ for (var i = 0; i < colSetCount; ++i) {
284
+ colSet = colSets[i];
285
+ colCount = colSet.columns.length;
286
+ var scalableColumns = [];
287
+ availSpace = pageWidth;
288
+ for (c = 0; c < colCount; ++c) {
289
+ colInfo = colSet.columns[c];
290
+ if (colInfo.scalability) {
291
+ scalableColumns.push(colInfo);
292
+ } else {
293
+ availSpace -= colInfo.width;
294
+ }
295
+ }
296
+ var scalableColCount = scalableColumns.length;
297
+ if (scalableColCount) {
298
+ var avgWidth = (availSpace / scalableColCount); // TODO: Take min width into account
299
+ for (c = 0; c < scalableColCount; ++c) {
300
+ scalableColumns[c].width = (avgWidth * (c + 1) | 0) - (avgWidth * c | 0);
301
+ }
302
+ }
303
+
304
+ var totalWidth = 0;
305
+ var widths = colSet.widths = new Array(colCount);
306
+ for (c = 0; c < colCount; ++c) {
307
+ colInfo = colSet.columns[c];
308
+ widths[c] = colInfo.width;
309
+ totalWidth += colInfo.width;
310
+ }
311
+ colSet.width = totalWidth;
312
+ }
313
+
314
+ // Find total row count per table. Remove all empty rows after the last occupied row.
315
+ var dv = grid.getDataSource();
316
+ var totalRowCount = pfInfo.totalRowcount = dv.getVisibleRowCount();
317
+
318
+ // Find cutoff point for each page
319
+ var titleSect = grid.getSection("title");
320
+ var headerHeight = pfInfo.headerHeight = (titleSect.isVisible()) ? titleSect.getDefaultRowHeight() : 0;
321
+ var rowHeight = pfInfo.rowHeight = grid.getSection("content").getDefaultRowHeight();
322
+ var maxRowPerPage = (pageHeight - headerHeight) / rowHeight | 0;
323
+ if (!(maxRowPerPage > 0)) { // NaN, negative number, or zero is not allowed.
324
+ maxRowPerPage = 1; // number of content rows
325
+ }
326
+ pfInfo.maxRowPerPage = maxRowPerPage;
327
+
328
+ var gridCount = pfInfo.gridCount = Math.ceil(totalRowCount / maxRowPerPage); // Number of grid require to render all of the rows. Grid must fit a single page
329
+ // var lastPageRowCount = totalRowCount - (totalRowCount / maxRowPerPage | 0);
330
+ pfInfo.pageCount = gridCount * colSetCount;
331
+
332
+
333
+ // TODO: Stack the table horizontally, if there is enough space
334
+ // if(colSetCount == 1 && pageCount > 1) {
335
+ // availSpace = pageWidth;
336
+ // tableWidth = tables[0]._width;
337
+ // var numGridPerPage = Math.floor(pageWidth / tableWidth);
338
+
339
+ // var margin = 4;
340
+ // var takenSpace = numTablePerPage * tableWidth + (numTablePerPage - 1) * margin;
341
+ // if(takenSpace > pageWidth) {
342
+ // numGridPerPage--; // There is not enough space for margin
343
+ // }
344
+ // if(numGridPerPage > 1) {
345
+
346
+ // }
347
+ // }
348
+
349
+ pfInfo._calculated = true; // Internal flag
350
+
351
+ return pfInfo;
352
+ };
353
+
354
+ /** @public
355
+ * @param {*} grid grid element, currently supports atlas-blotter, emerald-grid, tr.CompositeGrid, rt.Grid and Core
356
+ * @param {Object=} options
357
+ * @return {Element}
358
+ */
359
+ GridPrinter.createPrintElement = function (grid, options) {
360
+ if (!grid) {
361
+ grid = GridPrinter._grid;
362
+ } else {
363
+ grid = _getCoreGrid(grid);
364
+ }
365
+
366
+ if (!grid) {
367
+ return null;
368
+ }
369
+
370
+ var pfInfo = options || GridPrinter._printInfo;
371
+ if (!pfInfo || !pfInfo._calculated) {
372
+ pfInfo = GridPrinter.getPreFlightInfo(grid, pfInfo);
373
+ }
374
+
375
+ // TODO: Check if we need to recalculate everything again
376
+ var maxRowPerPage = pfInfo.maxRowPerPage;
377
+ var totalRowCount = pfInfo.totalRowcount;
378
+ var rowHeight = pfInfo.rowHeight;
379
+ var headerHeight = pfInfo.headerHeight;
380
+ var colSets = pfInfo.columnSets;
381
+ var colSetCount = pfInfo.columnSets.length;
382
+ var primaryColumn = pfInfo.primaryColumn;
383
+ var gridCount = pfInfo.gridCount;
384
+ var contentWriter = new SectionWriter(); // content section
385
+ var headerWriter = new SectionWriter();
386
+
387
+ var gridContentSection = grid.getSectionSettings("content");
388
+ gridContentSection.snapshot(contentWriter);
389
+
390
+ var gridHeaderSection = grid.getSectionSettings("title");
391
+ if (gridHeaderSection) {
392
+ gridHeaderSection.snapshot(headerWriter);
393
+ }
394
+
395
+ // Begin element construction
396
+ var tables = [];
397
+ for (var t = 0; t < gridCount; ++t) { // For each table
398
+ var rowStart = t * maxRowPerPage;
399
+ var rowEnd = rowStart + maxRowPerPage;
400
+ if (rowEnd > totalRowCount) {
401
+ rowEnd = totalRowCount;
402
+ }
403
+ var rowCount = rowEnd - rowStart;
404
+
405
+ for (var s = 0; s < colSetCount; ++s) { // For each column set
406
+ var colSet = colSets[s];
407
+ var colCount = colSet.columns.length;
408
+ var c, col, colIndex;
409
+ var tbl = new Table(null, {
410
+ colCount: colCount,
411
+ rowCount: rowCount,
412
+ rowHeight: rowHeight
413
+ });
414
+
415
+ tbl.setColumnWidths(colSet.widths);
416
+
417
+ if (headerHeight) {
418
+ tbl.addHeaderRows(); // TODO: support multiple rows
419
+ var thead = tbl.getHeader();
420
+ thead.setDefaultRowHeight(headerHeight);
421
+
422
+ // Render header columns
423
+ for (c = 0; c < colCount; ++c) { // Create header and columns
424
+ col = colSet.columns[c];
425
+ colIndex = col.index;
426
+ copyNode(headerWriter.getCellElement(colIndex, 0), thead.getCell(c, 0));
427
+ _setColumnAlignment(thead, c, col.alignment);
428
+ }
429
+
430
+ // TODO: Span the header rows
431
+ }
432
+
433
+ // Render content section
434
+ for (c = 0; c < colCount; ++c) { // Populate each cell
435
+ col = colSet.columns[c];
436
+ colIndex = col.index;
437
+ var isPrimary = colIndex === primaryColumn.index;
438
+
439
+ for (var r = 0; r < rowCount; ++r) {
440
+ var rowIndex = rowStart + r;
441
+ var masterCell = contentWriter.getCellElement(colIndex, rowIndex);
442
+ var cell = tbl.getCell(c, r);
443
+ if (isPrimary) {
444
+ copyNode(masterCell, cell);
445
+ } else {
446
+ swapNode(masterCell, cell);
447
+ }
448
+ }
449
+
450
+ _setColumnAlignment(tbl, c, col.alignment);
451
+ }
452
+
453
+ tbl._tableNum = t;
454
+ tables.push(tbl);
455
+ }
456
+ }
457
+
458
+ // Produce the root element to be appended to the page
459
+ var pageCount = pfInfo.pageCount;
460
+ var rootElem = document.createElement("div");
461
+ rootElem.className = "tr-printing-root";
462
+ rootElem.style.display = "block"; // This will beat any CSS selector
463
+ for (t = 0; t < pageCount; ++t) {
464
+ rootElem.appendChild(tables[t].getElement());
465
+ }
466
+ // rootElem.appendChild(headerWriter.getElement());
467
+ // rootElem.appendChild(contentWriter.getElement());
468
+
469
+ return rootElem;
470
+ };
471
+
472
+ GridPrinter._applyCss = function () {
473
+ if (GridPrinter._styles) {
474
+ return;
475
+ }
476
+ GridPrinter._styles = prettifyCss([
477
+ ".tr-printing-mode, .tr-printing-mode body", [
478
+ "width: 100%;",
479
+ "height: unset;",
480
+ "padding: 0;",
481
+ "margin: 0;",
482
+ "background-color: white;",
483
+ "overflow: hidden; /* No scrollbar for printing */"
484
+ ],
485
+ ".tr-printing-mode body > *", [
486
+ "display: none;"
487
+ ],
488
+ ".tr-printing-root", [
489
+ "width: 100%;",
490
+ "background-color: white;"
491
+ ],
492
+ ".tr-printing-root > *", [
493
+ "page-break-after: always;"
494
+ ],
495
+ ".tr-printing-root table", [
496
+ "border-collapse: collapse;",
497
+ "table-layout:fixed; /* To force clipping text */"
498
+ ],
499
+ ".tr-printing-root tr", [
500
+ "height: 21px;",
501
+ "color: unset;",
502
+ "background-color: unset;"
503
+ ],
504
+ ".tr-printing-root td", [
505
+ "border: 1px solid black;",
506
+ "padding: 0 5px;",
507
+ "height: unset;",
508
+ "font-size: 12px;",
509
+ "text-overflow: ellipsis;",
510
+ "white-space: nowrap;",
511
+ "overflow: hidden;"
512
+ ],
513
+ ".tr-printing-root td, .tr-printing-root tr:hover td, .tr-printing-root tr td:hover", [
514
+ "color: black;",
515
+ "background-color: white;"
516
+ ],
517
+ ".tr-printing-root .tr-align-left", [
518
+ "text-align: left;"
519
+ ],
520
+ ".tr-printing-root .tr-align-right", [
521
+ "text-align: right;"
522
+ ],
523
+ ".tr-printing-root .tr-align-center", [
524
+ "text-align: center;"
525
+ ],
526
+ ".tr-printing-root .cell coral-icon", [
527
+ "margin-top: 3px;"
528
+ ],
529
+ ".tr-printing-root .cell .sort-symbol", [
530
+ "margin-left: 4px;",
531
+ "display: inline-block;"
532
+ ],
533
+ ".tr-printing-root .cell .sort-symbol", [
534
+ "margin-left: 4px;",
535
+ "display: inline-block;"
536
+ ],
537
+ // eslint-disable-next-line no-multi-str
538
+ ".tr-printing-root .priority-symbol:last-child,\
539
+ .tr-printing-root .sortable-indicator:last-child,\
540
+ .tr-printing-root .sort-symbol:last-child", [
541
+ "margin-right: 0px;"
542
+ ],
543
+ ".tr-printing-root .sortable-indicator:last-child", [
544
+ "opacity: 0.5;"
545
+ ],
546
+ ".tr-printing-root .folder .expander", [
547
+ "margin-right: 7px;",
548
+ "margin-left: 3px;"
549
+ ],
550
+ ".tr-printing-root .folder .fallback-arrow", [
551
+ "vertical-align: middle;",
552
+ "font-size: 7px;",
553
+ "line-height: 1;",
554
+ "display: inline-block;", // For transformation
555
+ "transform: scaleX(1.5);",
556
+ "font-family: Arial;"
557
+ ],
558
+ ".tr-printing-root .folder.closed .expander", [
559
+ "transform: translate(0px, 1px) rotate(-90deg);"
560
+ ],
561
+ ".tr-printing-root .folder.closed .expander .fallback-arrow", [
562
+ "transform: translate(0px, -1px) rotate(-90deg) scaleX(1.5);"
563
+ ],
564
+ ".tr-printing-root .print-inline", [
565
+ "display:inline-block;"
566
+ ],
567
+ ".tr-printing-root .text", [
568
+ "vertical-align: middle;",
569
+ "overflow: hidden;",
570
+ "text-overflow: ellipsis;"
571
+ ],
572
+ ".tr-printing-root .group-header, .tr-printing-root .ric-arrow", [
573
+ "color: black !important;"
574
+ ],
575
+ ".tr-printing-root .group-header-box", [
576
+ "display: inline-block;",
577
+ "width: 180px;"
578
+ ],
579
+ ".tr-printing-root .group-header-box *", [
580
+ "display: inline-block;"
581
+ ],
582
+ ".tr-printing-root .tr-percent-bar", [
583
+ "position:relative;",
584
+ "text-align: left;",
585
+ "overflow: hidden;",
586
+ "white-space: nowrap;"
587
+ ],
588
+ ".tr-printing-root .tr-percent-bar>div", [
589
+ "position:relative;",
590
+ "display: inline-block;",
591
+ "height: 8px;",
592
+ "vertical-align: middle;"
593
+ ],
594
+ ".tr-printing-root .tr-percent-bar>div>div", [
595
+ "position:absolute;",
596
+ "height: 100%;"
597
+ ],
598
+ ".tr-printing-root .tr-percent-bar>span", [
599
+ "display: inline-block;",
600
+ "vertical-align: middle;",
601
+ "box-sizing: border-box;",
602
+ "padding-left: 4px;",
603
+ "padding-right: 4px;",
604
+ "overflow: hidden;",
605
+ "max-width: 100%;"
606
+ ],
607
+ ".tr-printing-wrapper", [
608
+ "white-space: nowrap;",
609
+ "overflow: hidden;"
610
+ ],
611
+ "/* For safety */",
612
+ ".tr-printing-wrapper > *", [
613
+ "display: inline-block;",
614
+ "vertical-align: top;"
615
+ ],
616
+ ".tr-printing-wrapper > * + *", [
617
+ "margin-left: 4px;"
618
+ ],
619
+ ".tr-blank-page", [
620
+ "page-break-after: always;",
621
+ "height: 1px;",
622
+ "visibility: hidden;"
623
+ ],
624
+ ".tr-printing-flex-row", [
625
+ "display: flex;",
626
+ "width: 100%;",
627
+ "min-width: 0;",
628
+ "align-items: center;"
629
+ ],
630
+ ".tr-printing-flex-row>*:not(.tr-printing-float-right)", [
631
+ "flex: 1 1 0;"
632
+ ],
633
+ "@media screen", [
634
+ prettifyCss([
635
+ "/* For debugging */",
636
+ ".tr-printing-mode", [
637
+ "overflow: auto;",
638
+ "background-color: lightgrey;"
639
+ ],
640
+ ".tr-printing-root > * + *", [
641
+ "margin-top: 4px;"
642
+ ]
643
+ ])
644
+ ]
645
+ ]);
646
+ injectCss(GridPrinter._styles, document.body);
647
+ };
648
+
649
+ /** @public
650
+ * @param {*} grid grid element, currently supports atlas-blotter, emerald-grid, tr.CompositeGrid, rt.Grid and Core
651
+ */
652
+ GridPrinter.print = function (grid) {
653
+ var core = null;
654
+ if (grid) {
655
+ GridPrinter._applyCss();
656
+ core = _getCoreGrid(grid);
657
+ }
658
+
659
+ if (core) {
660
+ GridPrinter._grid = core;
661
+
662
+ var pt = GridPrinter._getPrintTrait(); // initialize
663
+ if (!GridPrinter._isObserving) {
664
+ pt.observe(); // Observe current window
665
+ }
666
+
667
+ pt.print();
668
+ } else {
669
+ GridPrinter._grid = null;
670
+ }
671
+ };
672
+
673
+
674
+ /** @private
675
+ * @return {!PrintTrait}
676
+ */
677
+ GridPrinter._getPrintTrait = function () {
678
+ var pt = GridPrinter._printTrait;
679
+ if (!pt) {
680
+ pt = GridPrinter._printTrait = new PrintTrait();
681
+ // TODO: this._printTrait.fixPaperSize(); // WORKAROUND: We cannot detect the change in paper size during the browser's preview dialog.
682
+
683
+ pt.addEventListener('pageCounting', GridPrinter._onPageCounting);
684
+ pt.addEventListener('beforeprint', GridPrinter._onBeforePrint);
685
+ pt.addEventListener('afterprint', GridPrinter._onAfterPrint);
686
+ }
687
+ return pt;
688
+ };
689
+ /** @private
690
+ * @param {tr.Grid} grid
691
+ * @param {number} idx
692
+ * @return {!Object}
693
+ */
694
+ GridPrinter._newColInfo = function (grid, idx) {
695
+ var minWidth = grid.getMinimumColumnWidth(idx);
696
+ if (minWidth <= 0) {
697
+ minWidth = 50; // WARNING: Hard-coded value
698
+ }
699
+ var scalability = grid.getColumnScalability(idx);
700
+ var width = scalability ? minWidth : grid.getColumnWidth(idx);
701
+
702
+ return {
703
+ "index": idx,
704
+ "width": width,
705
+ "data": grid.getColumnData(idx),
706
+ "scalability": scalability,
707
+ "minWidth": minWidth,
708
+ "alignment": grid.getColumnAlignment(idx)
709
+ };
710
+ };
711
+ /** @private
712
+ */
713
+ GridPrinter._removePrintElements = function () {
714
+ Dom.removeParent(GridPrinter._blankPage);
715
+ GridPrinter._blankPage = null;
716
+
717
+ Dom.removeParent(GridPrinter._printElem);
718
+ GridPrinter._printElem = null;
719
+ };
720
+
721
+
722
+ /** @private
723
+ * @param {Object} e Event argument created from PrintTrait
724
+ */
725
+ GridPrinter._onPageCounting = function (e) {
726
+ GridPrinter._removePrintElements();
727
+
728
+ GridPrinter._printInfo = GridPrinter.getPreFlightInfo(GridPrinter._grid, e);
729
+ if (GridPrinter._printInfo.pageCount) {
730
+ // There is no need to render anything at this phase
731
+ GridPrinter._blankPage = PrintTrait.createBlankPages(GridPrinter._printInfo.pageCount);
732
+ e.bodyElement.appendChild(GridPrinter._blankPage);
733
+ }
734
+ };
735
+ /** @private
736
+ * @param {Object} e
737
+ */
738
+ GridPrinter._onBeforePrint = function (e) {
739
+ GridPrinter._removePrintElements();
740
+
741
+ if (GridPrinter._printInfo && GridPrinter._printInfo.pageCount) {
742
+ GridPrinter._printElem = GridPrinter.createPrintElement(GridPrinter._grid, GridPrinter._printInfo); // TODO: Use new sizes given from PrintTrait
743
+
744
+ // var elem = GridPrinter._printTrait.createClientBox(e.pageWidth, e.pageHeight);
745
+ e.bodyElement.appendChild(GridPrinter._printElem);
746
+ }
747
+ };
748
+ /** @private
749
+ * @param {Object} e
750
+ */
751
+ GridPrinter._onAfterPrint = function (e) {
752
+ GridPrinter._removePrintElements();
753
+
754
+ if (!GridPrinter._isObserving) {
755
+ GridPrinter._getPrintTrait().unobserve();
756
+ }
757
+
758
+ GridPrinter._printInfo = null;
759
+ if (GridPrinter._grid) {
760
+ GridPrinter._grid.updateLayout();
761
+ GridPrinter._grid = null;
762
+ }
763
+ };
764
+
765
+ /** @typedef {Object} GridPrinter~Options
766
+ * @description Configuration object for customizing priting behavior can be passed through `GridPrinter.setPrintOptions` method
767
+ * @property {number=} pageWidth Paper width in pixel. This limits number of columns to be shown on a single page
768
+ * @property {number=} pageHeight Paper height in pixel. This limits number of rows to be shown on a single page
769
+ * @property {number=} primaryColumn Column index that will be placed as the first column on each page.
770
+ */
771
+ GridPrinter.Options;
772
+
773
+
774
+ export { GridPrinter };