linkswidget.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // JavaScript Document
  2. function LinksWidget(id, sLinkedClass, sExtKeyToMe, sExtKeyToRemote, aAttributes)
  3. {
  4. this.id = id;
  5. this.sLinkedClass = sLinkedClass;
  6. this.sExtKeyToMe = sExtKeyToMe;
  7. this.sExtKeyToRemote = sExtKeyToRemote;
  8. this.aAttributes = aAttributes;
  9. this.aLinks = new Array();
  10. this.Init = function()
  11. {
  12. sLinks = $('#'+this.id).val();
  13. if (sLinks.length > 0)
  14. {
  15. this.aLinks = JSON.parse(sLinks);
  16. }
  17. this.Refresh();
  18. }
  19. this.Refresh = function ()
  20. {
  21. if (this.aLinks.length == 0)
  22. {
  23. $('#'+this.id+'_values').empty();
  24. }
  25. else
  26. {
  27. sLinks = JSON.stringify(this.aLinks);
  28. $('#'+this.id).val(sLinks);
  29. $('#'+this.id+'_values').load('ajax.render.php?operation=ui.linkswidget.linkedset&sclass='+this.sLinkedClass+'&sextkeytome='+this.sExtKeyToMe+'&sextkeytoremote='+this.sExtKeyToRemote+'&myid='+this.id,
  30. {'sset' : sLinks});
  31. }
  32. }
  33. this.OnOk = function()
  34. {
  35. this.aObjectBeingLinked = new Array();
  36. sSelected = 'selected_objects_'+this.id;
  37. oSelected = document.getElementById(sSelected);
  38. for(i=0; i<oSelected.length; i++)
  39. {
  40. this.aObjectBeingLinked[i] = oSelected.options[i].value;
  41. }
  42. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  43. this.aLinks = new Array(); // rebuild the list of links from scratch
  44. $('#LinkDlg_'+this.id).jqmShow();
  45. }
  46. this.OnCancel = function()
  47. {
  48. // Restore the links to their previous value (just in case)
  49. this.aLinks = this.aPreviousLinks;
  50. }
  51. this.OnLinkOk = function()
  52. {
  53. $('#LinkDlg_'+this.id).jqmHide();
  54. for(i=0; i<this.aObjectBeingLinked.length; i++)
  55. {
  56. oLink = {};
  57. oLink[this.sExtKeyToRemote] = this.aObjectBeingLinked[i];
  58. for(j=0; j<this.aAttributes.length; j++)
  59. {
  60. oLink[aAttributes[j]] = $('#'+this.id+'_'+j).val();
  61. }
  62. this.aLinks.push(oLink);
  63. }
  64. this.Refresh();
  65. }
  66. this.OnLinkCancel = function()
  67. {
  68. // Restore the links to their previous value (just in case)
  69. this.aLinks = this.aPreviousLinks;
  70. }
  71. this.RemoveLink = function(index)
  72. {
  73. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  74. this.Refresh();
  75. }
  76. this.AddObject = function()
  77. {
  78. linkedObjId = $('#id_ac_'+this.id).val();
  79. // Clears the selection
  80. $('#id_ac_'+this.id).val('');
  81. $('#ac_'+this.id).val('');
  82. // Add the object to the list
  83. this.aObjectBeingLinked = new Array();
  84. this.aObjectBeingLinked[0] = linkedObjId;
  85. // Add the object to the list of links
  86. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  87. $('#LinkDlg_'+this.id).jqmShow();
  88. }
  89. this.ModifyLink = function(index)
  90. {
  91. this.aObjectBeingLinked = new Array();
  92. this.aObjectBeingLinked[0] = this.aLinks[index][this.sExtKeyToRemote];
  93. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  94. // Set the default values of the dialog to the current ones
  95. for(j=0; j<this.aAttributes.length; j++)
  96. {
  97. $('#'+this.id+'_'+j).val(aLinks[index][aAttributes[j]]);
  98. }
  99. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  100. $('#LinkDlg_'+this.id).jqmShow(); // And add it again
  101. }
  102. }