Skip to content

Commit

Permalink
Unique description field in Presentation
Browse files Browse the repository at this point in the history
Issue #2 : The description goes from defined fields to a unique field
with Title and Content
Issue #10 : The JSON data stores the paragraphs of the description (with
type "Title" or "Paragraph")
Issue #3 : Print the Presentation to PDF
  • Loading branch information
Gulix committed Feb 10, 2016
1 parent 1f1a8c0 commit 96a4955
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 47 deletions.
6 changes: 3 additions & 3 deletions generator/generator.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h2>A playset-editor for Fiasco, with <s>a</s> Mustache ...</h2>

<div class='col-md-offset-1 col-md-2 loadsave-buttons'>
<form style="display:none" id="file-load-form">
<input type="file" id="file-load" name="files[]" multiple class="form-control" style="display:none" />
<input type="file" id="file-load" name="file" class="form-control" style="display:none" />
</form>

<button type="button" id='loadjson_button' class="btn btn-default">Load JSON</button>
Expand Down Expand Up @@ -55,7 +55,7 @@ <h2>A playset-editor for Fiasco, with <s>a</s> Mustache ...</h2>
{
// The mustache template
var mustacheTemplate = $('#mustache_template').val();
var jsonData = get_generated_json();
var jsonData = get_json_fromUI(true, true);

var blob = new Blob([Mustache.render(mustacheTemplate, jsonData)], {type: "text/plain;charset=utf-8"});
saveAs(blob, "generated_template_" + $('#input_title').val());
Expand Down Expand Up @@ -89,7 +89,7 @@ <h2>A playset-editor for Fiasco, with <s>a</s> Mustache ...</h2>
$('#generate_button').click(function() { generate_mustache(); });
$('#to-pdf-button').click(function() { generate_pdf(); });
$("#loadjson_button").click(function () { $("#file-load").click(); });
$("#file-load").change(load_files);
$("#file-load").change(load_json_file);
$('#savejson_button').click(function() { save_json(); });

instasetup_init();
Expand Down
16 changes: 2 additions & 14 deletions generator/html_template.mst
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,8 @@
</div>
<div class='row'>
<div class="form-group col-md-10 col-md-offset-1">
<label for="input_title">Presentation</label>
<textarea class="form-control" id="input_presentation" rows='3' placeholder='Playset presentation'></textarea>
</div>
</div>
<div class='row'>
<div class="form-group col-md-10 col-md-offset-1">
<label for="input_title">Inspirations</label>
<textarea class="form-control" id="input_inspirations" rows='3' placeholder='Playset inspirations'></textarea>
</div>
</div>
<div class='row'>
<div class="form-group col-md-10 col-md-offset-1">
<label for="input_title">Advices</label>
<textarea class="form-control" id="input_advices" rows='3' placeholder='Playset advices'></textarea>
<label for="input_title">Description</label>
<textarea class="form-control" id="input_description" rows='20' placeholder='Playset description'></textarea>
</div>
</div>
<div class='row'>
Expand Down
116 changes: 109 additions & 7 deletions generator/scripts/json.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
/*****************************************
JSON related functions
/*****************************************/
/****************************
** JSON related functions **
****************************/

function get_generated_json()
/**
* Get the JSON data from what has been set in the UI
* @param {boolean} withDescriptionParts - Is the description splitted in parts ?
* @param {boolean} withDescriptionParagraphs - Is the description splitted in paragraphs ?
* @return {json} Playset in JSON format
*/
function get_json_fromUI(withDescriptionParts, withDescriptionParagraphs)
{
var jsonData = { };

// Generic elements
jsonData.title = $('#input_title').val();
jsonData.subtitle = $('#input_subtitle').val();
jsonData.teaser = $('#input_teaser').val();
jsonData.presentation = $('#input_presentation').val();
jsonData.inspirations = $('#input_inspirations').val();
jsonData.advices = $('#input_advices').val();

// Description is one field
json_set_description(jsonData, withDescriptionParts, withDescriptionParagraphs);

jsonData.credits = $('#input_credits').val();

// Tables of elements
Expand Down Expand Up @@ -73,3 +80,98 @@ function get_instasetup_json()
}
return jsonIS;
}

/**
* Fill the "Description" part of the Json playset, from the UI
* @param {json} jsonData - The JSON data for the Playset, which will be completed
* @param {boolean} withParts - Is the description splitted in parts ? (Presentation, Movie Night, Advices, ...)
* @param {boolean} withParagraphs - Is the text splitted in paragraphs ? (Each line is a json data object)
*/
function json_set_description(jsonData, withParts, withParagraphs)
{
jsonData.description = $('#input_description').val();

if (withParts)
{
json_set_description_parts(jsonData);
}

if (withParagraphs)
{
json_set_description_paragraphs(jsonData);
}
}

/**
* Add a "description_parts" object to JSON
* The Description part is split in objects {keys, value} (title, content)
* @param {json} jsonData The json playset object
*/
function json_set_description_parts(jsonData)
{
jsonData["description_parts"] = { };
var arrayOfLines = jsonData.description.match(/[^\r\n]+/g);
// Lines starting with '#' represent the "Parts"
// The subsequent lines are the content of these Parts, until anoter "Part" is found
var currentPart = ' ';
var currentContent = '';
var existingParts = [ ];
for (var iLine = 0; iLine < arrayOfLines.length; iLine++)
{
var sLine = arrayOfLines[iLine];
// Is this line the start of a new part ?
if (sLine.substring(0, 1) === '#')
{
json_add_description_part(jsonData.description_parts, currentPart, currentContent);

currentContent = '';
currentPart = sLine.substring(1).trim();
}
else // Or is this a new line of content ?
{
if (currentContent.length > 0)
{
currentContent += '\n';
}
currentContent += sLine;
}
}
json_add_description_part(jsonData.description_parts, currentPart, currentContent);
}

/**
* Add a part of the description
* @param {json} jsonDescriptionParts json object which will contain the parts
* @param {string} part Name of the part (Presentation, Movie Night, ...)
* @param {string} content Content to be put in the part
*/
function json_add_description_part(jsonDescriptionParts, part, content)
{
if ((part.length > 0) && (content.length > 0))
{
while (jsonDescriptionParts.hasOwnProperty(part))
{
part += '-';
}
jsonDescriptionParts[part] = content;
}
}

function json_set_description_paragraphs(jsonData)
{
jsonData["description_paragraphs"] = [ ];
var arrayOfLines = jsonData.description.match(/[^\r\n]+/g);
for (var iLine = 0; iLine < arrayOfLines.length; iLine++)
{
var sLine = arrayOfLines[iLine];
// Is this line a Title (starts with #)
if (sLine.substring(0, 1) === '#')
{
jsonData.description_paragraphs.push({ "content": sLine.substring(1).trim(), "type": "title" });
}
else // Or is this a new line of content ?
{
jsonData.description_paragraphs.push({ "content": sLine, "type": "paragraph" });
}
}
}
85 changes: 68 additions & 17 deletions generator/scripts/loadsave.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
/**
* Save the Json data in a text file
*/
function save_json()
{
var blob = new Blob([JSON.stringify(get_generated_json())], {type: "text/plain;charset=utf-8"});
var blob = new Blob([JSON.stringify(get_json_fromUI(false, false))], {type: "text/plain;charset=utf-8"});
saveAs(blob, "fiasco_template_" + $('#input_title').val() + ".json");
}

function load_files(evt) {
/**
* Load a JSON file inside the editor
* @param {[type]} evt - Event for the files that are loaded
*/
function load_json_file(evt) {

var files = evt.target.files;
if (evt.target.files.length == 1)
{
var file = evt.target.files[0];
var reader = new FileReader();

for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
reader.onload = function (reader) {
var data = JSON.parse(this.result);
load_json_intoUI(data);
};

reader.onload = function (reader) {
var data = JSON.parse(this.result);
load_from_json(data);
};
reader.readAsText(file);

reader.readAsText(f);
// Reset file input
$("#file-load-form")[0].reset();
}

// Reset file input
$("#file-load-form")[0].reset();
}

function load_from_json(jsonData)
/**
* Load a JSON object into the UI
* @param {json} jsonData - Data to be loaded
*/
function load_json_intoUI(jsonData)
{
// Introduction elements
$('#input_title').val(jsonData.title);
$('#input_subtitle').val(jsonData.subtitle);
$('#input_teaser').val(jsonData.teaser);
$('#input_presentation').val(jsonData.presentation);
$('#input_inspirations').val(jsonData.inspirations);
$('#input_advices').val(jsonData.advices);
$('#input_description').val(jsonData.description);
load_old_description_version(jsonData);
$('#input_credits').val(jsonData.credits);

// Elements from a section
Expand All @@ -53,6 +63,10 @@ function load_from_json(jsonData)
instasetup_update_options_text();
}

/**
* Load the Insta-Setup part of a JSON object into the UI
* @param {[type]} jsonIS [description]
*/
function load_instasetup_fromjson(jsonIS)
{
if (jsonIS instanceof Array)
Expand All @@ -73,3 +87,40 @@ function load_instasetup_fromjson(jsonIS)
}
}
}

/**
* Load old & obsolete JSON values into UI (migrated to the new format)
* @param {[type]} jsonData [description]
*/
function load_old_description_version(jsonData)
{
var migratedDescription = '';
var blockSep = '';

if (jsonData.hasOwnProperty("description") && (jsonData.description.length > 0))
{
migratedDescription += blockSep + jsonData.description;
blockSep = '\n';
}

if (jsonData.hasOwnProperty("presentation") && (jsonData['presentation'].length > 0))
{
migratedDescription += blockSep + "# Presentation\n" + jsonData['presentation'];
blockSep = '\n';
}
if (jsonData.hasOwnProperty("inspirations") && (jsonData['inspirations'].length > 0))
{
migratedDescription += blockSep + "# Inspirations\n" + jsonData['inspirations'];
blockSep = '\n';
}
if (jsonData.hasOwnProperty("advices") && (jsonData['advices'].length > 0))
{
migratedDescription += blockSep + "# Advices\n" + jsonData['advices'];
blockSep = '\n';
}

if (migratedDescription.length > 0)
{
$('#input_description').val(migratedDescription);
}
}
36 changes: 30 additions & 6 deletions generator/scripts/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*/
function generate_pdf()
{
var jsonPlayset = get_generated_json();
var jsonPlayset = get_json_fromUI(false, true);

var docDefinition = {
content: [ ],
styles: get_pdf_style()
};

pdf_add_introduction(docDefinition.content, jsonPlayset);
pdf_add_description(docDefinition.content, jsonPlayset);

// Generation of the Sections (Relationships, Needs, Locations, Objects)
for(var iSection = 0; iSection < jsonPlayset.sections.length; iSection++)
Expand All @@ -23,14 +23,37 @@ function generate_pdf()
pdfMake.createPdf(docDefinition).download(customFilename);
}

function pdf_add_introduction(content, jsonPlayset)
/**
* [pdf_add_introduction description]
* @param {[type]} content [description]
* @param {[type]} jsonPlayset [description]
*/
function pdf_add_description(content, jsonPlayset)
{
// Title
// Title page with credits
content.push({ text: jsonPlayset.title, style: 'title', pageOrientation: 'portrait'});
content.push({ text: 'Credits', style: 'subTitle'});
content.push({ text: jsonPlayset.credits, style: 'description'});
content.push({ text: 'Boilerplate', style: 'subTitle'});
content.push({ text: jsonPlayset.credits, style: 'description'});
content.push({ text: jsonPlayset.credits, style: 'description', pageBreak: 'after'});

// Description page
content.push({ text: jsonPlayset.subtitle, style: 'title', pageOrientation: 'portrait'});
for(var iBlock = 0; iBlock < jsonPlayset.description_paragraphs.length; iBlock++)
{
var blockStyle = 'description';
switch(jsonPlayset.description_paragraphs[iBlock].type)
{
case 'title':
blockStyle = 'subTitle';
break;
case 'paragraph':
blockStyle = 'description';
break;
}

content.push({ text: jsonPlayset.description_paragraphs[iBlock].content, style: blockStyle });
}
}

/**
Expand Down Expand Up @@ -113,7 +136,8 @@ function get_pdf_style()
},
description: {
fontSize: 16,
marginBottom: 6
marginBottom: 6,
alignment: 'justify'
}
};
return styles;
Expand Down

0 comments on commit 96a4955

Please sign in to comment.