-
Notifications
You must be signed in to change notification settings - Fork 78
/
site-print-button.js
261 lines (260 loc) · 6.74 KB
/
site-print-button.js
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
/**
* Copyright 2019 The Pennsylvania State University
* @license Apache-2.0, see License.md for full text.
*/
import { LitElement, html, css } from "lit-element/lit-element.js";
import { store } from "@lrnwebcomponents/haxcms-elements/lib/core/haxcms-site-store.js";
/**
* @deprecatedApply - required for @apply / invoking @apply css var convention
*/
import "@polymer/polymer/lib/elements/custom-style.js";
/**
* `site-print-button`
* `Dynamic print button to request and generate what to print`
*
*/
class SitePrintButton extends LitElement {
/**
* LitElement constructable styles enhancement
*/
static get styles() {
return [
css`
:host {
display: block;
text-rendering: optimizelegibility;
position: relative;
}
`
];
}
/**
* Store the tag name to make it easier to obtain directly.
*/
static get tag() {
return "site-print-button";
}
constructor() {
super();
this.icon = "icons:print";
this.position = "bottom";
this.type = "page";
import("@polymer/paper-tooltip/paper-tooltip.js");
import("@polymer/paper-icon-button/paper-icon-button.js");
}
// render function
render() {
return html`
<custom-style>
<style>
paper-icon-button {
@apply --site-print-button-button;
}
paper-icon-button:hover,
paper-icon-button:focus,
paper-icon-button:active {
@apply --site-print-button-button-hover;
}
paper-tooltip {
@apply --site-print-button-tooltip;
}
</style>
</custom-style>
<paper-icon-button
id="btn"
icon="${this.icon}"
@click="${this.print}"
.title="${this.label}"
?disabled="${this.disabled}"
></paper-icon-button>
<paper-tooltip for="btn" position="${this.position}" offset="14">
${this.label}
</paper-tooltip>
`;
}
updated(changedProperties) {
changedProperties.forEach((oldValue, propName) => {
if (propName == "type") {
this._typeChanged(this[propName], oldValue);
}
});
}
/**
* Props
*/
static get properties() {
return {
/**
* icon
*/
icon: {
type: String
},
disabled: {
type: Boolean,
reflect: true
},
/**
* label for the button
*/
label: {
type: String
},
/**
* label for the button
*/
position: {
type: String
},
/**
* How much do you want to print right now
*/
type: {
type: String
}
};
}
/**
* ensure we have a label set per type if its empty
*/
_typeChanged(newValue) {
if (typeof this.label === typeof undefined) {
if (newValue === "page" || newValue === "site") {
this.label = `Print ${newValue}`;
} else {
this.label = `Print outline`;
}
}
}
/**
* Print the type in question
*/
async print(e) {
// now dynamic import the footer so we don't bloat all other page loads
import("@lrnwebcomponents/haxcms-elements/lib/ui-components/layout/site-footer.js");
const type = this.type;
let content = "";
if (type === "page") {
// oh there you are
content = `
<h1>${store.activeItem.title}</h1>
<div>${store.activeItemContent}</div>`;
} else if (type === "parent") {
// everything from the parent of this page, down
const result = store.computeItems(
1,
100,
store.activeId,
"parent",
store.routerManifest,
true
);
for (var i in result) {
content += await fetch("pages/" + result[i].location + "/index.html")
.then(function(response) {
return response.text();
})
.then(function(response) {
let indent = result[i].indent + 1;
if (indent > 6) {
indent = 6;
}
return `<h${indent}>${result[i].title}</h${indent}>
${response}`;
});
}
} else if (type === "ancestor") {
// everything from highest parent
const result = store.computeItems(
1,
100,
store.activeId,
"ancestor",
store.routerManifest,
true
);
for (var i in result) {
content += await fetch("pages/" + result[i].location + "/index.html")
.then(function(response) {
return response.text();
})
.then(function(response) {
let indent = result[i].indent + 1;
if (indent > 6) {
indent = 6;
}
return `<h${indent}>${result[i].title}</h${indent}>
${response}`;
});
}
} else if (type === "site") {
const result = store.routerManifest.items;
for (var i in result) {
content += await fetch("pages/" + result[i].location + "/index.html")
.then(function(response) {
return response.text();
})
.then(function(response) {
let indent = result[i].indent + 1;
if (indent > 6) {
indent = 6;
}
return `<h${indent}>${result[i].title}</h${indent}>
${response}`;
});
}
}
// get current date
var currentdate = new Date();
var datetime =
"Printed on: " +
currentdate.getDate() +
"/" +
(currentdate.getMonth() + 1) +
"/" +
currentdate.getFullYear() +
" at " +
currentdate.getHours() +
":" +
currentdate.getMinutes() +
":" +
currentdate.getSeconds();
let element = document.createElement("div");
element.innerHTML = `
<style>
@media print {
body {
padding: 0 15px 5px;
color: #000;
background-color: #ffffff;
border-top: 1px solid #aaa;
}
}
</style>
<h1>${store.siteTitle}</h1>
<site-footer></site-footer>
<p>${datetime}</p>
${content}`;
// From https://stackoverflow.com/questions/1071962/how-do-i-print-part-of-a-rendered-html-page-in-javascript#answer-1072151
let print = window.open(
"",
"",
"left=0,top=0,width=800,height=800,toolbar=0,scrollbars=0,status =0"
);
// ensure base tag moves with it if set
if (document.head.getElementsByTagName("base")) {
let base = document.createElement("base");
base.href = document.head
.getElementsByTagName("base")[0]
.getAttribute("href");
print.document.head.appendChild(base);
}
print.document.body.appendChild(element);
print.document.close();
print.focus();
print.print();
print.close();
}
}
window.customElements.define(SitePrintButton.tag, SitePrintButton);
export { SitePrintButton };