var ManageTemplate = function(core, target, data, returnCB, templateId, campaignId){ var events = new EventManager, data = null, element = null, elements = null; var action = getAction(); this.exit = exit; if(action === 'edit') new ApiManager({ 'campaignTemplate' : null }, templateId, init); else if(action === 'create') init(null, {}); function Data(d){ this.name = getValue(d, 'name', ''); this.icon = getValue(d, 'icon', ''); this.tags = getValue(d, 'tags', []); this.description = getValue(d, 'description', ''); this.action = capitalizeN(0, action); function getValue(d, k, r){ if(r === undefined) r = null; if(isDefined(d) && isDefined(d[k])) return d[k]; else return r; } } Data.prototype.set = function(k, v){ this[k] = v; } function Template(){ var header = '

{{ action }} Campaign Template

', name = '

Template Name:

', icon = '

Template Icon:

Upload 200x200
', tags = '

Tags:

', infoIntro = '

Your template will appear in the app store under "Campaign Templates". Once your template has been shared you cannot remove it as others might be using it within an active campaign. Sharing templates is a great way to increase your rep and make you an industry icon.

', tipOne = '

- Make an eye catching template icon

', tipTwo = '

- Name your template using relevant terms

', tipThree = '

- Put tips and use cases within your description

', tipFour = '

- Upload a YouTube tutorial or screenshots of your template performance or settings

', tipFive = '

- Add relevant tags to help users find your template

', infoTips = '

Tips:

' + tipOne + tipTwo + tipThree + tipFive, description = '

Description:

', sidePanel = '

Instructions:

' + infoIntro + infoTips + '
', section = '
' + name + icon + tags + description + '' + sidePanel + '
', footer = ''; return '
' + header + section + footer + '
'; } function init(error, d){ data = new Data(d.campaignTemplate); element = getElementFromString(render(Template(), data)); setElements(data); returnCB(); target.appendChild(element); events.add(elements.name, 'change', updateName); events.add(elements.iconButton, 'click', elements.iconUpload.open); events.add(elements.description, 'change', updateDescription); events.add(elements.save, 'click', function(){ new Save; }); } function getAction(){ if(templateId !== null) return 'edit' else if(campaignId !== null) return 'create' else return null } function setElements(d){ elements = new Elements(element, { 'name' : { 'selector' : '.name' }, 'iconButton' : { 'selector' : '.iconButton' }, 'iconImg' : { 'selector' : '.iconImg' }, 'iconUpload' : { 'target' : new ImageUpload(element, updateIcon) }, 'tags' : { 'target' : new TagContainer(element.querySelector('.tags'), { 'limit' : 10, 'data' : d.tags }, updateTags) }, 'description' : { 'selector' : '.description' }, 'save' : { 'selector' : '.saveButton' } }); } function updateName(e){ data.set('name', e.target.value); } function updateIcon(e){ if(e.width === 200 && e.height === 200){ elements.iconImg.src = e.data; elements.iconImg.classList.remove('noDisplay'); data.set('icon', e.data); } else { core.notifications.setError("I'm sorry, but your image must be 200x200, your image is " + e.width + "x" + e.height + "."); } } function updateTags(e){ data.set('tags', e); } function updateDescription(e){ data.set('description', e.target.value); } // Set this up function Save(){ var err = getErrors(); if(err.length > 0) return core.notifications.setMissingFields(err); if(action === 'edit') apiRequest('PUT', 'campaignTemplate', templateId, data, saveCb); else apiRequest('POST', 'campaignTemplate/' + campaignId, core.userID, data, saveCb); function getErrors(){ var err = []; if(data.name.length === 0) err.push('Name cannot be empty'); if(data.icon.length === 0) err.push('Icon must be set'); if(data.tags.length === 0) err.push('At least one tag must be declared'); if(data.description.length === 0) err.push('Description cannot be empty'); return err; } function saveCb(e, s){ if(s === 200) return pop('Pop!', 'Dashboard - Campaigns', '/dashboard/campaigns'); core.notifications.setError({ 'message' : (e && e.error) ? e.message : 'Oops, server error, please try to save again in few seconds.'}); } } function exit(){ target.removeChild(element); if(events) events.reset(); if(elements) elements.exit(); } };