Skip to content

Commit

Permalink
Merge pull request mycurelabs#96 from mycurelabs/refactor/branch-form…
Browse files Browse the repository at this point in the history
…atting

Refactor/fix-code-formatting
  • Loading branch information
jofftiquez authored Oct 31, 2021
2 parents 0992d39 + 509b99f commit d0506da
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 156 deletions.
79 changes: 41 additions & 38 deletions build/vue-html-to-paper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,65 @@
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.VueHtmlToPaper = factory());
}(this, (function () { 'use strict';

const addStyles = (win, styles) => {
styles.forEach((style) => {
function addStyles (win, styles) {
styles.forEach(style => {
let link = win.document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', style);
win.document.getElementsByTagName('head')[0].appendChild(link);
});
};
}

function openWindow (url, name, props) {
let windowRef = null;
windowRef = window.open(url, name, props);
if (!windowRef.opener) {
windowRef.opener = self;
}
windowRef.focus();
return windowRef;
}

const VueHtmlToPaper = {
install (Vue, options = {}) {
Vue.prototype.$htmlToPaper = (el, localOptions = {}, cb = () => true) => {
Vue.prototype.$htmlToPaper = (el, localOptions, cb = () => true) => {
let defaultName = '_blank',
defaultSpecs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
defaultStyles = [],
defaultTimeout = 1000,
defaultAutoClose = true,
defaultWindowTitle = window.document.title;
defaultReplace = true,
defaultStyles = [];
let {
name = defaultName,
specs = defaultSpecs,
replace = defaultReplace,
styles = defaultStyles,
timeout = defaultTimeout,
autoClose = defaultAutoClose,
windowTitle = defaultWindowTitle,
} = options;

if (localOptions.name) name = localOptions.name;
if (localOptions.specs) specs = localOptions.specs;
if (localOptions.styles) styles = localOptions.styles;
if (localOptions.timeout) timeout = localOptions.timeout;
if (localOptions.autoClose) autoClose = localOptions.autoClose;
if (localOptions.windowTitle) windowTitle = localOptions.windowTitle;

// If has localOptions
// TODO: improve logic
if (!!localOptions) {
if (localOptions.name) name = localOptions.name;
if (localOptions.specs) specs = localOptions.specs;
if (localOptions.replace) replace = localOptions.replace;
if (localOptions.styles) styles = localOptions.styles;
}

specs = !!specs.length ? specs.join(',') : '';

let element = el;
if (typeof el === 'string' || el instanceof String) {
element = window.document.getElementById(el);
}
const element = window.document.getElementById(el);

if (!element) {
alert(`Element to print #${el} not found!`);
return;
}

const url = '';
const win = window.open(url, name, specs);
const win = openWindow(url, name, specs);

win.document.write(`
<html>
<head>
<title>${windowTitle}</title>
<title>${window.document.title}</title>
</head>
<body>
${element.innerHTML}
Expand All @@ -65,19 +71,16 @@
`);

addStyles(win, styles);

const promise = new Promise((resolve) => {
setTimeout(() => {
win.focus();
win.print();
autoClose && win.document.close();
autoClose && win.close();
if (cb) cb();
resolve();
}, timeout);
});

return cb ? true : promise;

setTimeout(() => {
win.document.close();
win.focus();
win.print();
setTimeout(function () {window.close();}, 1);
cb();
}, 1000);

return true;
};
},
};
Expand Down
79 changes: 41 additions & 38 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,65 @@

Object.defineProperty(exports, '__esModule', { value: true });

const addStyles = (win, styles) => {
styles.forEach((style) => {
function addStyles (win, styles) {
styles.forEach(style => {
let link = win.document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', style);
win.document.getElementsByTagName('head')[0].appendChild(link);
});
};
}

function openWindow (url, name, props) {
let windowRef = null;
windowRef = window.open(url, name, props);
if (!windowRef.opener) {
windowRef.opener = self;
}
windowRef.focus();
return windowRef;
}

const VueHtmlToPaper = {
install (Vue, options = {}) {
Vue.prototype.$htmlToPaper = (el, localOptions = {}, cb = () => true) => {
Vue.prototype.$htmlToPaper = (el, localOptions, cb = () => true) => {
let defaultName = '_blank',
defaultSpecs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
defaultStyles = [],
defaultTimeout = 1000,
defaultAutoClose = true,
defaultWindowTitle = window.document.title;
defaultReplace = true,
defaultStyles = [];
let {
name = defaultName,
specs = defaultSpecs,
replace = defaultReplace,
styles = defaultStyles,
timeout = defaultTimeout,
autoClose = defaultAutoClose,
windowTitle = defaultWindowTitle,
} = options;

if (localOptions.name) name = localOptions.name;
if (localOptions.specs) specs = localOptions.specs;
if (localOptions.styles) styles = localOptions.styles;
if (localOptions.timeout) timeout = localOptions.timeout;
if (localOptions.autoClose) autoClose = localOptions.autoClose;
if (localOptions.windowTitle) windowTitle = localOptions.windowTitle;

// If has localOptions
// TODO: improve logic
if (!!localOptions) {
if (localOptions.name) name = localOptions.name;
if (localOptions.specs) specs = localOptions.specs;
if (localOptions.replace) replace = localOptions.replace;
if (localOptions.styles) styles = localOptions.styles;
}

specs = !!specs.length ? specs.join(',') : '';

let element = el;
if (typeof el === 'string' || el instanceof String) {
element = window.document.getElementById(el);
}
const element = window.document.getElementById(el);

if (!element) {
alert(`Element to print #${el} not found!`);
return;
}

const url = '';
const win = window.open(url, name, specs);
const win = openWindow(url, name, specs);

win.document.write(`
<html>
<head>
<title>${windowTitle}</title>
<title>${window.document.title}</title>
</head>
<body>
${element.innerHTML}
Expand All @@ -63,19 +69,16 @@ const VueHtmlToPaper = {
`);

addStyles(win, styles);

const promise = new Promise((resolve) => {
setTimeout(() => {
win.focus();
win.print();
autoClose && win.document.close();
autoClose && win.close();
if (cb) cb();
resolve();
}, timeout);
});

return cb ? true : promise;

setTimeout(() => {
win.document.close();
win.focus();
win.print();
setTimeout(function () {window.close();}, 1);
cb();
}, 1000);

return true;
};
},
};
Expand Down
Loading

0 comments on commit d0506da

Please sign in to comment.