property_field.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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, aForbiddenValues, sExplainForbiddenValues)
  205. {
  206. var currentVal = $('#'+sFieldId).val();
  207. var bValid = true;
  208. var sMessage = null;
  209. if (bMandatory && (currentVal == ''))
  210. {
  211. bValid = false;
  212. }
  213. if ((sPattern != '') && (currentVal != ''))
  214. {
  215. re = new RegExp(sPattern);
  216. bValid = re.test(currentVal);
  217. }
  218. if (aForbiddenValues)
  219. {
  220. for(var i in aForbiddenValues)
  221. {
  222. if (aForbiddenValues[i] == currentVal)
  223. {
  224. bValid = false;
  225. sMessage = sExplainForbiddenValues;
  226. break;
  227. }
  228. }
  229. }
  230. if (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = [];
  231. if (!bValid)
  232. {
  233. $('#v_'+sFieldId).addClass('ui-state-error');
  234. oFormValidation[sFormId].push(sFieldId);
  235. if (sMessage)
  236. {
  237. $('#'+sFieldId).attr('title', sMessage).tooltip();
  238. if ($('#'+sFieldId).is(":focus"))
  239. {
  240. $('#'+sFieldId).tooltip('open');
  241. }
  242. }
  243. }
  244. else
  245. {
  246. $('#v_'+sFieldId).removeClass('ui-state-error');
  247. if ($('#'+sFieldId).data('uiTooltip'))
  248. {
  249. $('#'+sFieldId).tooltip('close');
  250. }
  251. $('#'+sFieldId).removeAttr('title');
  252. // Remove the element from the array
  253. iFieldIdPos = jQuery.inArray(sFieldId, oFormValidation[sFormId]);
  254. if (iFieldIdPos > -1)
  255. {
  256. oFormValidation[sFormId].splice(iFieldIdPos, 1);
  257. }
  258. }
  259. }
  260. function ValidateForm(sFormId, bValidateAll)
  261. {
  262. oFormValidation[sFormId] = [];
  263. if (bValidateAll)
  264. {
  265. $('#'+sFormId+' :input').trigger('validate');
  266. }
  267. else
  268. {
  269. // Only the visible fields
  270. $('#'+sFormId+' :input:visible').each(function() {
  271. $(this).trigger('validate');
  272. });
  273. }
  274. return oFormValidation[sFormId];
  275. }
  276. function ReadFormParams(sFormId)
  277. {
  278. var oMap = { };
  279. $('#'+sFormId+' :input').each( function() {
  280. if ($(this).parent().is(':visible'))
  281. {
  282. var sName = $(this).attr('name');
  283. if (sName && sName != '')
  284. {
  285. if (this.type == 'checkbox')
  286. {
  287. oMap[sName] = ($(this).attr('checked') == 'checked');
  288. }
  289. else
  290. {
  291. oMap[sName] = $(this).val();
  292. }
  293. }
  294. }
  295. });
  296. return oMap;
  297. }
  298. function SubmitForm(sFormId, onSubmitResult)
  299. {
  300. var aErrors = ValidateForm(sFormId, false);
  301. if (aErrors.length == 0)
  302. {
  303. var oMap = ReadFormParams(sFormId);
  304. oMap.module_name = sCurrentModule;
  305. $('#'+sFormId+' :input').each( function() {
  306. if ($(this).parent().is(':visible'))
  307. {
  308. var sName = $(this).attr('name');
  309. if (sName && sName != '')
  310. {
  311. if (this.type == 'checkbox')
  312. {
  313. oMap[sName] = ($(this).attr('checked') == 'checked');
  314. }
  315. else
  316. {
  317. oMap[sName] = $(this).val();
  318. }
  319. }
  320. }
  321. });
  322. $.post(GetAbsoluteUrlAppRoot()+'designer/module.php', oMap, function(data)
  323. {
  324. onSubmitResult(data);
  325. });
  326. }
  327. else
  328. {
  329. // TODO: better error reporting !!!
  330. alert('Please fill all the fields before continuing...');
  331. }
  332. }