Skip to content

Instantly share code, notes, and snippets.

@jdavidbakr
Last active January 14, 2016 17:49
Show Gist options
  • Save jdavidbakr/a589c0476f4568d5de46 to your computer and use it in GitHub Desktop.
Save jdavidbakr/a589c0476f4568d5de46 to your computer and use it in GitHub Desktop.
JavaScript: JSON Processor
/*
* JsonProcessor.run() will do all the fun stuff we need to do for a json response.
* Return true if we are not redirecting to a new page (i.e. so that the form will unhide)
*/
var JsonProcessor = {
lastErrorFields: [],
run: function(data) {
var staying_on_page = true;
var i;
try {
var json = JSON.parse(data);
// Check for a message
if (json.message) {
alert(json.message);
}
// Check for a redirect
if (json.redirect) {
document.location.href = json.redirect;
staying_on_page = false;
}
// Check for a redirect_blank
if (json.redirect_blank) {
window.open(json.redirect_blank, '_blank');
}
// Check for fields to set
if (json.fields) {
for (i = 0; i < json.fields.length; i++) {
var field = document.querySelector(json.fields[i].id);
if (field) {
field.value = json.fields[i].value;
}
}
}
// Check for a regular dom fill
if(json.html && json.target) {
var html_target = document.querySelector(json.target);
if(html_target) {
html_target.innerHTML = json.html;
this.execute_scripts(html_target);
}
}
// Check for error fields
for(i = 0; i < this.lastErrorFields.length; ++i) {
removeClass(this.lastErrorFields[i],'error');
}
this.lastErrorFields = [];
if(json.error_fields) {
for(i = 0; i < json.error_fields.length; ++i) {
// Look for an elemnt with this ID
var element = document.querySelector('#'+json.error_fields[i]);
if(element) {
addClass(element,'error');
}
var error_fields = document.getElementsByName(json.error_fields[i]);
if(error_fields.length) {
for(j = 0; j < error_fields.length; j++) {
addClass(error_fields[j], 'error');
this.lastErrorFields.push(error_fields[j]);
// Look for a label neighbor
var label = error_fields[j].previousElementSibling;
if(label) {
addClass(label, 'error');
this.lastErrorFields.push(label);
} else {
var nextLabel = error_fields[j].nextElementSibling;
if(nextLabel) {
addClass(nextLabel,'error');
this.lastErrorFields.push(nextLabel);
}
}
}
}
}
}
if (json.elements) {
for (i = 0; i < json.elements.length; i++) {
var element = json.elements[i];
if (element.target) {
target = document.querySelector(element.target);
if (!target) {
continue;
}
if (element.html) {
if (element.add_to_target) {
// Remove any scripts in our target
var arr = target.getElementsByTagName('script');
for (var n = 0; n < arr.length; n++)
arr[n].innerHTML = '';
var e = document.createElement('div');
e.innerHTML = element.html;
while(e.firstChild) {
target.appendChild(e.firstChild);
}
this.execute_scripts(target);
} else {
target.innerHTML = element.html;
this.execute_scripts(target);
}
}
if(element.url) {
miroAjax(element.url, function(html) {
target.innerHTML = html;
this.execute_scripts(target);
});
}
}
if (element.attributes) {
for (var ea = 0; ea < element.attributes.length; ea++) {
var attribute = element.attributes[ea];
var target = document.querySelector(attribute.target);
if(target) {
if (attribute.addClass) {
target.classList.add(attribute.addClass);
}
if (attribute.removeClass) {
target.classList.remove(attribute.removeClass);
}
if (attribute.setAttribute) {
target.setAttribute(attribute.setAttribute.attribute, attribute.setAttribute.value);
}
}
}
}
}
}
if(json.execute) {
eval(json.execute);
}
return staying_on_page;
} catch(err) {
alert('An error has occurred.');
return true;
}
},
execute_scripts: function(node) {
var arr = node.getElementsByTagName('script');
for (var n = 0; n < arr.length; n++)
eval(arr[n].innerHTML);//run script inside div
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment