ElectionList over PdfMake

This commit is contained in:
Sean Engelhardt 2016-11-03 14:32:20 +01:00 committed by Emanuel Schütze
parent bc321bd714
commit 7069326655
5 changed files with 128 additions and 25 deletions

View File

@ -9,16 +9,16 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
'PdfPredefinedFunctions', 'PdfPredefinedFunctions',
function(gettextCatalog, PdfPredefinedFunctions) { function(gettextCatalog, PdfPredefinedFunctions) {
var createInstance = function(scope, polls) { var createInstance = function(assignment) {
//use the Predefined Functions to create the title //use the Predefined Functions to create the title
var title = PdfPredefinedFunctions.createTitle( var title = PdfPredefinedFunctions.createTitle(
gettextCatalog.getString("Election") + ": " + scope.assignment.title); gettextCatalog.getString("Election") + ": " + assignment.title);
//create the preamble //create the preamble
var createPreamble = function() { var createPreamble = function() {
var preambleText = gettextCatalog.getString("Number of posts to be elected") + ": "; var preambleText = gettextCatalog.getString("Number of posts to be elected") + ": ";
var memberNumber = ""+scope.assignment.open_posts; var memberNumber = ""+assignment.open_posts;
var preamble = { var preamble = {
text: [ text: [
{ {
@ -37,7 +37,7 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
//adds the description if present in the assignment //adds the description if present in the assignment
var createDescription = function() { var createDescription = function() {
if (scope.assignment.description) { if (assignment.description) {
var descriptionText = gettextCatalog.getString("Description") + ":"; var descriptionText = gettextCatalog.getString("Description") + ":";
var description = [ var description = [
{ {
@ -46,7 +46,7 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
style: 'textItem' style: 'textItem'
}, },
{ {
text: scope.assignment.description, text: assignment.description,
style: 'textItem', style: 'textItem',
margin: [10, 0, 0, 0] margin: [10, 0, 0, 0]
} }
@ -59,11 +59,11 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
//creates the candidate list in columns if the assignment phase is 'voting' //creates the candidate list in columns if the assignment phase is 'voting'
var createCandidateList = function() { var createCandidateList = function() {
if (scope.assignment.phase != 2) { if (assignment.phase != 2) {
var candidatesText = gettextCatalog.getString("Candidates") + ": "; var candidatesText = gettextCatalog.getString("Candidates") + ": ";
var userList = []; var userList = [];
angular.forEach(scope.assignment.assignment_related_users, function(assignmentsRelatedUser) { angular.forEach(assignment.assignment_related_users, function(assignmentsRelatedUser) {
userList.push({ userList.push({
text: assignmentsRelatedUser.user.get_full_name(), text: assignmentsRelatedUser.user.get_full_name(),
margin: [0, 0, 0, 10], margin: [0, 0, 0, 10],
@ -110,7 +110,7 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
//creates the pull result table //creates the pull result table
var createPollResultTable = function() { var createPollResultTable = function() {
var resultBody = []; var resultBody = [];
angular.forEach(polls, function(poll, pollIndex) { angular.forEach(assignment.polls, function(poll, pollIndex) {
if (poll.published) { if (poll.published) {
var voteNrTotal = poll.votescast; var voteNrTotal = poll.votescast;
var voteNrValid = poll.votesvalid; var voteNrValid = poll.votesvalid;
@ -253,6 +253,10 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
return resultBody; return resultBody;
}; };
var getTitle = function() {
return assignment.title;
};
var getContent = function() { var getContent = function() {
return [ return [
title, title,
@ -264,7 +268,8 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
}; };
return { return {
getContent: getContent getContent: getContent,
getTitle: getTitle
}; };
}; };
@ -440,7 +445,78 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf'])
return { return {
createInstance: createInstance createInstance: createInstance
}; };
}])
.factory('AssignmentCatalogContentProvider', [
'gettextCatalog',
'PdfPredefinedFunctions',
function(gettextCatalog, PdfPredefinedFunctions) {
var createInstance = function(allAssignmnets, Config) {
var title = PdfPredefinedFunctions.createTitle(Config.get('assignments_pdf_title').value);
var createPreamble = function() {
var preambleText = Config.get('assignments_pdf_preamble').value;
if (preambleText) {
return {
text: preambleText,
style: "preamble"
};
} else {
return "";
}
};
var createTOContent = function(assignmentTitles) {
var heading = {
text: gettextCatalog.getString("Table of contents"),
style: "heading",
};
var toc = [];
angular.forEach(assignmentTitles, function(title, key) {
toc.push({
text: gettextCatalog.getString("Election") + " " + (key+1) + ": " + title,
style: "tableofcontent",
});
});
return [
heading,
toc,
PdfPredefinedFunctions.addPageBreak()
];
};
var getContent = function() {
var content = [];
var assignmentContent = [];
var assignmentTitles = [];
angular.forEach(allAssignmnets, function(assignment, key) {
assignmentTitles.push(assignment.getTitle());
assignmentContent.push(assignment.getContent());
if (key < allAssignmnets.length - 1) {
assignmentContent.push(PdfPredefinedFunctions.addPageBreak());
}
});
content.push(title);
content.push(createPreamble());
content.push(createTOContent(assignmentTitles));
content.push(assignmentContent);
return content;
};
return {
getContent: getContent
};
};
return {
createInstance: createInstance
};
}]); }]);
}()); }());

View File

@ -40,6 +40,9 @@ angular.module('OpenSlidesApp.assignments.site', [
assignments: function(Assignment) { assignments: function(Assignment) {
return Assignment.findAll(); return Assignment.findAll();
}, },
users: function(User) { //TODO: Load only related users cause of better performance
return User.findAll();
},
tags: function(Tag) { tags: function(Tag) {
return Tag.findAll(); return Tag.findAll();
}, },
@ -239,7 +242,13 @@ angular.module('OpenSlidesApp.assignments.site', [
'phases', 'phases',
'Projector', 'Projector',
'ProjectionDefault', 'ProjectionDefault',
function($scope, ngDialog, AssignmentForm, Assignment, Tag, Agenda, phases, Projector, ProjectionDefault) { 'gettextCatalog',
'Config',
'AssignmentContentProvider',
'AssignmentCatalogContentProvider',
'PdfMakeDocumentProvider',
function($scope, ngDialog, AssignmentForm, Assignment, Tag, Agenda, phases, Projector, ProjectionDefault,
gettextCatalog, Config, AssignmentContentProvider, AssignmentCatalogContentProvider, PdfMakeDocumentProvider) {
Assignment.bindAll({}, $scope, 'assignments'); Assignment.bindAll({}, $scope, 'assignments');
Tag.bindAll({}, $scope, 'tags'); Tag.bindAll({}, $scope, 'tags');
$scope.$watch(function () { $scope.$watch(function () {
@ -338,6 +347,20 @@ angular.module('OpenSlidesApp.assignments.site', [
$scope.delete = function (assignment) { $scope.delete = function (assignment) {
Assignment.destroy(assignment.id); Assignment.destroy(assignment.id);
}; };
// create the PDF List
$scope.makePDF_assignmentList = function () {
var filename = gettextCatalog.getString("Elections") + ".pdf";
var assignmentContentProviderArray = [];
//convert the filtered assignments to content providers
angular.forEach($scope.assignmentsFiltered, function(assignment) {
assignmentContentProviderArray.push(AssignmentContentProvider.createInstance(assignment));
});
var assignmentCatalogContentProvider = AssignmentCatalogContentProvider.createInstance(assignmentContentProviderArray, Config);
var documentProvider = PdfMakeDocumentProvider.createInstance(assignmentCatalogContentProvider);
pdfMake.createPdf(documentProvider.getDocument()).download(filename);
};
} }
]) ])
@ -525,7 +548,7 @@ angular.module('OpenSlidesApp.assignments.site', [
//creates the document as pdf //creates the document as pdf
$scope.makePDF_singleAssignment = function() { $scope.makePDF_singleAssignment = function() {
var filename = gettextCatalog.getString("Election") + " " + $scope.assignment.title + ".pdf"; var filename = gettextCatalog.getString("Election") + " " + $scope.assignment.title + ".pdf";
var assignmentContentProvider = AssignmentContentProvider.createInstance($scope, assignment.polls); var assignmentContentProvider = AssignmentContentProvider.createInstance(assignment);
var documentProvider = PdfMakeDocumentProvider.createInstance(assignmentContentProvider); var documentProvider = PdfMakeDocumentProvider.createInstance(assignmentContentProvider);
pdfMake.createPdf(documentProvider.getDocument()).download(filename); pdfMake.createPdf(documentProvider.getDocument()).download(filename);
}; };

View File

@ -9,7 +9,7 @@
<i class="fa fa-tags fa-lg"></i> <i class="fa fa-tags fa-lg"></i>
<translate>Tags</translate> <translate>Tags</translate>
</a> </a>
<a ui-sref="assignments_pdf" target="_blank" class="btn btn-default btn-sm"> <a href="" ng-click="makePDF_assignmentList()" class="btn btn-default btn-sm">
<i class="fa fa-file-pdf-o fa-lg"></i> <i class="fa fa-file-pdf-o fa-lg"></i>
<translate>PDF</translate> <translate>PDF</translate>
</a> </a>

View File

@ -19,6 +19,16 @@ angular.module('OpenSlidesApp.core.pdf', [])
}; };
}; };
// function to apply a pagebreak-keyword
PdfPredefinedFunctions.addPageBreak = function() {
return [
{
text: '',
pageBreak: 'after'
}
];
};
PdfPredefinedFunctions.flipTableRowStyle = function(currentTableSize) { PdfPredefinedFunctions.flipTableRowStyle = function(currentTableSize) {
if (currentTableSize % 2 === 0) { if (currentTableSize % 2 === 0) {
return "tableEven"; return "tableEven";
@ -173,6 +183,10 @@ angular.module('OpenSlidesApp.core.pdf', [])
margin: [0,0,0,20], margin: [0,0,0,20],
bold: true bold: true
}, },
preamble: {
fontSize: 12,
margin: [0,0,0,10],
},
userDataTitle: { userDataTitle: {
fontSize: 26, fontSize: 26,
margin: [0,0,0,0], margin: [0,0,0,0],

View File

@ -283,7 +283,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf'])
return [ return [
heading, heading,
toc, toc,
addPageBreak() PdfPredefinedFunctions.addPageBreak()
]; ];
}; };
@ -317,7 +317,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf'])
return [ return [
heading, heading,
toc, toc,
addPageBreak() PdfPredefinedFunctions.addPageBreak()
]; ];
} else { } else {
// if there are no categories, return "empty string" // if there are no categories, return "empty string"
@ -326,16 +326,6 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf'])
} }
}; };
// function to apply a pagebreak-keyword
var addPageBreak = function() {
return [
{
text: '',
pageBreak: 'after'
}
];
};
// returns the pure content of the motion, parseable by makepdf // returns the pure content of the motion, parseable by makepdf
var getContent = function() { var getContent = function() {
var motionContent = []; var motionContent = [];
@ -345,7 +335,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf'])
motionTitles.push(motion.getIdentifier() + ": " + motion.getTitle()); motionTitles.push(motion.getIdentifier() + ": " + motion.getTitle());
motionContent.push(motion.getContent()); motionContent.push(motion.getContent());
if (key < allMotions.length - 1) { if (key < allMotions.length - 1) {
motionContent.push(addPageBreak()); motionContent.push(PdfPredefinedFunctions.addPageBreak());
} }
}); });