property_field.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (this.options.do_apply())
  99. {
  100. this.bModified = false;
  101. this.previous_value = this.value;
  102. this.value = this._get_field_value();
  103. this._refresh();
  104. }
  105. }
  106. else
  107. {
  108. // Validate the field
  109. sFormId = this.element.closest('form').attr('id');
  110. var oField = $('#'+this.options.field_id);
  111. oField.trigger('validate');
  112. if ( $.inArray(this.options.field_id, oFormValidation[sFormId]) == -1)
  113. {
  114. this.bModified = false;
  115. this.previous_value = this.value;
  116. this.value = this._get_field_value();
  117. this._do_submit();
  118. this._refresh();
  119. }
  120. }
  121. },
  122. _do_cancel: function()
  123. {
  124. if (this.options.do_cancel)
  125. {
  126. // specific behavior...
  127. this.options.do_cancel();
  128. }
  129. else
  130. {
  131. this.bModified = false;
  132. var oField = $('#'+this.options.field_id);
  133. if (oField.attr('type') == 'checkbox')
  134. {
  135. if (this.value)
  136. {
  137. oField.attr('checked', true);
  138. }
  139. else
  140. {
  141. oField.removeAttr('checked');
  142. }
  143. }
  144. else
  145. {
  146. oField.val(this.value);
  147. }
  148. this._refresh();
  149. oField.trigger('reverted', {type: 'designer', previous_value: this.value });
  150. oField.trigger('validate');
  151. }
  152. },
  153. _do_submit: function()
  154. {
  155. var oData = {};
  156. this.element.closest('form').find(':input[type=hidden]').each(function()
  157. {
  158. // Hidden form fields
  159. oData[$(this).attr('name')] = $(this).val();
  160. });
  161. this.element.closest('form').find('.itop-property-field').each(function()
  162. {
  163. var oWidget = $(this).data('property_field');
  164. if (oWidget && oWidget._is_visible())
  165. {
  166. var oVal = oWidget._get_committed_value();
  167. oData[oVal.name] = oVal.value;
  168. }
  169. });
  170. oPostedData = this.options.submit_parameters;
  171. oPostedData.params = oData;
  172. oPostedData.params.updated = [ $('#'+this.options.field_id).attr('name') ]; // only one field updated in this case
  173. oPostedData.params.previous_values = {};
  174. oPostedData.params.previous_values[oPostedData.params.updated] = this.previous_value; // pass also the previous value(s)
  175. $.post(this.options.submit_to, oPostedData, function(data)
  176. {
  177. $('#prop_submit_result').html(data);
  178. });
  179. },
  180. _is_visible: function()
  181. {
  182. return this.element.parent().is(':visible');
  183. }
  184. });
  185. });
  186. var oFormValidation = {};
  187. function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId)
  188. {
  189. var currentVal = $('#'+sFieldId).val();
  190. var bValid = true;
  191. if (bMandatory && (currentVal == ''))
  192. {
  193. bValid = false;
  194. }
  195. if ((sPattern != '') && (currentVal != ''))
  196. {
  197. re = new RegExp(sPattern);
  198. bValid = re.test(currentVal);
  199. }
  200. if (!bValid)
  201. {
  202. $('#v_'+sFieldId).html('<img style="vertical-align:middle;" src="'+GetAbsoluteUrlAppRoot()+'images/validation_error.png">');
  203. if (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = [];
  204. oFormValidation[sFormId].push(sFieldId);
  205. }
  206. else
  207. {
  208. $('#v_'+sFieldId).html('');
  209. }
  210. }
  211. function ValidateForm(sFormId, bValidateAll)
  212. {
  213. oFormValidation[sFormId] = [];
  214. if (bValidateAll)
  215. {
  216. $('#'+sFormId+' :input').trigger('validate');
  217. }
  218. else
  219. {
  220. // Only the visible fields
  221. $('#'+sFormId+' :input:visible').each(function() {
  222. $(this).trigger('validate');
  223. });
  224. }
  225. return oFormValidation[sFormId];
  226. }
  227. function ReadFormParams(sFormId)
  228. {
  229. var oMap = { };
  230. $('#'+sFormId+' :input').each( function() {
  231. if ($(this).parent().is(':visible'))
  232. {
  233. var sName = $(this).attr('name');
  234. if (sName && sName != '')
  235. {
  236. if (this.type == 'checkbox')
  237. {
  238. oMap[sName] = ($(this).attr('checked') == 'checked');
  239. }
  240. else
  241. {
  242. oMap[sName] = $(this).val();
  243. }
  244. }
  245. }
  246. });
  247. return oMap;
  248. }
  249. function SubmitForm(sFormId, onSubmitResult)
  250. {
  251. var aErrors = ValidateForm(sFormId, false);
  252. if (aErrors.length == 0)
  253. {
  254. var oMap = ReadFormParams(sFormId);
  255. oMap.module_name = sCurrentModule;
  256. $('#'+sFormId+' :input').each( function() {
  257. if ($(this).parent().is(':visible'))
  258. {
  259. var sName = $(this).attr('name');
  260. if (sName && sName != '')
  261. {
  262. if (this.type == 'checkbox')
  263. {
  264. oMap[sName] = ($(this).attr('checked') == 'checked');
  265. }
  266. else
  267. {
  268. oMap[sName] = $(this).val();
  269. }
  270. }
  271. }
  272. });
  273. $.post(GetAbsoluteUrlAppRoot()+'designer/module.php', oMap, function(data)
  274. {
  275. onSubmitResult(data);
  276. });
  277. }
  278. else
  279. {
  280. // TODO: better error reporting !!!
  281. alert('Please fill all the fields before continuing...');
  282. }
  283. }