Skip to content

Commit

Permalink
refactored es6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
crabbly committed Apr 27, 2017
1 parent b5ffff8 commit e37ce8e
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 328 deletions.
2 changes: 1 addition & 1 deletion dist/print.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "print.js",
"homepage": "http://printjs.crabbly.com",
"description": "A tiny javascript library to help printing from the web.",
"version": "1.0.16",
"version": "1.0.17",
"main": "src/print.js",
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import print from './js/init'

const printJS = print.init
const printjs = print.init

if (typeof window !== 'undefined') {
window.printJS = printJS
window.printjs = printjs
}

export default printJS
export default printjs
23 changes: 9 additions & 14 deletions src/js/browser.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
export default {
const Browser = {
// Firefox 1.0+
isFirefox: function () {
isFirefox: () => {
return typeof InstallTrigger !== 'undefined'
},

// Internet Explorer 6-11
isIE: function () {
isIE: () => {
return !!document.documentMode
},

// Edge 20+
isEdge: function () {
return !this.isIE() && !!window.StyleMedia
isEdge: () => {
return !Browser.isIE() && !!window.StyleMedia
},

// Chrome 1+
isChrome: function () {
isChrome: () => {
return !!window.chrome && !!window.chrome.webstore
},

// Opera 8.0+
// let isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0

// At least Safari 3+: "[object HTMLElementConstructor]"
isSafari: function () {
isSafari: () => {
return Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0
}
}

export default Browser
41 changes: 0 additions & 41 deletions src/js/class.js

This file was deleted.

48 changes: 19 additions & 29 deletions src/js/functions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
export function extend (a, b) {
for (let key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key]
}
}

return a
}

export function addWrapper (htmlData, params) {
let bodyStyle = 'font-family:' + params.font + ' !important; font-size: ' + params.font_size + ' !important; width:100%;'
return '<div style="' + bodyStyle + '">' + htmlData + '</div>'
Expand All @@ -22,24 +12,24 @@ export function collectStyles (element, params) {

let style = []

// String variable to hold styling for each element
// String variable to hold styling for each element
let elementStyle = ''

if (win.getComputedStyle) { // modern browsers
style = win.getComputedStyle(element, '')

// Styles including
// Styles including
let targetStyles = ['border', 'float', 'box', 'break', 'text-decoration']

// Exact match
// Exact match
let targetStyle = ['clear', 'display', 'width', 'min-width', 'height', 'min-height', 'max-height']

// Optional - include margin and padding
// Optional - include margin and padding
if (params.honorMarginPadding) {
targetStyles.push('margin', 'padding')
}

// Optional - include color
// Optional - include color
if (params.honorColor) {
targetStyles.push('color')
}
Expand All @@ -61,7 +51,7 @@ export function collectStyles (element, params) {
}
}

// Print friendly defaults
// Print friendly defaults
elementStyle += 'max-width: ' + params.maxWidth + 'px !important;' + params.font_size + ' !important;'

return elementStyle
Expand All @@ -71,38 +61,38 @@ export function loopNodesCollectStyles (elements, params) {
for (let n = 0; n < elements.length; n++) {
let currentElement = elements[n]

// Form Printing - check if is element Input
// Form Printing - check if is element Input
let tag = currentElement.tagName
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') {
// save style to variable
// Save style to variable
let textStyle = collectStyles(currentElement, params)

// remove INPUT element and insert a text node
// Remove INPUT element and insert a text node
let parent = currentElement.parentNode

// get text value
// Get text value
let textNode = tag === 'SELECT'
? document.createTextNode(currentElement.options[currentElement.selectedIndex].text)
: document.createTextNode(currentElement.value)

// create text element
// Create text element
let textElement = document.createElement('div')
textElement.appendChild(textNode)

// add style to text
// Add style to text
textElement.setAttribute('style', textStyle)

// add text
// Add text
parent.appendChild(textElement)

// remove input
// Remove input
parent.removeChild(currentElement)
} else {
// get all styling for print element
// Get all styling for print element
currentElement.setAttribute('style', collectStyles(currentElement, params))
}

// check if more elements in tree
// Check if more elements in tree
let children = currentElement.children

if (children && children.length) {
Expand All @@ -112,13 +102,13 @@ export function loopNodesCollectStyles (elements, params) {
}

export function addHeader (printElement, header) {
// create header element
// Create header element
let headerElement = document.createElement('h1')

// create header text node
// Create header text node
let headerNode = document.createTextNode(header)

// build and style
// Build and style
headerElement.appendChild(headerNode)
headerElement.setAttribute('style', 'font-weight:300;')

Expand Down
45 changes: 23 additions & 22 deletions src/js/html.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import { collectStyles, loopNodesCollectStyles, addWrapper, addHeader } from './functions'
import Print from './print'

export default function (PrintJS) {
PrintJS.prototype.html = function () {
// get HTML printable element
let printElement = document.getElementById(this.params.printable)
export default {
print: (params, printFrame) => {
// Get HTML printable element
let printElement = document.getElementById(params.printable)

// check if element exists
// Check if element exists
if (!printElement) {
window.console.error('Invalid HTML element id: ' + this.params.printable)
window.console.error('Invalid HTML element id: ' + params.printable)

return false
}

// make a copy of the printElement to prevent DOM changes
// Make a copy of the printElement to prevent DOM changes
let printableElement = document.createElement('div')
printableElement.appendChild(printElement.cloneNode(true))

// add cloned element to DOM, to have DOM element methods available. It will also be easier to colect styles
// Add cloned element to DOM, to have DOM element methods available. It will also be easier to colect styles
printableElement.setAttribute('style', 'display:none;')
printableElement.setAttribute('id', 'printJS-html')
printElement.parentNode.appendChild(printableElement)

// update printableElement variable with newly created DOM element
// Update printableElement variable with newly created DOM element
printableElement = document.getElementById('printJS-html')

// get main element styling
printableElement.setAttribute('style', collectStyles(printableElement, this.params) + 'margin:0 !important;')
// Get main element styling
printableElement.setAttribute('style', collectStyles(printableElement, params) + 'margin:0 !important;')

// get all children elements
// Get all children elements
let elements = printableElement.children

// get styles for all children elements
loopNodesCollectStyles(elements, this.params)
// Get styles for all children elements
loopNodesCollectStyles(elements, params)

// add header if any
if (this.params.header) {
addHeader(printableElement, this.params.header)
// Add header if any
if (params.header) {
addHeader(printableElement, params.header)
}

// remove DOM printableElement
// Remove DOM printableElement
printableElement.parentNode.removeChild(printableElement)

// store html data
this.params.htmlData = addWrapper(printableElement.innerHTML, this.params)
// Store html data
params.htmlData = addWrapper(printableElement.innerHTML, params)

// print html element contents
this.print()
// Print html element contents
Print.send(params, printFrame)
}
}
38 changes: 20 additions & 18 deletions src/js/image.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import { addHeader } from './functions'
import browser from './browser'
import Browser from './browser'
import Print from './print'

export default function (PrintJS) {
PrintJS.prototype.image = function () {
// create the image element
export default {
print: (params, printFrame) => {
// Create the image element
let img = document.createElement('img')

// Set image src with image file url
img.src = params.printable

img.setAttribute('style', 'width:100%;')
img.setAttribute('id', 'printableImage')

// set image src with image file url
img.src = this.params.printable

// create wrapper
// Create wrapper
let printableElement = document.createElement('div')
printableElement.setAttribute('style', 'width:100%')

// to prevent firefox from not loading images within iframe, we can use base64-encoded data URL of images pixel data
if (browser.isFirefox()) {
// let's make firefox happy
// To prevent firefox from not loading images within iframe, we can use base64-encoded data URL of images pixel data
if (Browser.isFirefox()) {
// Let's make firefox happy
let canvas = document.createElement('canvas')
canvas.setAttribute('width', img.width)
canvas.setAttribute('height', img.height)
let context = canvas.getContext('2d')
context.drawImage(img, 0, 0)

// reset img src attribute with canvas dataURL
// Reset img src attribute with canvas dataURL
img.setAttribute('src', canvas.toDataURL('JPEG', 1.0))
}

printableElement.appendChild(img)

// add header if any
if (this.params.header) {
// Check if we are adding a header for the image
if (params.header) {
addHeader(printableElement)
}

// store html data
this.params.htmlData = printableElement.outerHTML
// Store html data
params.htmlData = printableElement.outerHTML

// print image
this.print()
// Print image
Print.send(params, printFrame)
}
}
Loading

0 comments on commit e37ce8e

Please sign in to comment.