Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using without mixin #29

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 62 additions & 46 deletions build/vue-html-to-paper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.VueHtmlToPaper = factory());
}(this, function () { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.VueHtmlToPaper = {}));
}(this, function (exports) { 'use strict';

function addStyles (win, styles) {
styles.forEach(style => {
Expand All @@ -14,56 +14,72 @@
});
}

const VueHtmlToPaper = {
install (Vue, options = {}) {
Vue.mixin({
methods: {
$htmlToPaper (el, cb = () => true) {
let {
name = '_blank',
specs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
replace = true,
styles = []
} = options;
specs = !!specs.length ? specs.join(',') : '';
const options = {
name: '_blank',
specs: ['fullscreen=yes', 'titlebar=yes', 'scrollbars=yes'].join(','),
replace: true,
styles: []
};

const setOptions = ({ name, specs, replace, styles }) => {
specs = !!specs.length ? specs.join(',') : '';

const element = document.getElementById(el);
options.name = name || options.name;
options.specs = specs || options.specs;
options.replace = replace || options.replace;
options.styles = styles || options.styles;
};

const htmlToPaper = (el, cb = () => true) => {
const element = document.getElementById(el);

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

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

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

win.document.write(`
<html>
<head>
<title>${document.title}</title>
</head>
<body>
${element.innerHTML}
</body>
</html>
`);
win.document.write(`
<html>
<head>
<title>${document.title}</title>
</head>
<body>
${element.innerHTML}
</body>
</html>
`);

addStyles(win, styles);

setTimeout(() => {
win.document.close();
win.focus();
win.print();
win.close();
cb();
}, 1000);
return true;
}
addStyles(win, options.styles);

setTimeout(() => {
win.document.close();
win.focus();
win.print();
win.close();
cb();
}, 1000);
return true;
};

const VueHtmlToPaper = {
install(Vue, options = {}) {
setOptions(options);

Vue.mixin({
methods: {
$htmlToPaper: htmlToPaper
}
});
}
};

return VueHtmlToPaper;
exports.default = VueHtmlToPaper;
exports.htmlToPaper = htmlToPaper;
exports.setOptions = setOptions;

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

}));
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 57 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,67 @@ function addStyles (win, styles) {
});
}

const options = {
name: '_blank',
specs: ['fullscreen=yes', 'titlebar=yes', 'scrollbars=yes'].join(','),
replace: true,
styles: []
};

const setOptions = ({ name, specs, replace, styles }) => {
specs = !!specs.length ? specs.join(',') : '';

options.name = name || options.name;
options.specs = specs || options.specs;
options.replace = replace || options.replace;
options.styles = styles || options.styles;
};

const htmlToPaper = (el, cb = () => true) => {
const element = document.getElementById(el);

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

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

win.document.write(`
<html>
<head>
<title>${document.title}</title>
</head>
<body>
${element.innerHTML}
</body>
</html>
`);

addStyles(win, options.styles);

setTimeout(() => {
win.document.close();
win.focus();
win.print();
win.close();
cb();
}, 1000);
return true;
};

const VueHtmlToPaper = {
install (Vue, options = {}) {
install(Vue, options = {}) {
setOptions(options);

Vue.mixin({
methods: {
$htmlToPaper (el, cb = () => true) {
let {
name = '_blank',
specs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
replace = true,
styles = []
} = options;
specs = !!specs.length ? specs.join(',') : '';

const element = document.getElementById(el);

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

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

win.document.write(`
<html>
<head>
<title>${document.title}</title>
</head>
<body>
${element.innerHTML}
</body>
</html>
`);

addStyles(win, styles);

setTimeout(() => {
win.document.close();
win.focus();
win.print();
win.close();
cb();
}, 1000);
return true;
}
$htmlToPaper: htmlToPaper
}
});
}
}
};

export default VueHtmlToPaper;
export default VueHtmlToPaper;
export { htmlToPaper, setOptions };