From d52e4d58e2805582af3d3f94ceb282e4393f30d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sun, 8 Oct 2017 01:29:50 +0000 Subject: [PATCH] Fix: BFT Tag : JS fail sur le reset des input quand init_val != "" Ajoute une fonction init_input qui fait ce que faisait reset_input avant et maintenant reset_input, se contente de mettre "" dans les input --- .../templatetags/bootstrap_form_typeahead.py | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/machines/templatetags/bootstrap_form_typeahead.py b/machines/templatetags/bootstrap_form_typeahead.py index 5f10f5f6..e076b52c 100644 --- a/machines/templatetags/bootstrap_form_typeahead.py +++ b/machines/templatetags/bootstrap_form_typeahead.py @@ -232,41 +232,49 @@ def typeahead_js( f_name, f_value, f_bound, js_content = ( 'var choices_{f_name} = {choices};' + 'var engine_{f_name};' 'var setup_{f_name} = function() {{' - 'var engine_{f_name} = {engine};' + 'engine_{f_name} = {engine};' '$( "#{input_id}" ).typeahead( "destroy" );' '$( "#{input_id}" ).typeahead( {datasets} );' - '{reset_input}' '}};' '$( "#{input_id}" ).bind( "typeahead:select", {updater} );' '$( "#{input_id}" ).bind( "typeahead:change", {change} );' '{updates}' - '$( "#{input_id}" ).ready( setup_{f_name} );' + '$( "#{input_id}" ).ready( function() {{' + 'setup_{f_name}();' + '{init_input}' + '}} );' ).format( f_name = f_name, choices = choices, engine = engine, input_id = input_id( f_name ), datasets = default_datasets( f_name, match_func ), - reset_input = reset_input( f_name, f_bound ), updater = typeahead_updater( f_name ), change = typeahead_change( f_name ), - updates = ''.join( - ['$( "#{u_id}").change( setup_{f_name} );'.format( + updates = ''.join( [ ( + '$( "#{u_id}" ).change( function() {{' + 'setup_{f_name}();' + '{reset_input}' + '}} );' + ).format( u_id = u_id, + reset_input = reset_input( f_name ), f_name = f_name ) for u_id in update_on ] - ) + ), + init_input = init_input( f_name, f_bound ), ) return render_tag( 'script', content=mark_safe( js_content ) ) -def reset_input( f_name, f_bound ) : - """ The JS script to reset the fields values """ +def init_input( f_name, f_bound ) : + """ The JS script to init the fields values """ init_key = f_bound.value() or '""' return ( '$( "#{input_id}" ).typeahead("val", {init_val});' - '$( "#{hidden_id}").val( {init_key} );' + '$( "#{hidden_id}" ).val( {init_key} );' ).format( input_id = input_id( f_name ), init_val = '""' if init_key == '""' else @@ -278,6 +286,16 @@ def reset_input( f_name, f_bound ) : hidden_id = hidden_id( f_name ) ) +def reset_input( f_name ) : + """ The JS script to reset the fields values """ + return ( + '$( "#{input_id}" ).typeahead("val", "");' + '$( "#{hidden_id}" ).val( "" );' + ).format( + input_id = input_id( f_name ), + hidden_id = hidden_id( f_name ) + ) + def default_choices( f_value ) : """ The JS script creating the variable choices_ """ return '[ {objects} ]'.format(