var Schedule = function(d, t){ var ele = new Elements(t, { 'start' : { 'target' : new CalendarInput(tqs('.startDateRow'), null, updateStart) }, 'end' : { 'target' : new CalendarInput(tqs('.endDateRow'), null, updateEnd) }, 'rows' : { 'target' : t.querySelectorAll('.calendarRow') }, 'toggle' : {} }), init = false; this.exit = ele.exit; ele.toggle = new ToggleInput(tqs('.unrestrictedToggle'), updateToggle, { 'initialValue': d.scheduled }); // if it's an old campaign with a pre-saved schedule, make sure to reset it if(!d.scheduled){ d.start = d.end = 0; } setSchedule(d.scheduled, d.start, d.end); init = true; function updateToggle(e) { if(!e && d.budgetType === 'flight') { return core.notifications.setError('Sorry, flight budget depends on scheduling.'); } d.scheduled = e; if (!e) { d.start = d.end = 0; hide(); } else { if(!d.start){ d.start = ele.start.get().toUnix(); // init the dates } if(!d.end){ d.end = ele.end.get().toUnix() + (60 * 60 * 24) - 1;; } show(); } } function updateStart(e){ if(!init){ return; } d.start = e.toUnix(); } function updateEnd(e){ if(!init){ return; } d.end = e.toUnix() + (60 * 60 * 24) - 1; } function show(){ if(ele.rows) forEach(ele.rows, a); function a(t){ t.classList.remove('noDisplay'); } } function hide(){ if(ele.rows) forEach(ele.rows, a); function a(t){ t.classList.add('noDisplay'); } } function setSchedule(scheduled, sD, eD){ if(sD !== 0){ ele.start.set(sD); } if(eD !== 0){ // Remove day so calendar populates correctly ele.end.set(eD - (60 * 60 * 24) + 1); } if(!scheduled){ ele.toggle.unset(); } } function tqs(s){ return t.querySelector(s); } };