Skip to content

Commit

Permalink
Made changeTab function less horrible.
Browse files Browse the repository at this point in the history
About 150 lines less horrible.
  • Loading branch information
guntrip committed Oct 8, 2015
1 parent 1cc7ee0 commit a665f64
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 217 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

<div class="tabnav">
<nav class="tabnav-tabs">
<a href="#" class="tabnav-tab" id="tab-csv" onclick="changeTab('csv');"><span class="octicon octicon-file-text"></span> CSV</a>
<a href="#" class="tabnav-tab selected" id="tab-csv" onclick="changeTab('csv');"><span class="octicon octicon-file-text"></span> CSV</a>
<a href="#" class="tabnav-tab" id="tab-md" onclick="changeTab('md');"><span class="octicon octicon-code"></span> Markdown</a>
<a href="#" class="tabnav-tab selected" id="tab-html" onclick="changeTab('html');"><span class="octicon octicon-file-code"></span> HTML</a>
<a href="#" class="tabnav-tab" id="tab-html" onclick="changeTab('html');"><span class="octicon octicon-file-code"></span> HTML</a>
<a href="#" class="tabnav-tab" id="tab-form" onclick="changeTab('form');"><span class="octicon octicon-pencil"></span> Form</a>
<a href="#" class="tabnav-tab" id="tab-preview" onclick="changeTab('preview');"><span class="octicon octicon-browser"></span> Preview</a>
</nav>
Expand Down
256 changes: 41 additions & 215 deletions magic.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var tab = "html", header_alignment=[], array_storage="", form_rows=0, form_cols=0, prettify_md=true, debug=true;
var tab = "csv", header_alignment=[], array_storage="", form_rows=0, form_cols=0, prettify_md=true, debug=false;

var example_csv='Feature, Description, Example\n'+
'Renders markdown, Uses showdown library to render contents of table cells, **Just** *like* ``this``\n'+
'Escapes quotes, Easier to edit without so only uses them when necessary, "It does an \\\"okay\\\" job"\n'+
'Preview table matches GitHub style, As closely as possible, Look!\n'+
'Preserves alignment, Between all views, Switch to CSV and back\n'+
'Import HTML, Converts back and forth, Copy from *Inspect Element*\n'+
'Markdown formatting, "Adds spaces, makes things more legible",Markdown tab\n'+
'Form view, "Create tables with buttons!",Form tab\n'+
'Hasn\\\'t caught fire yet, So far, Huzzah!';
Expand Down Expand Up @@ -66,228 +67,53 @@ function changeTab(newTab) {

if (tab!==newTab) {

var input = $("textarea").val();
// Convert to array
var input = $("textarea").val(), array = [];

// What direction?
if (tab==="md") { array = md2array(input); }
if (tab==="csv") { array = csv2array(input); }
if (tab==="html") { array = html2array(input); }
if (tab==="form") { array = array = form2array(); }
if (tab==="preview") { array = array_storge; }

// md > csv
if ( (tab==="md") && (newTab==="csv") ) {
// Convert from array into new format, set other vars..
var new_layout=true, output="";

layout(true);

// Convert md to array
var array = md2array(input);

// Array to csv
var csv = array2csv(array);

// Pop into textarea
$('textarea').val(csv);

}

// csv > md
if ( (tab==="csv") && (newTab==="md") ) {

layout(true);

// Convert csv to array
var array = csv2array(input);

// Array to md
var md = array2md(array);

// Pop into textarea
$('textarea').val(md);

}

// csv > form
if ( (tab==="csv") && (newTab==="form") ) {

layout(false);

// Convert md to array
var array = csv2array(input);

// And to html
var html = array2form(array);

array_storge = array; // Store the array

$('.preview').html(html);

}

// md > form
if ( (tab==="md") && (newTab==="form") ) {

layout(false);

// Convert md to array
var array = md2array(input);

// And to html
var html = array2form(array);

$('.preview').html(html);

}

// form > md
if ( (tab==="form") && (newTab==="md") ) {

// Convert form into array
var array = form2array();

// Convet array into md
var md = array2md(array);

// Update design
layout(true);

// Pop into text area
$('textarea').val(md);

}

// form > csv
if ( (tab==="form") && (newTab==="csv") ) {

// Convert form into array
var array = form2array();

// Convet array into md
var csv = array2csv(array);

// Update design
layout(true);

// Pop into text area
$('textarea').val(csv);

}

// csv > preview
if ( (tab==="csv") && (newTab==="preview") ) {

layout(false);

// Convert md to array
var array = csv2array(input);

// And to html
var html = array2preview(array);

array_storge = array; // Store the array

$('.preview').html(html);

}

// md > preview
if ( (tab==="md") && (newTab==="preview") ) {

layout(false);

// Convert md to array
var array = md2array(input);

// And to html
var html = array2html(array);

array_storge = array; // Store the array

$('.preview').html(html);

}

// form > preview
if ( (tab==="form") && (newTab==="preview") ) {

// Convert form to array
var array = form2array();

// And to html
var html = array2preview(array);

// Update design late
layout(false);

array_storge = array; // Store the array

$('.preview').html(html);

}

// preview > csv
if ( (tab==="preview") && (newTab==="csv") ) {

layout(true);

// Convert stored array into csv
var csv = array2csv(array_storge);

// Pop into text area
$('textarea').val(csv);

}

// preview > md
if ( (tab==="preview") && (newTab==="md") ) {

layout(true);

// Convert stored array into md
var md = array2md(array_storge);

// Pop into text area
$('textarea').val(md);
}

// preview > form
if ( (tab==="preview") && (newTab==="form") ) {

layout(false);

// Convert array storage to form
var html = array2form(array_storge);

$('.preview').html(html);

}

// html > form
if ( (tab==="html") && (newTab==="form") ) {

layout(false);

// Convert md to array
var array = html2array(input);

// And to form
var html = array2form(array);

$('.preview').html(html);

}
if (newTab==="md") {
output = array2md(array);
new_layout=true;
}

// form > html
if ( (tab==="form") && (newTab==="html") ) {
if (newTab==="csv") {
output = array2csv(array);
new_layout=true;
}

// Convert form into array
var array = form2array();
if (newTab==="html") {
output = array2html(array);
new_layout=true;
}

// Convet array into html
var html = array2html(array);
if (newTab==="form") {
output = array2form(array);
new_layout=false;
}

// Update design
layout(true);
if (newTab==="preview") {
output = array2preview(array);
array_storge = array; // store array
new_layout=false;
}

// Pop into text area
$('textarea').val(html);
// Update DOM
layout(new_layout);

}
// Output
if (new_layout) {
$('textarea').val(output);
} else {
$('.preview').html(output);
}

// Update classes
$('.tabnav-tab').removeClass('selected');
Expand Down Expand Up @@ -521,7 +347,7 @@ function html2array(html) {
var html_row = tr[t];
row = html2array_cells(html_row, "td");

if (row.length>0) { console.log(row);
if (row.length>0) {

// If col_count is not set by thead, set it.
if (array.length===0) { col_count=row.length; }
Expand Down

0 comments on commit a665f64

Please sign in to comment.