form_handler.js 3.9 KB

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