1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
/**
* $Id: mxPrintPreview.js,v 1.61 2012-05-15 14:12:40 gaudenz Exp $
* Copyright (c) 2006-2010, JGraph Ltd
*/
/**
* Class: mxPrintPreview
*
* Implements printing of a diagram across multiple pages. The following opens
* a print preview for an existing graph:
*
* (code)
* var preview = new mxPrintPreview(graph);
* preview.open();
* (end)
*
* Use <mxUtils.getScaleForPageCount> as follows in order to print the graph
* across a given number of pages:
*
* (code)
* var pageCount = mxUtils.prompt('Enter page count', '1');
*
* if (pageCount != null)
* {
* var scale = mxUtils.getScaleForPageCount(pageCount, graph);
* var preview = new mxPrintPreview(graph, scale);
* preview.open();
* }
* (end)
*
* Headers:
*
* Apart from setting the title argument in the mxPrintPreview constructor you
* can override <renderPage> as follows to add a header to any page:
*
* (code)
* var oldRenderPage = mxPrintPreview.prototype.renderPage;
* mxPrintPreview.prototype.renderPage = function(w, h, dx, dy, scale, pageNumber)
* {
* var div = oldRenderPage.apply(this, arguments);
*
* var header = document.createElement('div');
* header.style.position = 'absolute';
* header.style.top = '0px';
* header.style.width = '100%';
* header.style.textAlign = 'right';
* mxUtils.write(header, 'Your header here - Page ' + pageNumber + ' / ' + this.pageCount);
* div.firstChild.appendChild(header);
*
* return div;
* };
* (end)
*
* Page Format:
*
* For landscape printing, use <mxConstants.PAGE_FORMAT_A4_LANDSCAPE> as
* the pageFormat in <mxUtils.getScaleForPageCount> and <mxPrintPreview>.
* Keep in mind that one can not set the defaults for the print dialog
* of the operating system from JavaScript so the user must manually choose
* a page format that matches this setting.
*
* You can try passing the following CSS directive to <open> to set the
* page format in the print dialog to landscape. However, this CSS
* directive seems to be ignored in most major browsers, including IE.
*
* (code)
* @page {
* size: landscape;
* }
* (end)
*
* Note that the print preview behaves differently in IE when used from the
* filesystem or via HTTP so printing should always be tested via HTTP.
*
* If you are using a DOCTYPE in the source page you can override <getDoctype>
* and provide the same DOCTYPE for the print preview if required. Here is
* an example for IE8 standards mode.
*
* (code)
* var preview = new mxPrintPreview(graph);
* preview.getDoctype = function()
* {
* return '<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=5,IE=8" ><![endif]-->';
* };
* preview.open();
* (end)
*
* Constructor: mxPrintPreview
*
* Constructs a new print preview for the given parameters.
*
* Parameters:
*
* graph - <mxGraph> to be previewed.
* scale - Optional scale of the output. Default is 1 / <mxGraph.pageScale>.
* border - Border in pixels along each side of every page. Note that the
* actual print function in the browser will add another border for
* printing.
* pageFormat - <mxRectangle> that specifies the page format (in pixels).
* This should match the page format of the printer. Default uses the
* <mxGraph.pageFormat> of the given graph.
* x0 - Optional left offset of the output. Default is 0.
* y0 - Optional top offset of the output. Default is 0.
* borderColor - Optional color of the page border. Default is no border.
* Note that a border is sometimes useful to highlight the printed page
* border in the print preview of the browser.
* title - Optional string that is used for the window title. Default
* is 'Printer-friendly version'.
* pageSelector - Optional boolean that specifies if the page selector
* should appear in the window with the print preview. Default is true.
*/
function mxPrintPreview(graph, scale, pageFormat, border, x0, y0, borderColor, title, pageSelector)
{
this.graph = graph;
this.scale = (scale != null) ? scale : 1 / graph.pageScale;
this.border = (border != null) ? border : 0;
this.pageFormat = (pageFormat != null) ? pageFormat : graph.pageFormat;
this.title = (title != null) ? title : 'Printer-friendly version';
this.x0 = (x0 != null) ? x0 : 0;
this.y0 = (y0 != null) ? y0 : 0;
this.borderColor = borderColor;
this.pageSelector = (pageSelector != null) ? pageSelector : true;
};
/**
* Variable: graph
*
* Reference to the <mxGraph> that should be previewed.
*/
mxPrintPreview.prototype.graph = null;
/**
* Variable: pageFormat
*
* Holds the <mxRectangle> that defines the page format.
*/
mxPrintPreview.prototype.pageFormat = null;
/**
* Variable: scale
*
* Holds the scale of the print preview.
*/
mxPrintPreview.prototype.scale = null;
/**
* Variable: border
*
* The border inset around each side of every page in the preview. This is set
* to 0 if autoOrigin is false.
*/
mxPrintPreview.prototype.border = 0;
/**
/**
* Variable: x0
*
* Holds the horizontal offset of the output.
*/
mxPrintPreview.prototype.x0 = 0;
/**
* Variable: y0
*
* Holds the vertical offset of the output.
*/
mxPrintPreview.prototype.y0 = 0;
/**
* Variable: autoOrigin
*
* Specifies if the origin should be automatically computed based on the top,
* left corner of the actual diagram contents. If this is set to false then the
* values for <x0> and <y0> will be overridden in <open>. Default is true.
*/
mxPrintPreview.prototype.autoOrigin = true;
/**
* Variable: printOverlays
*
* Specifies if overlays should be printed. Default is false.
*/
mxPrintPreview.prototype.printOverlays = false;
/**
* Variable: borderColor
*
* Holds the color value for the page border.
*/
mxPrintPreview.prototype.borderColor = null;
/**
* Variable: title
*
* Holds the title of the preview window.
*/
mxPrintPreview.prototype.title = null;
/**
* Variable: pageSelector
*
* Boolean that specifies if the page selector should be
* displayed. Default is true.
*/
mxPrintPreview.prototype.pageSelector = null;
/**
* Variable: wnd
*
* Reference to the preview window.
*/
mxPrintPreview.prototype.wnd = null;
/**
* Variable: pageCount
*
* Holds the actual number of pages in the preview.
*/
mxPrintPreview.prototype.pageCount = 0;
/**
* Function: getWindow
*
* Returns <wnd>.
*/
mxPrintPreview.prototype.getWindow = function()
{
return this.wnd;
};
/**
* Function: getDocType
*
* Returns the string that should go before the HTML tag in the print preview
* page. This implementation returns an empty string.
*/
mxPrintPreview.prototype.getDoctype = function()
{
return '';
};
/**
* Function: open
*
* Shows the print preview window. The window is created here if it does
* not exist.
*
* Parameters:
*
* css - Optional CSS string to be used in the new page's head section.
*/
mxPrintPreview.prototype.open = function(css)
{
// Closing the window while the page is being rendered may cause an
// exception in IE. This and any other exceptions are simply ignored.
var previousInitializeOverlay = this.graph.cellRenderer.initializeOverlay;
var div = null;
try
{
// Temporarily overrides the method to redirect rendering of overlays
// to the draw pane so that they are visible in the printout
if (this.printOverlays)
{
this.graph.cellRenderer.initializeOverlay = function(state, overlay)
{
overlay.init(state.view.getDrawPane());
};
}
if (this.wnd == null)
{
this.wnd = window.open();
var doc = this.wnd.document;
var dt = this.getDoctype();
if (dt != null && dt.length > 0)
{
doc.writeln(dt);
}
doc.writeln('<html>');
doc.writeln('<head>');
this.writeHead(doc, css);
doc.writeln('</head>');
doc.writeln('<body class="mxPage">');
// Adds all required stylesheets and namespaces
mxClient.link('stylesheet', mxClient.basePath + '/css/common.css', doc);
if (mxClient.IS_IE && document.documentMode != 9)
{
doc.namespaces.add('v', 'urn:schemas-microsoft-com:vml');
doc.namespaces.add('o', 'urn:schemas-microsoft-com:office:office');
var ss = doc.createStyleSheet();
ss.cssText = 'v\\:*{behavior:url(#default#VML)}o\\:*{behavior:url(#default#VML)}';
mxClient.link('stylesheet', mxClient.basePath + '/css/explorer.css', doc);
}
// Computes the horizontal and vertical page count
var bounds = this.graph.getGraphBounds().clone();
var currentScale = this.graph.getView().getScale();
var sc = currentScale / this.scale;
var tr = this.graph.getView().getTranslate();
// Uses the absolute origin with no offset for all printing
if (!this.autoOrigin)
{
this.x0 = -tr.x * this.scale;
this.y0 = -tr.y * this.scale;
bounds.width += bounds.x;
bounds.height += bounds.y;
bounds.x = 0;
bounds.y = 0;
this.border = 0;
}
// Compute the unscaled, untranslated bounds to find
// the number of vertical and horizontal pages
bounds.width /= sc;
bounds.height /= sc;
// Store the available page area
var availableWidth = this.pageFormat.width - (this.border * 2);
var availableHeight = this.pageFormat.height - (this.border * 2);
var hpages = Math.max(1, Math.ceil((bounds.width + this.x0) / availableWidth));
var vpages = Math.max(1, Math.ceil((bounds.height + this.y0) / availableHeight));
this.pageCount = hpages * vpages;
var writePageSelector = mxUtils.bind(this, function()
{
if (this.pageSelector && (vpages > 1 || hpages > 1))
{
var table = this.createPageSelector(vpages, hpages);
doc.body.appendChild(table);
// Workaround for position: fixed which isn't working in IE
if (mxClient.IS_IE)
{
table.style.position = 'absolute';
var update = function()
{
table.style.top = (doc.body.scrollTop + 10) + 'px';
};
mxEvent.addListener(this.wnd, 'scroll', function(evt)
{
update();
});
mxEvent.addListener(this.wnd, 'resize', function(evt)
{
update();
});
}
}
});
// Stores pages for later retrieval
var pages = null;
// Workaround for aspect of image shapes updated asynchronously
// in VML so we need to fetch the markup of the DIV containing
// the image after the udpate of the style of the DOM node.
// LATER: Allow document for display markup to be customized.
if (mxClient.IS_IE && document.documentMode != 9)
{
pages = [];
// Overrides asynchronous loading of images for fetching HTML markup
var waitCounter = 0;
var isDone = false;
var mxImageShapeScheduleUpdateAspect = mxImageShape.prototype.scheduleUpdateAspect;
var mxImageShapeUpdateAspect = mxImageShape.prototype.updateAspect;
var writePages = function()
{
if (isDone && waitCounter == 0)
{
// Restores previous implementations
mxImageShape.prototype.scheduleUpdateAspect = mxImageShapeScheduleUpdateAspect;
mxImageShape.prototype.updateAspect = mxImageShapeUpdateAspect;
var markup = '';
for (var i = 0; i < pages.length; i++)
{
markup += pages[i].outerHTML;
pages[i].parentNode.removeChild(pages[i]);
if (i < pages.length - 1)
{
markup += '<hr/>';
}
}
doc.body.innerHTML = markup;
writePageSelector();
}
};
// Overrides functions to implement wait counter
mxImageShape.prototype.scheduleUpdateAspect = function()
{
waitCounter++;
mxImageShapeScheduleUpdateAspect.apply(this, arguments);
};
// Overrides functions to implement wait counter
mxImageShape.prototype.updateAspect = function()
{
mxImageShapeUpdateAspect.apply(this, arguments);
waitCounter--;
writePages();
};
}
// Appends each page to the page output for printing, making
// sure there will be a page break after each page (ie. div)
for (var i = 0; i < vpages; i++)
{
var dy = i * availableHeight / this.scale - this.y0 / this.scale +
(bounds.y - tr.y * currentScale) / currentScale;
for (var j = 0; j < hpages; j++)
{
if (this.wnd == null)
{
return null;
}
var dx = j * availableWidth / this.scale - this.x0 / this.scale +
(bounds.x - tr.x * currentScale) / currentScale;
var pageNum = i * hpages + j + 1;
div = this.renderPage(this.pageFormat.width, this.pageFormat.height,
-dx, -dy, this.scale, pageNum);
// Gives the page a unique ID for later accessing the page
div.setAttribute('id', 'mxPage-'+pageNum);
// Border of the DIV (aka page) inside the document
if (this.borderColor != null)
{
div.style.borderColor = this.borderColor;
div.style.borderStyle = 'solid';
div.style.borderWidth = '1px';
}
// Needs to be assigned directly because IE doesn't support
// child selectors, eg. body > div { background: white; }
div.style.background = 'white';
if (i < vpages - 1 || j < hpages - 1)
{
div.style.pageBreakAfter = 'always';
}
// NOTE: We are dealing with cross-window DOM here, which
// is a problem in IE, so we copy the HTML markup instead.
// The underlying problem is that the graph display markup
// creation (in mxShape, mxGraphView) is hardwired to using
// document.createElement and hence we must use document
// to create the complete page and then copy it over to the
// new window.document. This can be fixed later by using the
// ownerDocument of the container in mxShape and mxGraphView.
if (mxClient.IS_IE)
{
// For some obscure reason, removing the DIV from the
// parent before fetching its outerHTML has missing
// fillcolor properties and fill children, so the div
// must be removed afterwards to keep the fillcolors.
// For delayed output we remote the DIV from the
// original document when we write out all pages.
doc.writeln(div.outerHTML);
if (pages != null)
{
pages.push(div);
}
else
{
div.parentNode.removeChild(div);
}
}
else
{
div.parentNode.removeChild(div);
doc.body.appendChild(div);
}
if (i < vpages - 1 || j < hpages - 1)
{
var hr = doc.createElement('hr');
hr.className = 'mxPageBreak';
doc.body.appendChild(hr);
}
}
}
doc.writeln('</body>');
doc.writeln('</html>');
doc.close();
// Marks the printing complete for async handling
if (pages != null)
{
isDone = true;
writePages();
}
else
{
writePageSelector();
}
// Removes all event handlers in the print output
mxEvent.release(doc.body);
}
this.wnd.focus();
}
catch (e)
{
// Removes the DIV from the document in case of an error
if (div != null && div.parentNode != null)
{
div.parentNode.removeChild(div);
}
}
finally
{
this.graph.cellRenderer.initializeOverlay = previousInitializeOverlay;
}
return this.wnd;
};
/**
* Function: writeHead
*
* Writes the HEAD section into the given document, without the opening
* and closing HEAD tags.
*/
mxPrintPreview.prototype.writeHead = function(doc, css)
{
if (this.title != null)
{
doc.writeln('<title>' + this.title + '</title>');
}
// Makes sure no horizontal rulers are printed
doc.writeln('<style type="text/css">');
doc.writeln('@media print {');
doc.writeln(' table.mxPageSelector { display: none; }');
doc.writeln(' hr.mxPageBreak { display: none; }');
doc.writeln('}');
doc.writeln('@media screen {');
// NOTE: position: fixed is not supported in IE, so the page selector
// position (absolute) needs to be updated in IE (see below)
doc.writeln(' table.mxPageSelector { position: fixed; right: 10px; top: 10px;' +
'font-family: Arial; font-size:10pt; border: solid 1px darkgray;' +
'background: white; border-collapse:collapse; }');
doc.writeln(' table.mxPageSelector td { border: solid 1px gray; padding:4px; }');
doc.writeln(' body.mxPage { background: gray; }');
doc.writeln('}');
if (css != null)
{
doc.writeln(css);
}
doc.writeln('</style>');
};
/**
* Function: createPageSelector
*
* Creates the page selector table.
*/
mxPrintPreview.prototype.createPageSelector = function(vpages, hpages)
{
var doc = this.wnd.document;
var table = doc.createElement('table');
table.className = 'mxPageSelector';
table.setAttribute('border', '0');
var tbody = doc.createElement('tbody');
for (var i = 0; i < vpages; i++)
{
var row = doc.createElement('tr');
for (var j = 0; j < hpages; j++)
{
var pageNum = i * hpages + j + 1;
var cell = doc.createElement('td');
// Needs anchor for all browers to work without JavaScript
// LATER: Does not work in Firefox because the generated document
// has the URL of the opening document, the anchor is appended
// to that URL and the full URL is loaded on click.
if (!mxClient.IS_NS || mxClient.IS_SF || mxClient.IS_GC)
{
var a = doc.createElement('a');
a.setAttribute('href', '#mxPage-' + pageNum);
mxUtils.write(a, pageNum, doc);
cell.appendChild(a);
}
else
{
mxUtils.write(cell, pageNum, doc);
}
row.appendChild(cell);
}
tbody.appendChild(row);
}
table.appendChild(tbody);
return table;
};
/**
* Function: renderPage
*
* Creates a DIV that prints a single page of the given
* graph using the given scale and returns the DIV that
* represents the page.
*
* Parameters:
*
* w - Width of the page in pixels.
* h - Height of the page in pixels.
* dx - Horizontal translation for the diagram.
* dy - Vertical translation for the diagram.
* scale - Scale for the diagram.
* pageNumber - Number of the page to be rendered.
*/
mxPrintPreview.prototype.renderPage = function(w, h, dx, dy, scale, pageNumber)
{
var div = document.createElement('div');
try
{
div.style.width = w + 'px';
div.style.height = h + 'px';
div.style.overflow = 'hidden';
div.style.pageBreakInside = 'avoid';
var innerDiv = document.createElement('div');
innerDiv.style.top = this.border + 'px';
innerDiv.style.left = this.border + 'px';
innerDiv.style.width = (w - 2 * this.border) + 'px';
innerDiv.style.height = (h - 2 * this.border) + 'px';
innerDiv.style.overflow = 'hidden';
if (this.graph.dialect == mxConstants.DIALECT_VML)
{
innerDiv.style.position = 'absolute';
}
div.appendChild(innerDiv);
document.body.appendChild(div);
var view = this.graph.getView();
var previousContainer = this.graph.container;
this.graph.container = innerDiv;
var canvas = view.getCanvas();
var backgroundPane = view.getBackgroundPane();
var drawPane = view.getDrawPane();
var overlayPane = view.getOverlayPane();
if (this.graph.dialect == mxConstants.DIALECT_SVG)
{
view.createSvg();
}
else if (this.graph.dialect == mxConstants.DIALECT_VML)
{
view.createVml();
}
else
{
view.createHtml();
}
// Disables events on the view
var eventsEnabled = view.isEventsEnabled();
view.setEventsEnabled(false);
// Disables the graph to avoid cursors
var graphEnabled = this.graph.isEnabled();
this.graph.setEnabled(false);
// Resets the translation
var translate = view.getTranslate();
view.translate = new mxPoint(dx, dy);
var temp = null;
try
{
// Creates the temporary cell states in the view and
// draws them onto the temporary DOM nodes in the view
var model = this.graph.getModel();
var cells = [model.getRoot()];
temp = new mxTemporaryCellStates(view, scale, cells);
}
finally
{
// Removes overlay pane with selection handles
// controls and icons from the print output
if (mxClient.IS_IE)
{
view.overlayPane.innerHTML = '';
}
else
{
// Removes everything but the SVG node
var tmp = innerDiv.firstChild;
while (tmp != null)
{
var next = tmp.nextSibling;
var name = tmp.nodeName.toLowerCase();
// Note: Width and heigh are required in FF 11
if (name == 'svg')
{
tmp.setAttribute('width', parseInt(innerDiv.style.width));
tmp.setAttribute('height', parseInt(innerDiv.style.height));
}
// Tries to fetch all text labels and only text labels
else if (tmp.style.cursor != 'default' && name != 'table')
{
tmp.parentNode.removeChild(tmp);
}
tmp = next;
}
}
// Completely removes the overlay pane to remove more handles
view.overlayPane.parentNode.removeChild(view.overlayPane);
// Restores the state of the view
this.graph.setEnabled(graphEnabled);
this.graph.container = previousContainer;
view.canvas = canvas;
view.backgroundPane = backgroundPane;
view.drawPane = drawPane;
view.overlayPane = overlayPane;
view.translate = translate;
temp.destroy();
view.setEventsEnabled(eventsEnabled);
}
}
catch (e)
{
div.parentNode.removeChild(div);
div = null;
throw e;
}
return div;
};
/**
* Function: print
*
* Opens the print preview and shows the print dialog.
*/
mxPrintPreview.prototype.print = function()
{
var wnd = this.open();
if (wnd != null)
{
wnd.print();
}
};
/**
* Function: close
*
* Closes the print preview window.
*/
mxPrintPreview.prototype.close = function()
{
if (this.wnd != null)
{
this.wnd.close();
this.wnd = null;
}
};
|