form_configurable.js.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. $ ->
  2. getFormData = (elem) ->
  3. form_data = $("#edit_agent, #new_agent").serializeObject()
  4. attribute = $(elem).data('attribute')
  5. form_data['attribute'] = attribute
  6. delete form_data['_method']
  7. form_data
  8. window.initializeFormCompletable = ->
  9. returnedResults = {}
  10. completableDefaultOptions = (input) ->
  11. results: [
  12. (returnedResults[$(input).data('attribute')] || {text: 'Options', children: [{id: undefined, text: 'loading ...'}]}),
  13. {
  14. text: 'Current',
  15. children: [id: $(input).val(), text: $(input).val()]
  16. },
  17. {
  18. text: 'Custom',
  19. children: [id: 'manualInput', text: 'manual input']
  20. },
  21. ]
  22. $("input[role~=validatable], select[role~=validatable]").on 'change', (e) =>
  23. form_data = getFormData(e.currentTarget)
  24. form_group = $(e.currentTarget).closest('.form-group')
  25. $.ajax '/agents/validate',
  26. type: 'POST',
  27. data: form_data
  28. success: (data) ->
  29. form_group.addClass('has-feedback').removeClass('has-error')
  30. form_group.find('span').addClass('hidden')
  31. form_group.find('.glyphicon-ok').removeClass('hidden')
  32. returnedResults = {}
  33. error: (data) ->
  34. form_group.addClass('has-feedback').addClass('has-error')
  35. form_group.find('span').addClass('hidden')
  36. form_group.find('.glyphicon-remove').removeClass('hidden')
  37. returnedResults = {}
  38. $("input[role~=validatable], select[role~=validatable]").trigger('change')
  39. $.each $("input[role~=completable]"), (i, input) ->
  40. $(input).select2(
  41. data: ->
  42. completableDefaultOptions(input)
  43. ).on("change", (e) ->
  44. if e.added && e.added.id == 'manualInput'
  45. $(e.currentTarget).select2("destroy")
  46. $(e.currentTarget).val(e.removed.id)
  47. )
  48. updateDropdownData = (form_data, element, data) ->
  49. returnedResults[form_data.attribute] = {text: 'Options', children: data}
  50. $(element).trigger('change')
  51. $("input[role~=completable]").off 'select2-opening', select2OpeningCallback
  52. $(element).select2('open')
  53. $("input[role~=completable]").on 'select2-opening', select2OpeningCallback
  54. select2OpeningCallback = (e) ->
  55. form_data = getFormData(e.currentTarget)
  56. delete returnedResults[form_data.attribute] if returnedResults[form_data.attribute] && !$(e.currentTarget).data('cacheResponse')
  57. return if returnedResults[form_data.attribute]
  58. $.ajax '/agents/complete',
  59. type: 'POST',
  60. data: form_data
  61. success: (data) ->
  62. updateDropdownData(form_data, e.currentTarget, data)
  63. error: (data) ->
  64. updateDropdownData(form_data, e.currentTarget, [{id: undefined, text: 'Error loading data.'}])
  65. $("input[role~=completable]").on 'select2-opening', select2OpeningCallback
  66. $("input[type=radio][role~=form-configurable]").change (e) ->
  67. input = $(e.currentTarget).parents().siblings("input[data-attribute=#{$(e.currentTarget).data('attribute')}]")
  68. if $(e.currentTarget).val() == 'manual'
  69. input.removeClass('hidden')
  70. else
  71. input.val($(e.currentTarget).val())
  72. input.addClass('hidden')