property_field.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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)
  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. });
  175. });
  176. var oFormValidation = {};
  177. function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId)
  178. {
  179. var currentVal = $('#'+sFieldId).val();
  180. var bValid = true;
  181. if (bMandatory && (currentVal == ''))
  182. {
  183. bValid = false;
  184. }
  185. if ((sPattern != '') && (currentVal != ''))
  186. {
  187. re = new RegExp(sPattern);
  188. bValid = re.test(currentVal);
  189. }
  190. if (!bValid)
  191. {
  192. $('#v_'+sFieldId).html('<img style="vertical-align:middle;" src="'+GetAbsoluteUrlAppRoot()+'images/validation_error.png">');
  193. if (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = [];
  194. oFormValidation[sFormId].push(sFieldId);
  195. }
  196. else
  197. {
  198. $('#v_'+sFieldId).html('');
  199. }
  200. }
  201. function ValidateForm(sFormId, bValidateAll)
  202. {
  203. oFormValidation[sFormId] = [];
  204. if (bValidateAll)
  205. {
  206. $('#'+sFormId+' :input').trigger('validate');
  207. }
  208. else
  209. {
  210. // Only the visible fields
  211. $('#'+sFormId+' :input:visible').each(function() {
  212. $(this).trigger('validate');
  213. });
  214. }
  215. return oFormValidation[sFormId];
  216. }
  217. function ReadFormParams(sFormId)
  218. {
  219. var oMap = { };
  220. $('#'+sFormId+' :input').each( function() {
  221. if ($(this).parent().is(':visible'))
  222. {
  223. var sName = $(this).attr('name');
  224. if (sName && sName != '')
  225. {
  226. if (this.type == 'checkbox')
  227. {
  228. oMap[sName] = ($(this).attr('checked') == 'checked');
  229. }
  230. else
  231. {
  232. oMap[sName] = $(this).val();
  233. }
  234. }
  235. }
  236. });
  237. return oMap;
  238. }
  239. function SubmitForm(sFormId, onSubmitResult)
  240. {
  241. var aErrors = ValidateForm(sFormId, false);
  242. if (aErrors.length == 0)
  243. {
  244. var oMap = ReadFormParams(sFormId);
  245. oMap.module_name = sCurrentModule;
  246. $('#'+sFormId+' :input').each( function() {
  247. if ($(this).parent().is(':visible'))
  248. {
  249. var sName = $(this).attr('name');
  250. if (sName && sName != '')
  251. {
  252. if (this.type == 'checkbox')
  253. {
  254. oMap[sName] = ($(this).attr('checked') == 'checked');
  255. }
  256. else
  257. {
  258. oMap[sName] = $(this).val();
  259. }
  260. }
  261. }
  262. });
  263. $.post(GetAbsoluteUrlAppRoot()+'designer/module.php', oMap, function(data)
  264. {
  265. onSubmitResult(data);
  266. });
  267. }
  268. else
  269. {
  270. // TODO: better error reporting !!!
  271. alert('Please fill all the fields before continuing...');
  272. }
  273. }