ngx-print 20.1.0-beta.4 → 20.1.0-beta.6
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.
- package/fesm2022/ngx-print.mjs +502 -0
- package/fesm2022/ngx-print.mjs.map +1 -0
- package/index.d.ts +241 -0
- package/index.d.ts.map +1 -0
- package/package.json +43 -68
- package/.editorconfig +0 -13
- package/.eslintrc.json +0 -50
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/dependabot.yml +0 -17
- package/.github/workflows/PullRequest.yml +0 -32
- package/.github/workflows/stale.yml +0 -26
- package/.prettierignore +0 -42
- package/.prettierrc.json +0 -14
- package/CHANGELOG.md +0 -17
- package/_config.yml +0 -1
- package/angular.json +0 -64
- package/karma.conf.js +0 -43
- package/ng-package.json +0 -7
- package/src/lib/ngx-print.base.ts +0 -320
- package/src/lib/ngx-print.directive.spec.ts +0 -139
- package/src/lib/ngx-print.directive.ts +0 -113
- package/src/lib/ngx-print.module.ts +0 -8
- package/src/lib/ngx-print.service.spec.ts +0 -201
- package/src/lib/ngx-print.service.ts +0 -51
- package/src/lib/print-helper.ts +0 -24
- package/src/lib/print-options.ts +0 -16
- package/src/public_api.ts +0 -18
- package/tsconfig.json +0 -35
- package/tsconfig.lib.json +0 -14
- package/tsconfig.spec.json +0 -17
- package/tslint.json +0 -17
package/angular.json
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
|
3
|
-
"version": 1,
|
|
4
|
-
"newProjectRoot": "projects",
|
|
5
|
-
"projects": {
|
|
6
|
-
"ngx-print": {
|
|
7
|
-
"root": "",
|
|
8
|
-
"sourceRoot": "src",
|
|
9
|
-
"projectType": "library",
|
|
10
|
-
"prefix": "lib",
|
|
11
|
-
"architect": {
|
|
12
|
-
"build": {
|
|
13
|
-
"builder": "@angular/build:ng-packagr",
|
|
14
|
-
"options": {
|
|
15
|
-
"tsConfig": "tsconfig.lib.json",
|
|
16
|
-
"project": "ng-package.json"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"test": {
|
|
20
|
-
"builder": "@angular/build:karma",
|
|
21
|
-
"options": {
|
|
22
|
-
"tsConfig": "tsconfig.spec.json",
|
|
23
|
-
"karmaConfig": "karma.conf.js"
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"lint": {
|
|
27
|
-
"builder": "@angular-eslint/builder:lint",
|
|
28
|
-
"options": {
|
|
29
|
-
"lintFilePatterns": ["src/**/*.ts", "src/**/*.html"]
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
"cli": {
|
|
36
|
-
"analytics": false
|
|
37
|
-
},
|
|
38
|
-
"schematics": {
|
|
39
|
-
"@schematics/angular:component": {
|
|
40
|
-
"type": "component"
|
|
41
|
-
},
|
|
42
|
-
"@schematics/angular:directive": {
|
|
43
|
-
"type": "directive"
|
|
44
|
-
},
|
|
45
|
-
"@schematics/angular:service": {
|
|
46
|
-
"type": "service"
|
|
47
|
-
},
|
|
48
|
-
"@schematics/angular:guard": {
|
|
49
|
-
"typeSeparator": "."
|
|
50
|
-
},
|
|
51
|
-
"@schematics/angular:interceptor": {
|
|
52
|
-
"typeSeparator": "."
|
|
53
|
-
},
|
|
54
|
-
"@schematics/angular:module": {
|
|
55
|
-
"typeSeparator": "."
|
|
56
|
-
},
|
|
57
|
-
"@schematics/angular:pipe": {
|
|
58
|
-
"typeSeparator": "."
|
|
59
|
-
},
|
|
60
|
-
"@schematics/angular:resolver": {
|
|
61
|
-
"typeSeparator": "."
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
package/karma.conf.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// Karma configuration file, see link for more information
|
|
2
|
-
// https://karma-runner.github.io/1.0/config/configuration-file.html
|
|
3
|
-
|
|
4
|
-
module.exports = function (config) {
|
|
5
|
-
config.set({
|
|
6
|
-
basePath: "",
|
|
7
|
-
frameworks: ["jasmine", "@angular-devkit/build-angular"],
|
|
8
|
-
plugins: [
|
|
9
|
-
require("karma-jasmine"),
|
|
10
|
-
require("karma-chrome-launcher"),
|
|
11
|
-
require("karma-jasmine-html-reporter"),
|
|
12
|
-
require("karma-coverage"),
|
|
13
|
-
],
|
|
14
|
-
client: {
|
|
15
|
-
clearContext: false, // leave Jasmine Spec Runner output visible in browser
|
|
16
|
-
},
|
|
17
|
-
ccoverageReporter: {
|
|
18
|
-
dir: require("path").join(__dirname, "./coverage/<project-name>"),
|
|
19
|
-
subdir: ".",
|
|
20
|
-
reporters: [{ type: "html" }, { type: "text-summary" }],
|
|
21
|
-
check: {
|
|
22
|
-
global: {
|
|
23
|
-
statements: 80,
|
|
24
|
-
branches: 80,
|
|
25
|
-
functions: 80,
|
|
26
|
-
lines: 80,
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
port: 9876,
|
|
31
|
-
colors: true,
|
|
32
|
-
logLevel: config.LOG_INFO,
|
|
33
|
-
autoWatch: true,
|
|
34
|
-
browsers: ["Chrome"],
|
|
35
|
-
customLaunchers: {
|
|
36
|
-
ChromeHeadlessCI: {
|
|
37
|
-
base: "ChromeHeadless",
|
|
38
|
-
flags: ["--no-sandbox"],
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
singleRun: true,
|
|
42
|
-
});
|
|
43
|
-
};
|
package/ng-package.json
DELETED
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
import { CSP_NONCE, Injectable, inject } from '@angular/core';
|
|
2
|
-
import { Subject } from 'rxjs';
|
|
3
|
-
import { PrintOptions } from './print-options';
|
|
4
|
-
|
|
5
|
-
@Injectable({
|
|
6
|
-
providedIn: 'root',
|
|
7
|
-
})
|
|
8
|
-
export class PrintBase {
|
|
9
|
-
private nonce = inject(CSP_NONCE, { optional: true });
|
|
10
|
-
|
|
11
|
-
private _printStyle: string[] = [];
|
|
12
|
-
private _styleSheetFile: string = '';
|
|
13
|
-
protected printComplete = new Subject<void>();
|
|
14
|
-
|
|
15
|
-
//#region Getters and Setters
|
|
16
|
-
/**
|
|
17
|
-
* Sets the print styles based on the provided values.
|
|
18
|
-
*
|
|
19
|
-
* @param {Object} values - Key-value pairs representing print styles.
|
|
20
|
-
* @protected
|
|
21
|
-
*/
|
|
22
|
-
protected setPrintStyle(values: { [key: string]: { [key: string]: string } }) {
|
|
23
|
-
this._printStyle = [];
|
|
24
|
-
for (const key in values) {
|
|
25
|
-
if (Object.prototype.hasOwnProperty.call(values, key)) {
|
|
26
|
-
this._printStyle.push((key + JSON.stringify(values[key])).replace(/['"]+/g, ''));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @returns the string that create the stylesheet which will be injected
|
|
35
|
-
* later within <style></style> tag.
|
|
36
|
-
*
|
|
37
|
-
* -join/replace to transform an array objects to css-styled string
|
|
38
|
-
*/
|
|
39
|
-
public returnStyleValues() {
|
|
40
|
-
const styleNonce = this.nonce ? ` nonce="${this.nonce}"` : '';
|
|
41
|
-
return `<style${styleNonce}> ${this._printStyle.join(' ').replace(/,/g, ';')} </style>`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @returns string which contains the link tags containing the css which will
|
|
46
|
-
* be injected later within <head></head> tag.
|
|
47
|
-
*
|
|
48
|
-
*/
|
|
49
|
-
private returnStyleSheetLinkTags() {
|
|
50
|
-
return this._styleSheetFile;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Sets the style sheet file based on the provided CSS list.
|
|
55
|
-
*
|
|
56
|
-
* @param {string} cssList - CSS file or list of CSS files.
|
|
57
|
-
* @protected
|
|
58
|
-
*/
|
|
59
|
-
protected setStyleSheetFile(cssList: string) {
|
|
60
|
-
const linkTagFn = function (cssFileName: string) {
|
|
61
|
-
return `<link rel="stylesheet" type="text/css" href="${cssFileName}">`;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
if (cssList.indexOf(',') !== -1) {
|
|
65
|
-
const valueArr = cssList.split(',');
|
|
66
|
-
this._styleSheetFile = valueArr.map(val => linkTagFn(val)).join('');
|
|
67
|
-
} else {
|
|
68
|
-
this._styleSheetFile = linkTagFn(cssList);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
//#endregion
|
|
73
|
-
|
|
74
|
-
//#region Private methods used by PrintBase
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Updates the default values for input elements.
|
|
78
|
-
*
|
|
79
|
-
* @param {HTMLCollectionOf<HTMLInputElement>} elements - Collection of input elements.
|
|
80
|
-
* @private
|
|
81
|
-
*/
|
|
82
|
-
private updateInputDefaults(elements: HTMLCollectionOf<HTMLInputElement>): void {
|
|
83
|
-
for (let i = 0; i < elements.length; i++) {
|
|
84
|
-
const element = elements[i];
|
|
85
|
-
element['defaultValue'] = element.value;
|
|
86
|
-
if (element['checked']) element['defaultChecked'] = true;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Updates the default values for select elements.
|
|
92
|
-
*
|
|
93
|
-
* @param {HTMLCollectionOf<HTMLSelectElement>} elements - Collection of select elements.
|
|
94
|
-
* @private
|
|
95
|
-
*/
|
|
96
|
-
private updateSelectDefaults(elements: HTMLCollectionOf<HTMLSelectElement>): void {
|
|
97
|
-
for (let i = 0; i < elements.length; i++) {
|
|
98
|
-
const element = elements[i];
|
|
99
|
-
const selectedIdx = element.selectedIndex;
|
|
100
|
-
const selectedOption: HTMLOptionElement = element.options[selectedIdx];
|
|
101
|
-
|
|
102
|
-
selectedOption.defaultSelected = true;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Updates the default values for textarea elements.
|
|
108
|
-
*
|
|
109
|
-
* @param {HTMLCollectionOf<HTMLTextAreaElement>} elements - Collection of textarea elements.
|
|
110
|
-
* @private
|
|
111
|
-
*/
|
|
112
|
-
private updateTextAreaDefaults(elements: HTMLCollectionOf<HTMLTextAreaElement>): void {
|
|
113
|
-
for (let i = 0; i < elements.length; i++) {
|
|
114
|
-
const element = elements[i];
|
|
115
|
-
element['defaultValue'] = element.value;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Converts a canvas element to an image and returns its HTML string.
|
|
121
|
-
*
|
|
122
|
-
* @param {HTMLCanvasElement} element - The canvas element to convert.
|
|
123
|
-
* @returns {string} - HTML string of the image.
|
|
124
|
-
* @private
|
|
125
|
-
*/
|
|
126
|
-
private canvasToImageHtml(element: HTMLCanvasElement): string {
|
|
127
|
-
const dataUrl = element.toDataURL();
|
|
128
|
-
return `<img src="${dataUrl}" style="max-width: 100%;">`;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Includes canvas contents in the print section via img tags.
|
|
133
|
-
*
|
|
134
|
-
* @param {HTMLCollectionOf<HTMLCanvasElement>} elements - Collection of canvas elements.
|
|
135
|
-
* @private
|
|
136
|
-
*/
|
|
137
|
-
private updateCanvasToImage(elements: HTMLCollectionOf<HTMLCanvasElement>): void {
|
|
138
|
-
for (let i = 0; i < elements.length; i++) {
|
|
139
|
-
const element = this.canvasToImageHtml(elements[i]);
|
|
140
|
-
elements[i].insertAdjacentHTML('afterend', element);
|
|
141
|
-
elements[i].remove();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Retrieves the HTML content of a specified printing section.
|
|
147
|
-
*
|
|
148
|
-
* @param {string} printSectionId - Id of the printing section.
|
|
149
|
-
* @returns {string | null} - HTML content of the printing section, or null if not found.
|
|
150
|
-
* @private
|
|
151
|
-
*/
|
|
152
|
-
private getHtmlContents(printSectionId: string): string | null {
|
|
153
|
-
const printContents = document.getElementById(printSectionId);
|
|
154
|
-
if (!printContents) return null;
|
|
155
|
-
|
|
156
|
-
const inputEls = printContents.getElementsByTagName('input');
|
|
157
|
-
const selectEls = printContents.getElementsByTagName('select');
|
|
158
|
-
const textAreaEls = printContents.getElementsByTagName('textarea');
|
|
159
|
-
const canvasEls = printContents.getElementsByTagName('canvas');
|
|
160
|
-
|
|
161
|
-
this.updateInputDefaults(inputEls);
|
|
162
|
-
this.updateSelectDefaults(selectEls);
|
|
163
|
-
this.updateTextAreaDefaults(textAreaEls);
|
|
164
|
-
this.updateCanvasToImage(canvasEls);
|
|
165
|
-
|
|
166
|
-
return printContents.innerHTML;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Retrieves the HTML content of elements with the specified tag.
|
|
171
|
-
*
|
|
172
|
-
* @param {keyof HTMLElementTagNameMap} tag - HTML tag name.
|
|
173
|
-
* @returns {string} - Concatenated outerHTML of elements with the specified tag.
|
|
174
|
-
* @private
|
|
175
|
-
*/
|
|
176
|
-
private getElementTag(tag: keyof HTMLElementTagNameMap): string {
|
|
177
|
-
const html: string[] = [];
|
|
178
|
-
const elements = document.getElementsByTagName(tag);
|
|
179
|
-
for (let index = 0; index < elements.length; index++) {
|
|
180
|
-
html.push(elements[index].outerHTML);
|
|
181
|
-
}
|
|
182
|
-
return html.join('\r\n');
|
|
183
|
-
}
|
|
184
|
-
//#endregion
|
|
185
|
-
|
|
186
|
-
protected notifyPrintComplete() {
|
|
187
|
-
this.printComplete.next();
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Prints the specified content using the provided print options.
|
|
192
|
-
*
|
|
193
|
-
* @param {PrintOptions} printOptions - Options for printing.
|
|
194
|
-
* @public
|
|
195
|
-
*/
|
|
196
|
-
protected print(printOptions: PrintOptions): void {
|
|
197
|
-
let styles = '',
|
|
198
|
-
links = '',
|
|
199
|
-
popOut = 'top=0,left=0,height=auto,width=auto';
|
|
200
|
-
const baseTag = this.getElementTag('base');
|
|
201
|
-
|
|
202
|
-
if (printOptions.useExistingCss) {
|
|
203
|
-
styles = this.getElementTag('style');
|
|
204
|
-
links = this.getElementTag('link');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// If the openNewTab option is set to true, then set the popOut option to an empty string.
|
|
208
|
-
// This will cause the print dialog to open in a new tab.
|
|
209
|
-
if (printOptions.openNewTab) {
|
|
210
|
-
popOut = '';
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const printContents = this.getHtmlContents(printOptions.printSectionId);
|
|
214
|
-
if (!printContents) {
|
|
215
|
-
// Handle the case where the specified print section is not found.
|
|
216
|
-
console.error(`Print section with id ${printOptions.printSectionId} not found.`);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const popupWin = window.open('', '_blank', popOut);
|
|
221
|
-
|
|
222
|
-
if (!popupWin) {
|
|
223
|
-
// the popup window could not be opened.
|
|
224
|
-
console.error('Could not open print window.');
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
popupWin.document.open();
|
|
229
|
-
|
|
230
|
-
// Create the HTML structure
|
|
231
|
-
const doc = popupWin.document;
|
|
232
|
-
|
|
233
|
-
// Set up the basic HTML structure
|
|
234
|
-
const html = doc.createElement('html');
|
|
235
|
-
const head = doc.createElement('head');
|
|
236
|
-
const body = doc.createElement('body');
|
|
237
|
-
|
|
238
|
-
// Set title
|
|
239
|
-
const title = doc.createElement('title');
|
|
240
|
-
title.textContent = printOptions.printTitle || '';
|
|
241
|
-
head.appendChild(title);
|
|
242
|
-
|
|
243
|
-
// Add base tag, styles, and links
|
|
244
|
-
if (baseTag) {
|
|
245
|
-
head.innerHTML += baseTag;
|
|
246
|
-
}
|
|
247
|
-
head.innerHTML += this.returnStyleValues();
|
|
248
|
-
head.innerHTML += this.returnStyleSheetLinkTags();
|
|
249
|
-
head.innerHTML += styles;
|
|
250
|
-
head.innerHTML += links;
|
|
251
|
-
|
|
252
|
-
// Set body class if provided
|
|
253
|
-
if (printOptions.bodyClass) {
|
|
254
|
-
body.className = printOptions.bodyClass;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Insert print contents
|
|
258
|
-
body.innerHTML += printContents;
|
|
259
|
-
|
|
260
|
-
// Add script
|
|
261
|
-
const script = doc.createElement('script');
|
|
262
|
-
script.defer = true;
|
|
263
|
-
|
|
264
|
-
if (this.nonce) {
|
|
265
|
-
script.setAttribute('nonce', this.nonce);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
script.textContent = `
|
|
269
|
-
function triggerPrint(event) {
|
|
270
|
-
window.removeEventListener('load', triggerPrint, false);
|
|
271
|
-
${
|
|
272
|
-
printOptions.previewOnly
|
|
273
|
-
? ''
|
|
274
|
-
: `setTimeout(function() {
|
|
275
|
-
closeWindow(window.print());
|
|
276
|
-
}, ${printOptions.printDelay});`
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
function closeWindow(){
|
|
280
|
-
${printOptions.closeWindow ? 'window.close();' : ''}
|
|
281
|
-
}
|
|
282
|
-
window.addEventListener('load', triggerPrint, false);
|
|
283
|
-
window.addEventListener('afterprint', function () {
|
|
284
|
-
if (window.opener) {
|
|
285
|
-
window.opener.postMessage({ type: 'print-complete' }, '*');
|
|
286
|
-
}
|
|
287
|
-
closeWindow();
|
|
288
|
-
}, { once: true });
|
|
289
|
-
`;
|
|
290
|
-
body.appendChild(script);
|
|
291
|
-
|
|
292
|
-
// Assemble the document
|
|
293
|
-
html.appendChild(head);
|
|
294
|
-
html.appendChild(body);
|
|
295
|
-
doc.appendChild(html);
|
|
296
|
-
|
|
297
|
-
popupWin.document.close();
|
|
298
|
-
|
|
299
|
-
// Listen for the print-complete message
|
|
300
|
-
const handleMessage = (event: MessageEvent) => {
|
|
301
|
-
if (event.data?.type === 'print-complete') {
|
|
302
|
-
this.notifyPrintComplete();
|
|
303
|
-
window.removeEventListener('message', handleMessage);
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
window.addEventListener('message', handleMessage);
|
|
307
|
-
|
|
308
|
-
// Post the print options to the new window after it loads
|
|
309
|
-
popupWin.addEventListener('load', () => {
|
|
310
|
-
// Send the options to the helper function in the child window
|
|
311
|
-
// The child window calls the function exposed by the library (bundle)
|
|
312
|
-
if ((popupWin as any).initPrintWindow) {
|
|
313
|
-
(popupWin as any).initPrintWindow(popupWin, printOptions);
|
|
314
|
-
} else {
|
|
315
|
-
// fallback: send via postMessage if the child will read it
|
|
316
|
-
popupWin.postMessage({ type: 'init-print', options: printOptions }, '*');
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { Component, DebugElement, provideZonelessChangeDetection } from '@angular/core';
|
|
2
|
-
import { TestBed, ComponentFixture } from '@angular/core/testing';
|
|
3
|
-
import { By } from '@angular/platform-browser';
|
|
4
|
-
import { NgxPrintDirective } from './ngx-print.directive';
|
|
5
|
-
|
|
6
|
-
@Component({
|
|
7
|
-
template: `
|
|
8
|
-
<div id="print-section">
|
|
9
|
-
<h1>Welcome to ngx-print</h1>
|
|
10
|
-
<img
|
|
11
|
-
width="300"
|
|
12
|
-
alt="Angular Logo"
|
|
13
|
-
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==" />
|
|
14
|
-
<h2>Here are some links to help you start:</h2>
|
|
15
|
-
<ul>
|
|
16
|
-
<li>
|
|
17
|
-
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
|
|
18
|
-
</li>
|
|
19
|
-
<li>
|
|
20
|
-
<h2><a target="_blank" rel="noopener" href="https://github.com/angular/angular-cli/wiki">CLI Documentation</a></h2>
|
|
21
|
-
</li>
|
|
22
|
-
<li>
|
|
23
|
-
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
|
|
24
|
-
</li>
|
|
25
|
-
</ul>
|
|
26
|
-
<table border="1">
|
|
27
|
-
<tr>
|
|
28
|
-
<td>Row 1, Column 1</td>
|
|
29
|
-
<td>Row 1, Column 2</td>
|
|
30
|
-
</tr>
|
|
31
|
-
<tr>
|
|
32
|
-
<td>Row 2, Column 1</td>
|
|
33
|
-
<td>Row 2, Column 2</td>
|
|
34
|
-
</tr>
|
|
35
|
-
</table>
|
|
36
|
-
</div>
|
|
37
|
-
<button printSectionId="print-section" ngxPrint bodyClass="theme-dark"></button>
|
|
38
|
-
`,
|
|
39
|
-
imports: [NgxPrintDirective],
|
|
40
|
-
})
|
|
41
|
-
class TestNgxPrintComponent {}
|
|
42
|
-
|
|
43
|
-
describe('NgxPrintDirective', () => {
|
|
44
|
-
let buttonEl: DebugElement;
|
|
45
|
-
let component: TestNgxPrintComponent;
|
|
46
|
-
let fixture: ComponentFixture<TestNgxPrintComponent>;
|
|
47
|
-
|
|
48
|
-
// To change this later, so it'll depend on TestNgxPrintComponent
|
|
49
|
-
const styleSheet: { [key: string]: { [key: string]: string } } = {
|
|
50
|
-
'h2': { 'border': 'solid 1px' },
|
|
51
|
-
'h1': { 'color': 'red', 'border': '1px solid' },
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
beforeEach(() => {
|
|
55
|
-
// Configure a NgModule-like decorator metadata
|
|
56
|
-
TestBed.configureTestingModule({
|
|
57
|
-
providers: [provideZonelessChangeDetection()],
|
|
58
|
-
imports: [TestNgxPrintComponent],
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// Create a fixture object (that is going to allows us to create an instance of that component)
|
|
62
|
-
fixture = TestBed.createComponent(TestNgxPrintComponent);
|
|
63
|
-
|
|
64
|
-
// Create a component instance ( ~ new Component)
|
|
65
|
-
component = fixture.componentInstance;
|
|
66
|
-
|
|
67
|
-
// Get the button element (on which we tag the directive) to simulate clicks on it
|
|
68
|
-
buttonEl = fixture.debugElement.query(By.directive(NgxPrintDirective));
|
|
69
|
-
|
|
70
|
-
fixture.detectChanges();
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('should create an instance', () => {
|
|
74
|
-
const directive = buttonEl.injector.get(NgxPrintDirective);
|
|
75
|
-
expect(directive).toBeTruthy();
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('should test the @Input printStyle', () => {
|
|
79
|
-
const directive = buttonEl.injector.get(NgxPrintDirective);
|
|
80
|
-
|
|
81
|
-
// Create a spy on the instance's method
|
|
82
|
-
spyOn(directive, 'returnStyleValues').and.callThrough();
|
|
83
|
-
|
|
84
|
-
// Call the function before checking if it has been called
|
|
85
|
-
directive.returnStyleValues();
|
|
86
|
-
|
|
87
|
-
// Check if returnStyleValues has been called
|
|
88
|
-
expect(directive.returnStyleValues).toHaveBeenCalled();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should returns a string from array of objects', () => {
|
|
92
|
-
const directive = buttonEl.injector.get(NgxPrintDirective);
|
|
93
|
-
directive.printStyle = styleSheet;
|
|
94
|
-
|
|
95
|
-
// Ensure the print styles are correctly formatted in the document
|
|
96
|
-
expect(directive.returnStyleValues()).toEqual('<style> h2{border:solid 1px} h1{color:red;border:1px solid} </style>');
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it(`should popup a new window`, () => {
|
|
100
|
-
spyOn(window, 'open').and.callThrough();
|
|
101
|
-
// simulate click
|
|
102
|
-
buttonEl.triggerEventHandler('click', {});
|
|
103
|
-
expect(window.open).toHaveBeenCalled();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should apply class list to body element in new window', () => {
|
|
107
|
-
const windowOpenSpy = spyOn(window, 'open').and.callThrough();
|
|
108
|
-
|
|
109
|
-
// Simulate click
|
|
110
|
-
buttonEl.triggerEventHandler('click', {});
|
|
111
|
-
|
|
112
|
-
const newWindow = windowOpenSpy.calls.mostRecent().returnValue;
|
|
113
|
-
|
|
114
|
-
// Ensure newWindow is not null before accessing properties
|
|
115
|
-
if (newWindow && newWindow.document && newWindow.document.body) {
|
|
116
|
-
expect(newWindow.document.body.classList.contains('theme-dark')).toBeTrue();
|
|
117
|
-
} else {
|
|
118
|
-
fail('Window was not opened or document/body is not accessible.');
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should emit printComplete when printing finishes', done => {
|
|
123
|
-
let emitted = false;
|
|
124
|
-
|
|
125
|
-
const directive = buttonEl.injector.get(NgxPrintDirective);
|
|
126
|
-
|
|
127
|
-
directive.printCompleted.subscribe(() => {
|
|
128
|
-
emitted = true;
|
|
129
|
-
expect(emitted).toBeTrue();
|
|
130
|
-
done();
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// Trigger the print
|
|
134
|
-
buttonEl.triggerEventHandler('click', null);
|
|
135
|
-
|
|
136
|
-
// Simulate popup posting "print-complete" message back to opener
|
|
137
|
-
window.dispatchEvent(new MessageEvent('message', { data: { type: 'print-complete' } }));
|
|
138
|
-
});
|
|
139
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Directive, HostListener, Input, output } from '@angular/core';
|
|
2
|
-
import { PrintBase } from './ngx-print.base';
|
|
3
|
-
import { PrintOptions } from './print-options';
|
|
4
|
-
import { take } from 'rxjs';
|
|
5
|
-
@Directive({
|
|
6
|
-
selector: '[ngxPrint]',
|
|
7
|
-
standalone: true,
|
|
8
|
-
})
|
|
9
|
-
export class NgxPrintDirective extends PrintBase {
|
|
10
|
-
private printOptions = new PrintOptions();
|
|
11
|
-
/**
|
|
12
|
-
* Prevents the print dialog from opening on the window
|
|
13
|
-
*
|
|
14
|
-
* @memberof NgxPrintDirective
|
|
15
|
-
*/
|
|
16
|
-
@Input() set previewOnly(value: boolean) {
|
|
17
|
-
this.printOptions = { ...this.printOptions, previewOnly: value };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* @memberof NgxPrintDirective
|
|
24
|
-
*/
|
|
25
|
-
@Input() set printSectionId(value: string) {
|
|
26
|
-
this.printOptions = { ...this.printOptions, printSectionId: value };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @memberof NgxPrintDirective
|
|
33
|
-
*/
|
|
34
|
-
@Input() set printTitle(value: string) {
|
|
35
|
-
this.printOptions = { ...this.printOptions, printTitle: value };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* @memberof NgxPrintDirective
|
|
42
|
-
*/
|
|
43
|
-
@Input() set useExistingCss(value: boolean) {
|
|
44
|
-
this.printOptions = { ...this.printOptions, useExistingCss: value };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* A delay in milliseconds to force the print dialog to wait before opened. Default: 0
|
|
49
|
-
*
|
|
50
|
-
* @memberof NgxPrintDirective
|
|
51
|
-
*/
|
|
52
|
-
@Input() set printDelay(value: number) {
|
|
53
|
-
this.printOptions = { ...this.printOptions, printDelay: value };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Whether to close the window after print() returns.
|
|
58
|
-
*
|
|
59
|
-
*/
|
|
60
|
-
@Input() set closeWindow(value: boolean) {
|
|
61
|
-
this.printOptions = { ...this.printOptions, closeWindow: value };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Class attribute to apply to the body element.
|
|
66
|
-
*
|
|
67
|
-
*/
|
|
68
|
-
@Input() set bodyClass(value: string) {
|
|
69
|
-
this.printOptions = { ...this.printOptions, bodyClass: value };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Whether to open a new window or default to new window.
|
|
74
|
-
*
|
|
75
|
-
*/
|
|
76
|
-
@Input() set openNewTab(value: boolean) {
|
|
77
|
-
this.printOptions = { ...this.printOptions, openNewTab: value };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* @memberof NgxPrintDirective
|
|
84
|
-
*/
|
|
85
|
-
@Input()
|
|
86
|
-
set printStyle(values: { [key: string]: { [key: string]: string } }) {
|
|
87
|
-
super.setPrintStyle(values);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @memberof NgxPrintDirective
|
|
92
|
-
* @param cssList
|
|
93
|
-
*/
|
|
94
|
-
@Input()
|
|
95
|
-
set styleSheetFile(cssList: string) {
|
|
96
|
-
super.setStyleSheetFile(cssList);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @memberof NgxPrintDirective
|
|
103
|
-
*/
|
|
104
|
-
@HostListener('click')
|
|
105
|
-
public print(): void {
|
|
106
|
-
super.print(this.printOptions);
|
|
107
|
-
this.printComplete.pipe(take(1)).subscribe(() => {
|
|
108
|
-
this.printCompleted.emit(undefined);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
readonly printCompleted = output<void>();
|
|
113
|
-
}
|