// --------------------------------------------------------------------
function get_nationality_display_name( nat_id )
{
// get the nationality's display name
return gTemplatePack.nationalities[ nat_id ].display_name ;
}
function get_player_nat( player_no )
{
// get the player's nationality
if ( player_no === null )
return null ;
return $( "select[name='PLAYER_" + player_no + "']" ).val() ;
}
function get_sorted_nats()
{
// sort the nationalities by display name
var nats = Object.keys( gTemplatePack.nationalities ) ;
nats.sort( function( lhs, rhs ) {
lhs = gTemplatePack.nationalities[lhs].display_name.toUpperCase() ;
rhs = gTemplatePack.nationalities[rhs].display_name.toUpperCase() ;
if ( lhs < rhs )
return -1 ;
else if ( lhs > rhs )
return +1 ;
else
return 0 ;
} ) ;
return nats ;
}
function get_player_colors( player_no )
{
// get the colors for the specified player
var player_nat = get_player_nat( player_no ) ;
return gTemplatePack.nationalities[ player_nat ].ob_colors ;
}
function get_player_colors_for_element( $elem )
{
// get the player colors (if any) for the specified element
if ( $elem.attr( "id" ).substr( 0, 18 ) === "ob_notes-sortable_" )
return null ;
var player_no = get_player_no_for_element( $elem ) ;
if ( player_no === null )
return null ;
return get_player_colors( player_no ) ;
}
function make_player_flag_url( nat, for_snippet ) {
var flag = gTemplatePack.nationalities[nat].flag ;
if ( flag )
return flag ; // nb: custom flag, just use that
if ( for_snippet && gUserSettings["scenario-images-source"] == SCENARIO_IMAGES_SOURCE_INTERNET )
return gAppConfig.ONLINE_IMAGES_URL_BASE + "/flags/" + nat + ".png" ;
else
return APP_URL_BASE + "/flags/" + nat ;
}
function get_player_no_for_element( $elem )
{
// get the player that owns the specified element
if ( $.contains( $("#tabs-ob1")[0], $elem[0] ) )
return 1 ;
if ( $.contains( $("#tabs-ob2")[0], $elem[0] ) )
return 2 ;
return null ;
}
function get_scenario_date()
{
// get the scenario date
var scenario_date = $("input[name='SCENARIO_DATE']").datepicker( "getDate" ) ;
if ( ! scenario_date )
return null ;
scenario_date.setMinutes( scenario_date.getMinutes() - scenario_date.getTimezoneOffset() ) ;
return scenario_date ;
}
function is_template_available( template_id )
{
// check if the specified template is available
if ( template_id.match( /^ob_(vehicles|ordnance).*_[12]$/ ) )
template_id = template_id.substring( 0, template_id.length-2 ) ;
return gTemplatePack.templates[ template_id ] !== undefined ;
}
// --------------------------------------------------------------------
function copyToClipboard( val )
{
if ( getUrlParam( "store_clipboard" ) ) {
// store the value where the tests can retrieve it
$("#_clipboard_").text( val ) ;
return ;
}
// IE-specific code path to prevent textarea being shown while dialog is visible
if ( window.clipboardData && window.clipboardData.setData ) {
clipboardData.setData( "Text", val ) ;
return ;
}
if ( document.queryCommandSupported && document.queryCommandSupported("copy") ) {
// create a textarea to hold the content
var textarea = document.createElement( "textarea" ) ;
textarea.style.position = "fixed" ; // prevent scrolling to bottom in MS Edge
document.body.appendChild( textarea ) ;
textarea.textContent = val ;
// copy the textarea content to the clipboard
textarea.select() ;
try {
document.execCommand( "copy" ) ;
if ( getUrlParam("log-clipboard") )
console.log( "CLIPBOARD:", val ) ;
}
catch( ex ) {
showErrorMsg( "Can't copy to the clipboard:
" + escapeHTML(ex) + "
" ) ;
}
finally {
document.body.removeChild( textarea ) ;
}
}
}
// --------------------------------------------------------------------
// Connect a text box to a select box, and filter the available options.
( function( $ ) {
$.fn.filterByText = function( $textbox )
{
function compressSpaces( val ) { return val.replace( /\s/g, "" ).trim() ; }
return this.each( function() {
// initialize
var $select = $(this) ;
var $options = [] ;
$select.find( "option" ).each( function() {
$options.push( { value: $(this).val(), text: $(this).text() } ) ;
} ) ;
$select.data( "options", $options ) ;
$textbox.bind( "input", function() {
// prepare the value we will filter on
var val = $(this).val() ;
var adjustCase ;
if ( val !== val.toLowerCase() )
adjustCase = function(val) { return val ; } ; // nb: mixed-case => case-sensitive filtering
else
adjustCase = function(val) { return val.toLowerCase() ; } ;
val = compressSpaces( adjustCase( val ) ) ;
// filter the options
var $options = $select.empty().scrollTop(0).data( "options" ) ;
$.each( $options, function(i) {
var $opt = $options[i] ;
var optVal = compressSpaces( adjustCase( $opt.text ) ) ;
if ( optVal.indexOf( val ) !== -1 ) {
$select.append(
$("