property_field.js 6.7 KB

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