console_form_handler.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2010-2016 Combodo SARL
  2. //
  3. // This file is part of iTop.
  4. //
  5. // iTop is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // iTop is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  17. //iTop Form handler
  18. ;
  19. $(function()
  20. {
  21. // the widget definition, where 'itop' is the namespace,
  22. // 'consoleform_handler' the widget name
  23. $.widget( 'itop.console_form_handler', $.itop.form_handler,
  24. {
  25. // default options
  26. options:
  27. {
  28. wizard_helper_var_name: '', // Name of the global variable pointing to the wizard helper
  29. custom_field_attcode: ''
  30. },
  31. // the constructor
  32. _create: function()
  33. {
  34. var me = this;
  35. this.element
  36. .addClass('console_form_handler');
  37. this.options.oWizardHelper = window[this.options.wizard_helper_var_name];
  38. this._super();
  39. },
  40. // events bound via _bind are removed automatically
  41. // revert other modifications here
  42. _destroy: function()
  43. {
  44. this.element
  45. .removeClass('console_form_handler');
  46. this._super();
  47. },
  48. _onUpdateFields: function(event, data)
  49. {
  50. var me = this;
  51. var sFormPath = data.form_path;
  52. var sUpdateUrl = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php';
  53. $(this.element.find('[data-form-path="' + sFormPath + '"]')).block({message:''});
  54. $.post(
  55. sUpdateUrl,
  56. {
  57. operation: 'custom_fields_update',
  58. attcode: this.options.custom_field_attcode,
  59. //current_values: this.getCurrentValues(),
  60. requested_fields: data.requested_fields,
  61. form_path: sFormPath,
  62. json_obj: this.options.oWizardHelper.UpdateWizardToJSON()
  63. },
  64. function(data){
  65. if ('form' in data) {
  66. me._onUpdateSuccess(data, sFormPath);
  67. }
  68. }
  69. )
  70. .fail(function(data){ me._onUpdateFailure(data, sFormPath); })
  71. .always(function(data){
  72. me.alignColumns();
  73. var oContainer = $(me.element.find('[data-form-path="' + sFormPath + '"]'));
  74. oContainer.unblock();
  75. if ('error' in data) {
  76. oContainer.block({message: data.error});
  77. console.log('Update field failure: '+data.error);
  78. $('.blockOverlay').click(function(){
  79. oContainer.unblock();
  80. });
  81. }
  82. me._onUpdateAlways(data, sFormPath);
  83. });
  84. },
  85. // On initialization or update
  86. alignColumns: function()
  87. {
  88. var iMaxWidth = 0;
  89. var oLabels = $(this.element.find('td.form-field-label'));
  90. // Reset the width to the automatic (original) value
  91. oLabels.width('');
  92. oLabels.each(function() {
  93. iMaxWidth = Math.max(iMaxWidth, $(this).width());
  94. });
  95. oLabels.width(iMaxWidth);
  96. },
  97. // Intended for overloading in derived classes
  98. _onSubmitClick: function()
  99. {
  100. },
  101. // Intended for overloading in derived classes
  102. _onCancelClick: function()
  103. {
  104. },
  105. // Intended for overloading in derived classes
  106. _onUpdateFailure: function(data)
  107. {
  108. },
  109. // Intended for overloading in derived classes
  110. _disableFormBeforeLoading: function()
  111. {
  112. },
  113. // Intended for overloading in derived classes
  114. _enableFormAfterLoading: function()
  115. {
  116. },
  117. });
  118. });