mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-13 22:54:25 +01:00
167 lines
87 KiB
JavaScript
167 lines
87 KiB
JavaScript
|
/**
|
||
|
* jsPDFEditor
|
||
|
* @return {[type]} [description]
|
||
|
*/
|
||
|
var jsPDFEditor = function() {
|
||
|
|
||
|
var editor;
|
||
|
|
||
|
var demos = {
|
||
|
'images.js': 'Images',
|
||
|
'font-faces.js': 'Font faces',
|
||
|
'from-html.js': 'HTML Renderer (Early stages)',
|
||
|
'two-page.js': 'Two page Hello World',
|
||
|
'circles.js': 'Circles',
|
||
|
'font-size.js': 'Font sizes',
|
||
|
//'kitchen-sink.js': 'Kitchen Sink', // @TODO
|
||
|
'landscape.js': 'Landscape',
|
||
|
'lines.js': 'Lines',
|
||
|
'rectangles.js': 'Rectangles',
|
||
|
'string-splitting.js': 'String Splitting',
|
||
|
'text-colors.js': 'Text colors',
|
||
|
'triangles.js': 'Triangles',
|
||
|
'user-input.js': 'User input'
|
||
|
};
|
||
|
|
||
|
var aceEditor = function() {
|
||
|
editor = ace.edit("editor");
|
||
|
editor.setTheme("ace/theme/twilight");
|
||
|
//editor.setTheme("ace/theme/ambiance");
|
||
|
editor.getSession().setMode("ace/mode/javascript");
|
||
|
|
||
|
var timeout = setTimeout(function(){ }, 0);
|
||
|
|
||
|
editor.getSession().on('change', function() {
|
||
|
// Hacky workaround to disable auto refresh on user input
|
||
|
if ($('#auto-refresh').is(':checked') && $('#template').val() != 'user-input.js') {
|
||
|
clearTimeout(timeout);
|
||
|
timeout = setTimeout(function() {
|
||
|
jsPDFEditor.update();
|
||
|
|
||
|
}, 200);
|
||
|
}
|
||
|
|
||
|
});
|
||
|
};
|
||
|
|
||
|
var populateDropdown = function() {
|
||
|
var options = '';
|
||
|
for (var demo in demos) {
|
||
|
options += '<option value="' + demo + '">' + demos[demo] + '</option>';
|
||
|
}
|
||
|
$('#template').html(options).on('change', loadSelectedFile);
|
||
|
|
||
|
};
|
||
|
|
||
|
var loadSelectedFile = function() {
|
||
|
if ($('#template').val() == 'user-input.js') {
|
||
|
$('.controls .checkbox').hide();
|
||
|
$('.controls .alert').show();
|
||
|
jsPDFEditor.update(true);
|
||
|
} else {
|
||
|
$('.controls .checkbox').show();
|
||
|
$('.controls .alert').hide();
|
||
|
}
|
||
|
|
||
|
|
||
|
$.get('examples/js/' + $('#template').val(), function(response) {
|
||
|
editor.setValue(response);
|
||
|
editor.gotoLine(0);
|
||
|
|
||
|
// If autorefresh isn't on, then force it when we change examples
|
||
|
if (! $('#auto-refresh').is(':checked')) {
|
||
|
jsPDFEditor.update();
|
||
|
}
|
||
|
|
||
|
}).error(function() {
|
||
|
|
||
|
$('.template-picker').html('<p class="source">More examples in <b>examples/js/</b>. We can\'t load them in automatically because of local filesystem security precautions.</p>');
|
||
|
|
||
|
// Fallback source code
|
||
|
var source = "// You'll need to make your image into a Data URL\n";
|
||
|
source += "// Use http://dataurl.net/#dataurlmaker\n";
|
||
|
source += "var imgData = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4QCqRXhpZgAATU0AKgAAAAgABgESAAMAAAABAAEAAAEaAAUAAAABAAAAVgEbAAUAAAABAAAAXgEoAAMAAAABAAIAAAExAAIAAAAeAAAAZodpAAQAAAABAAAAhAAAAAAAAABIAAAAAQAAAEgAAAABQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2gAAAKgAgAEAAAAAQAAA4CgAwAEAAAAAQAAA4AAAAAA/9sAQwADAgICAgIDAgICAwMDAwQHBAQEBAQIBgYFBwoJCgoKCQoJCwwQDQsMDwwJCg4TDg8QERISEgsNExUTERUQERIR/9sAQwEDAwMEBAQIBAQIEQsKCxERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERER/8AAEQgDgAOAAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscE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A4Ls4HXaa/QP4Ef8ABXL9nD4oG20j4jpe/DbW5sKf7Sb7RprNjot2gG0deZUjA45NAH3LRVLRta0bxHpdtrnh7VrLVNNvUEtteWc6TwTIf4kkUlWHuDV2gAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAOb+JXjK3+Hfw58U/EC7VGh8M6Ld6vIrnCssELykH67K/mBvr271K9uNRv53nubqVp5pXOWd2JLMfckk1/QN/wUq8Vnwj+xT8SruKYpPqNpbaTEASC/2i6hicZH/TNpDz1AI71/PnQAUUUUAFFFFABRRRQAUUUUAeo/A/9pz44/s66uNU+E/j/UdJhZ99xpzP51hdevmWz5RiRxuwGHZhX6hfs3f8Fifhh44+y+G/2g9D/wCEH1l8R/2xZB7jSZm9WXmW3yfXzFHJLqK/GyigD+prRdc0XxLpVrr3h3V7LVdMvoxNa3tlcJPBOh6MkiEqw9wavV/OB+zn+2B8dP2X9YW8+GnitzpMkokvNB1DdPpt367osjYx/vxlX98cV+x37JP/AAUZ+C/7UC23hi7lHg3x46gNoWoXCmO7bv8AZJ+BN/uEK/X5SBuoA+saKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAPgv/AILN6+dK/ZU0jSI3G/WvGNnbsucExpb3UpPTnDRoO3UfQ/iXX69f8FvtVMPw0+GOh7pMXeu3l3gY2HyoFXJ75/f8fU1+QtAB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61j/4HHIJD+EVfSHw+/bd/ZN+KDRxeEfjz4Ua4mIWO11G6Om3EhPZYroRux9gDQB7hRTIZoriJJ4JUkikUOjowKsDyCCOop9ABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRR
|
||
|
source += "\n";
|
||
|
source += "var doc = new jsPDF();\n";
|
||
|
source += "\n";
|
||
|
source += "doc.setFontSize(40);\n";
|
||
|
source += "doc.text(40, 20, \"Octocat loves jsPDF\");\n";
|
||
|
source += "doc.addImage(imgData, 'JPEG', 10, 40, 180, 180);\n";
|
||
|
editor.setValue(source);
|
||
|
editor.gotoLine(0);
|
||
|
});
|
||
|
};
|
||
|
|
||
|
var initAutoRefresh = function() {
|
||
|
$('#auto-refresh').on('change', function() {
|
||
|
if ($('#auto-refresh').is(':checked')) {
|
||
|
$('.run-code').hide();
|
||
|
jsPDFEditor.update();
|
||
|
} else {
|
||
|
$('.run-code').show();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
$('.run-code').click(function() {
|
||
|
jsPDFEditor.update();
|
||
|
return false;
|
||
|
});
|
||
|
};
|
||
|
|
||
|
var initDownloadPDF = function() {
|
||
|
$('.download-pdf').click(function(){
|
||
|
eval(editor.getValue());
|
||
|
|
||
|
var file = demos[$('#template').val()];
|
||
|
if (file === undefined) {
|
||
|
file = 'demo';
|
||
|
}
|
||
|
doc.save(file + '.pdf');
|
||
|
});
|
||
|
return false;
|
||
|
};
|
||
|
|
||
|
return {
|
||
|
/**
|
||
|
* Start the editor demo
|
||
|
* @return {void}
|
||
|
*/
|
||
|
init: function() {
|
||
|
|
||
|
// Init the ACE editor
|
||
|
aceEditor();
|
||
|
|
||
|
populateDropdown();
|
||
|
loadSelectedFile();
|
||
|
// Do the first update on init
|
||
|
jsPDFEditor.update();
|
||
|
|
||
|
initAutoRefresh();
|
||
|
|
||
|
initDownloadPDF();
|
||
|
},
|
||
|
/**
|
||
|
* Update the iframe with current PDF.
|
||
|
*
|
||
|
* @param {boolean} skipEval If true, will skip evaluation of the code
|
||
|
* @return
|
||
|
*/
|
||
|
update: function(skipEval) {
|
||
|
setTimeout(function() {
|
||
|
if (! skipEval) {
|
||
|
eval(editor.getValue());
|
||
|
}
|
||
|
if (doc !== undefined) {
|
||
|
var string = doc.output('datauristring');
|
||
|
$('.preview-pane').attr('src', string);
|
||
|
}
|
||
|
}, 0);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
}();
|
||
|
|
||
|
$(document).ready(function() {
|
||
|
jsPDFEditor.init();
|
||
|
});
|