Skip to content

Instantly share code, notes, and snippets.

@sbellware
Created September 1, 2009 05:34
Show Gist options
  • Save sbellware/178918 to your computer and use it in GitHub Desktop.
Save sbellware/178918 to your computer and use it in GitHub Desktop.
function viewMessages(data) {
this.flashSelector = "div[id='flash']";
if (data) {
this.notice = data.notice;
this.error = data.error;
this.fieldErrors = data.fieldErrors;
if (data.flashSelector) {
this.flashSelector = data.flashSelector;
}
}
this.show = function() {
this.showFlash();
this.showFieldErrors();
}
this.showFlash = function() {
this.removeFlashes();
this.flashNotice();
this.flashError();
};
this.flashNotice = function() {
if (this.notice) {
$(this.flashSelector).append('<div class="notice">' + this.notice + '</div>');
}
};
this.flashError = function() {
if (this.error) {
$(this.flashSelector).append('<div class="error">' + this.error + '</div>');
}
};
this.removeFlashes = function() {
this.removeNoticeFlash();
this.removeErrorFlash();
};
this.removeNoticeFlash = function() {
$(this.flashSelector + " > .notice").remove();
};
this.removeErrorFlash = function() {
$(this.flashSelector + " > .error").remove();
};
this.showFieldErrors = function() {
this.removeErrors();
if (!this.fieldErrors) {
return;
}
for (var fieldId in this.fieldErrors) {
label = this.labelFor(fieldId);
field = this.field(fieldId);
this.wrapLabelWithLabelForFieldWithErrorsSpan(label)
this.wrapFieldWithFieldWithErrorsSpan(field)
errorMessage = this.fieldErrors[fieldId]
errorMessageSpan = this.createFieldWithErrorsMessageSpan(fieldId, errorMessage)
this.appendFieldWithErrorsMessageSpanToField(field, errorMessageSpan)
}
};
this.wrapLabelWithLabelForFieldWithErrorsSpan = function(label) {
label.wrap("<span class=\"label_for_field_with_errors\"></span>");
};
this.wrapFieldWithFieldWithErrorsSpan = function(field) {
field.wrap("<span class=\"field_with_errors\"></span>");
};
this.createFieldWithErrorsMessageSpan = function(fieldId, errorMessage) {
return "<span class=\"field_with_errors_message\" id=\"" + fieldId + "_error_message\">" + errorMessage + "</span>";
};
this.appendFieldWithErrorsMessageSpanToField = function(field, errorMessageSpan) {
field.after(" " + errorMessageSpan);
};
this.labelFor = function(fieldId) {
selector = "label[for='" + fieldId + "']";
return $(selector);
};
this.field = function(fieldId) {
selector = "input[id='" + fieldId + "']";
return $(selector);
};
this.removeErrors = function() {
this.removeLabelForFieldWithErrorsSpans();
this.removeFieldWithErrorsSpans();
this.removeErrorMessages();
}
this.removeLabelForFieldWithErrorsSpans = function() {
spans = $('span[class="label_for_field_with_errors"]');
var i=0;
for (i=0;i<=spans.length;i++) {
span = spans.eq(i);
span.before(span.children());
span.remove();
}
}
this.removeFieldWithErrorsSpans = function() {
spans = $('span[class="field_with_errors"]');
var i=0;
for (i=0;i<=spans.length;i++) {
span = spans.eq(i);
span.before(span.children());
span.remove();
}
}
this.removeErrorMessages = function() {
$('span[class="field_with_errors_message"]').remove();
}
this.success = function() {
return (this.error == null && this.fieldErrors == null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment