var CreateDynamicAds = function(core, target, data, returnCB){ var events = new EventManager, template = new CreateDynamicAdsTemplate, element = getElementFromString(render(template.index, {})), elements = new Elements(element, { "stepOne" : { "target" : new StepOne(element) }, "stepTwo" : { "target" : new StepTwo(element) } }), data = new Data; this.exit = exit; new ApiManager({ 'adGroupsList' : null }, core.userID, init); function Data(){ this.adGroupsList = null; this.adTemplate = null; this.logoWidth = null; this.logoHeight = null; this.extendedName = false; this.adGroup = null; this.logo = null; this.buttonText = null; this.backupBanner = null; this.backupLandingUrl = null; this.impTrack = null; this.clickTrack = null; } function Image(data, rects, name, fileSize){ this.data = data; this.width = rects.width; this.height = rects.height; this.name = name; this.fileSize = fileSize; } function Rects(w, h){ this.width = w; this.height = h; } function init(e, d){ if(!e){ returnCB(); insertElement(); data.adGroupsList = d.adGroupsList; elements.stepOne.init(); } else { core.setError("I'm sorry, but there was an error loading this page.") } } function insertElement(){ target.appendChild(element); } function StepOne(e){ var tmpl = template.stepOne, element = null, obj = { "adTemplates" : [ { "img" : '/public/images/adBuilder/dynamicAdExample1_300x250.png', "buttonText" : "Select", "buttonClass" : "mint", 'value' : 'one' }, { "img" : '/public/images/adBuilder/dynamicAdExample2_300x250.png', "buttonText" : "Select", "buttonClass" : "mint", 'value' : 'two' }, { "img" : '/public/images/adBuilder/dynamicAdExample3_300x250.png', "buttonText" : "Select", "buttonClass" : "mint", 'value' : 'three' }, { "img" : '/public/images/adBuilder/customTemplate.png', "buttonText" : "Call: (972) 559-4197", "buttonClass" : "black auto" } ] }, closed = false; this.init = init; this.exit = exit; function TemplateContainer(e, d, i){ var a = e.querySelector('a'); events.add(a, 'click', click, 'stepOne'); function click(e){ preventDefault(e); if(!!d.value){ data.adTemplate = d.value; switch(d.value){ case 'one': data.footerContent = true; data.logoWidth = 300; data.logoHeight = 50; break; case 'two': data.postImageContent = true; data.logoWidth = 300; data.logoHeight = 250; break; case 'three': data.preImageContent = true; data.logoWidth = 300; data.logoHeight = 250; data.extendedName = true; break; } exit(); elements.stepTwo.init(); } } } function init(){ element = getElementFromString(render(tmpl, obj)); e.appendChild(element); bindContainers(); } function bindContainers(){ var c = element.querySelectorAll('container'), len = c.length, cnt = len; while(cnt){ var i = len - cnt--; new TemplateContainer(c[i], obj.adTemplates[i], i) } } function exit(){ if(!closed){ removeChild(e, element); events.removeByTag('stepOne'); closed = true; } } } function StepTwo(e){ var tmpl = template.stepTwo, element = null, adGroup = null, logo = null, buttonText = null, backupBanner = null, backupLandingUrl = null, save = null, closed = false, elements = null; this.init = init; this.exit = exit; function init(){ element = getElementFromString(render(tmpl, data)); elements = new Elements(element, { 'trackers' : { 'selector' : '.trackers + div' }, 'impTrack' : { 'selector' : '.impTrack' }, 'clickTrack' : { 'selector' : '.clickTrack' } }); elements.trackersToggle = new ToggleInput(element.querySelector('.trackers'), toggleTrackers, { 'initialValue': false }); events.add(elements.impTrack, 'change', getInputSetter(data, 'impTrack', trackerCheck)); events.add(elements.clickTrack, 'change', getInputSetter(data, 'clickTrack', trackerCheck)); e.appendChild(element); adGroup = new AdGroup(element); logo = new Logo(element); buttonText = new ButtonText(element); backupBanner = new BackupBanner(element); backupLandingUrl = new BackupLandingUrl(element); save = new Save(element); } function trackerCheck(url, e) { if(typeof url === 'string' && url.toLowerCase().indexOf('https://') === 0 || url.length === 0) { return url; } core.notifications.setError("Please use an HTTPS url for the tracker."); } function toggleTrackers(e) { if(e) { return elements.trackers.classList.remove('noDisplay') } elements.trackers.classList.add('noDisplay') data.impTrack = null; data.clickTrack = null; } function AdGroup(e){ var t = new Dropdown(e.querySelector('.adGroup'), { "data" : getList(), "options" : { "dropdownClass" : "cool", "menuItemClass" : "cool", "carrotClass" : "cool" } }, update); this.exit = exit; function Item(d){ this.value = d.id; this.title = d.data.name; this.placeholder = !!d.placeholder; } function getList(){ var a = [], len = data.adGroupsList.length, c = len; a.push(new Item(getPlaceholder())) while(c){ a.push(new Item(data.adGroupsList[len - c--])); } return a; } function getPlaceholder(){ return { 'id' : null, 'data' : { 'name' : 'Please select ad group' }, 'placeholder' : true }; } function update(e){ data.adGroup = e; } function exit(){ if(!!t) t.exit(); t = null; } } function Logo(e){ var button = e.querySelector('.uploadLogo'), upload = new HiddenFileInput(uploadCb), container = e.querySelector('.logo'), canvas = container.querySelector('canvas'), ctx = canvas.getContext('2d'), img = document.createElement('img'); events.add(img, 'load', setImg); events.add(button, 'click', uploadOpen); this.exit = exit; function uploadOpen(){ upload.open(); } function uploadCb(e){ img.src = e[0].result } function setImg(e){ var r = getCnvsRects(); container.classList.remove('empty'); ctx.drawImage(img, r[0], r[1], r[2], r[3]); var d = canvas.toDataURL(); data.logo = new Image(d, new Rects(data.logoWidth, data.logoHeight), 'Dynamic ads logo', getKilobytesFromBytes(d.length) + 'K'); setBtn(); } function setBtn(){ button.classList.add('mist'); button.classList.remove('cool'); button.textContent = 'File selected'; } function getCnvsRects(){ var w = img.width, h = img.height, rW = 0, rH = 0; if(w <= data.logoWidth && h <= data.logoHeight){ rW = w; rH = h; } else { var wD = w / data.logoWidth, hD = h / data.logoHeight; if(wD > hD){ rW = w / wD; rH = h / wD; } else { rW = w / hD; rH = h / hD; } } return [(data.logoWidth - rW) / 2, (data.logoHeight - rH) / 2, rW, rH]; } function exit(){ button = upload = container = canvas = ctx = img = null; } } function ButtonText(e){ var input = e.querySelector('.buttonText'), button = e.querySelector('ad').querySelector('.button'); events.add(input, 'change', update); this.exit = exit; function update(e){ var v = input.value; button.textContent = v; data.buttonText = v; } function exit(){ input = button = null; } } function BackupBanner(e){ var button = e.querySelector('.uploadBackupBanner'), upload = new HiddenFileInput(uploadCb), img = document.createElement('img'); events.add(img, 'load', setImg); events.add(button, 'click', uploadOpen); this.exit = exit; function uploadOpen(){ upload.open(); } function uploadCb(e){ img.src = e[0].result } function setImg(e){ if(img.width !== 300 || img.height !== 250){ data.backupBanner = null; unsetBtn(); core.notifications.setError('Please ensure your backup banner is 300x250.'); } else { var d = img.src; data.backupBanner = new Image(d, new Rects(300, 250), 'Dynamic ads backup banner', getKilobytesFromBytes(d.length) + 'K'); setBtn(); } } function setBtn(){ button.classList.add('mist'); button.classList.remove('cool'); button.textContent = 'File selected'; } function unsetBtn(){ button.classList.add('cool'); button.classList.remove('mist'); button.textContent = 'Select file'; } function exit(){ button = upload = img = null; } } function BackupLandingUrl(e){ var input = e.querySelector('.backupLandingUrl'); events.add(input, 'change', update); this.exit = exit; function update(e){ var v = input.value; if(isValidURL(v)) data.backupLandingUrl = v; else core.notifications.setError('Please ensure you have entered a valid url for your backup landing page url.'); } function exit(){ input = null; } } function Save(e){ var s = e.querySelector('.save'), f = s.parentNode; events.add(s, 'click', save); function save(e){ preventDefault(e); if(isValid() && validateTrackers()){ s.parentNode.classList.add('noDisplay'); new DynamicSave(core, data, core.userID, saveCb); } else core.notifications.setError("Please ensure that all the required fields have been populated before saving.") } function validateTrackers() { var it = data.impTrack, ct = data.clickTrack; if(!!it && it.length > 0 && it.indexOf('https://') !== 0) return false; if(!!ct && ct.length > 0 && ct.indexOf('https://') !== 0) return false; return true; } function saveCb(e){ if(!e){ core.notifications.setSuccess("w00t!"); exit(); pop('Pop!', 'Dashboard - Ads', '/dashboard/ads'); } else { s.parentNode.classList.remove('noDisplay'); core.notifications.setError("I'm sorry, but there was an error saving your ad."); } } function isValid(){ return !!data.adGroup && !!data.logo && !!data.buttonText && !!data.backupBanner && !!data.backupLandingUrl; } } function exit(){ if(!closed){ adGroup.exit(); logo.exit(); buttonText.exit(); backupBanner.exit(); backupLandingUrl.exit(); removeChild(e, element); events.removeByTag('stepTwo'); closed = true; } } } function exit(){ if(element) removeChild(target, element); if(events) events.reset(); if(elements) elements.exit(); housekeeping(); } function housekeeping(){ core = target = data = returnCB = events = template = element = data = elements = null; } };