htmlBuilder = function() {
var agent_element_suffix = "_element";
var agent_dropdown_suffix = "_dropdown";
var agent_status_suffix = "_status";
var my_side;
var blue_force_target;
var village_sentiment_target;
var hover_cb;
var select_cb;
var level_indicators = {};
return {
initialize : function(side, blue_target, village_target,
option_hover, option_select) {
my_side = side;
blue_force_target = blue_target;
village_sentiment_target = village_target;
hover_cb = option_hover;
select_cb = option_select;
initialize_global_status_elements();
build_bomb_report();
setup_side_specific_elements();
},
changeSide : function(side) {
$("#side").removeClass(my_side);
$("#my_status").empty();
my_side = side;
setup_side_specific_elements();
},
updateStatus : function(agent_type, status) {
var agent = agents[agent_type];
var status_id = agent_type + agent_status_suffix;
var element_id = agent_type + agent_element_suffix;
$('#'+status_id).text(status);
if (agent.color_class) {
remove_functional_classes(element_id);
$('#'+element_id).addClass(agent.color_class);
}
if (agent.fuel) {
level_indicators[agent_type+"_fuel"].val(
agent.fuel_level);
}
},
setDropdown : function(agent_type, entries) {
var dropdown_id = agent_type + agent_dropdown_suffix;
/* remove old entries and renable dropdown menu */
$("#"+dropdown_id).empty();
$("#"+dropdown_id).prop("disabled", false);
/* This is for the buggy select element hack */
agents[agent_type].mousedown_count = 0;
/* Now add the entries. */
entries.forEach(function(entry, index, arr) {
var option_id = entry.value + '_' +
dropdown_id;
var option_html = '';
$(option_html).appendTo('#'+dropdown_id);
$('#'+option_id).hover(function(evt) {
hover_cb(agent_type,
entry.value,
(evt.type === "mouseenter" ?
true : false));
});
$('#'+option_id).click(function(evt) {
select_cb(agent_type);
});
});
},
getDropdownValue : function(agent_type) {
var dropdown_id = agent_type + agent_dropdown_suffix;
return $('#'+dropdown_id).val();
},
dropdownDisable : function(agent_type) {
var dropdown_id = agent_type + agent_dropdown_suffix;
$("#"+dropdown_id).focusout();
$("#"+dropdown_id).prop("disabled", true);
},
setBombCount : function(location_name, count) {
$('#'+location_name+'_bomb_count').text(String(count));
},
setVillageSentiment : function(village, sentiment) {
level_indicators[village + "_sentiment"].val(sentiment);
},
setBlueForceStrength : function(strength) {
level_indicators.bf_strength.val(strength);
}
}
function initialize_global_status_elements() {
villages.forEach(function(village) {
var id = village + "_sentiment";
var text = locations[village].display_name +
" Sentiment";
level_indicators[id] = new PercentRangeIndicator(
id, text, 0, village_sentiment_target);
} );
level_indicators.bf_strength = new PercentRangeIndicator(
"bf_strength", "Blue Force Strength",
blue_force_target, 100, true);
}
function build_bomb_report() {
var location_name;
var bomb_html;
var height;
/* table with all of the bombable locations */
for (location_name in bombs) {
bomb_html = '
' +
'
' +
locations[location_name].display_name +
'
0' +
'
';
$(bomb_html).appendTo("#bomb_report");
}
/*
* Now set the height of the intelligence section
* to match the bomb report. In order for the
* intelligence section to have a scroll bar,
* the height must be fixed.
*/
height = $("#bomb_report")[0].clientHeight;
$("#intelligence_data").css("height", String(height) + "px");
}
function setup_side_specific_elements() {
/* You are... banner */
$("#side").addClass(my_side);
$("#player").text(sides[my_side].name);
/* agent icons, status, and dropdown menus */
sides[my_side].agents.forEach(function(agent_type, index, arr) {
var agent = agents[agent_type];
var element_id, element_html;
var dropdown_id, dropdown_html;
var fuel_id, fuel_html = '';
var status_id;
var img_html = '';
var agent_html;
agent = agents[agent_type];
/*
* this builds the basic cell which will contain
* the agent name, icon, and status (to be
* filled in later)
*/
element_id = agent_type + agent_element_suffix;
element_html = '
';
/*
* if this agent is moveable, turn this into
* table with two cells, one for the cell above,
* and one for the dropdown menu
*/
if (agent.moveable) {
dropdown_id = agent_type +
agent_dropdown_suffix;
dropdown_html = '
';
}
/* Now add element(s) to the page */
$(element_html).appendTo("#my_status");
/*
* Now fill in the agent name, icon and placeholder
* for status, and set the class for the element
*/
status_id = agent_type + agent_status_suffix;
if (agent.file !== null)
img_html = '';
agent_html = '' +
agent.display_name + ' ' +
img_html + ' ' +
'';
$('#'+element_id).html(agent_html);
/*
* Add indicator level for fuel level
*/
if (agent.fuel) {
level_indicators[fuel_id] =
new TextEmbeddedIndicator(fuel_id,
"Fuel Level", 0,
agent.fuel_capacity, true);
}
/*
* For moveable agents, put in hacks for browsers
* with buggy (or non-existant) implementation
* for select elements.
*/
if (agent.moveable)
set_dropdown_hacks(agent_type);
});
}
function select_html(dropdown_id) {
return '