var AdminBlockTypeParserController = function(params) { this.init(params); };
AdminBlockTypeParserController.prototype = {
container: 'block_type_parser',
authenticity_token: false,
//original_html: "
\n
\n
\n
\n
Beautiful content. Responsive.
\n
Lorem Ipsum is simply dummy text.
\n
\n
\n
\n
\n

\n
\n
\n
\n",
//original_html: "\n",
//original_html: "" +
// "\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
Beautiful content. Responsive.
\n" +
// "
Lorem Ipsum is simply dummy text.
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "

\n" +
// "
\n" +
// "
\n" +
// "
\n",
original_html: "",
parsable_tags: { heading: 'Headings' , richtext: 'Rich Text' , img: 'Images' , link: 'Links' },
tags_to_parse: { heading: true , richtext: true , img: true , link: true },
render_function: false,
children: false,
init: function(params) {
var that = this;
for (var i in params)
this[i] = params[i];
that.print_step1();
},
print_step1: function()
{
var that = this;
var tr = $('
');
var all_checked = true;
$.each(that.parsable_tags, function(tag, name) { if (!that.tags_to_parse[tag]) { all_checked = false; return false; }});
tr.append($(' | ').append($('').attr('type', 'checkbox').attr('id', 'all').prop('checked', all_checked).click(function() {
var checked = $(this).is(':checked');
$.each(that.parsable_tags, function(tag, name) { $('#'+tag).prop('checked', checked); });
})))
.append($(' | ').append($('').attr('for', 'all').append('All')));
$.each(that.parsable_tags, function(tag, name) {
tr.append($(' | ').append($('').attr('type', 'checkbox').attr('id', tag).val('1').prop('checked', that.tags_to_parse[tag])))
.append($(' | ').append($('').attr('for', tag).append(name)));
});
var tags_table = $('').append($('').append(tr));
var div = $('')
.append(tags_table)
.append($('').append($('').attr('id', 'html').css('width', '90%').css('height', '400px').attr('placeholder', 'HTML to Parse').html(that.original_html)))
.append($('').attr('id', 'message'))
.append($('').append($('').attr('type', 'button').val("Parse!").click(function() {
that.original_html = $('#html').val();
$.each(that.parsable_tags, function(tag, name) { that.tags_to_parse[tag] = $('#'+tag).is(':checked'); });
that.parse_tags();
})));
$('#'+that.container).empty().append(div);
},
parse_tags: function(show_json)
{
var that = this;
var tags = [];
$.each(that.tags_to_parse, function(tag, checked) { if (checked) tags.push(tag); });
$.ajax({
url: '/admin/block-types/parse-tags',
type: 'post',
data: {
html: that.original_html,
tags: tags,
children: that.children
},
success: function(resp) {
that.last_response = resp;
that.original_html = resp.original_html;
that.render_function = resp.render_function;
that.children = resp.children;
},
async: false
});
if (show_json)
{
$('#'+that.container).empty()
.append($('').attr('id', 'json').css('width', '90%').css('height', '400px').html(JSON.stringify(that.last_response)))
.append($('')
.append($('').attr('type', 'button').val("< Back to HTML").click(function() { that.print_step1(); })).append(' ')
.append($('').attr('type', 'button').val("Show Parsed").click(function() { that.parse_tags(); }))
);
return;
}
var tbody = $('')
.append($('
')
.append($(' | ').append('Field Type'))
.append($(' | ').append('Name'))
.append($(' | ').append('Description'))
.append($(' | ').append('Default Value'))
.append($(' | ').attr('colspan', '2').append('Child Values'))
);
var field_types = {
heading: 'Heading',
image2: 'Image',
button: 'Link',
richtext: 'Rich Text'
};
$.each(that.children, function(i, v) {
var tr = $('
')
.append(field_types[v.field_type])
.append($(' | ').attr('valign', 'top').append($('').data('i', i).val(v.name ).on('keyup', function(e) { var x = $(this).val().toLowerCase().replace(' ', '_'); $(this).val(x); that.children[parseInt($(this).data('i'))].name = x; })))
.append($(' | ').attr('valign', 'top').append($('').data('i', i).val(v.description ).on('keyup', function(e) { that.children[parseInt($(this).data('i'))].description = $(this).val(); })))
.append($(' | ').attr('valign', 'top').append($('').data('i', i).val(v.default ).on('keyup', function(e) { that.children[parseInt($(this).data('i'))].default = $(this).val(); })));
if (v.child_values)
{
j = 0;
$.each(v.child_values, function(k,v) {
if (j > 0)
tr.append($(' | ').attr('colspan', '4').html(" "));
tr.append($(' | ').attr('align', 'right').append(k))
.append($(' | ').append($('').data('i', i).data('k', k).val(v).on('keyup', function(e) { that.children[parseInt($(this).data('i'))].child_values[$(this).data('k')] = $(this).val(); })));
tbody.append(tr);
tr = $('
');
j++;
});
}
else
tbody.append(tr);
});
var vars_table = $('').append(tbody);
$('#'+that.container).empty()
.append(vars_table)
.append($('').attr('id', 'parsed').css('width', '90%').css('height', '400px').html(that.render_function))
.append($('')
.append($('').attr('type', 'button').val("< Back to HTML").click(function() { that.print_step1(); })).append(' ')
.append($('').attr('type', 'button').val("Re-parse with Updated Variables").click(function() { that.parse_tags(); })).append(' ')
.append($('').attr('type', 'button').val("Show JSON").click(function() { that.parse_tags(true); }))
);
},
};