property_field.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. get_field_value: null,
  14. equals: null,
  15. submit_to: 'index.php',
  16. submit_parameters: {operation: 'async_action'},
  17. do_apply: null,
  18. do_cancel: null,
  19. auto_apply: false,
  20. can_apply: true
  21. },
  22. // the constructor
  23. _create: function()
  24. {
  25. var me = this;
  26. this.element
  27. .addClass( "itop-property-field" )
  28. .bind('apply_changes.itop-property-field', function(){me._do_apply();} );
  29. this.bModified = false;
  30. if (this.options.field_id != '')
  31. {
  32. // In case there is an hidden input having the same id (somewhere else in the page), the change event does not occur unless the input loses the focus
  33. // To reduce the impact, let's handle keyup as well
  34. $('#'+this.options.field_id, this.element).bind('change.itop-property-field keyup.itop-property-field', function() { me._on_change(); });
  35. this.value = this._get_field_value();
  36. }
  37. this.element.find(".prop_apply").bind('click.itop-property-field', function() { me._do_apply(); });
  38. this.element.find(".prop_cancel").bind('click.itop-property-field', function() { me._do_cancel(); });
  39. this._refresh();
  40. },
  41. // called when created, and later when changing options
  42. _refresh: function()
  43. {
  44. if (this.bModified)
  45. {
  46. this.element.addClass("itop-property-field-modified");
  47. if (this.options.can_apply)
  48. {
  49. this.element.find(".prop_icon span.ui-icon-circle-check").css({visibility: ''});
  50. }
  51. else
  52. {
  53. this.element.find(".prop_icon span.ui-icon-circle-check").css({visibility: 'hidden'});
  54. }
  55. this.element.find(".prop_icon span.ui-icon-circle-close").css({visibility: ''});
  56. }
  57. else
  58. {
  59. this.element.removeClass("itop-property-field-modified");
  60. this.element.find(".prop_icon span.ui-icon-circle-check").css({visibility: 'hidden'});
  61. this.element.find(".prop_icon span.ui-icon-circle-close").css({visibility: 'hidden'});
  62. }
  63. },
  64. // events bound via _bind are removed automatically
  65. // revert other modifications here
  66. _destroy: function()
  67. {
  68. this.element.removeClass( "itop-property-field" );
  69. this.element.removeClass("itop-property-field-modified");
  70. },
  71. // _setOptions is called with a hash of all options that are changing
  72. // always refresh when changing options
  73. _setOptions: function()
  74. {
  75. // in 1.9 would use _superApply
  76. this._superApply(arguments);
  77. this._refresh();
  78. },
  79. // _setOption is called for each individual option that is changing
  80. _setOption: function( key, value )
  81. {
  82. // in 1.9 would use _super
  83. this._superApply(arguments);
  84. },
  85. _on_change: function()
  86. {
  87. var new_value = this._get_field_value();
  88. if (this._equals(new_value, this.value))
  89. {
  90. this.bModified = false;
  91. }
  92. else
  93. {
  94. this.bModified = true;
  95. if (this.options.auto_apply && this.options.can_apply)
  96. {
  97. this._do_apply();
  98. }
  99. }
  100. this._refresh();
  101. if (this.options.parent_selector)
  102. {
  103. $(this.options.parent_selector).trigger('subitem_changed');
  104. }
  105. },
  106. _equals: function( value1, value2 )
  107. {
  108. if (this.options.equals === null)
  109. {
  110. return value1 == value2;
  111. }
  112. else
  113. {
  114. return this.options.equals(value1, value2);
  115. }
  116. },
  117. _get_field_value: function()
  118. {
  119. if (this.options.get_field_value === null)
  120. {
  121. var oField = $('#'+this.options.field_id, this.element);
  122. if (oField.attr('type') == 'checkbox')
  123. {
  124. return (oField.attr('checked') == 'checked');
  125. }
  126. else
  127. {
  128. return oField.val();
  129. }
  130. }
  131. else
  132. {
  133. return this.options.get_field_value();
  134. }
  135. },
  136. get_field_name: function()
  137. {
  138. return $('#'+this.options.field_id, this.element).attr('name');
  139. },
  140. _get_committed_value: function()
  141. {
  142. return { name: $('#'+this.options.field_id, this.element).attr('name'), value: this.value };
  143. },
  144. _get_current_value: function()
  145. {
  146. return { name: $('#'+this.options.field_id, this.element).attr('name'), value: this._get_field_value() };
  147. },
  148. _do_apply: function()
  149. {
  150. if (this.options.parent_selector)
  151. {
  152. $(this.options.parent_selector).trigger('mark_as_modified');
  153. }
  154. if (this.options.do_apply)
  155. {
  156. // specific behavior...
  157. if (this.options.do_apply())
  158. {
  159. this.bModified = false;
  160. this.previous_value = this.value;
  161. this.value = this._get_field_value();
  162. this._refresh();
  163. }
  164. }
  165. else
  166. {
  167. // Validate the field
  168. sFormId = this.element.closest('form').attr('id');
  169. var oField = $('#'+this.options.field_id, this.element);
  170. oField.trigger('validate');
  171. if ( $.inArray(this.options.field_id, oFormValidation[sFormId]) == -1)
  172. {
  173. this.bModified = false;
  174. this.previous_value = this.value;
  175. this.value = this._get_field_value();
  176. this._do_submit();
  177. this._refresh();
  178. }
  179. }
  180. },
  181. _do_cancel: function()
  182. {
  183. if (this.options.do_cancel)
  184. {
  185. // specific behavior...
  186. this.options.do_cancel();
  187. }
  188. else
  189. {
  190. this.bModified = false;
  191. var oField = $('#'+this.options.field_id, this.element);
  192. if (oField.attr('type') == 'checkbox')
  193. {
  194. if (this.value)
  195. {
  196. oField.attr('checked', true);
  197. }
  198. else
  199. {
  200. oField.removeAttr('checked');
  201. }
  202. }
  203. else
  204. {
  205. oField.val(this.value);
  206. }
  207. this._refresh();
  208. oField.trigger('reverted', {type: 'designer', previous_value: this.value });
  209. oField.trigger('validate');
  210. if (this.options.parent_selector)
  211. {
  212. $(this.options.parent_selector).trigger('subitem_changed');
  213. }
  214. }
  215. },
  216. _do_submit: function()
  217. {
  218. var oData = {};
  219. this.element.closest('form').find(':input[type=hidden]').each(function()
  220. {
  221. // Hidden form fields
  222. oData[$(this).attr('name')] = $(this).val();
  223. });
  224. this.element.closest('form').find('.itop-property-field').each(function()
  225. {
  226. var oWidget = $(this).data('itopProperty_field');
  227. if (oWidget && oWidget._is_visible())
  228. {
  229. var oVal = oWidget._get_committed_value();
  230. oData[oVal.name] = oVal.value;
  231. }
  232. });
  233. oPostedData = this.options.submit_parameters;
  234. oPostedData.params = oData;
  235. oPostedData.params.updated = [ $('#'+this.options.field_id, this.element).attr('name') ]; // only one field updated in this case
  236. oPostedData.params.previous_values = {};
  237. oPostedData.params.previous_values[oPostedData.params.updated] = this.previous_value; // pass also the previous value(s)
  238. $.post(this.options.submit_to, oPostedData, function(data)
  239. {
  240. $('#prop_submit_result').html(data);
  241. });
  242. },
  243. _is_visible: function()
  244. {
  245. return this.element.is(':visible');
  246. },
  247. mark_as_applied: function()
  248. {
  249. this.bModified = false;
  250. this.previous_value = this.value;
  251. this.value = this._get_field_value();
  252. this._refresh();
  253. },
  254. validate: function()
  255. {
  256. var oField = $('#'+this.options.field_id, this.element);
  257. oField.trigger('validate');
  258. }
  259. });
  260. });
  261. $(function()
  262. {
  263. // the widget definition, where "itop" is the namespace,
  264. // "selector_property_field" the widget name
  265. $.widget( "itop.selector_property_field", $.itop.property_field,
  266. {
  267. // default options
  268. options:
  269. {
  270. parent_selector: null,
  271. field_id: '',
  272. get_field_value: null,
  273. equals: null,
  274. submit_to: 'index.php',
  275. submit_parameters: {operation: 'async_action'},
  276. do_apply: null,
  277. do_cancel: null,
  278. auto_apply: false,
  279. can_apply: true,
  280. data_selector: ''
  281. },
  282. // the constructor
  283. _create: function()
  284. {
  285. var me = this;
  286. this._superApply();
  287. this.element
  288. .addClass( "itop-selector-property-field" );
  289. $('#'+this.options.field_id).bind('reverted init', function() {
  290. me._update_subform();
  291. }).trigger('init'); // initial refresh
  292. this.element.bind('subitem_changed', function() {
  293. me._on_subitem_changed();
  294. });
  295. },
  296. _update_subform: function()
  297. {
  298. var sSelector = this.options.data_selector;
  299. var me = this;
  300. // Mark all the direct children as hidden
  301. $('tr[data-selector="'+sSelector+'"]').attr('data-state', 'hidden');
  302. // Mark the selected one as visible
  303. var sSelectedHierarchy = sSelector+'-'+$('#'+this.options.field_id).val();
  304. $('tr[data-path="'+sSelectedHierarchy+'"]').attr('data-state', 'visible');
  305. // Show all items behind the current one
  306. $('tr[data-path^="'+sSelector+'"]').show();
  307. // Hide items behind the current one as soon as it is behind a hidden node (or itself is marked as hidden)
  308. $('tr[data-path^="'+sSelector+'"][data-state="hidden"]').each(function() {
  309. $(this).hide();
  310. var sPath = $(this).attr('data-path');
  311. $('tr[data-path^="'+sPath+'/"]').hide();
  312. });
  313. $('tr[data-path^="'+sSelector+'"]').each(function() {
  314. if($(this).is(':visible'))
  315. {
  316. var oPropField = $(this).closest('.itop-property-field');
  317. oPropField.property_field('option', {can_apply: !me.bModified, parent_selector: '#'+me.element.attr('id') });
  318. oPropField.property_field('validate');
  319. }
  320. });
  321. },
  322. // events bound via _bind are removed automatically
  323. // revert other modifications here
  324. _destroy: function()
  325. {
  326. this.element.removeClass( "itop-selector-property-field" );
  327. this._superApply();
  328. },
  329. _on_change: function()
  330. {
  331. var new_value = this._get_field_value();
  332. if (this._equals(new_value, this.value))
  333. {
  334. this.bModified = false;
  335. }
  336. else
  337. {
  338. this.bModified = true;
  339. }
  340. this._update_subform();
  341. if (this.options.auto_apply && this.options.can_apply)
  342. {
  343. this._do_apply();
  344. }
  345. this._on_subitem_changed(); // initial validation
  346. this._refresh();
  347. },
  348. _do_apply: function()
  349. {
  350. this._superApply();
  351. this._update_subform();
  352. },
  353. _do_submit: function()
  354. {
  355. var oData = {};
  356. this.element.closest('form').find(':input[type=hidden]').each(function()
  357. {
  358. // Hidden form fields
  359. oData[$(this).attr('name')] = $(this).val();
  360. });
  361. var sSelector = this.options.data_selector;
  362. var me = this;
  363. var aUpdated = [];
  364. $('tr[data-path^="'+sSelector+'"]').each(function() {
  365. if($(this).is(':visible'))
  366. {
  367. var sName = $(this).closest('.itop-property-field').property_field('mark_as_applied').property_field('get_field_name');
  368. if (typeof sName == 'string')
  369. {
  370. aUpdated.push(sName);
  371. }
  372. }
  373. });
  374. this.element.closest('form').find('.itop-property-field').each(function()
  375. {
  376. var oWidget = $(this).data('itopProperty_field');
  377. if (!oWidget)
  378. {
  379. // try the form selector widget
  380. oWidget = $(this).data('itopSelector_property_field');
  381. }
  382. if (oWidget && oWidget._is_visible())
  383. {
  384. var oVal = oWidget._get_committed_value();
  385. oData[oVal.name] = oVal.value;
  386. }
  387. });
  388. var oPostedData = this.options.submit_parameters;
  389. var sName = $('#'+this.options.field_id, this.element).attr('name');
  390. oPostedData.params = oData;
  391. oPostedData.params.updated = [];
  392. aUpdated.push(sName); // several fields updated in this case
  393. oPostedData.params.updated = aUpdated;
  394. oPostedData.params.previous_values = {};
  395. oPostedData.params.previous_values[sName] = this.previous_value; // pass also the previous value(s)
  396. $.post(this.options.submit_to, oPostedData, function(data)
  397. {
  398. $('#prop_submit_result').html(data);
  399. });
  400. },
  401. _on_subitem_changed : function()
  402. {
  403. sFormId = this.element.closest('form').attr('id');
  404. oFormValidation[sFormId] = [];
  405. this.options.can_apply = true;
  406. var sSelector = this.options.data_selector
  407. $('tr[data-path^="'+sSelector+'"]').each(function() {
  408. if($(this).is(':visible'))
  409. {
  410. var oPropField = $(this).closest('.itop-property-field');
  411. oPropField.property_field('validate');
  412. }
  413. });
  414. this.options.can_apply = (oFormValidation[sFormId].length == 0); // apply allowed only if no error
  415. this._refresh();
  416. }
  417. });
  418. });
  419. var oFormValidation = {};
  420. function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId, aForbiddenValues, sExplainForbiddenValues)
  421. {
  422. var currentVal = $('#'+sFieldId).val();
  423. var bValid = true;
  424. var sMessage = null;
  425. if (bMandatory && (currentVal == ''))
  426. {
  427. bValid = false;
  428. }
  429. if ((sPattern != '') && (currentVal != ''))
  430. {
  431. re = new RegExp(sPattern);
  432. bValid = re.test(currentVal);
  433. }
  434. if (aForbiddenValues)
  435. {
  436. for(var i in aForbiddenValues)
  437. {
  438. if (aForbiddenValues[i] == currentVal)
  439. {
  440. bValid = false;
  441. sMessage = sExplainForbiddenValues;
  442. break;
  443. }
  444. }
  445. }
  446. if (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = [];
  447. if (!bValid)
  448. {
  449. $('#v_'+sFieldId).addClass('ui-state-error');
  450. iFieldIdPos = jQuery.inArray(sFieldId, oFormValidation[sFormId]);
  451. if (iFieldIdPos == -1)
  452. {
  453. oFormValidation[sFormId].push(sFieldId);
  454. }
  455. if (sMessage)
  456. {
  457. $('#'+sFieldId).attr('title', sMessage).tooltip();
  458. if ($('#'+sFieldId).is(":focus"))
  459. {
  460. $('#'+sFieldId).tooltip('open');
  461. }
  462. }
  463. }
  464. else
  465. {
  466. $('#v_'+sFieldId).removeClass('ui-state-error');
  467. if ($('#'+sFieldId).data('uiTooltip'))
  468. {
  469. $('#'+sFieldId).tooltip('close');
  470. }
  471. $('#'+sFieldId).removeAttr('title');
  472. // Remove the element from the array
  473. iFieldIdPos = jQuery.inArray(sFieldId, oFormValidation[sFormId]);
  474. if (iFieldIdPos > -1)
  475. {
  476. oFormValidation[sFormId].splice(iFieldIdPos, 1);
  477. }
  478. }
  479. }
  480. function ValidateInteger(sFieldId, bMandatory, sFormId, iMin, iMax, sExplainFormat)
  481. {
  482. var currentVal = $('#'+sFieldId).val();
  483. var bValid = true;
  484. var sMessage = null;
  485. if (bMandatory && (currentVal == ''))
  486. {
  487. bValid = false;
  488. }
  489. re = new RegExp('^$|^-?[0-9]+$');
  490. bValid = re.test(currentVal);
  491. if (bValid && (currentVal != ''))
  492. {
  493. // It is a valid number, let's check the boundaries
  494. var iValue = parseInt(currentVal, 10);
  495. if ((iMin != null) && (iValue < iMin))
  496. {
  497. bValid = false;
  498. }
  499. if ((iMax != null) && (iValue > iMax))
  500. {
  501. bValid = false;
  502. }
  503. if (!bValid && (sExplainFormat != undefined))
  504. {
  505. sMessage = sExplainFormat;
  506. }
  507. }
  508. if (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = [];
  509. if (!bValid)
  510. {
  511. $('#v_'+sFieldId).addClass('ui-state-error');
  512. iFieldIdPos = jQuery.inArray(sFieldId, oFormValidation[sFormId]);
  513. if (iFieldIdPos == -1)
  514. {
  515. oFormValidation[sFormId].push(sFieldId);
  516. }
  517. if (sMessage)
  518. {
  519. $('#'+sFieldId).attr('title', sMessage).tooltip();
  520. if ($('#'+sFieldId).is(":focus"))
  521. {
  522. $('#'+sFieldId).tooltip('open');
  523. }
  524. }
  525. }
  526. else
  527. {
  528. $('#v_'+sFieldId).removeClass('ui-state-error');
  529. if ($('#'+sFieldId).data('uiTooltip'))
  530. {
  531. $('#'+sFieldId).tooltip('close');
  532. }
  533. $('#'+sFieldId).removeAttr('title');
  534. // Remove the element from the array
  535. iFieldIdPos = jQuery.inArray(sFieldId, oFormValidation[sFormId]);
  536. if (iFieldIdPos > -1)
  537. {
  538. oFormValidation[sFormId].splice(iFieldIdPos, 1);
  539. }
  540. }
  541. }
  542. function ValidateForm(sFormId, bValidateAll)
  543. {
  544. oFormValidation[sFormId] = [];
  545. if (bValidateAll)
  546. {
  547. $('#'+sFormId+' :input').trigger('validate');
  548. }
  549. else
  550. {
  551. // Only the visible fields
  552. $('#'+sFormId+' :input:visible').each(function() {
  553. $(this).trigger('validate');
  554. });
  555. }
  556. return oFormValidation[sFormId];
  557. }
  558. function ReadFormParams(sFormId)
  559. {
  560. var oMap = { };
  561. $('#'+sFormId+' :input').each( function() {
  562. if ($(this).parent().is(':visible'))
  563. {
  564. var sName = $(this).attr('name');
  565. if (sName && sName != '')
  566. {
  567. if (this.type == 'checkbox')
  568. {
  569. oMap[sName] = ($(this).attr('checked') == 'checked');
  570. }
  571. else
  572. {
  573. oMap[sName] = $(this).val();
  574. }
  575. }
  576. }
  577. });
  578. return oMap;
  579. }
  580. function SubmitForm(sFormId, onSubmitResult)
  581. {
  582. var aErrors = ValidateForm(sFormId, false);
  583. if (aErrors.length == 0)
  584. {
  585. var oMap = ReadFormParams(sFormId);
  586. oMap.module_name = sCurrentModule;
  587. $('#'+sFormId+' :input').each( function() {
  588. if ($(this).parent().is(':visible'))
  589. {
  590. var sName = $(this).attr('name');
  591. if (sName && sName != '')
  592. {
  593. if (this.type == 'checkbox')
  594. {
  595. oMap[sName] = ($(this).attr('checked') == 'checked');
  596. }
  597. else
  598. {
  599. oMap[sName] = $(this).val();
  600. }
  601. }
  602. }
  603. });
  604. $.post(GetAbsoluteUrlAppRoot()+'designer/module.php', oMap, function(data)
  605. {
  606. onSubmitResult(data);
  607. });
  608. }
  609. else
  610. {
  611. // TODO: better error reporting !!!
  612. alert('Please fill all the fields before continuing...');
  613. }
  614. }
  615. function RemoveSubForm(sId, sUrl, oParams)
  616. {
  617. $.post(sUrl, oParams, function(data) {
  618. $('body').append(data);
  619. });
  620. }
  621. function AddSubForm(sId, sUrl, oParams)
  622. {
  623. var aIndexes = JSON.parse($('#'+sId).val());
  624. var iLast = aIndexes[aIndexes.length - 1];
  625. var iNewIdx = 1 + iLast;
  626. oParams.new_index = iNewIdx;
  627. $.post(sUrl, oParams, function(data) {
  628. $('body').append(data);
  629. });
  630. }