var ManageManager = function(core, target, data, returnCB, clientValue){ var focusDefined = isDefined(clientValue), events = new EventManager, template = Template(focusDefined), element = getElementFromString(render(template, { 'sidePanel' : null })), elements = null, dataObj = null; this.exit = exit; if(focusDefined) new ApiManager({'users' : null, 'profiles' : null }, clientValue, init); else init(null, {profiles: {}, users: {}}); function DataObj(d){ this.notes = getDataObjValue(d.users, 'notes'); this.email = getDataObjValue(d.profiles, 'email'); this.pass = null; this.company = getDataObjValue(d.profiles, 'company'); this.createdAt = getDataObjValue(d.users, 'createdAt'); this.id = getDataObjValue(d.users, 'id'); this.type = getDataObjValue(d.users, 'type'); } function User(d){ this.notes = d.notes; this.createdAt = d.createdAt; this.id = d.id; this.type = d.type; } function Profile(d){ this.address = d.address; this.billingEmail = d.billingEmail; 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){ returnCB(); if(!error){ initDefault(d); insertElement(); } returnCB = null; } function initDefault(d){ dataObj = new DataObj(d); sidePanelObj = {}; //new SidePanelObj(d); element = getElementFromString(render(template, dataObj)) 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(isDefined(elements.password)) events.add(elements.password, 'change', updatePassword); events.add(elements.save, 'click', saveAction); } function insertElement(){ target.appendChild(element); } function getDataObjValue(d, k, r){ if(r === undefined) r = null; if(isDefined(d) && isDefined(d[k])) return d[k]; else return r; } 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(dataObj); if(invalid === null) !focusDefined ? save() : edit(); else setMissing(invalid); function save(){ dataObj.passConfirm = dataObj.pass; apiRequest('POST', 'signUp/manager', null, dataObj, redirect); } function edit(){ var count = 0; apiRequest('PUT', 'users', clientValue, new User(dataObj), editCB); apiRequest('PUT', 'profiles', clientValue, new Profile(dataObj), editCB); function editCB(e){ if(++count == 2) redirect(e); } } function redirect(e){ if(!!e && !!e.error && !!e.message) { return core.notifications.setError(e.message); } pop('Pop!', 'Dashboard - Clients', '/dashboard/managers'); } } 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){ dataObj[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 = dataObj = element = elements = null; } };