property_field.js 7.6 KB

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