Confirm closing the "add/edit simple note" dialog if changes have been made.

master
Pacman Ghost 2 years ago
parent 95167f4888
commit 0312fa2fc7
  1. 11
      vasl_templates/webapp/static/main.js
  2. 53
      vasl_templates/webapp/static/simple_notes.js
  3. 2
      vasl_templates/webapp/tests/test_snippets.py

@ -1145,8 +1145,15 @@ function handle_escape( evt )
var $topmost = findTopmostDialog() ; var $topmost = findTopmostDialog() ;
if ( $topmost ) { if ( $topmost ) {
var $dlg = $topmost.children( ".ui-dialog-content" ) ; var $dlg = $topmost.children( ".ui-dialog-content" ) ;
if ( ["please-wait","ask","lfa"].indexOf( $dlg.attr("id") ) === -1 ) if ( [ "please-wait", "ask", "lfa" ].indexOf( $dlg.attr("id") ) === -1 ) {
$topmost.children( ".ui-dialog-content" ).dialog( "close" ) ; // NOTE: We prefer clicking on a Cancel button, so that the dialog has a chance
// to confirm (and perhaps cancel) the operation.
var $btn = $topmost.find( ".ui-dialog-buttonpane button.cancel" ) ;
if ( $btn.length > 0 )
$btn.trigger( "click" ) ;
else
$topmost.children( ".ui-dialog-content" ).dialog( "close" ) ;
}
} }
} }

@ -39,8 +39,7 @@ function _do_edit_simple_note( template_id, player_no, $sortable2, $entry, defau
// initialize // initialize
var $btnPane = $( ".ui-dialog.edit-simple_note .ui-dialog-buttonpane" ) ; var $btnPane = $( ".ui-dialog.edit-simple_note .ui-dialog-buttonpane" ) ;
var $btn = $btnPane.find( "button.snippet" ) ; var $btn = $btnPane.find( "button.snippet" ) ;
var caption = $caption.val().trim() ; var data = unloadData() ;
var width = $width.val().trim() ;
// prepare the template parameters // prepare the template parameters
// NOTE: We don't bother handling the case of an empty caption. // NOTE: We don't bother handling the case of an empty caption.
var extraParams = {} ; var extraParams = {} ;
@ -54,29 +53,39 @@ function _do_edit_simple_note( template_id, player_no, $sortable2, $entry, defau
// find and update the SSR being edited // find and update the SSR being edited
$( "#ssr-sortable > li" ).each( function( index ) { $( "#ssr-sortable > li" ).each( function( index ) {
if ( $(this)[0] === $entry[0] ) if ( $(this)[0] === $entry[0] )
ssrs[ index ] = caption ; ssrs[ index ] = data.caption ;
} ) ; } ) ;
} else { } else {
// add the new SSR to the end of the list // add the new SSR to the end of the list
ssrs.push( caption ) ; ssrs.push( data.caption ) ;
} }
extraParams.SSR = ssrs ; extraParams.SSR = ssrs ;
} else { } else {
// override the template parameters unloaded from the UI with the current values from the dialog // override the template parameters unloaded from the UI with the current values from the dialog
var paramKey = template_id.toUpperCase() ; var paramKey = template_id.toUpperCase() ;
extraParams[ paramKey ] = caption ; extraParams[ paramKey ] = data.caption ;
extraParams[ paramKey+"_WIDTH" ] = width ; extraParams[ paramKey+"_WIDTH" ] = data.width ;
} }
// generate the snippet // generate the snippet
generate_snippet( $btn, evt.shiftKey, extraParams ) ; generate_snippet( $btn, evt.shiftKey, extraParams ) ;
} }
function unloadData() {
// unload the snippet data
return {
caption: $caption.val().trim(),
width: $width.val().trim(),
} ;
}
// let the user edit the note // let the user edit the note
var $caption, $width ; var dlgTitle = ($entry ? "Edit " : "Add ") + SORTABLE_DISPLAY_NAMES[note_type][0] ;
$("#edit-simple_note").dialog( { var $caption, $width, origData ;
var $dlg = $("#edit-simple_note").dialog( {
dialogClass: "edit-simple_note", dialogClass: "edit-simple_note",
title: ($entry ? "Edit " : "Add ") + SORTABLE_DISPLAY_NAMES[note_type][0], title: dlgTitle,
modal: true, modal: true,
closeOnEscape: false,
minWidth: 600, minWidth: 600,
minHeight: 250, minHeight: 250,
create: function() { create: function() {
@ -129,27 +138,27 @@ function _do_edit_simple_note( template_id, player_no, $sortable2, $entry, defau
// load the dialog // load the dialog
$caption.val( entryData ? entryData.caption : "" ).focus() ; $caption.val( entryData ? entryData.caption : "" ).focus() ;
$width.val( entryData ? entryData.width : default_width ) ; $width.val( entryData ? entryData.width : default_width ) ;
origData = unloadData() ;
$(this).height( $(this).height() ) ; // fudge: force the textarea to resize $(this).height( $(this).height() ) ; // fudge: force the textarea to resize
}, },
buttons: { buttons: {
Snippet: { text:" Snippet", class: "snippet", click: makeSimpleSnippet }, Snippet: { text:" Snippet", class: "snippet", click: makeSimpleSnippet },
OK: function() { OK: function() {
var caption = $caption.val().trim() ; var data = unloadData() ;
var width = $width.val().trim() ;
if ( $entry ) { if ( $entry ) {
// update the existing note // update the existing note
if ( caption === "" ) if ( data.caption === "" )
$sortable2.sortable2( "delete", { entry: $entry } ) ; $sortable2.sortable2( "delete", { entry: $entry } ) ;
else { else {
$entry.data("sortable2-data").caption = caption ; $entry.data("sortable2-data").caption = data.caption ;
$entry.data("sortable2-data").width = width ; $entry.data("sortable2-data").width = data.width ;
$entry.empty().append( _make_simple_note( note_type, caption ) ) ; $entry.empty().append( _make_simple_note( note_type, data.caption ) ) ;
} }
} }
else { else {
// create a new note // create a new note
if ( caption !== "" ) { if ( data.caption !== "" ) {
data = { caption: caption, width: width } ; data = { caption: data.caption, width: data.width } ;
if ( note_type === "scenario_notes" || note_type === "ob_setups" || note_type === "ob_notes" ) if ( note_type === "scenario_notes" || note_type === "ob_setups" || note_type === "ob_notes" )
data.id = nextAvailableId ; data.id = nextAvailableId ;
$entry = _do_add_simple_note( $sortable2, data ) ; $entry = _do_add_simple_note( $sortable2, data ) ;
@ -163,7 +172,15 @@ function _do_edit_simple_note( template_id, player_no, $sortable2, $entry, defau
} }
$(this).dialog( "close" ) ; $(this).dialog( "close" ) ;
}, },
Cancel: function() { $(this).dialog( "close" ) ; }, Cancel: function() {
if ( JSON.stringify( unloadData() ) != JSON.stringify( origData ) ) {
ask( dlgTitle, "Discard your changes?", {
ok: function() { $dlg.dialog( "close" ) ; },
} ) ;
return ;
}
$(this).dialog( "close" ) ;
},
}, },
} ) ; } ) ;
} }

@ -277,6 +277,7 @@ def test_simple_snippets_from_dialog( webapp, webdriver ):
] ), re.DOTALL ) ] ), re.DOTALL )
wait_for_clipboard( 2, expected ) wait_for_clipboard( 2, expected )
click_dialog_button( "Cancel" ) click_dialog_button( "Cancel" )
click_dialog_button( "OK", find_child(".ui-dialog.ask") )
def test_new_simple_note( sortable, expected ) : def test_new_simple_note( sortable, expected ) :
# add a new simple note # add a new simple note
@ -298,6 +299,7 @@ def test_simple_snippets_from_dialog( webapp, webdriver ):
] ), re.DOTALL ) ] ), re.DOTALL )
wait_for_clipboard( 2, expected ) wait_for_clipboard( 2, expected )
click_dialog_button( "Cancel" ) click_dialog_button( "Cancel" )
click_dialog_button( "OK", find_child(".ui-dialog.ask") )
# test scenario notes # test scenario notes
sortable = find_child( "#scenario_notes-sortable" ) sortable = find_child( "#scenario_notes-sortable" )

Loading…
Cancel
Save