var ManageDeveloper = function(core, target, data, returnCB, clientValue){
var focusDefined = isDefined(clientValue),
events = new EventManager,
template = Template(focusDefined),
element = null,
elements = null,
data = null;
this.exit = exit;
if(focusDefined) new ApiManager({'users' : null, 'profiles' : null }, clientValue, init);
else init(null, {profiles: {}, users: {}});
function Data(d){
this.notes = getValue(d.users, 'notes');
this.email = getValue(d.profiles, 'email');
this.pass = null;
this.company = getValue(d.profiles, 'company');
this.createdAt = getValue(d.users, 'createdAt');
this.id = getValue(d.users, 'id');
this.type = getValue(d.users, 'type');
function getValue(d, k, r){ if(r === undefined) r = null; if(!!d && !!d[k]) return d[k]; else return r; }
}
function User(d){
this.notes = d.notes;
this.createdAt = d.createdAt;
this.id = d.id;
this.type = d.type;
}
function Profile(d){
this.company = d.company;
this.email = d.email;
this.phone = d.phone;
}
function Template(editState){
var actionName = editState === true ? 'Edit' : 'Create',
updateName = editState === true ? 'Save' : 'Create'
var header = '' + actionName + ' Manager
',
footer = '';
var name = 'Name:
',
notes = 'Notes:
',
setUsername = 'Set username:
',
setPassword = '{{^ id }}Set password:
{{/ id }}',
main = '' + name + notes + setUsername + setPassword + '
';
return '
' + header + main + footer + '
';
}
function init(error, d){
if(!!error){
core.notifications.setError("I'm sorry, but there was an error loading this page!")
return
}
data = new Data(d);
element = getElementFromString(render(template, data));
elements = new Elements(element, {
'name' : { 'selector' : '.name' },
'notes' : { 'selector' : '.notes' },
'email' : { 'selector' : '.username' },
'password' : { 'selector' : '.password' },
'save' : { 'selector' : '.saveButton', 'ignoreReset' : true },
'missing' : { 'selector' : null, 'ignoreReset' : true },
});
events.add(elements.name, 'change', updateName);
events.add(elements.notes, 'change', updateNotes);
events.add(elements.email, 'change', updateEmail);
if(!!elements.password) events.add(elements.password, 'change', updatePassword);
events.add(elements.save, 'click', saveAction);
returnCB();
target.appendChild(element);
}
function getInvalidItems(o){
var a = [];
if(!isDefined(o.email)) a.push('Missing email');
if(!focusDefined){
if(!isDefined(o.pass)) a.push('Missing password');
else if(!isValidPassword(o.pass)) a.push('Invalid password length, please ensure your password is at least 8 characters');
}
return a.length > 0 ? a : null;
}
function isValidPassword(s){ var l = s.length; return typeof s == 'string' && (l >= 8 || l == 0); }
function saveAction(e){
if(e && e.preventDefault) e.preventDefault();
var invalid = getInvalidItems(data);
if(invalid === null) !focusDefined ? save() : edit();
else setMissing(invalid);
function save(){
data.passConfirm = data.pass;
apiRequest('POST', 'signUp/developer', null, data, redirect);
}
function edit(e){
var count = 0;
apiRequest('PUT', 'users', clientValue, new User(data), editCB);
apiRequest('PUT', 'profiles', clientValue, new Profile(data), editCB);
function editCB(){
if(!!e.error) {
return core.notifications.setError(e.error);
}
if(++count == 2) redirect(); }
}
function redirect(){ pop('Pop!', 'Dashboard - Clients', '/dashboard/developers'); }
}
function setMissing(a){ core.notifications.setMissingFields(a); }
function updateName(e){ updateData('company', e.target.value); }
function updateNotes(e){ updateData('notes', e.target.value); }
function updateEmail(e){ updateData('email', e.target.value); }
function updatePassword(e){ updateData('pass', e.target.value); }
function updateData(k, v){ data[k] = v; }
function exit(){
if(element) removeChild(target, element);
if(events) events.reset();
if(elements) elements.exit();
housekeeping();
}
function housekeeping(){ core = target = returnCB = clientValue = events = template = data = element = elements = null; }
};