// Throttle app // =============================================== // Takes in data obj, an element to append to, and utility functions // // Data is an object // - rules: [{"from": {"hour": 23, "min": 0}, "to": {"hour": 23, "min": 50}, "rate": "+"}, ....] // var throttle = (function(){ 'use strict' var template = new Template(); function newThrottle(d, t, fns){ return new Throttle(d, t, fns); } function Throttle(d, t, fns){ var e = getElementFromString(template.index, fns.getAppName('throttle')), row = new ToggleRow(e, updateToggle, { 'status' : d.status, 'headerClass' : 'medium', 'removeAppFn' : removeAction }), eles = new Elements(e, { 'status': { 'target' : row }, 'rows' : { 'target' : new DynamicInputList(row.querySelector('.rules'), template.rule, updateRules, { values: getSavedRules(d.rules), onRemove: updateRules }) } }); this.exit = exit; t.appendChild(e); function getSavedRules(r) { if(!isArray(r)) return []; return r.map(function(v) { return { fromHour: v.from.hour, fromMinute: v.from.min, toHour: v.to.hour, toMinute: v.to.min, bidRate: v.rate }; }); } function updateToggle(e) { d.status = e; } function updateRules(e, v) { d.rules = []; v.forEach(function(v) { d.rules.push({ 'from' : {'hour': parseInt(v.fromHour), 'min': parseInt(v.fromMinute) }, 'to' : {'hour': parseInt(v.toHour), 'min': parseInt(v.toMinute) }, 'rate' : v.bidRate }); }) } function removeAction(e){ fns.removeApp("throttle"); } function exit(){ if(!!e) removeChild(t, e); if(!!eles) eles.exit(); e = eles = null; } } function Template(){ var fromInput = '

From

:
', toInput = '

To

:
', bidRate = '

Bid Rate

'; this.index = '

{{ . }}:

'; this.rule = '
' + fromInput + toInput + bidRate + '
'; } return newThrottle; })();