var EditProfile = function(core, target, data, returnCB){
var events = new EventManager(),
template = new Template(),
dataObj = null,
userIndexObj = null,
sidePanelObj = null,
renderObj = null,
element = null,
elements = null,
geo = new MeteoraMap.Geocoder(),
allowEmptyEmail = false;
new ApiManager({ 'profiles' : null }, core.userID, init);
this.exit = exit;
function RenderObj(d){
var p = d.profiles,
a = d.userIndex;
this.company = p.company || '';
this.logo = p.logo || '';
this.domain = p.domain || '';
this.description = p.description || '';
this.address = p.address || '';
this.phone = p.phone || '';
this.email = p.email || '';
this.billingEmail = p.billingEmail || '';
this.advanced = isDefined(a) ? {
'leadSource' : a.leadSource,
'representative' : a.representative
} : null;
this.hasLogo = !!p.logo;
}
function DataObj(d){
this.company = d.company || '';
this.logo = d.logo || '';
this.domain = d.domain || '';
this.description = d.description || '';
this.address = d.address || '';
this.phone = d.phone || '';
this.email = d.email || '';
this.billingEmail = d.billingEmail || '';
this.currentPassword = '';
this.newPassword = '';
this.confirmPassword = '';
allowEmptyEmail = this.email === '';
}
function UserIndexObj(d){
this.leadSource = d.leadSource;
this.representative = d.representative;
}
function SidePanelObj(d){
this.status = d.status || null;
this.credit = d.credit || null;
this.tracking = d.tracking || null;
this.segmentCount = d.segmentCount || null;
this.adCount = d.adCount || null;
this.campaignCount = d.campaignCount || null;
this.roi = d.roi || null;
this.charges = d.charges || null;
this.weekSpend = d.weekSpend || null;
this.lifeSpend = d.lifeSpend || null;
}
function Template(){
var header = '',
tc = '',
overview = '{{ title }}:
{{# info }}{{ title }}:{{ value }}
{{/ info }}',
section = '',
footer = '';
var companyName = 'Company:
',
logo = 'Your logo:
',
domain = 'Website URL:
',
description = 'Description:
',
address = 'Address:
',
phone = 'Phone:
',
email = 'Email:
',
billingEmail = 'Billing email:
',
leadSource = 'Lead Source:
',
representative = 'Representative:
',
advanced = '{{# advanced }}' + leadSource + representative + '{{/ advanced }}';
var currentPassword = 'Current password:
',
newPassword = 'New password:
',
confirmPassword = 'Confirm password:
';
this.sidePanel = overview;
this.index = '
' + header + section + footer + '
';
this.main = '' + companyName + logo + domain + description + address + phone + email + billingEmail + advanced + '
';
this.changePassword = '' + currentPassword + newPassword + confirmPassword + '
';
}
function init(error, d){
inboundData = d.profiles;
var p = d.profiles || {};
dataObj = new DataObj(p);
sidePanelObj = new SidePanelObj({});
renderObj = new RenderObj(d);
userIndexObj = new UserIndexObj(isDefined(d.userIndex) ? d.userIndex : {});
element = getElementFromString(template.index);
setElements();
initTabbedWindow();
refreshSidePanel();
returnCB();
insertElement();
events.add(elements.save, 'click', saveAction);
}
function initTabbedWindow(){
var obj = {
'data' : [
{ 'title' : 'Main', 'element' : elements.mainTab, 'initFn' : initMainElement },
{ 'title' : 'Change password', 'element' : elements.changePasswordTab, 'initFn' : initChangePassword }
],
'config' : {
'prepend' : true
}
};
elements.tabbedContainer = new TabbedContainer(element.querySelector('section'), obj);
}
function initMainElement(){
var e = elements;
e.companyName = eqs('.companyName');
e.logo = eqs('.logoImg');
e.logoBtn = eqs('.logoBtn');
e.logoUpload = new ImageUpload(element, updateLogo);
e.domain = eqs('.domain');
e.description = eqs('.description');
e.address = eqs('.address');
e.phone = eqs('.phone');
e.email = eqs('.email');
e.billingEmail = eqs('.billingEmail');
events.add(e.companyName, 'change', updateCompanyName);
events.add(e.logoBtn, 'click', elements.logoUpload.open);
events.add(e.domain, 'change', updateDomain);
events.add(e.description, 'change', updateDescription);
events.add(e.address, 'change', updateAddress);
events.add(e.phone, 'change', updatePhone);
events.add(e.email, 'change', updateEmail);
events.add(e.billingEmail, 'change', updateBillingEmail);
if(isDefined(renderObj.advanced)){
e.leadSource = element.querySelector('.leadSource');
e.representative = element.querySelector('.representative');
events.add(e.leadSource, 'change', updateLeadSource);
events.add(e.representative, 'change', updateRepresentative);
}
function eqs(s){ return element.querySelector(s); }
}
function initChangePassword(){
var e = elements;
e.currentPassword = element.querySelector('.currentPassword');
e.newPassword = element.querySelector('.newPassword');
e.confirmPassword = element.querySelector('.confirmPassword');
events.add(e.currentPassword, 'change', updateCurrentPassword);
events.add(e.newPassword, 'change', updateNewPassword);
events.add(e.confirmPassword, 'change', updateConfirmPassword);
}
function insertElement(){ target.appendChild(element); }
function getInvalidItems(o){
var a = [];
if(!isDefined(o.company) || o.company === '') a.push('Missing company name');
if(!isDefined(o.email) || o.email === '' && !allowEmptyEmail) a.push('Missing email');
if(!!o.domain && !isValidDomain(o.domain)) a.push('Missing valid domain');
var cu = o.currentPassword,
n = o.newPassword,
co = o.confirmPassword,
cuD = isDefined(cu),
nD = isDefined(n),
coD = isDefined(co);
if(cuD || nD || coD){
if(cuD && nD && coD){
if(!isValidPassword(n)) a.push('New password must be at least 8 characters in length');
if(n != co) a.push('New password and confirm password must match');
}
else a.push('If changing password, please sure to fill all required fields');
}
return a.length > 0 ? a : null;
}
function getOverviewObj(d){
return {
'title' : 'Overview',
'info' : [
{ 'title' : 'Status', 'value' : d.status ? 'On' : 'Off' },
{ 'title' : 'Credit', 'value' : d.credit ? d.credit : 'Not set' },
{ 'title' : 'Pixels', 'value' : d.tracking ? 'Tracking' : 'Not Tracking' },
{ 'title' : '# Segments', 'value' : d.segmentCount ? d.segmentCount : 'N/A' },
{ 'title' : '# Ads', 'value' : d.adCount ? d.adCount : 'N/A' },
{ 'title' : '# Campaigns', 'value' : d.campaignCount ? d.campaignCount : 'N/A' },
{ 'title' : 'ROI', 'value' : d.roi ? d.roi : 'N/A' },
{ 'title' : 'Total charges', 'value' : d.charges ? d.charges : 'None' }
]
};
}
function setElements(){
elements = new Elements(element, {
'sidePanel' : { 'selector' : 'side-panel' },
'save' : { 'selector' : '.saveButton', 'ignoreReset' : true },
'missing' : { 'selector' : null, 'ignoreReset' : true },
'tabbedContainer' : {},
'mainTab' : {},
'changePasswordTab' : {},
'section' : { 'selector' : 'section' },
'companyName' : {},
'logo' : {},
'logoBtn' : {},
'logoUpload' : {},
'domain' : {},
'description' : {},
'address' : {},
'phone' : {},
'email' : {},
'billingEmail' : {},
'currentPassword' : {},
'newPassword' : {},
'confirmPassword' : {},
});
elements.mainTab = getElementFromString(render(template.main, renderObj));
elements.changePasswordTab = getElementFromString(template.changePassword);
}
function setMissing(a){ elements.missing = core.notifications.setMissingFields(a); }
function updateCompanyName(e){ updateData('company', e.target.value); }
function updateLogo(e){
if(e.width === 200 && e.height === 200){
elements.logo.src = e.data;
elements.logo.classList.remove('noDisplay');
dataObj.logo = e.data;
} else {
core.notifications.setError("I'm sorry, but your image must be 200x200, your image is " + e.width + "x" + e.height + ".");
}
}
function updateDomain(e){
if(!isValidDomain(e.target.value)) return core.notifications.setError('You must enter a valid domain name first.');
updateData('domain', e.target.value);
}
function updateDescription(e){
var v = e.target.value;
if(v.length > 2000){
core.notifications.setError("Description must be 2000 or less characters.")
return
}
dataObj.description = v;
}
function updateAddress(e){ updateData('address', e.target.value); }
function updatePhone(e){ updateData('phone', e.target.value); }
function updateEmail(e){ updateData('email', e.target.value); }
function updateBillingEmail(e){ updateData('billingEmail', e.target.value); }
function updateCurrentPassword(e){ updateData('currentPassword', e.target.value); }
function updateNewPassword(e){ updateData('newPassword', e.target.value); }
function updateConfirmPassword(e){ updateData('confirmPassword', e.target.value); }
function updateData(k, v){ dataObj[k] = v; }
function updateLeadSource(e){ updateUserIndex('leadSource', e.target.value); }
function updateRepresentative(e){ updateUserIndex('representative', e.target.value); }
function updateUserIndex(k, v){ userIndexObj[k] = v; }
function refreshSidePanel(){ swapSidePanel(elements.section, elements.sidePanel, getSidePanelRender()); }
function swapSidePanel(p, o, n){ p.insertBefore(n, o); p.removeChild(o); elements.sidePanel = n; }
function getSidePanelRender(){ return getElementFromString(render(template.sidePanel, getOverviewObj(sidePanelObj || {}))); }
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),
passUpdate = isPasswordUpdate(dataObj),
indexUpdate = isIndexUpdate(renderObj);
if(invalid === null) save();
else setMissing(invalid);
function save(){
if(passUpdate) savePassword()
else apiRequest('PUT', 'profiles', core.userID, dataObj, redirect);
}
function savePassword(){
//var fn = (indexUpdate ? saveUserIndex : redirect);
apiRequest('POST', 'changePassword', core.userID, { 'currentPass' : dataObj.currentPassword, 'pass' : dataObj.newPassword }, redirect);
}
function saveUserIndex(){ apiRequest('PUT', 'userIndex', core.userID, userIndexObj, redirect); }
function isPasswordUpdate(d){ return d.newPassword !== ''; }
function isIndexUpdate(d){ return isDefined(d.advanced); }
function redirect(){ pop('Pop!', 'Dashboard', '/dashboard'); }
}
function exit(){
if(events){
events.reset();
}
if(element) removeChild(target, element);
if(elements) elements.exit();
housekeeping();
}
function housekeeping(){ core = target = data = returnCB = events = template = dataObj = sidePanelObj = element = elements = inboundData = null; }
};