var MissionControlList = function(core, target, data, returnCB, listType){ 'use strict' var events = new EventManager, element = null, elements = null, data = null, reqObj = {}; this.exit = exit; this.hasCart = true; switch(listType){ case "apps": reqObj.apps = { 'source' : 'apps/list' } reqObj.categories = { 'source' : 'resources/appCategories' } break; case "tmpls": reqObj.apps = { 'source' : 'campaignTemplates/list' } break; case "agencies": reqObj.apps = { 'source' : 'agencies/list' } } new ApiManager(reqObj, null, init); function init(e, d){ data = new Data(d); element = getElementFromString(render(Template(), data)); elements = new Elements(element, { "apps" : { "selector" : "apps" }, "payFilter" : {}, "categoriesFilter" : {}, "staffPickFilter" : getStaffPickFilter(), "topAppFilter" : getTopAppFilter() }); switch(listType){ case "apps": elements.payFilter = getPayFilter() elements.categoriesFilter = getCategoriesFilter(); break; case "tmpls": break; } bindApps(); checkQueryVars(); returnCB(); target.appendChild(element); if(core.userType === "advertiser"){ core.shoppingCart.show(); } } function Data(o){ var categories = getCategories(o.categories), apps = getApps(o.apps), costTypes = ["all", "paid", "free"], filters = { "category" : 'all', "costType" : 'all', "staffPick" : false, "topApp" : false, }; this.listType = getListType(); this.apps = apps; this.categories = categories; this.getCategoryTitle = getCategoryTitle; this.setCategory = setCategory; this.setCostType = setCostType; this.setStaffPick = setStaffPick; this.setTopApp = setTopApp; this.filter = filter; function getListType(){ switch(listType){ case "tmpls": return new ListType("Template", "Templates"); case "agencies": return new ListType("Agency", "Agencies"); default: return new ListType("App", "Apps");; } } function ListType(s, p){ this.singular = s; this.plural = p; } function getApps(apps){ if(!apps) return null; var a = []; apps.sort(compareIds); loop(apps, insert); return a; function compareIds(a, b){ return parseFloat(a.id) - parseFloat(b.id); } function insert(tgt, i){ tgt.categoryTitle = getCategoryTitle(tgt.category); a[i] = new App(tgt); } } function getCategories(cats){ if(!cats) return null; cats.splice(0,0,{ 'value' : 'all', 'title' : 'All Categories' }); return cats; } function getCategoryTitle(v){ if(!categories) return null; var t = categories.length, c = t; while(c){ var d = categories[t-c--]; if(d.value === v) return d.title; } return null; } function setCategory(v){ if(v === filters.category){ return true; } var state = false; loop(categories, check); return state; function check(tgt){ if(tgt.value === v){ filters.category = v; filter(); state = true; return false; } } } function setCostType(o){ var v = o.value; if(v === filters.costType){ return true; } var state = false; loop(costTypes, check) return state; function check(tgt){ if(tgt === v){ filters.costType = v; filter(); state = true; return false; } } } function setStaffPick(v){ if(typeof v !== "boolean"){ return false; } if(filters.staffPick !== v){ filters.staffPick = v filter(); } return true; } function setTopApp(v){ if(typeof v !== "boolean"){ return false; } if(filters.topApp !== v){ filters.topApp = v filter(); } return true; } function filter(){ if(!elements || !elements.apps) return; var fgmt = document.createDocumentFragment(); loop(apps, action); elements.apps.innerHTML = ''; elements.apps.appendChild(fgmt); function action(e){ if(isCat(e) && isCostType(e) && isStaffPick(e) && isTopApp(e)){ e.insertE(fgmt); } } function isCat(e){ return filters.category === "all" || filters.category === e.category; } function isCostType(e){ return filters.costType === "all" || filters.costType === e.costType; } function isStaffPick(e){ return filters.staffPick === false || filters.staffPick === e.staffPick; } function isTopApp(e){ return filters.topApp === false || e.topN > 0; } } function loop(tgt, fn){ var t = tgt.length, c = t; while(c){ var i = t-c--; if(fn(tgt[i], i) === false){ return true; } } return true; } } function App(o){ var e = null, id = o.id; this.icon = !!o.icon ? o.icon : "/public/dashboard/images/noAppIcon.png"; this.name = o.name; this.author = o.author; this.category = o.category; this.categoryTitle = o.categoryTitle; this.costType = o.cost == 0 ? "free" : "paid"; this.staffPick = o.staffPick === true ? true : false; this.topN = getTopN(); this.likes = typeof o.likes === 'number' && o.likes > 0 ? o.likes : 0; this.hasCat = !!o.category && !!o.categoryTitle; this.hasAuthor = !!o.author; this.insertE = insertE; this.setE = setE; function getTopN(){ var t = 0 if(!!o.topNApp && typeof o.topNApp === 'number' && o.topNApp > 0){ t = o.topNApp; } else if(!!o.topN && typeof o.topN === 'number' && o.topN > 0){ t = o.topN; } return t; } function insertE(fgmt){ if(!!e) return fgmt.appendChild(e); } function setE(t){ e = t; events.add(e, 'click', clickAction); } function clickAction(e){ preventDefault(e); switch(listType){ case "tmpls": pop('Pop!', 'Dashboard - App', '/dashboard/pages/template/' + id); break; case "agencies": pop('Pop!', 'Dashboard - App', '/dashboard/pages/agency/' + id); break; default: pop('Pop!', 'Dashboard - App', '/dashboard/pages/app/' + id); break; } } } function Template(){ var t = '

{{ listType.plural }} List

', pf = '
', cf = '
', sf = '

Staff Pick:

', ta = '

Top {{ listType.singular }}:

', chks = '
' + sf + ta + '
', hdr = '
' + t + pf + cf + chks + '
', app = appTmpl(), apps = '{{# apps }}' + app + '{{/ apps }}'; return '
' + hdr + apps + '
'; function appTmpl(){ var cat = '{{# hasCat }}

{{ categoryTitle }}

{{/ hasCat }}', auth = '{{# hasAuthor }}

By: {{ author }}

{{/ hasAuthor }}'; return '

{{ name }}

' + cat + '
'; } } function getPayFilter(){ return new ToggleNavigation(element.querySelector('.payFilter'), { "data" : [ { "title" : "All", "value" : "all" }, { "title" : "Paid", "value" : "paid" }, { "title" : "Free", "value" : "free" } ], "className" : "toggle" }, data.setCostType); } function getCategoriesFilter(){ return new Dropdown(element.querySelector('.categoriesFilter'), { "data" : data.categories, "options" : { "dropdownClass" : "mint", "menuItemClass" : "mint", "carrotClass" : "mint" } }, data.setCategory); } function getStaffPickFilter(){ return { "target" : new Checkbox(element.querySelector('.staffPickFilter'), data.setStaffPick) }; } function getTopAppFilter(){ return { "target" : new Checkbox(element.querySelector('.topAppFilter'), data.setTopApp) }; } function bindApps(){ if(!data.apps) return false; var es = element.querySelectorAll('app'), t = data.apps.length, c = t; while(c){ var i = t-c--; data.apps[i].setE(es[i]); } return true; } function checkQueryVars(){ var bv = getQueryVariables(); if(!!bv.category){ elements.categoriesFilter.setByValue(bv.category); } if(!!bv.staffPick && bv.staffPick === "true"){ elements.staffPickFilter.set(); data.setStaffPick(true); } if(!!bv.topApp && bv.topApp === "true"){ elements.topAppFilter.set(); data.setTopApp(true); } if(!!bv.costType){ elements.payFilter.setByValue(bv.costType); } } function exit(){ if(element) removeChild(target, element); if(events) events.reset(); if(elements) elements.exit(); housekeeping(); } function housekeeping(){ events = element = elements = data = null; } };