form_handler.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //iTop Form handler
  2. ;
  3. $(function()
  4. {
  5. // the widget definition, where 'itop' is the namespace,
  6. // 'form_handler' the widget name
  7. $.widget( 'itop.form_handler',
  8. {
  9. // default options
  10. options:
  11. {
  12. formmanager_class: null,
  13. formmanager_data: null,
  14. submit_btn_selector: null,
  15. cancel_btn_selector: null,
  16. endpoint: null,
  17. is_modal: false,
  18. field_set: null
  19. },
  20. // the constructor
  21. _create: function()
  22. {
  23. var me = this;
  24. this.element
  25. .addClass('form_handler');
  26. this.element.bind('update_fields', function(event, data){
  27. this._onUpdateFields(event, data);
  28. });
  29. // Binding buttons
  30. if(this.options.submit_btn_selector !== null)
  31. {
  32. this.options.submit_btn_selector.off('click').on('click', function(event){ me._onSubmitClick(event); });
  33. }
  34. if(this.options.cancel_btn_selector !== null)
  35. {
  36. this.options.cancel_btn_selector.off('click').on('click', function(event){ me._onCancelClick(event); });
  37. }
  38. },
  39. // called when created, and later when changing options
  40. _refresh: function()
  41. {
  42. },
  43. // events bound via _bind are removed automatically
  44. // revert other modifications here
  45. _destroy: function()
  46. {
  47. this.element
  48. .removeClass('form_handler');
  49. },
  50. // _setOptions is called with a hash of all options that are changing
  51. // always refresh when changing options
  52. _setOptions: function()
  53. {
  54. this._superApply(arguments);
  55. },
  56. // _setOption is called for each individual option that is changing
  57. _setOption: function( key, value )
  58. {
  59. this._super( key, value );
  60. },
  61. getCurrentValues: function()
  62. {
  63. return this.options.field_set.triggerHandler('get_current_values');
  64. },
  65. _onUpdateFields: function(event, data)
  66. {
  67. var me = this;
  68. var sFormPath = data.form_path;
  69. // Data checks
  70. if(this.options.endpoint === null)
  71. {
  72. console.log('Form handler : An endpoint must be defined.');
  73. return false;
  74. }
  75. if(this.options.formmanager_class === null)
  76. {
  77. console.log('Form handler : Form manager class must be defined.');
  78. return false;
  79. }
  80. if(this.options.formmanager_data === null)
  81. {
  82. console.log('Form handler : Form manager data must be defined.');
  83. return false;
  84. }
  85. this._disableFormBeforeLoading();
  86. $.post(
  87. this.options.endpoint,
  88. {
  89. operation: 'update',
  90. formmanager_class: this.options.formmanager_class,
  91. formmanager_data: JSON.stringify(this.options.formmanager_data),
  92. current_values: this.getCurrentValues(),
  93. requested_fields: data.requested_fields,
  94. form_path: sFormPath
  95. },
  96. function(data){
  97. me._onUpdateSuccess(data, sFormPath);
  98. }
  99. )
  100. .fail(function(data){ me._onUpdateFailure(data, sFormPath); })
  101. .always(function(data){ me._onUpdateAlways(data, sFormPath); });
  102. },
  103. // Intended for overloading in derived classes
  104. _onSubmitClick: function(event)
  105. {
  106. },
  107. // Intended for overloading in derived classes
  108. _onCancelClick: function(event)
  109. {
  110. },
  111. // Intended for overloading in derived classes
  112. _onUpdateSuccess: function(data, sFormPath)
  113. {
  114. if(data.form.updated_fields !== undefined)
  115. {
  116. this.element.find('[data-form-path="'+sFormPath+'"]').trigger('update_form', {updated_fields: data.form.updated_fields});
  117. }
  118. },
  119. // Intended for overloading in derived classes
  120. _onUpdateFailure: function(data, sFormPath)
  121. {
  122. },
  123. // Intended for overloading in derived classes
  124. _onUpdateAlways: function(data, sFormPath)
  125. {
  126. // Check all touched AFTER ajax is complete, otherwise the renderer will redraw the field in the mean time.
  127. this.element.find('[data-form-path="'+sFormPath+'"]').trigger('validate');
  128. this._enableFormAfterLoading();
  129. },
  130. // Intended for overloading in derived classes
  131. _disableFormBeforeLoading: function()
  132. {
  133. },
  134. // Intended for overloading in derived classes
  135. _enableFormAfterLoading: function()
  136. {
  137. },
  138. showOptions: function() // Debug helper
  139. {
  140. console.log(this.options);
  141. }
  142. });
  143. });