property_field.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //iTop Designer widget for editing properties line by line
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "property_field" the widget name
  6. $.widget( "itop.property_field",
  7. {
  8. // default options
  9. options:
  10. {
  11. field_id: '',
  12. submit_to: 'index.php',
  13. submit_parameters: {operation: 'async_action'}
  14. },
  15. // the constructor
  16. _create: function()
  17. {
  18. this.element.addClass( "itop-property-field" );
  19. this.bModified = false;
  20. var me = this;
  21. if (this.options.field_id != '')
  22. {
  23. $('#'+this.options.field_id).bind('change.itop-property-field', function() { me._on_change(); });
  24. this.value = this._get_field_value();
  25. }
  26. this.element.find(".prop_apply").bind('click.itop-property-field', function() { me._do_apply(); });
  27. this.element.find(".prop_cancel").bind('click.itop-property-field', function() { me._do_cancel(); });
  28. this._refresh();
  29. },
  30. // called when created, and later when changing options
  31. _refresh: function()
  32. {
  33. if (this.bModified)
  34. {
  35. this.element.find(".prop_icon span.ui-icon").css({visibility: ''});
  36. }
  37. else
  38. {
  39. this.element.find(".prop_icon span.ui-icon").css({visibility: 'hidden'});
  40. }
  41. },
  42. // events bound via _bind are removed automatically
  43. // revert other modifications here
  44. _destroy: function()
  45. {
  46. this.element.removeClass( "itop-property-field" );
  47. },
  48. // _setOptions is called with a hash of all options that are changing
  49. // always refresh when changing options
  50. _setOptions: function()
  51. {
  52. // in 1.9 would use _superApply
  53. $.Widget.prototype._setOptions.apply( this, arguments );
  54. this._refresh();
  55. },
  56. // _setOption is called for each individual option that is changing
  57. _setOption: function( key, value )
  58. {
  59. // in 1.9 would use _super
  60. $.Widget.prototype._setOption.call( this, key, value );
  61. },
  62. _on_change: function()
  63. {
  64. var new_value = this._get_field_value();
  65. if (new_value != this.value)
  66. {
  67. this.bModified = true;
  68. }
  69. else
  70. {
  71. this.bModified = false;
  72. }
  73. this._refresh();
  74. },
  75. _get_field_value: function()
  76. {
  77. var oField = $('#'+this.options.field_id);
  78. if (oField.attr('type') == 'checkbox')
  79. {
  80. return (oField.attr('checked') == 'checked');
  81. }
  82. else
  83. {
  84. return oField.val();
  85. }
  86. },
  87. _get_committed_value: function()
  88. {
  89. return { name: $('#'+this.options.field_id).attr('name'), value: this.value };
  90. },
  91. _do_apply: function()
  92. {
  93. // Validate the field
  94. sFormId = this.element.closest('form').attr('id');
  95. var oField = $('#'+this.options.field_id);
  96. oField.trigger('validate');
  97. if ( $.inArray(this.options.field_id, oFormValidation[sFormId]) == -1)
  98. {
  99. this.bModified = false;
  100. this.previous_value = this.value;
  101. this.value = this._get_field_value();
  102. this._do_submit();
  103. this._refresh();
  104. }
  105. },
  106. _do_cancel: function()
  107. {
  108. this.bModified = false;
  109. var oField = $('#'+this.options.field_id);
  110. if (oField.attr('type') == 'checkbox')
  111. {
  112. if (this.value)
  113. {
  114. oField.attr('checked', true);
  115. }
  116. else
  117. {
  118. oField.removeAttr('checked');
  119. }
  120. }
  121. else
  122. {
  123. oField.val(this.value);
  124. }
  125. this._refresh();
  126. oField.trigger('reverted', {type: 'designer', previous_value: this.value });
  127. oField.trigger('validate');
  128. },
  129. _do_submit: function()
  130. {
  131. var oData = {};
  132. this.element.closest('form').find(':input[type=hidden]').each(function()
  133. {
  134. // Hidden form fields
  135. oData[$(this).attr('name')] = $(this).val();
  136. });
  137. this.element.closest('form').find('.itop-property-field').each(function()
  138. {
  139. var oWidget = $(this).data('property_field');
  140. if (oWidget)
  141. {
  142. var oVal = oWidget._get_committed_value();
  143. oData[oVal.name] = oVal.value;
  144. }
  145. });
  146. oPostedData = this.options.submit_parameters;
  147. oPostedData.params = oData;
  148. oPostedData.params.updated = [ $('#'+this.options.field_id).attr('name') ]; // only one field updated in this case
  149. oPostedData.params.previous_values = {};
  150. oPostedData.params.previous_values[oPostedData.params.updated] = this.previous_value; // pass also the previous value(s)
  151. $.post(this.options.submit_to, oPostedData, function(data)
  152. {
  153. $('#prop_submit_result').html(data);
  154. });
  155. }
  156. });
  157. });
  158. var oFormValidation = {};
  159. function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId)
  160. {
  161. var currentVal = $('#'+sFieldId).val();
  162. var bValid = true;
  163. if (bMandatory && (currentVal == ''))
  164. {
  165. bValid = false;
  166. }
  167. if ((sPattern != '') && (currentVal != ''))
  168. {
  169. re = new RegExp(sPattern);
  170. bValid = re.test(currentVal);
  171. }
  172. if (!bValid)
  173. {
  174. $('#v_'+sFieldId).html('<img style="vertical-align:middle;" src="'+GetAbsoluteUrlAppRoot()+'images/validation_error.png">');
  175. if (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = [];
  176. oFormValidation[sFormId].push(sFieldId);
  177. }
  178. else
  179. {
  180. $('#v_'+sFieldId).html('');
  181. }
  182. }
  183. function ValidateForm(sFormId, bValidateAll)
  184. {
  185. oFormValidation[sFormId] = [];
  186. if (bValidateAll)
  187. {
  188. $('#'+sFormId+' :input').trigger('validate');
  189. }
  190. else
  191. {
  192. // Only the visible fields
  193. $('#'+sFormId+' :input:visible').each(function() {
  194. $(this).trigger('validate');
  195. });
  196. }
  197. return oFormValidation[sFormId];
  198. }
  199. function ReadFormParams(sFormId)
  200. {
  201. var oMap = { };
  202. $('#'+sFormId+' :input').each( function() {
  203. var sName = $(this).attr('name');
  204. if (sName && sName != '')
  205. {
  206. if (this.type == 'checkbox')
  207. {
  208. oMap[sName] = ($(this).attr('checked') == 'checked');
  209. }
  210. else
  211. {
  212. oMap[sName] = $(this).val();
  213. }
  214. }
  215. });
  216. return oMap;
  217. }
  218. function SubmitForm(sFormId, onSubmitResult)
  219. {
  220. var aErrors = ValidateForm(sFormId, false);
  221. if (aErrors.length == 0)
  222. {
  223. var oMap = ReadFormParams(sFormId);
  224. oMap.module_name = sCurrentModule;
  225. $('#'+sFormId+' :input').each( function() {
  226. var sName = $(this).attr('name');
  227. if (sName && sName != '')
  228. {
  229. if (this.type == 'checkbox')
  230. {
  231. oMap[sName] = ($(this).attr('checked') == 'checked');
  232. }
  233. else
  234. {
  235. oMap[sName] = $(this).val();
  236. }
  237. }
  238. });
  239. $.post(GetAbsoluteUrlAppRoot()+'designer/module.php', oMap, function(data)
  240. {
  241. onSubmitResult(data);
  242. });
  243. }
  244. else
  245. {
  246. // TODO: better error reporting !!!
  247. alert('Please fill all the fields before continuing...');
  248. }
  249. }