console_form_handler.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. .append('<div class="last-error"></div>')
  37. .addClass('console_form_handler');
  38. this.options.oWizardHelper = window[this.options.wizard_helper_var_name];
  39. this._super();
  40. },
  41. // events bound via _bind are removed automatically
  42. // revert other modifications here
  43. _destroy: function()
  44. {
  45. this.element
  46. .removeClass('console_form_handler');
  47. this._super();
  48. },
  49. _onUpdateFields: function(event, data)
  50. {
  51. var me = this;
  52. var sFormPath = data.form_path;
  53. var sUpdateUrl = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php';
  54. this.element.find('[data-form-path="' + sFormPath + '"]').block({message:''});
  55. $.post(
  56. sUpdateUrl,
  57. {
  58. operation: 'custom_fields_update',
  59. attcode: this.options.custom_field_attcode,
  60. //current_values: this.getCurrentValues(),
  61. requested_fields: data.requested_fields,
  62. form_path: sFormPath,
  63. json_obj: this.options.oWizardHelper.UpdateWizardToJSON()
  64. },
  65. function(data){
  66. me.element.find('.last-error').text('');
  67. if ('form' in data) {
  68. me._onUpdateSuccess(data, sFormPath);
  69. }
  70. }
  71. )
  72. .fail(function(data){ me._onUpdateFailure(data, sFormPath); })
  73. .always(function(data){
  74. me.alignColumns();
  75. me.element.find('[data-form-path="' + sFormPath + '"]').unblock();
  76. if ('error' in data) {
  77. console.log('Update field failure: '+data.error);
  78. me.element.find('.last-error').text(data.error);
  79. }
  80. me._onUpdateAlways(data, sFormPath);
  81. });
  82. },
  83. // On initialization or update
  84. alignColumns: function()
  85. {
  86. var iMaxWidth = 0;
  87. var oLabels = this.element.find('td.form-field-label');
  88. // Reset the width to the automatic (original) value
  89. oLabels.width('');
  90. oLabels.each(function() {
  91. iMaxWidth = Math.max(iMaxWidth, $(this).width());
  92. });
  93. oLabels.width(iMaxWidth);
  94. },
  95. // Intended for overloading in derived classes
  96. _onSubmitClick: function()
  97. {
  98. },
  99. // Intended for overloading in derived classes
  100. _onCancelClick: function()
  101. {
  102. },
  103. // Intended for overloading in derived classes
  104. _onUpdateFailure: function(data)
  105. {
  106. },
  107. // Intended for overloading in derived classes
  108. _disableFormBeforeLoading: function()
  109. {
  110. },
  111. // Intended for overloading in derived classes
  112. _enableFormAfterLoading: function()
  113. {
  114. },
  115. });
  116. });