Commit 87a677347e9df53f4746aa95f1f4e490d03a8e0e

Authored by Rodrigo Souto
1 parent ecc6a631

Including a helper method for using token_input

Showing 1 changed file with 49 additions and 0 deletions   Show diff stats
app/helpers/application_helper.rb
... ... @@ -1337,4 +1337,53 @@ module ApplicationHelper
1337 1337 _("Are you sure that you want to remove the item \"#{article.name}\"?")
1338 1338 end
1339 1339 end
  1340 +
  1341 + def token_input_field_tag(name, element_id, search_action, options = {}, text_field_options = {}, html_options = {})
  1342 + options[:min_chars] ||= 3
  1343 + options[:hint_text] ||= _("Type in a search term")
  1344 + options[:no_results_text] ||= _("No results")
  1345 + options[:searching_text] ||= _("Searching...")
  1346 + options[:search_delay] ||= 1000
  1347 + options[:prevent_duplicates] ||= true
  1348 + options[:backspace_delete_item] ||= false
  1349 + options[:focus] ||= false
  1350 + options[:avoid_enter] ||= true
  1351 + options[:on_result] ||= 'null'
  1352 + options[:on_add] ||= 'null'
  1353 + options[:on_delete] ||= 'null'
  1354 + options[:on_ready] ||= 'null'
  1355 +
  1356 + result = text_field_tag(name, nil, text_field_options.merge(html_options.merge({:id => element_id})))
  1357 + result +=
  1358 + "
  1359 + <script type='text/javascript'>
  1360 + jQuery('##{element_id}')
  1361 + .tokenInput('#{url_for(search_action)}', {
  1362 + minChars: #{options[:min_chars].to_json},
  1363 + prePopulate: #{options[:pre_populate].to_json},
  1364 + hintText: #{options[:hint_text].to_json},
  1365 + noResultsText: #{options[:no_results_text].to_json},
  1366 + searchingText: #{options[:searching_text].to_json},
  1367 + searchDelay: #{options[:serach_delay].to_json},
  1368 + preventDuplicates: #{options[:prevent_duplicates].to_json},
  1369 + backspaceDeleteItem: #{options[:backspace_delete_item].to_json},
  1370 + queryParam: #{name.to_json},
  1371 + tokenLimit: #{options[:token_limit].to_json},
  1372 + onResult: #{options[:on_result]},
  1373 + onAdd: #{options[:on_add]},
  1374 + onDelete: #{options[:on_delete]},
  1375 + onReady: #{options[:on_ready]},
  1376 + })
  1377 + "
  1378 + result += options[:focus] ? ".focus();" : ";"
  1379 + if options[:avoid_enter]
  1380 + result += "jQuery('#token-input-#{element_id}')
  1381 + .live('keydown', function(event){
  1382 + if(event.keyCode == '13') return false;
  1383 + });"
  1384 + end
  1385 + result += "</script>"
  1386 + result
  1387 + end
  1388 +
1340 1389 end
... ...