var Pages = function(core, target, data, returnCB, pageType, targetId){ var events = new EventManager, data = null, element = null, elements = null; this.exit = exit; this.hasCart = true; new ApiManager(getRequestObj(pageType), targetId, init); function Data(d){ var dmn = getValue('page', 'domain', ''), pType = capitalizeN(0, pageType), category = d.page.category; this.id = getValue('page', 'id', ''); this.key = getValue('page', 'key', ''); this.name = getValue('page', 'name', ''); this.domain = '//' + dmn; this.description = getValue('page', 'description', "I'm sorry, this " + pType + " does not have a profile set up yet!"); this.category = category; this.categoryTitle = getCategory(); this.icon = getValue('page', 'icon', ''); this.staffPick = getValue('page', 'staffPick', false); this.topN = getValue('page', 'topN', 0); this.topNApp = getValue('page', 'topNApp', 0); this.likes = getValue('page', 'likes', 0).toLocaleString(); this.tmpls = d.tmpls; this.tags = getTags(getValue('page', 'tags', null)); this.photos = !!d.media ? d.media : null; this.hasDomain = !!dmn; this.hasIcon = !!d.page.icon; this.hasTmpls = !!d.tmpls && d.tmpls.length > 0; this.hasTags = !!d.page.tags && d.page.tags.length > 0; this.hasLiked = !!d.hasLiked && d.hasLiked.state; this.hasUseTmpl = pageType === 'template' && core.userType === "advertiser"; this.hasPhotos = !!d.media && d.media.length > 0; this.hasCat = !!category; this.isAgncy = pageType === "agency" this.isTmpl = pageType === "template" this.isApp = pageType === "application" this.isAdv = core.userType === "advertiser" this.appCost = !!d.page.cost ? '$' + d.page.cost + " CPM" : "Free"; this.pType = pType; this.likeBtnClass = this.hasLiked ? 'mint' : 'cool'; // For Apps this.campaigns = getCampaigns(d.campaigns); function getCampaigns(a){ var cost = !!d.page.cost ? '$' + d.page.cost + " CPM" : "Free", n = []; if(!a){ a = []; } a.splice(0, 0, { "id" : "", "name" : "Use App - " + cost }); a.splice(1, 0, { "id" : "create", "name" : "Create new campaign" }); loop(a, p); return n; function p(k, v){ n[k] = new cmp(v); } function cmp(v){ this.title = v.name; this.value = v.id; } } function getValue(o, k, r){ if(r === undefined) r = null; if(!!d && !!d[o] && !!d[o][k]) return d[o][k]; else return r; } function getCategory(){ if(!category || !d.categories){ return ""; } var t = d.categories.length, c = t; while(c){ var tgt = d.categories[t-c--]; if(category === tgt.value){ return tgt.title; } } return ""; } } function Tag(n, c){ this.name = n; this.color = c; } function getTags(d){ if(!d) return null; var colors = ["fresh", "mint", "cool", "hot", "royal", "grey"], cl = colors.length, ci = 0, t = d.length, c = t, a = []; while(c){ a.push(new Tag(d[t-c--], colors[ci++])) if(ci == cl) ci = 0; } return a; } Data.prototype.set = function(k, v){ this[k] = v; } function Template(){ var icon = '{{# hasIcon }}{{/ hasIcon }}' topN = '{{# topN }}

Top {{ . }} Agency

{{/ topN }}', topNApp = '{{# topNApp }}

Top {{ . }} {{ pType }}

{{/ topNApp }}', staffPick = '{{# staffPick }}

Staff Pick

{{/ staffPick }}', left = '
' + icon + topN + topNApp + staffPick + '
', cats = '{{# hasCat }}{{ categoryTitle }}{{/ hasCat }}', isAgncy = '{{# isAgncy }}Agencies{{/ isAgncy }}', isTmpl = '{{# isTmpl }}Templates{{/ isTmpl }}', name = '

{{ name }}:' + cats + isAgncy + isTmpl + '

', visitWebsite = '{{# hasDomain }}Visit Website{{/ hasDomain }}', useTmpl = '{{# hasUseTmpl }}Use Template{{/ hasUseTmpl }}', isApp = '{{# isApp }}{{# isAdv }}Use App - {{ appCost }}{{/ isAdv }}{{/ isApp }}', likes = '{{ likes }}', desc = '

{{ description }}

', anchors = '
' + visitWebsite + isApp + useTmpl + likes + '
', middle = '
' + name + anchors + desc + '
', tmpls = '{{# tmpls }}{{ name }}-{{ likes }}{{/ tmpls }}', hasTmpls = '{{# hasTmpls }}

{{ pType }} Templates:

' + tmpls + '{{/ hasTmpls}}', tags = '{{# tags }}{{ name }}{{/ tags }}', hasTags = '{{# hasTags }}

Who this works for:

' + tags + '{{/ hasTags}}', right = '
' + hasTmpls + hasTags + '
', info = '' + left + middle + right + '', photos = '{{# hasPhotos }}

{{ pType }} Photos:

{{# photos }}{{/ photos }}
{{/ hasPhotos }}'; this.index = '
' + info + photos + '
'; this.error = '

I\'m sorry, but the requested Id does not exist.

'; } function init(error, d){ var tmpl = new Template; if(!error){ data = new Data(d); element = getElementFromString(render(tmpl.index, data)); setElements(data); if(!!elements.photos) elements.preview = elements.photos.querySelector('preview').querySelector('img'); if(!!elements.useTmpl) events.add(elements.useTmpl, 'click', popAnchor); if(!!elements.useApp) events.add(elements.useApp, 'click', useApp); events.add(elements.likesBtn, 'click', likeAction); bindTmplAnchors(); bindPhotos(); if(core.userType === "advertiser"){ core.shoppingCart.show(); } } else { element = getElementFromString(tmpl.error); } returnCB(); target.appendChild(element); } function bindTmplAnchors(){ if(!elements.tmpls) return null; var a = elements.tmpls.querySelectorAll('a'), t = a.length, c = t; while(c){ events.add(a[t-c--], 'click', popAnchor); } } function bindPhotos(){ if(!elements.photos) return null; var a = elements.photos.querySelector('thumbnails').querySelectorAll('img'), t = a.length, c = t; while(c){ var i = t-c--; if(i === 0) elements.preview.src = data.photos[i].loc; events.add(a[i], 'click', function(loc){ return function(e){ preventDefault(e); elements.preview.src = loc; } }(data.photos[i].loc)); } } function getRequestObj(pt){ var o = { 'page' : { 'source' : 'pages/' + pt }, 'hasLiked' : { 'source' : 'like/' + pt }, } if(pt === "agency") o.tmpls = { 'source' : 'campaignTemplates/list/byId' }; if(pt === "application" && core.userType === "advertiser") o.campaigns = { 'source' : 'campaignsList/' + core.userID, 'noID' : true }; switch(pageType){ case 'application': o.media = { 'source' : 'appMedia/byId/all' }; o.categories = { 'source' : 'resources/appCategories', 'noID' : true }; break; case 'agency': o.media = { 'source' : 'media/byUser/all' }; break; } return o; } function setElements(d){ elements = new Elements(element, { 'name' : {}, 'likesBtn' : { 'selector' : '.likes' }, 'useTmpl' : { 'selector' : '.useTmpl' }, 'useApp' : { 'selector' : '.useApp' }, 'tmpls' : { 'selector' : 'tmpls' }, 'photos' : { 'selector' : 'photos' }, 'preview' : {}, }); } function likeAction(e){ preventDefault(e); apiRequest(data.hasLiked ? 'DELETE' : 'PUT', 'like/' + pageType, targetId, null, likeCb); } function likeCb(e, s){ if(s !== 200) return; var t = elements.likesBtn; data.hasLiked = !data.hasLiked data.likes = e.count.toLocaleString(); t.classList.remove(data.hasLiked ? 'cool' : 'mint') t.classList.add(data.hasLiked ? 'mint' : 'cool') t.querySelector('span').textContent = data.likes; } function useApp(e){ preventDefault(e); var d = core.db.Get("shoppingCart"), match = false; loop(d, p); if(d === null){ d = []; } if(!match){ core.db.Put("shoppingCart", d.concat({ 'key' : data.key, 'name' : data.name, 'id' : data.id })); } return null; function p(k, v){ if(v.key === data.key){ match = true; return true; } } } function exit(){ if(!!element) target.removeChild(element); if(!!elements) elements.exit(); if(!!events) events.reset(); } };