/* global define */
define(['underscore', 'jquery', 'views/metadata/BaseEMLPartyView'],
function(_, $, BaseEMLPartyView) {
/*
The EMLParty renders the content of an EMLParty model
*/
var EMLPartyView = BaseEMLPartyView.extend({
/**
* Override parent validation handling for email address when the person is a contact
*
* Validates and displays error messages for the persons' name, email
*
* @function showValidation
*/
showValidation: function() {
if (this.model.isValid()) {
this.$(".notification").empty();
this.$(".error").removeClass("error");
}
else{
// Clear error marks from fields
this.$("[data-attribute='givenName']").removeClass("error");
this.$("[data-attribute='surName']").removeClass("error");
this.$("[data-attribute='email']").removeClass("error");
var hasFirstName = this.model.get("individualName") &&
this.model.get("individualName").givenName;
var hasLastName = this.model.get("individualName") &&
this.model.get("individualName").surName;
var hasEmail = this.model.get("email") &&
this.model.get("email").length != 0 &&
this.model.get("email") != "";
var isContact = this.model.get("type") == "contact";
if(hasFirstName) {
var firstName = this.model.get("individualName").givenName;
if(Array.isArray(firstName) && firstName.length == 0) {
hasFirstName = false;
}
}
if(hasEmail) {
var email = this.model.get("email");
if(Array.isArray(email) && email.length == 0) {
hasEmail = false;
}
}
// Check if user has supplied required fields.
if(hasFirstName && hasLastName) {
if(!isContact){
return;
} else if(isContact && hasEmail){
return;
}
}
if(!hasFirstName) {
this.$("[data-attribute='givenName']").addClass("error");
}
if(!hasLastName) {
this.$("[data-attribute='surName']").addClass("error");
}
// If type is contact, check if there isn't an email address
if(isContact && !hasEmail) {
this.$("[data-attribute='email']").addClass("error");
}
this.$(".notification").text(this.model.validationError.errorMessage).addClass("error");
}
}
});
return EMLPartyView;
}
);