8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-26 00:43:46 +00:00

Proprification de code du bft tag + fix mineur

Utilise la fonction .format plutôt que la concténation pour rendre le
code plus lisible.
Le fix concernait le cas où il n'y a pas de initial_value, le JS sortait
une erreur
This commit is contained in:
Maël Kervella 2017-10-08 00:48:32 +00:00
parent 69ffc04d6a
commit 9d4d67e032
2 changed files with 147 additions and 105 deletions

View file

@ -226,105 +226,143 @@ def typeahead_js( f_name, f_value, f_bound,
else default_engine ( f_name ) else default_engine ( f_name )
match_func = mark_safe(t_match_func[f_name]) \ match_func = mark_safe(t_match_func[f_name]) \
if f_name in t_match_func.keys() \ if f_name in t_match_func.keys() else default_match_func( f_name )
else default_match_func( f_name )
update_on = t_update_on[f_name] if f_name in t_update_on.keys() else [] update_on = t_update_on[f_name] if f_name in t_update_on.keys() else []
js_content = \ js_content = (
'var choices_'+f_name+' = ' + choices + ';\n' + \ 'var choices_{f_name} = {choices};'
'var setup_'+f_name+' = function() {\n' + \ 'var setup_{f_name} = function() {{'
'var engine_'+f_name+' = ' + engine + ';\n' + \ 'var engine_{f_name} = {engine};'
'$("#'+input_id(f_name) + '").typeahead("destroy");\n' + \ '$( "#{input_id}" ).typeahead( "destroy" );'
'$("#'+input_id(f_name) + '").typeahead(\n' + \ '$( "#{input_id}" ).typeahead( {datasets} );'
default_datasets( f_name, match_func ) + '\n' + \ '{reset_input}'
');\n' + \ '}};'
reset_input( f_name, f_bound ) + '\n' + \ '$( "#{input_id}" ).bind( "typeahead:select", {updater} );'
'};\n' + \ '$( "#{input_id}" ).bind( "typeahead:change", {change} );'
'$("#'+input_id(f_name) + '").bind(\n' + \ '{updates}'
'"typeahead:select", ' + \ '$( "#{input_id}" ).ready( setup_{f_name} );'
typeahead_updater( f_name ) + '\n' + \ ).format(
').bind(\n' + \ f_name = f_name,
'"typeahead:change", ' + \ choices = choices,
typeahead_change( f_name ) + '\n' + \ engine = engine,
');\n' input_id = input_id( f_name ),
for u_id in update_on : datasets = default_datasets( f_name, match_func ),
js_content += '$("#'+u_id+'").change( setup_'+f_name+' );\n' reset_input = reset_input( f_name, f_bound ),
js_content += '$("#'+input_id(f_name)+'").ready( setup_'+f_name+' );\n' updater = typeahead_updater( f_name ),
change = typeahead_change( f_name ),
updates = ''.join(
['$( "#{u_id}").change( setup_{f_name} );'.format(
u_id = u_id,
f_name = f_name
) for u_id in update_on ]
)
)
return render_tag( 'script', content=mark_safe( js_content ) ) return render_tag( 'script', content=mark_safe( js_content ) )
def reset_input( f_name, f_bound ) : def reset_input( f_name, f_bound ) :
""" The JS script to reset the fields values """ """ The JS script to reset the fields values """
return '$("#'+input_id(f_name)+'").typeahead(' \ init_key = f_bound.value() or '""'
'"val", ' \ return (
'engine_'+f_name+'.get('+str(f_bound.value())+')[0].value' \ '$( "#{input_id}" ).typeahead("val", {init_val});'
');\n' \ '$( "#{hidden_id}").val( {init_key} );'
'$("#'+hidden_id(f_name)+'").val('+str(f_bound.value())+');' ).format(
input_id = input_id( f_name ),
init_val = '""' if init_key == '""' else
'engine_{f_name}.get( {init_key} )[0].value'.format(
f_name = f_name,
init_key = init_key
),
init_key = init_key,
hidden_id = hidden_id( f_name )
)
def default_choices( f_value ) : def default_choices( f_value ) :
""" The JS script creating the variable choices_<fieldname> """ """ The JS script creating the variable choices_<fieldname> """
return '[' + \ return '[ {objects} ]'.format(
', '.join([ \ objects = ', '.join(
'{key: ' + (str(choice[0]) if choice[0] != '' else '""') + \ [ '{{ key: {k}, value: "{k}" }}'.format(
', value: "' + str(choice[1]) + '"}' \ k = choice[0] if choice[0] != '' else '""',
for choice in f_value.choices \ v = choice[1]
]) + \ ) for choice in f_value.choices ]
']' )
)
def default_engine ( f_name ) : def default_engine ( f_name ) :
""" The JS script creating the variable engine_<field_name> """ """ The JS script creating the variable engine_<field_name> """
return 'new Bloodhound({ ' \ return (
'datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"), ' \ 'new Bloodhound({{'
'queryTokenizer: Bloodhound.tokenizers.whitespace, ' \ 'datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),'
'local: choices_'+f_name+', ' \ 'queryTokenizer: Bloodhound.tokenizers.whitespace,'
'identify: function(obj) { return obj.key; } ' \ 'local: choices_{f_name},'
'})' 'identify: function(obj) {{ return obj.key; }}'
'}})'
).format(
f_name = f_name
)
def default_datasets( f_name, match_func ) : def default_datasets( f_name, match_func ) :
""" The JS script creating the datasets to use with typeahead """ """ The JS script creating the datasets to use with typeahead """
return '{ ' \ return (
'hint: true, ' \ '{{'
'highlight: true, ' \ 'hint: true,'
'minLength: 0 ' \ 'highlight: true,'
'}, ' \ 'minLength: 0'
'{ ' \ '}},'
'display: "value", ' \ '{{'
'name: "'+f_name+'", ' \ 'display: "value",'
'source: '+match_func + \ 'name: "{f_name}",'
'}' 'source: {match_func}'
'}}'
).format(
f_name = f_name,
match_func = match_func
)
def default_match_func ( f_name ) : def default_match_func ( f_name ) :
""" The JS script creating the matching function to use with typeahed """ """ The JS script creating the matching function to use with typeahed """
return 'function(q, sync) {' \ return (
'if (q === "") {' \ 'function ( q, sync ) {{'
'var nb = 10;' \ 'if ( q === "" ) {{'
'var first = [] ;' \ 'var first = choices_{f_name}.slice( 0, 5 ).map('
'for ( var i=0 ; i<nb && i<choices_'+f_name+'.length; i++ ) {' \ 'function ( obj ) {{ return obj.key; }}'
'first.push(choices_'+f_name+'[i].key);' \ ');'
'}' \ 'sync( engine_{f_name}.get( first ) );'
'sync(engine_'+f_name+'.get(first));' \ '}} else {{'
'} else {' \ 'engine_{f_name}.search( q, sync );'
'engine_'+f_name+'.search(q, sync);' \ '}}'
'}' \ '}}'
'}' ).format(
f_name = f_name
)
def typeahead_updater( f_name ): def typeahead_updater( f_name ):
""" The JS script creating the function triggered when an item is """ The JS script creating the function triggered when an item is
selected through typeahead """ selected through typeahead """
return 'function(evt, item) { ' \ return (
'$("#'+hidden_id(f_name)+'").val( item.key ); ' \ 'function(evt, item) {{'
'$("#'+hidden_id(f_name)+'").change();' \ '$( "#{hidden_id}" ).val( item.key );'
'return item; ' \ '$( "#{hidden_id}" ).change();'
'}' 'return item;'
'}}'
).format(
hidden_id = hidden_id( f_name )
)
def typeahead_change( f_name ): def typeahead_change( f_name ):
""" The JS script creating the function triggered when an item is changed """ The JS script creating the function triggered when an item is changed
(i.e. looses focus and value has changed since the moment it gained focus (i.e. looses focus and value has changed since the moment it gained focus
""" """
return 'function(evt) { ' \ return (
'if ($("#'+input_id(f_name)+'").typeahead("val") === "") {' \ 'function(evt) {{'
'$("#'+hidden_id(f_name)+'").val(""); ' \ 'if ( $( "#{input_id}" ).typeahead( "val" ) === "" ) {{'
'$("#'+hidden_id(f_name)+'").change();' \ '$( "#{hidden_id}" ).val( "" );'
'}' \ '$( "#{hidden_id}" ).change();'
'}' '}}'
'}}'
).format(
input_id = input_id( f_name ),
hidden_id = hidden_id( f_name )
)

View file

@ -96,9 +96,14 @@ def generate_ipv4_choices( form ) :
if mtype_id != ip.mtype_id : if mtype_id != ip.mtype_id :
mtype_id = ip.mtype_id mtype_id = ip.mtype_id
used_mtype_id.append(mtype_id) used_mtype_id.append(mtype_id)
choices += '], "'+str(mtype_id)+'": [' choices += '],"{t}":[{{key:"",value:"{v}"}},'.format(
choices += '{key: "", value: "' + str(f_ipv4.empty_label) + '"},' t = mtype_id,
choices += '{key: ' + str(ip.id) + ', value: "' + str(ip.ipv4) + '"},' v = f_ipv4.empty_label or '""'
)
choices += '{{key:{k},value:"{v}"}},'.format(
k = ip.id,
v = ip.ipv4
)
for t in form.fields['type'].queryset.exclude(id__in=used_mtype_id) : for t in form.fields['type'].queryset.exclude(id__in=used_mtype_id) :
choices += '], "'+str(t.id)+'": [' choices += '], "'+str(t.id)+'": ['
@ -109,34 +114,33 @@ def generate_ipv4_choices( form ) :
def generate_ipv4_engine( is_type_tt ) : def generate_ipv4_engine( is_type_tt ) :
""" Generate the parameter engine for the bootstrap_form_typeahead tag """ Generate the parameter engine for the bootstrap_form_typeahead tag
""" """
return 'new Bloodhound({ ' \ return (
'datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"), ' \ 'new Bloodhound( {{'
'queryTokenizer: Bloodhound.tokenizers.whitespace, ' \ 'datumTokenizer: Bloodhound.tokenizers.obj.whitespace( "value" ),'
'local: choices_ipv4[$("#'+f_type_id(is_type_tt)+'").val()], ' \ 'queryTokenizer: Bloodhound.tokenizers.whitespace,'
'identify: function(obj) { return obj.key; } ' \ 'local: choices_ipv4[ $( "#{type_id}" ).val() ],'
'})' 'identify: function( obj ) {{ return obj.key; }}'
'}} )'
).format(
type_id = f_type_id( is_type_tt )
)
def generate_ipv4_match_func( is_type_tt ) : def generate_ipv4_match_func( is_type_tt ) :
""" Generate the parameter match_func for the bootstrap_form_typeahead tag """ Generate the parameter match_func for the bootstrap_form_typeahead tag
""" """
return 'function(q, sync) {' \ return (
'if (q === "") {' \ 'function(q, sync) {{'
'var nb = 10;' \ 'if (q === "") {{'
'var first = [] ;' \ 'var first = choices_ipv4[$("#{type_id}").val()].slice(0, 5);'
'for(' \ 'first = first.map( function (obj) {{ return obj.key; }} );'
'var i=0 ;' \ 'sync(engine_ipv4.get(first));'
'i<nb && i<choices_ipv4[' \ '}} else {{'
'$("#'+f_type_id(is_type_tt)+'").val()' \ 'engine_ipv4.search(q, sync);'
'].length ;' \ '}}'
'i++' \ '}}'
') { first.push(' \ ).format(
'choices_ipv4[$("#'+f_type_id(is_type_tt)+'").val()][i].key' \ type_id = f_type_id( is_type_tt )
'); }' \ )
'sync(engine_ipv4.get(first));' \
'} else {' \
'engine_ipv4.search(q, sync);' \
'}' \
'}'
def generate_ipv4_bft_param( form, is_type_tt ): def generate_ipv4_bft_param( form, is_type_tt ):
""" Generate all the parameters to use with the bootstrap_form_typeahead """ Generate all the parameters to use with the bootstrap_form_typeahead