Controllers.prototype.billingHistory = (function(){ var tmpl = new Template(); return newBillingHistory; function newBillingHistory(core, target, rcb){ return new BillingHistory(core, target, rcb) } function BillingHistory(core, target, rcb){ var events = new EventManager(), ele = getElementFromString(tmpl.main), data = null, closed = false; rcb(); target.appendChild(ele); new ApiManager(new APIRequest(), core.userID, init); this.exit = exit; function init(err, resp){ data = new APIResponse(resp); removeChild(ele, ele.querySelector('.loading')); if(data.history.length == 0){ ele.appendChild(getElementFromString(tmpl.empty)); return } ele.appendChild(getElementFromString(render(tmpl.historyItems, { 'history' : getHistory() }))); } function getHistory(){ var a = []; loop(data.history, process); return a.sort(compareHistoryItems).reverse(); function process(k, v){ a.push(new HistoryItem(v.id, getNameByCID(v.id), v.amount, v.authCode, v.timestamp, v.note)); } } function getNameByCID(cid){ var name = ""; loop(data.campaigns, process); return name; function process(_,v){ if(v.id === cid){ name = v.name; return true; } } } function exit(){ if(closed){ return } removeChild(target, ele); ele.innerHTML = ''; closed = true } } function HistoryItem(cid, name, amount, authCode, ts, note){ var d = fromUnix(ts); this.cid = cid; this.name = name; this.usd = !isNaN(amount) ? amount.toFixed(2) : 0; this.authCode = authCode; this.date = (d.getMonth() + 1) + "-" + d.getDate() + "-" + d.getFullYear(); this.ts = ts; this.note = note; this.isAdjustment = authCode == "ADJUSTMENT"; } function APIRequest(){ this.campaigns = { "source" : "campaignsList" }; this.history = { "source" : "history/billing/byAdv" }; } function APIResponse(data){ this.campaigns = data.campaigns || []; this.history = data.history || {}; } function Template(){ var header = '

Billing History

', loading = '

One moment, your billing history is loading..

', hi = getHistoryItem(); this.main = '
' + header + loading + '
'; this.historyItems = '{{# history }}' + hi + '{{/ history }}'; this.empty = '

Oh! It appears that your billing history is empty.

'; function getHistoryItem(){ var d = "

Date: {{ date}}

", c = "

Campaign: {{ name }} [{{ cid }}]

", a = "

Amount: ${{ usd }}

", n = "{{# note }}

Note: {{ . }}

{{/ note }}"; return '' + d + c + a + n + ''; } } function compareHistoryItems(a, b){ if(a.ts < b.ts){ return -1; } if(a.ts > b.ts){ return 1; } return 0; } })();