var ManageAdvertiser = function(core, target, data, returnCB, clientValue){
var focusDefined = isDefined(clientValue),
events = new EventManager(),
template = new Template(focusDefined),
element = getElementFromString(render(template.index, { 'sidePanel' : null })),
elements = null,
dataObj = null,
sidePanelObj = null,
inboundData = null,
apiObj = { 'advertisersList' : null };
this.exit = exit;
if(focusDefined) new ApiManager({ 'users' : null, 'profiles' : null }, clientValue, init);
else init(null, apiObj);
function SidePanelObj(o){
this.status = isDefined(o.status) ? o.status : null;
this.credit = isDefined(o.credit) ? o.credit : null;
this.tracking = isDefined(o.tracking) ? o.tracking : null;
this.segmentCount = isDefined(o.segmentCount) ? o.segmentCount : null;
this.adCount = isDefined(o.adCount) ? o.adCount : null;
this.campaignCount = isDefined(o.campaignCount) ? o.campaignCount : null;
this.roi = isDefined(o.roi) ? o.roi : null;
this.chargeHistory = isDefined(o.chargeHistory) ? o.chargeHistory : null;
}
function DataObj(d){
this.status = getDataObjValue(d.users, 'status', false);
this.notes = getDataObjValue(d.users, 'notes');
this.advertiserFee = getDataObjValue(d.users, 'advertiserFee') || 0;
this.agencyFee = parseInt(getDataObjValue(d.users, 'agencyFee')) || 0;
this.fixedFee = parseFloat(getDataObjValue(d.users, 'fixedFee')) || 0;
this.agencyId = getDataObjValue(d.users, 'agencyId');
this.email = getDataObjValue(d.profiles, 'email');
this.pass = null;
this.company = getDataObjValue(d.profiles, 'company');
this.domain = getDataObjValue(d.profiles, 'domain');
this.spent = getDataObjValue(d, 'spent', 0);
this.conversions = getDataObjValue(d, 'conversions', 0);
this.totalBudget = getDataObjValue(d, 'totalBudget', 0);
this.createdAt = getDataObjValue(d.users, 'createdAt');
this.id = getDataObjValue(d.users, 'id');
this.type = getDataObjValue(d.users, 'type');
this.address = getDataObjValue(d.profiles, 'address');
this.billingEmail = getDataObjValue(d.profiles, 'billingEmail');
this.phone = getDataObjValue(d.profiles, 'phone');
this.salesRep = getDataObjValue(d.users, 'salesRep');
}
function Template(editState){
var actionName = editState === true ? 'Edit' : 'Create',
updateName = editState === true ? 'Save' : 'Create'
var header = '' + actionName + ' Advertiser
',
overview = '{{ title }}:
{{# info }}{{ title }}:{{ value }}
{{/ info }}',
section = '',
footer = '';
var status = 'Status:
',
name = 'Advertiser name:
',
domain = 'Domain:
',
notes = 'Notes:
',
advertiserFee = 'Advertiser fee:
',
fixedFee = 'Fixed fee:
',
salesRep = 'Sales Rep:
',
setUsername = 'Set username:
',
setPassword = '{{^ id }}Set password:
{{/ id }}';
this.sidePanel = overview;
this.index = '
' + header + section + footer + '
';
this.main = '' + status + name + domain + notes + advertiserFee + (core.isAdmin ? fixedFee : '') + salesRep + setUsername + setPassword + '
';
this.chargeHistory = 'Charge history:
{{# chargeHistory }}{{ date }}
{{# charges }}{{ campaignName }} | ID ({{ id }}) | {{ amount }}
{{/ charges }}{{/ chargeHistory }}|
';
}
function User(d){
this.status = d.status;
this.notes = d.notes;
this.advertiserFee = parseInt(d.advertiserFee);
this.agencyFee = parseInt(d.agencyFee);
this.fixedFee = parseFloat(d.fixedFee);
this.agencyId = d.agencyId;
this.createdAt = d.createdAt;
this.id = d.id;
this.type = d.type;
this.salesRep = d.salesRep;
}
function Profile(d){
this.address = d.address;
this.billingEmail = d.billingEmail;
this.company = d.company;
this.domain = d.domain;
this.email = d.email;
this.phone = d.phone;
}
function init(error, d){
returnCB();
clientsData = d.advertisers;
inboundData = getCreateClientData();
if(!error) initDefault(d);
insertElement();
returnCB = null;
}
function initDefault(d){
dataObj = new DataObj(d);
sidePanelObj = new SidePanelObj(d);
elements = new Elements(element, {
'tabbedContainer' : {},
'status' : {},
'company' : {},
'domain' : {},
'notes' : {},
'agencyFee' : {},
'salesRep' : {},
'email' : {},
'password' : {},
'save' : { 'selector' : '.saveButton', 'ignoreReset' : true },
'missing' : { 'selector' : null, 'ignoreReset' : true },
'section' : { 'selector' : 'section' },
'sidePanel' : { 'selector' : 'side-panel' },
'mainTab' : { 'target' : getElementFromString(render(template.main, dataObj)) },
'chargesTab' : { 'target' : getElementFromString(render(template.chargeHistory, inboundData)) }
});
initTabbedWindow();
events.add(elements.save, 'click', saveAction);
}
function initTabbedWindow(){
var obj = {
'data' : [
{ 'title' : 'Main', 'element' : elements.mainTab, 'initFn' : initMainElement }
// { 'title' : 'Charges', 'element' : elements.chargesTab, 'initFn' : initChargesElement }
],
'config' : {
'prepend' : true
}
};
elements.tabbedContainer = new TabbedContainer(element.querySelector('section'), obj);
}
function initMainElement(){
var e = elements,
setStatus = dataObj.status;
e.status = new ToggleInput(elements.mainTab.querySelector('.statusToggle'), updateStatus);
e.company = element.querySelector('.name');
e.domain = element.querySelector('.domain');
e.notes = element.querySelector('.notes');
e.advertiserFee = element.querySelector('.advertiserFee');
e.fixedFee = element.querySelector('.fixedFee');
e.salesRep = element.querySelector('.salesRep');
e.email = element.querySelector('.username');
e.password = element.querySelector('.password');
events.add(e.company, 'change', updateCompany);
events.add(e.domain, 'change', updateDomain);
events.add(e.notes, 'change', updateNotes);
events.add(e.advertiserFee, 'change', updateAdvertiserFee);
events.add(e.fixedFee, 'change', updateFixedFee);
events.add(e.salesRep, 'change', updateSalesRep);
events.add(e.email, 'change', updateEmail);
if(isDefined(e.password)) events.add(e.password, 'change', updatePassword);
if(e.status.getState() !== setStatus) setStatus ? e.status.set() : e.status.unset();
}
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 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.chargeHistory ? d.chargeHistory : 'None' }
]
};
}
function getInvalidItems(o){
var a = [];
if(!isDefined(o.company)) a.push('Missing company name');
// if(!isDefined(o.advertiserFee)) a.push('Please set advertiser fee');
if(!!o.domain && !isValidDomain(o.domain)) a.push('Missing valid domain');
if(!focusDefined){
if(isDefined(o.pass) && !isValidPassword(o.pass)) a.push('Invalid password, 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/advertiser', core.userID, 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(e && e.error && e.message) {
return core.notifications.setError(e.message);
}
if(++count == 2) redirect();
}
}
function redirect(e){
if(e && e.error) {
return core.notifications.setError(e.error);
}
pop('Pop!', 'Dashboard - Clients', '/dashboard/advertisers');
}
}
function setMissing(a){ core.notifications.setMissingFields(a); }
function updateStatus(e){ updateData('status', e); updateSidePanel('status', e); }
function updateCompany(e){ updateData('company', e.target.value); }
function updateDomain(e){
e.target.value = e.target.value.replace(/^(?:\w+:\/\/)?([^/]+)\/?/, "$1").toLowerCase();
updateData('domain', e.target.value);
}
function updateNotes(e){ updateData('notes', e.target.value); }
function updateAdvertiserFee(e){ updateData('advertiserFee', parseFloat(e.target.value)); }
function updateFixedFee(e){ updateData('fixedFee', parseFloat(e.target.value)); }
function updateSalesRep(e){ updateData('salesRep', 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 updateSidePanel(k, v){ sidePanelObj[k] = v; refreshSidePanel(); }
function refreshSidePanel(){
var e = getElementFromString(render(template.sidePanel, getOverviewObj(sidePanelObj))),
s = elements.section;
s.insertBefore(e, elements.sidePanel);
s.removeChild(elements.sidePanel);
elements.sidePanel = e;
}
function exit(){
if(element) removeChild(target, element);
if(events) events.reset();
if(elements) elements.exit();
housekeeping();
}
function housekeeping(){ core = target = data = returnCB = clientValue = events = template = dataObj = sidePanelObj = element = elements = inboundData = clientsData = apiObj = null; }
};