Allow flags to be included in snippets.

master
Pacman Ghost 6 years ago
parent 4192bf9654
commit 7a1b7735ce
  1. 2
      vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2
  2. 2
      vasl_templates/webapp/data/default-template-pack/ob_setup.j2
  3. 2
      vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2
  4. 10
      vasl_templates/webapp/snippets.py
  5. 8
      vasl_templates/webapp/static/snippets.js
  6. 7
      vasl_templates/webapp/static/user_settings.js
  7. 3
      vasl_templates/webapp/templates/user-settings-dialog.html
  8. 61
      vasl_templates/webapp/tests/test_user_settings.py

@ -19,7 +19,7 @@ sup { font-size: 75% ; }
padding: 2px 5px ;
font-weight: bold ;
">
{{PLAYER_NAME}} Ordnance
{%if PLAYER_FLAG%}<img src="{{PLAYER_FLAG}}" height=15 width=15>&nbsp;{%endif%}{{PLAYER_NAME}} Ordnance
{%for ord in OB_ORDNANCE%}
<tr style="border-bottom:1px dotted #e0e0e0;">

@ -10,7 +10,7 @@
font-weight: bold ;
{%if OB_SETUP_WIDTH%} width: {{OB_SETUP_WIDTH}} ; {%endif%}
">
{{OB_SETUP}}
{%if PLAYER_FLAG%}<img src="{{PLAYER_FLAG}}" height=15 width=15>&nbsp;{%endif%}{{OB_SETUP}}
</table>

@ -19,7 +19,7 @@ sup { font-size: 75% ; }
padding: 2px 5px ;
font-weight: bold ;
">
{{PLAYER_NAME}} Vehicles
{%if PLAYER_FLAG%}<img src="{{PLAYER_FLAG}}" height=15 width=15>&nbsp;{%endif%}{{PLAYER_NAME}} Vehicles
{%for veh in OB_VEHICLES%}
<tr style="border-bottom:1px dotted #e0e0e0;">

@ -2,6 +2,7 @@
import os
import json
import re
import zipfile
from flask import jsonify, abort
@ -85,3 +86,12 @@ def _do_get_template_pack( dname ):
if words[1] == ".j2":
templates[words[0]] = fp.read()
return nationalities, templates
# ---------------------------------------------------------------------
@app.route( "/flags/<nat>" )
def get_flag( nat ):
"""Get a flag image."""
if not re.search( "^[-a-z]+$", nat ):
abort( 404 )
return app.send_static_file( "images/flags/{}.png".format( nat ) )

@ -27,6 +27,10 @@ function generate_snippet( $btn, extra_params )
var params = unload_snippet_params( true, template_id ) ;
// set player-specific parameters
function make_player_flag_url( player_no ) {
var player_nat = get_player_nat( player_no ) ;
return APP_URL_BASE + "/flags/" + player_nat ;
}
var curr_tab = $("#tabs .ui-tabs-active a").attr( "href" ) ;
var colors ;
if ( curr_tab === "#tabs-ob1" ) {
@ -34,11 +38,15 @@ function generate_snippet( $btn, extra_params )
colors = get_player_colors( 1 ) ;
params.OB_COLOR = colors[0] ;
params.OB_COLOR_2 = colors[2] ;
if ( gUserSettings["include-flags-in-snippets"] )
params.PLAYER_FLAG = make_player_flag_url( 1 ) ;
} else if ( curr_tab === "#tabs-ob2" ) {
params.PLAYER_NAME = get_nationality_display_name( params.PLAYER_2 ) ;
colors = get_player_colors( 2 ) ;
params.OB_COLOR = colors[0] ;
params.OB_COLOR_2 = colors[2] ;
if ( gUserSettings["include-flags-in-snippets"] )
params.PLAYER_FLAG = make_player_flag_url( 2 ) ;
}
// set player-specific parameters

@ -2,6 +2,7 @@ gUserSettings = Cookies.getJSON( "user-settings" ) || {} ;
USER_SETTINGS = {
"include-vasl-images-in-snippets": "checkbox",
"include-flags-in-snippets": "checkbox",
} ;
// --------------------------------------------------------------------
@ -36,9 +37,10 @@ function user_settings()
function update_ui() {
// update the UI
var $dlg = $( ".ui-dialog.user-settings" ) ;
var is_checked = $dlg.find( "input[name='include-vasl-images-in-snippets']" ).prop( "checked" ) ;
var is_server = $dlg.find( "input[name='include-vasl-images-in-snippets']" ).prop( "checked" ) ||
$dlg.find( "input[name='include-flags-in-snippets']" ).prop( "checked" ) ;
$dlg.find( ".include-vasl-images-in-snippets-hint" ).css(
"color", is_checked ? "#444" : "#aaa"
"color", is_server ? "#444" : "#aaa"
) ;
}
@ -53,6 +55,7 @@ function user_settings()
create: function() {
init_dialog( $(this), "OK", false ) ;
$(this).find( "input[name='include-vasl-images-in-snippets']" ).change( update_ui ) ;
$(this).find( "input[name='include-flags-in-snippets']" ).change( update_ui ) ;
},
open: function() {
// load the current user settings

@ -1,4 +1,5 @@
<div id="user-settings" style="display:none;">
<input type="checkbox" name="include-vasl-images-in-snippets">&nbsp;Include VASL images in snippets
<input type="checkbox" name="include-vasl-images-in-snippets">&nbsp;Include VASL images in snippets <br>
<input type="checkbox" name="include-flags-in-snippets">&nbsp;Include flags in snippets
<div class="note include-vasl-images-in-snippets-hint" style="margin-left:20px;">This program must be running before you load the scenario in VASL.</div>
</div>

@ -4,7 +4,8 @@ import json
from vasl_templates.webapp.tests.utils import \
init_webapp, find_child, _get_clipboard, \
wait_for, select_menu_option, click_dialog_button
wait_for, wait_for_clipboard, select_tab, select_menu_option, click_dialog_button, \
add_simple_note
from vasl_templates.webapp.tests.test_vehicles_ordnance import add_vo
from vasl_templates.webapp.config.constants import DATA_DIR as REAL_DATA_DIR
@ -31,7 +32,7 @@ def test_include_vasl_images_in_snippets( webapp, webdriver, monkeypatch ):
# make sure that it took effect
snippet_btn = find_child( "button[data-id='ob_vehicles_1']" )
snippet_btn.click()
wait_for( 2, lambda: "/counter/2524/front" in _get_clipboard() )
wait_for_clipboard( 2, "/counter/2524/front", contains=True )
# disable "show VASL images in snippets"
select_menu_option( "user_settings" )
@ -47,10 +48,64 @@ def test_include_vasl_images_in_snippets( webapp, webdriver, monkeypatch ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def test_include_flags_in_snippets( webapp, webdriver, monkeypatch ):
"""Test the user settings."""
# initialize
monkeypatch.setitem( webapp.config, "DATA_DIR", REAL_DATA_DIR )
init_webapp( webapp, webdriver )
# prepare the scenario
select_tab( "ob1" )
sortable = find_child( "#ob_setups-sortable_1" )
add_simple_note( sortable, "OB setup note", None )
# enable "show flags in snippets"
select_menu_option( "user_settings" )
elem = find_child( ".ui-dialog.user-settings input[name='include-flags-in-snippets']" )
assert not elem.is_selected()
elem.click()
click_dialog_button( "OK" )
_check_cookies( webdriver, "include-flags-in-snippets", True )
# make sure that it took effect
ob_setup_snippet_btn = find_child( "li img.snippet", sortable )
ob_setup_snippet_btn.click()
wait_for_clipboard( 2, "/flags/german", contains=True )
# make sure it also affects vehicle/ordnance snippets
ob_vehicles_snippet_btn = find_child( "button.generate[data-id='ob_vehicles_1']" )
ob_vehicles_snippet_btn.click()
wait_for_clipboard( 2, "/flags/german", contains=True )
ob_ordnance_snippet_btn = find_child( "button.generate[data-id='ob_ordnance_1']" )
ob_ordnance_snippet_btn.click()
wait_for_clipboard( 2, "/flags/german", contains=True )
# disable "show flags in snippets"
select_menu_option( "user_settings" )
elem = find_child( ".ui-dialog.user-settings input[name='include-flags-in-snippets']" )
assert elem.is_selected()
elem.click()
click_dialog_button( "OK" )
_check_cookies( webdriver, "include-flags-in-snippets", False )
# make sure that it took effect
ob_setup_snippet_btn.click()
wait_for( 2, lambda: "/flags/german" not in _get_clipboard() )
# make sure it also affects vehicle/ordnance snippets
ob_vehicles_snippet_btn.click()
wait_for( 2, lambda: "/flags/german" not in _get_clipboard() )
ob_ordnance_snippet_btn.click()
wait_for( 2, lambda: "/flags/german" not in _get_clipboard() )
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def _check_cookies( webdriver, name, expected ):
"""Check that a user setting was stored in the cookies correctly."""
cookies = [ c for c in webdriver.get_cookies() if c["name"] == "user-settings" ]
assert len(cookies) == 1
val = cookies[0]["value"].replace( "%22", '"' )
val = cookies[0]["value"].replace( "%22", '"' ).replace( "%2C", "," )
print( val )
user_settings = json.loads( val )
assert user_settings[name] == expected

Loading…
Cancel
Save