Confirm if the user wants to change a player's nationality (and lose OB changes).

master
Pacman Ghost 6 years ago
parent 7957d6603f
commit 742cef4cc9
  1. 40
      vasl_templates/webapp/static/main.js
  2. 4
      vasl_templates/webapp/static/simple_notes.js
  3. 6
      vasl_templates/webapp/static/snippets.js
  4. 10
      vasl_templates/webapp/static/utils.js
  5. 2
      vasl_templates/webapp/static/vo.js
  6. 14
      vasl_templates/webapp/tests/test_dirty_scenario_checks.py
  7. 72
      vasl_templates/webapp/tests/test_players.py
  8. 3
      vasl_templates/webapp/tests/utils.py

@ -158,11 +158,13 @@ $(document).ready( function () {
// add player change handlers
$("select[name='PLAYER_1']").selectmenu( {
width: "7em",
select: function() { on_player_change( $(this) ) ; },
open: function() { $(this).data("prev-val",$(this).val()) ; },
select: function() { on_player_change_with_confirm( 1 ) ; },
} ) ;
$("select[name='PLAYER_2']").selectmenu( {
width: "7em",
select: function() { on_player_change( $(this) ) ; },
open: function() { $(this).data("prev-val",$(this).val()) ; },
select: function() { on_player_change_with_confirm( 2 ) ; },
} ) ;
// load the ELR's and SAN's
@ -429,14 +431,36 @@ function install_template_pack( data )
// --------------------------------------------------------------------
function on_player_change( $select )
function on_player_change_with_confirm( player_no )
{
// FIXME! We should really ask the user to confirm this operation.
// figure out which player was changed
var name = $select.attr( "name" ) ;
var player_no = name.substring( name.length-1 ) ;
// 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
var is_empty = true ;
$( "#tabs-ob" + player_no + " .sortable" ).each( function() {
if ( $(this).children( "li" ).length > 0 )
is_empty = false ;
} ) ;
if ( is_empty ) {
// nope - just do it
on_player_change( player_no ) ;
} else {
// yup - make it so
ask( "Change player nationality",
"<p>Do you want to change this player's nationality?<p>You will lose changes made to their OB.", {
ok: function() { on_player_change( player_no ) ; },
cancel: function() {
$select.val( $select.data("prev-val") ).selectmenu( "refresh" ) ;
},
} ) ;
}
}
function on_player_change( player_no )
{
// update the tab label
var player_nat = update_ob_tab_header( player_no ) ;

@ -39,7 +39,7 @@ function _do_edit_simple_note( $sortable2, $entry, default_width )
open: function() {
// initialize
$caption = $(this).children( "textarea" ) ;
var $btn_pane = $(".ui-dialog-buttonpane") ;
var $btn_pane = $(".ui-dialog.edit-simple_note .ui-dialog-buttonpane") ;
$width = $btn_pane.children( "input[name='width']" ) ;
if ( $width.length === 0 ) {
// create the width controls
@ -54,7 +54,7 @@ function _do_edit_simple_note( $sortable2, $entry, default_width )
var colors = get_player_colors_for_element( $sortable2 ) ;
if ( ! colors )
colors = [ "d0d0d0", "ca0a0a0" ] ;
$(".ui-dialog-titlebar").css( {
$(".ui-dialog.edit-simple_note .ui-dialog-titlebar").css( {
background: "#"+colors[0],
border: "1px solid #"+colors[1]
} ) ;

@ -520,11 +520,11 @@ function do_load_scenario_data( params )
// FUDGE! We must set the players first, since changing these will reset the OB tabs.
if ( "PLAYER_1" in params ) {
set_param( $("select[name='PLAYER_1']"), "PLAYER_1" ) ;
on_player_change( $("select[name='PLAYER_1']") ) ;
on_player_change( 1 ) ;
}
if ( "PLAYER_2" in params ) {
set_param( $("select[name='PLAYER_2']"), "PLAYER_2" ) ;
on_player_change( $("select[name='PLAYER_2']") ) ;
on_player_change( 2 ) ;
}
var i ;
for ( var key in params ) {
@ -733,7 +733,7 @@ function reset_scenario()
// nb: there's no way to reset the player droplist's
var player_no ;
for ( player_no=1 ; player_no <= 2 ; ++player_no ) {
on_player_change( $( "select[name='PLAYER_" + player_no + "']" ) ) ;
on_player_change( player_no ) ;
$("select[name='PLAYER_" + player_no + "_ELR']").val( 0 ).selectmenu( "refresh" ) ;
$("select[name='PLAYER_" + player_no + "_SAN']").val( "" ).selectmenu( "refresh" ) ;
}

@ -130,9 +130,17 @@ function ask( title, msg, args )
$dlg.dialog( {
dialogClass: "ask",
modal: true,
closeOnEscape:false,
title: title,
create: function() {
// we handle ESCAPE ourself, to make it the same as clicking Cancel, not just closing the dialog
$(this).closest( ".ui-dialog" ).keydown( function( evt ) {
if ( evt.keyCode == $.ui.keyCode.ESCAPE )
$(".ui-dialog.ask button:contains(Cancel)").click() ;
} ) ;
},
open: function() {
$(".ui-dialog button:contains(Cancel)").focus();
$(".ui-dialog.ask button:contains(Cancel)").focus();
},
buttons: {
OK: function() {

@ -42,7 +42,7 @@ function add_vo( vo_type, player_no )
$("#select-vo select").filterByText( $("#select-vo input[type='text']") ) ;
// set the titlebar color
var colors = get_player_colors_for_element( $sortable2 ) ;
$(".ui-dialog-titlebar").css( {
$(".ui-dialog.select-vo .ui-dialog-titlebar").css( {
background: "#"+colors[0],
border: "1px solid #"+colors[1],
} ) ;

@ -10,7 +10,7 @@ from vasl_templates.webapp.tests.test_vehicles_ordnance import add_vo
from vasl_templates.webapp.tests.utils import \
init_webapp, select_tab, select_menu_option, add_simple_note, select_droplist_val, select_droplist_index, \
drag_sortable_entry_to_trash, get_sortable_entry_count, \
get_stored_msg, set_stored_msg_marker, find_child, find_children, wait_for
get_stored_msg, set_stored_msg_marker, find_child, wait_for, click_dialog_button
# ---------------------------------------------------------------------
@ -92,12 +92,6 @@ def test_dirty_scenario_checks( webapp, webdriver ):
else:
assert False
def cancel_confirmation():
"""Cancel the confirmation dialog."""
btns = find_children( ".ui-dialog-buttonset button" )
btn = next( b for b in btns if b.text == "Cancel" )
btn.click()
def do_test( tab_id, param ):
"""Test checking for a dirty scenario."""
@ -111,8 +105,8 @@ def test_dirty_scenario_checks( webapp, webdriver ):
elem = find_child( "#ask" )
assert "This scenario has been changed" in elem.text
# cancel the confirmation requiest, make sure the change we made is still there
cancel_confirmation()
# cancel the confirmation request, make sure the change we made is still there
click_dialog_button( "Cancel" )
select_tab( tab_id )
check_field( param, state )
@ -135,7 +129,7 @@ def test_dirty_scenario_checks( webapp, webdriver ):
assert "This scenario has been changed" in elem.text
# cancel the confirmation request, make sure the change we made is still there
cancel_confirmation()
click_dialog_button( "Cancel" )
select_tab( tab_id )
check_field( param, state )

@ -3,7 +3,8 @@
from selenium.webdriver.support.ui import Select
from vasl_templates.webapp.tests.utils import get_nationalities, select_tab, find_child, \
select_droplist_val, init_webapp, load_scenario_params, get_sortable_entry_count
select_droplist_val, init_webapp, load_scenario_params, \
wait_for, get_sortable_entry_count, click_dialog_button
# ---------------------------------------------------------------------
@ -29,52 +30,57 @@ def test_player_change( webapp, webdriver ):
expected = "{} OB".format( nationalities[player_id]["display_name"] )
assert ob_tabs[player_no].text.strip() == expected
# check that we can change the player nationalities without being asked to confirm
# nb: the frontend ignores the vehicle/ordnance snippet widths when deciding if to ask for confirmation
VO_WIDTHS = {
"ob1": { "VEHICLES_WIDTH_1": 123 },
"ob2": { "ORDNANCE_WIDTH_2": 456 },
}
load_scenario_params( VO_WIDTHS )
select_tab( "scenario" )
select_droplist_val( player_sel[1], "russian" )
assert ob_tabs[1].text.strip() == "{} OB".format( nationalities["russian"]["display_name"] )
select_droplist_val( player_sel[2], "german" )
assert ob_tabs[2].text.strip() == "{} OB".format( nationalities["german"]["display_name"] )
# load the OB tabs
SCENARIO_PARAMS = {
"ob1": {
"OB_SETUPS_1": [ { "caption": "an ob setup", "width": "" } ],
"OB_NOTES_1": [ { "caption": "an ob note", "width": "" } ],
"VEHICLES_1": [ "a german vehicle" ],
"VEHICLES_WIDTH_1": "101",
"ORDNANCE_1": [ "a german ordnance" ],
"ORDNANCE_WIDTH_1": "102",
},
"ob2": {
"OB_SETUPS_2": [ { "caption": "another ob setup", "width": "" } ],
"OB_NOTES_2": [ { "caption": "another ob note", "width": "" } ],
"VEHICLES_2": [ "a russian vehicle" ],
"VEHICLES_WIDTH_2": "201",
"ORDNANCE_2": [ "a russian ordnance" ],
"ORDNANCE_WIDTH_2": "202",
"VEHICLES_2": [ "a german vehicle" ],
},
}
load_scenario_params( SCENARIO_PARAMS )
def is_ob_tab_empty( player_no ):
"""Check if an OB tab is empty."""
select_tab( "ob{}".format( player_no ) )
def get_sortable_counts( player_no ):
"""Get the contents of the player's OB tab."""
sortables = [
find_child( "#{}-sortable_{}".format( key, player_no ) )
for key in ["ob_setups","ob_notes","vehicles","ordnance"]
]
if any( get_sortable_entry_count(s) > 0 for s in sortables ):
return False
widths = [
find_child( "input[name='{}_WIDTH_{}']".format( key, player_no ) )
for key in ["VEHICLES","ORDNANCE"]
]
if any( w.get_attribute("value") for w in widths ):
return False
return True
return [ get_sortable_entry_count(s) for s in sortables ]
# change player 1
select_tab( "scenario" )
select_droplist_val( player_sel[1], "finnish" )
assert ob_tabs[1].text.strip() == "{} OB".format( nationalities["finnish"]["display_name"] )
assert is_ob_tab_empty( 1 )
for player_no in [1,2]:
# change player 2
select_tab( "scenario" )
select_droplist_val( player_sel[2], "japanese" )
assert ob_tabs[2].text.strip() == "{} OB".format( nationalities["japanese"]["display_name"] )
assert is_ob_tab_empty( 2 )
# try to change the player's nationality
select_droplist_val( player_sel[player_no], "finnish" )
wait_for( 2, lambda: find_child("#ask") )
# cancel the confirmation request and make sure nothing changed
click_dialog_button( "Cancel" )
nat_id = "russian" if player_no == 1 else "german"
assert ob_tabs[player_no].text.strip() == "{} OB".format( nationalities[nat_id]["display_name"] )
assert get_sortable_counts( player_no ) == \
[1,0,0,0] if player_no == 1 else [0,0,1,0]
# try to change the player's nationality
select_droplist_val( player_sel[player_no], "finnish" )
wait_for( 2, lambda: find_child("#ask") )
# confirm the request and make sure the OB tab was cleared
click_dialog_button( "OK" )
assert ob_tabs[player_no].text.strip() == "{} OB".format( nationalities["finnish"]["display_name"] )
assert get_sortable_counts( player_no ) == [0,0,0,0]

@ -77,6 +77,9 @@ def for_each_template( func ): #pylint: disable=too-many-branches
for nat,template_ids in _NAT_TEMPLATES.items():
select_tab( "scenario" )
select_droplist_val( player1_sel, nat )
ask = find_child( "#ask" )
if ask and ask.is_displayed():
click_dialog_button( "OK" ) # nb: if the front-end is asking to confirm the player nationality change
select_tab( "ob1" )
for template_id in template_ids:
func( template_id, template_id )

Loading…
Cancel
Save