parent
6dee1f6e36
commit
ec7e9f00b3
@ -0,0 +1,163 @@ |
||||
/*! |
||||
* JavaScript Cookie v2.2.0 |
||||
* https://github.com/js-cookie/js-cookie
|
||||
* |
||||
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack |
||||
* Released under the MIT license |
||||
*/ |
||||
;(function (factory) { |
||||
var registeredInModuleLoader; |
||||
if (typeof define === 'function' && define.amd) { |
||||
define(factory); |
||||
registeredInModuleLoader = true; |
||||
} |
||||
if (typeof exports === 'object') { |
||||
module.exports = factory(); |
||||
registeredInModuleLoader = true; |
||||
} |
||||
if (!registeredInModuleLoader) { |
||||
var OldCookies = window.Cookies; |
||||
var api = window.Cookies = factory(); |
||||
api.noConflict = function () { |
||||
window.Cookies = OldCookies; |
||||
return api; |
||||
}; |
||||
} |
||||
}(function () { |
||||
function extend () { |
||||
var i = 0; |
||||
var result = {}; |
||||
for (; i < arguments.length; i++) { |
||||
var attributes = arguments[ i ]; |
||||
for (var key in attributes) { |
||||
result[key] = attributes[key]; |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
function decode (s) { |
||||
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent); |
||||
} |
||||
|
||||
function init (converter) { |
||||
function api() {} |
||||
|
||||
function set (key, value, attributes) { |
||||
if (typeof document === 'undefined') { |
||||
return; |
||||
} |
||||
|
||||
attributes = extend({ |
||||
path: '/' |
||||
}, api.defaults, attributes); |
||||
|
||||
if (typeof attributes.expires === 'number') { |
||||
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5); |
||||
} |
||||
|
||||
// We're using "expires" because "max-age" is not supported by IE
|
||||
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : ''; |
||||
|
||||
try { |
||||
var result = JSON.stringify(value); |
||||
if (/^[\{\[]/.test(result)) { |
||||
value = result; |
||||
} |
||||
} catch (e) {} |
||||
|
||||
value = converter.write ? |
||||
converter.write(value, key) : |
||||
encodeURIComponent(String(value)) |
||||
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); |
||||
|
||||
key = encodeURIComponent(String(key)) |
||||
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent) |
||||
.replace(/[\(\)]/g, escape); |
||||
|
||||
var stringifiedAttributes = ''; |
||||
for (var attributeName in attributes) { |
||||
if (!attributes[attributeName]) { |
||||
continue; |
||||
} |
||||
stringifiedAttributes += '; ' + attributeName; |
||||
if (attributes[attributeName] === true) { |
||||
continue; |
||||
} |
||||
|
||||
// Considers RFC 6265 section 5.2:
|
||||
// ...
|
||||
// 3. If the remaining unparsed-attributes contains a %x3B (";")
|
||||
// character:
|
||||
// Consume the characters of the unparsed-attributes up to,
|
||||
// not including, the first %x3B (";") character.
|
||||
// ...
|
||||
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0]; |
||||
} |
||||
|
||||
return (document.cookie = key + '=' + value + stringifiedAttributes); |
||||
} |
||||
|
||||
function get (key, json) { |
||||
if (typeof document === 'undefined') { |
||||
return; |
||||
} |
||||
|
||||
var jar = {}; |
||||
// To prevent the for loop in the first place assign an empty array
|
||||
// in case there are no cookies at all.
|
||||
var cookies = document.cookie ? document.cookie.split('; ') : []; |
||||
var i = 0; |
||||
|
||||
for (; i < cookies.length; i++) { |
||||
var parts = cookies[i].split('='); |
||||
var cookie = parts.slice(1).join('='); |
||||
|
||||
if (!json && cookie.charAt(0) === '"') { |
||||
cookie = cookie.slice(1, -1); |
||||
} |
||||
|
||||
try { |
||||
var name = decode(parts[0]); |
||||
cookie = (converter.read || converter)(cookie, name) || |
||||
decode(cookie); |
||||
|
||||
if (json) { |
||||
try { |
||||
cookie = JSON.parse(cookie); |
||||
} catch (e) {} |
||||
} |
||||
|
||||
jar[name] = cookie; |
||||
|
||||
if (key === name) { |
||||
break; |
||||
} |
||||
} catch (e) {} |
||||
} |
||||
|
||||
return key ? jar[key] : jar; |
||||
} |
||||
|
||||
api.set = set; |
||||
api.get = function (key) { |
||||
return get(key, false /* read as raw */); |
||||
}; |
||||
api.getJSON = function (key) { |
||||
return get(key, true /* read as json */); |
||||
}; |
||||
api.remove = function (key, attributes) { |
||||
set(key, '', extend(attributes, { |
||||
expires: -1 |
||||
})); |
||||
}; |
||||
|
||||
api.defaults = {}; |
||||
|
||||
api.withConverter = init; |
||||
|
||||
return api; |
||||
} |
||||
|
||||
return init(function () {}); |
||||
})); |
@ -0,0 +1,82 @@ |
||||
gUserSettings = Cookies.getJSON( "user-settings" ) || {} ; |
||||
|
||||
USER_SETTINGS = { |
||||
"include-vasl-images-in-snippets": "checkbox", |
||||
} ; |
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
function user_settings() |
||||
{ |
||||
function load_settings() { |
||||
// load each user setting
|
||||
for ( var name in USER_SETTINGS ) { |
||||
var $elem = $( ".ui-dialog.user-settings [name='" + name + "']" ) ; |
||||
var func = handlers[ "load_" + USER_SETTINGS[name] ] ; |
||||
func( $elem, gUserSettings[name] ) ; |
||||
} |
||||
} |
||||
|
||||
function unload_settings() { |
||||
// unload each user setting
|
||||
var settings = {} ; |
||||
for ( var name in USER_SETTINGS ) { |
||||
var $elem = $( ".ui-dialog.user-settings [name='" + name + "']" ) ; |
||||
func = handlers[ "unload_" + USER_SETTINGS[name] ] ; |
||||
settings[name] = func( $elem ) ; |
||||
} |
||||
return settings ; |
||||
} |
||||
|
||||
var handlers = { |
||||
load_checkbox: function( $elem, val ) { $elem.prop( "checked", val?true:false ) ; }, |
||||
unload_checkbox: function( $elem ) { return $elem.prop( "checked" ) ; }, |
||||
} ; |
||||
|
||||
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" ) ; |
||||
$dlg.find( ".include-vasl-images-in-snippets-hint" ).css( |
||||
"color", is_checked ? "#444" : "#aaa" |
||||
) ; |
||||
} |
||||
|
||||
// show the "user settings" dialog
|
||||
$( "#user-settings" ).dialog( { |
||||
title: "User settings", |
||||
dialogClass: "user-settings", |
||||
modal: true, |
||||
width: 450, |
||||
height: 150, |
||||
resizable: false, |
||||
create: function() { |
||||
init_dialog( $(this), "OK", false ) ; |
||||
$(this).find( "input[name='include-vasl-images-in-snippets']" ).change( update_ui ) ; |
||||
}, |
||||
open: function() { |
||||
// load the current user settings
|
||||
load_settings( $(this) ) ; |
||||
update_ui() ; |
||||
}, |
||||
buttons: { |
||||
OK: function() { |
||||
// unload and install the new user settings
|
||||
var settings = unload_settings() ; |
||||
gUserSettings = settings ; |
||||
Cookies.set( "user-settings", settings, { expires: 999 } ) ; |
||||
if ( gWebChannelHandler ) |
||||
gWebChannelHandler.on_user_settings_change( JSON.stringify( settings ) ) ; |
||||
$(this).dialog( "close" ) ; |
||||
}, |
||||
Cancel: function() { $(this).dialog( "close" ) ; }, |
||||
}, |
||||
} ) ; |
||||
} |
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
function install_user_settings( user_settings ) // nb: this is called by the PyQT desktop application
|
||||
{ |
||||
gUserSettings = JSON.parse( user_settings ) ; |
||||
} |
@ -0,0 +1,4 @@ |
||||
<div id="user-settings" style="display:none;"> |
||||
<input type="checkbox" name="include-vasl-images-in-snippets"> Include VASL images in snippets |
||||
<div class="note include-vasl-images-in-snippets-hint" style="margin-left:20px;">This program must be running before you load the VASL scenario.</div> |
||||
</div> |
@ -0,0 +1,56 @@ |
||||
""" Test the user settings. """ |
||||
|
||||
import json |
||||
|
||||
from vasl_templates.webapp.tests.utils import \ |
||||
init_webapp, find_child, _get_clipboard, \ |
||||
wait_for, select_menu_option, click_dialog_button |
||||
from vasl_templates.webapp.tests.test_vehicles_ordnance import add_vo |
||||
from vasl_templates.webapp.config.constants import DATA_DIR as REAL_DATA_DIR |
||||
|
||||
# --------------------------------------------------------------------- |
||||
|
||||
def test_include_vasl_images_in_snippets( webapp, webdriver, monkeypatch ): |
||||
"""Test the user settings.""" |
||||
|
||||
# initialize |
||||
monkeypatch.setitem( webapp.config, "DATA_DIR", REAL_DATA_DIR ) |
||||
init_webapp( webapp, webdriver ) |
||||
|
||||
# add a vehicle |
||||
add_vo( webdriver, "vehicles", 1, "PzKpfw IB (Tt)" ) |
||||
|
||||
# enable "show VASL images in snippets" |
||||
select_menu_option( "user_settings" ) |
||||
elem = find_child( ".ui-dialog.user-settings input[name='include-vasl-images-in-snippets']" ) |
||||
assert not elem.is_selected() |
||||
elem.click() |
||||
click_dialog_button( "OK" ) |
||||
_check_cookies( webdriver, "include-vasl-images-in-snippets", True ) |
||||
|
||||
# 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() ) |
||||
|
||||
# disable "show VASL images in snippets" |
||||
select_menu_option( "user_settings" ) |
||||
elem = find_child( ".ui-dialog.user-settings input[name='include-vasl-images-in-snippets']" ) |
||||
assert elem.is_selected() |
||||
elem.click() |
||||
click_dialog_button( "OK" ) |
||||
_check_cookies( webdriver, "include-vasl-images-in-snippets", False ) |
||||
|
||||
# make sure that it took effect |
||||
snippet_btn.click() |
||||
wait_for( 2, lambda: "/counter/2524/front" 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", '"' ) |
||||
user_settings = json.loads( val ) |
||||
assert user_settings[name] == expected |
Loading…
Reference in new issue