" ) ;
} ) ;
// get the VASL piece info
$.getJSON( gGetVaslPieceInfoUrl, function(data) {
gVaslPieceInfo = data ;
update_page_load_status( "vasl-piece-info" ) ;
} ).fail( function( xhr, status, errorMsg ) {
showErrorMsg( "Can't get the VASL piece info:
" + escapeHTML(errorMsg) + "
" ) ;
} ) ;
// get the template pack
$.getJSON( gGetTemplatePackUrl, function(data) {
if ( "error" in data )
showErrorMsg( "Can't get the template pack:
" + escapeHTML(data.error) + "
" ) ;
else {
if ( "_path_" in data ) {
showInfoMsg( "Auto-loaded template pack:
" + escapeHTML(data._path_) + "
" ) ;
delete data._path_ ;
}
}
gDefaultTemplatePack = $.extend( true, {}, data ) ;
install_template_pack( data ) ;
// NOTE: If we are loading a user-defined template pack, then what we think
// is the set of valid template ID's will depend on what's in it :-/
gValidTemplateIds = Object.keys( data.templates ) ;
update_page_load_status( "template-pack" ) ;
} ).fail( function( xhr, status, errorMsg ) {
showErrorMsg( "Can't get the template pack:
" + escapeHTML(errorMsg) + "
" ) ;
} ) ;
// fixup the layout
var prevHeight = [] ;
$(window).resize( function() {
// FUDGE! CSS grids don't seem to update their layout vertically when
// inside a jQuery tab control - we do it manually :-/
$(".ui-tabs-panel").each( function() {
$(this).css( "padding", "5px" ) ; // FUDGE! doesn't work when set in the CSS :-/
var id = $(this).attr( "id" ) ;
var h = $(this).parent().innerHeight() - navHeight - 20 ;
if ( h !== prevHeight[id] )
{
$(this).css( "height", h+"px" ) ;
prevHeight[id] = h ;
}
} ) ;
// FUDGE! Some panels are rendering with the wrong width in IE :-/
if ( isIE() ) {
var set_width = function($elem) { $elem.width( $elem.parent().width() ) ; } ;
set_width( $("#panel-vc textarea") ) ;
set_width( $("#panel-ssr .content") ) ;
}
} ) ;
$(window).trigger( "resize" ) ;
// replace all the "generate" buttons with "generate/edit" button/droplist's
$("button.generate").each( function() { init_snippet_button( $(this) ) ; } ) ;
// handle requests to edit the templates
$("button.edit-template").click( function() {
edit_template( $(this).data( "id" ) ) ;
} ).html( "
Edit
" )
.attr( "title", "Edit the template." )
.button( {} ) ;
// watch for changes to the scenario details
$("input[name='SCENARIO_NAME']").on( "input propertychange paste", update_scenario_status ) ;
$("input[name='SCENARIO_ID']").on( "input propertychange paste", update_scenario_status ) ;
// NOTE: The following is to add/remove the "scenario modified" indicator. It's pretty inefficent
// to do this using a timer, but we would otherwise have to attach a "on change" event handler
// to every single input field, simple note, etc., which would be far more complicated and error-prone.
if ( ! getUrlParam( "disable-dirty-scenario-check" ) )
setInterval( update_scenario_status, 1*1000 ) ;
// adjust the layout on resize
$(window).resize( function() {
// update the max height of sortable2 entries
var tab_id = $("#tabs .ui-tabs-tab.ui-state-active").attr( "aria-controls" ) ;
$( "#"+tab_id ).find( ".sortable" ).each( function() {
$(this).sortable2( "adjust-entry-heights" ) ;
} ) ;
} ) ;
// initialize hotkeys
init_hotkeys() ;
// check for a dirty scenario before leaving the page
if ( ! getUrlParam( "disable_close_window_check" ) ) {
window.addEventListener( "beforeunload", function(evt) {
// NOTE: We don't check for this if we're running inside the desktop app, since it will intercept the click
// and open the page in a new external browser window (see AppWebPage.acceptNavigationRequest()).
if ( !gWebChannelHandler && is_scenario_dirty() ) {
evt.returnValue = "This scenario has been changed. Do you want to leave the page, and lose your changes?" ;
return evt.returnValue ;
}
} ) ;
}
// prevent files from being dragged in
// NOTE: It would be nice to stop the cursor from changing, but there doesn't seem to be any way of doing that :-/
// In particualar, the dragstart events doesn't fire if something is being dragged into the browser from outside.
$(document).on( { dragenter: stopEvent, dragleave: stopEvent, dragover: stopEvent, drop: stopEvent } ) ;
// figure out how many pixels an em is
var $em = $( "M" ) ;
$("body").append( $em ) ;
gEmSize = $em.width() ;
$em.remove() ;
// add some dummy links for the test suite to edit templates
if ( getUrlParam( "edit_template_links" ) ) {
$("button.generate").each( function() {
var template_id = $(this).attr( "data-id" ) ;
if ( template_id.substring(0,9) === "ob_setup_" )
template_id = "ob_setup" ;
else if ( template_id.substring(0,21) === "ob_vehicles_ma_notes_" )
template_id = "ob_vehicles_ma_notes" ;
else if ( template_id.substring(0,21) === "ob_ordnance_ma_notes_" )
template_id = "ob_ordnance_ma_notes" ;
else if ( template_id.substring(0,12) === "ob_vehicles_" )
template_id = "ob_vehicles" ;
else if ( template_id.substring(0,12) === "ob_ordnance_" )
template_id = "ob_ordnance" ;
else if ( template_id.substring(0,9) === "nat_caps_" )
template_id = "nat_caps" ;
$( ""
).appendTo( "body" ) ;
} ) ;
}
// flag that we've finished initialization
update_page_load_status( "main" ) ;
$("input[name='SCENARIO_NAME']").focus().focus() ;
} ) ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function init_snippet_button( $btn )
{
// figure out what template we're dealing with
var template_id = $btn.attr( "data-id" ) ;
var template_id2 ;
if ( template_id.substring(0,9) === "ob_setup_" )
template_id2 = "ob_setup" ;
else if ( template_id.substring(0,21) === "ob_vehicles_ma_notes_" )
template_id2 = "ob_vehicles_ma_notes" ;
else if ( template_id.substring(0,21) === "ob_ordnance_ma_notes_" )
template_id2 = "ob_ordnance_ma_notes" ;
else if ( template_id.substring(0,12) === "ob_vehicles_" )
template_id2 = "ob_vehicles" ;
else if ( template_id.substring(0,12) === "ob_ordnance_" )
template_id2 = "ob_ordnance" ;
else if ( template_id.substring(0,9) === "nat_caps_" )
template_id2 = "nat_caps" ;
else
template_id2 = template_id ;
// create the new button
var buf = [ "
",
$btn.prop( "outerHTML" ),
"",
"
"
] ;
var $newBtn = $( buf.join("") ) ;
var fname="snippet.png", style="" ;
if ( template_id.substring( 0, 9 ) === "nat_caps_" ) {
fname = "nat-caps.png" ;
style = "height:15px;margin-right:0;" ;
}
$newBtn.find( "button" )
.prepend( $( "" ) )
.click( function( evt ) {
generate_snippet( $(this), evt, null ) ;
return false ;
} )
.attr( "title", GENERATE_SNIPPET_HINT ) ;
// add in the droplist
$newBtn.controlgroup() ;
$newBtn.children( "select" ).each( function() {
$(this).selectmenu( {
classes: {
"ui-selectmenu-button": "ui-button-icon-only",
"ui-selectmenu-menu": "snippet-control-menu-item",
},
} ) ;
} ) ;
$newBtn.children( ".ui-button-icon-only" ).css( "width", "1em" ) ;
$newBtn.children( ".ui-selectmenu-button" ).click( function() { $btn.blur() ; } ) ;
// handle requests to edit the template
$newBtn.children( "select" ).on( "selectmenuselect", function() {
edit_template( $(this).attr("data-id") ) ;
} ) ;
// replace the existing button with the new replacement button
$btn.replaceWith( $newBtn ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
gPageLoadStatus = [
"main", "app-config",
"vehicle-listings", "ordnance-listings", "reset-scenario",
"vehicle-notes", "ordnance-notes",
"vasl-piece-info", "template-pack", "default-scenario"
] ;
function update_page_load_status( id )
{
// track the page load progress
var pos = gPageLoadStatus.indexOf( id ) ;
if ( pos === -1 ) {
if ( id !== "default-scenario" )
console.log( "Multiple page-load status:", id ) ;
return ;
}
gPageLoadStatus.splice( pos, 1 ) ;
if ( id === "template-pack" )
$("fieldset[name='scenario']").fadeIn( 2*1000 ) ;
// check if the vehicle/ordnance listings have finished loading
if ( gPageLoadStatus.indexOf( "vehicle-listings" ) === -1 && gPageLoadStatus.indexOf( "ordnance-listings" ) === -1 ) {
// NOTE: If the default scenario contains any vehicles or ordnance, it will look up the V/O listings,
// so we need to wait until those have arrived. Note that while the default scenario will normally
// be empty, having stuff in it is very useful during development.
if ( gPageLoadStatus.indexOf( "reset-scenario" ) !== -1 ) {
do_on_new_scenario( false ) ;
update_page_load_status( "reset-scenario" ) ;
}
}
function show_startup_msgs( msgs, msg_type ) {
if ( msg_type in msgs ) {
for ( var i=0 ; i < msgs[msg_type].length ; ++i )
doShowNotificationMsg( msg_type, msgs[msg_type][i] ) ;
}
}
// check if the page has finished loading
if ( gPageLoadStatus.length === 0 ) {
// yup - update the UI
apply_user_settings() ;
$( "a[href='#tabs-extras'] div" ).html(
" Extras"
) ;
$("#tabs").tabs({ disabled: [] }) ;
$("#loader").fadeOut( 500 ) ;
adjust_footer_vspacers() ;
// position the PLAYERS snippet button
// FUDGE! Just setting the button's "left" attribute works, except in the Windows desktop app :-/
// I think there's a weird timing error wrt wrapping it as a snippet button, but positioning it
// via CSS seems to work everywhere :-/
var $btn = $( ".snippet-control[data-id='players']" ) ;
var $sel = $( ".select2[name='PLAYER_2_SAN']" ) ;
var newLeft = $sel.offset().left + $sel.outerWidth() - $btn.outerWidth() ;
newLeft -= 5 ; // nb: for the page margin
$btn.parent().height( $btn.parent().height() ) ; // nb: this forces a redraw
$btn.css( { position: "absolute", left: newLeft, top: $btn.position().top+2 } ) ;
// NOTE: The watermark image appears briefly in IE when reloading the page, but not even
// creating the watermark dynamically and removing it when the page unloads fixes it :-(
$("#watermark").fadeIn( 5*1000 ) ;
// notify the test suite
$("body").append( $("") ) ;
// notify the PyQT desktop application
if ( gWebChannelHandler )
gWebChannelHandler.on_app_loaded() ;
// show any startup messages
$.get( gGetStartupMsgsUrl, function( resp ) {
$("body").append( $("") ) ;
show_startup_msgs( resp, "error" ) ;
show_startup_msgs( resp, "warning" ) ;
show_startup_msgs( resp, "info" ) ;
} ).fail( function( xhr, status, errorMsg ) {
showErrorMsg( "Can't get the startup messages:
" + escapeHTML(errorMsg) + "
" ) ;
} ) ;
// preload the flag images (so that the player droplist renders immediately)
for ( var nat in gTemplatePack.nationalities ) {
$("body").append( $(
""
) ) ;
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function init_hotkeys()
{
// initialize hotkeys
jQuery.hotkeys.options.filterInputAcceptingElements = false ;
jQuery.hotkeys.options.filterContentEditable = false ;
jQuery.hotkeys.options.filterTextInputs = false ;
function set_focus_to( tab, $ctrl ) {
if ( $(".ui-widget-overlay").length > 0 )
return ; // nb: a dialog is up
if ( $( ".select2-dropdown" ).length > 0 ) {
// FUDGE! A select2 dropdown is showing, but which one? We just close them all :-/
$( ".app-select2" ).each( function() {
$(this).select2( "close" ) ;
} ) ;
}
var curr_tab = $("#tabs .ui-tabs-active a").attr( "href" ) ;
if ( curr_tab !== tab )
$("#tabs .ui-tabs-nav a[href='"+tab+"']").trigger( "click" ) ;
if ( $ctrl )
$ctrl.focus() ;
}
$(document).bind( "keydown", "alt+c", function() {
set_focus_to( "#tabs-scenario", $("input[name='SCENARIO_NAME']") ) ;
} ) ;
$(document).bind( "keydown", "alt+p", function() {
set_focus_to( "#tabs-scenario", $("select[name='PLAYER_1']") ) ;
} ) ;
$(document).bind( "keydown", "alt+y", function() {
set_focus_to( "#tabs-scenario", $("textarea[name='VICTORY_CONDITIONS']") ) ;
} ) ;
$(document).bind( "keydown", "alt+0", function() {
set_focus_to( "#tabs-scenario", $("input[name='SCENARIO_NAME']") ) ; // nb: for consistency with Alt-1 and Alt-2
} ) ;
$(document).bind( "keydown", "alt+1", function() {
set_focus_to( "#tabs-ob1", $("textarea[name='OB_SETUP_1']") ) ;
} ) ;
$(document).bind( "keydown", "alt+2", function() {
set_focus_to( "#tabs-ob2", $("textarea[name='OB_SETUP_2']") ) ;
} ) ;
$(document).bind( "keydown", "alt+x", function() {
set_focus_to( "#tabs-extras" ) ;
} ) ;
}
// --------------------------------------------------------------------
function install_template_pack( data )
{
// install the template pack
gTemplatePack = data ;
init_extras() ;
// update the player droplists
var nats = get_sorted_nats() ;
var curSel = {
1: $("select[name='PLAYER_1']").val(),
2: $("select[name='PLAYER_2']").val()
} ;
var buf = [] ;
for ( var i=0 ; i < nats.length ; ++i )
buf.push( "" ) ;
buf = buf.join( "" ) ;
for ( var player_no=1 ; player_no <= 2 ; ++player_no ) {
var $sel = $( "select[name='PLAYER_" + player_no + "']" ) ;
$sel.html( buf ) ;
if ( curSel[player_no] )
$sel.val( curSel[player_no] ) ; // nb: we don't trigger a "change" event
}
// update the OB tab headers
// NOTE: We don't do this while the page is initially loading, it will be done when the default scenario loaded.
if ( gPageLoadStatus.indexOf( "template-pack" ) === -1 ) {
update_ob_tab_header( 1 ) ;
update_ob_tab_header( 2 ) ;
}
// update the snippet buttons
function update_button( $btn ) {
var template_id = $btn.attr( "data-id" ) ;
if ( template_id.substr( 0, 7 ) === "extras/" )
return ;
if ( template_id.match( /^ob_(vehicles|ordnance).*_[12]$/ ) )
template_id = template_id.substring( 0, template_id.length-2 ) ;
var enable = is_template_available( template_id ) ;
if ( $btn.parent().hasClass( "snippet-control" ) )
$btn.parent().controlgroup( enable ? "enable" : "disable" ) ;
else
$btn.button( enable ? "enable": "disable" ) ;
}
$( "button.generate" ).each( function() { update_button( $(this) ) ; } ) ;
$( "button.edit-template" ).each( function() { update_button( $(this) ) ; } ) ;
}
// --------------------------------------------------------------------
function on_player_change_with_confirm( player_no )
{
// check if we need to do anything
var $select = $( "select[name='PLAYER_" + player_no + "']" ) ;
if ( $select.val() == $select.data("prev-val") )
return ;
// check if we should confirm this operation
if ( is_player_ob_empty( player_no ) ) {
// nope - just do it
on_player_change( player_no ) ;
} else {
// yup - make it so
ask( "Change player nationality",
"
Do you want to change this player's nationality?
You will lose changes made to their OB.", {
ok: function() { on_player_change( player_no ) ; },
cancel: function() {
$select.val( $select.data("prev-val") ).trigger( "change" ) ;
},
} ) ;
}
}
function is_player_ob_empty( player_no )
{
// check if the specified player's OB is empty
var is_empty = true ;
$( "#tabs-ob" + player_no + " .sortable" ).each( function() {
if ( $(this).children( "li" ).length > 0 )
is_empty = false ;
} ) ;
return is_empty ;
}
function on_player_change( player_no )
{
// update the tab label
var player_nat = update_ob_tab_header( player_no ) ;
// show/hide the nationality-specific buttons
update_nationality_specific_buttons( player_no ) ;
// show/hide the vehicle/ordnance multi-applicable notes controls
function update_ma_notes_controls( vo_type ) {
var show = ( gVehicleOrdnanceNotes[vo_type] && gVehicleOrdnanceNotes[vo_type][player_nat] ) ||
["allied-minor","axis-minor"].indexOf( gTemplatePack.nationalities[ player_nat ].type ) !== -1 ||
player_nat === "free-french" ;
var $fieldset = $( "#tabs-ob" + player_no + " fieldset[name='ob_" + vo_type + "_" + player_no ) ;
$fieldset.find( ".snippets-notes" ).css( "display", show?"block":"none" ) ;
$fieldset.find( "label[for='ob']" ).css( "display", show?"inline-block":"none" ) ;
}
update_ma_notes_controls( "vehicles" ) ;
update_ma_notes_controls( "ordnance" ) ;
// TO DO: We should also show a button that lets the ob_vehicle/ordnance_note template to be edited.
// reset the OB params
$( "#ob_setups-sortable_" + player_no ).sortable2( "delete-all" ) ;
$("input[name='OB_SETUP_WIDTH_"+player_no+"']").val( "" ) ;
$( "#ob_notes-sortable_" + player_no ).sortable2( "delete-all" ) ;
$( "#ob_vehicles-sortable_" + player_no ).sortable2( "delete-all" ) ;
$("input[name='OB_VEHICLES_WIDTH_"+player_no+"']").val( "" ) ;
$( "#ob_ordnance-sortable_" + player_no ).sortable2( "delete-all" ) ;
$("input[name='OB_ORDNANCE_WIDTH_"+player_no+"']").val( "" ) ;
// enable/disable the "add vehicle/ordnance" buttons
function update_add_vo_button( vo_type ) {
$( "#ob_"+vo_type+"-add_"+player_no ).button(
gVehicleOrdnanceListings[vo_type][player_nat] ? "enable": "disable"
) ;
}
update_add_vo_button( "vehicles" ) ;
update_add_vo_button( "ordnance" ) ;
// update the ROAR info panel
set_roar_scenario( $("input[name='ROAR_ID']").val() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function make_oba_info_tooltip()
{
// initialize
var buf = [ "
" ] ;
buf.push( "
", "
Off-Board Artillery" ) ;
// initialize
var params = {
SCENARIO_THEATER: $( "select.param[name='SCENARIO_THEATER']" ).val()
} ;
var scenario_date = get_scenario_date() ;
if ( scenario_date ) {
params.SCENARIO_MONTH = 1 + scenario_date.getMonth() ;
params.SCENARIO_YEAR = scenario_date.getFullYear() ;
}
// add the OBA info for each player
for ( var player_no=1 ; player_no <= 2 ; ++player_no ) {
buf.push( "