JavaScript/Notes/PrivateProxy
<source lang="javascript"> var Program = function() {
"use strict";
var phaseListClassName = "program-phase-list",
phaseClassName = "program-phase";
function Program(id, config) {
var start = config.startDate,
end = config.endDate;
this.id = id;
this.dateRange = new A.AcxDateRange(start, end);
this.phases = config.phases.slice();
}
var programs = {},
programProxies = {};
var phaseListElement = document.createElement("ul"),
phaseElement = document.createElement("li");
phaseListElement.className = phaseListClassName;
phaseElement.className = phaseClassName;
function ProgramProxy(id, config) {
this.id = id;
this.start = config.startDate;
this.displayName = config.name;
this.end = config.endDate;
programs[id] = new Program(id, config);
}
ProgramProxy.prototype = {
setPhases: function(phases) {
if(phases && phases.slice) {
programs[id].phases = phases.slice();
}
},
buildHTML: function() {
var program = programs[this.id],
html = "";
html += buildPhaseHTML(program);
html += buildCampaignHTML(program);
return html;
}
};
var div = document.createElement("div");
function buildPhaseHTML(program) {
div.innerHTML = "";
var i, li,
phases = program.phases,
len = phases.length,
phaseRationalValue,
ul = phaseListElement.cloneNode(false);
ul.id = "program-" + program.id;
for(i = 0; i < len; i++) {
li = phaseElement.cloneNode(false);
li.id = "program-"
+ program.id
+ "-phase-"
+ phases[i].name
+ "";
li.style.width =
getPhaseWidth(phases[i], program.dateRange.duration);
ul.appendChild(li);
}
div.appendChild(ul);
return div.innerHTML;
}
function buildCampaignHTML() {
return"";
"
- "
+ "
- fixme:\u00A0buildCampaignHTML" + "
";
}
function getPhaseWidth(phase, programDuration) {
var phaseDateRange = new A.AcxDateRange(
phase.startDate, phase.endDate);
var rationalValue = phaseDateRange.duration/programDuration;
return (0|100 * rationalValue) + "%";
}
function getOrCreateProgram(config) {
return programProxies[config.id] ||
(programProxies[config.id] = new ProgramProxy(config.id, config));
}
function removeProgram(id) {
if(!programs[id]) {
throw new ReferenceError("Program: "
+ id
+ " does not exist." );
}
delete programs[id];
delete programProxies[id];
}
return {
getOrCreate: getOrCreateProgram,
remove: removeProgram
};
}(); </source>