linkswidget.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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}, function()
  31. {
  32. // Refresh the style of the loaded table
  33. $('#'+this.id+' table.listResults').tableHover();
  34. $('#'+this.id+' .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
  35. }
  36. );
  37. }
  38. }
  39. this.OnOk = function()
  40. {
  41. this.aObjectBeingLinked = new Array();
  42. sSelected = 'selected_objects_'+this.id;
  43. oSelected = document.getElementById(sSelected);
  44. for(i=0; i<oSelected.length; i++)
  45. {
  46. this.aObjectBeingLinked[i] = oSelected.options[i].value;
  47. }
  48. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  49. this.aLinks = new Array(); // rebuild the list of links from scratch
  50. $('#LinkDlg_'+this.id).jqmShow();
  51. }
  52. this.OnCancel = function()
  53. {
  54. // Restore the links to their previous value (just in case)
  55. this.aLinks = this.aPreviousLinks;
  56. }
  57. this.OnLinkOk = function()
  58. {
  59. $('#LinkDlg_'+this.id).jqmHide();
  60. for(i=0; i<this.aObjectBeingLinked.length; i++)
  61. {
  62. oLink = {};
  63. oLink[this.sExtKeyToRemote] = this.aObjectBeingLinked[i];
  64. for(j=0; j<this.aAttributes.length; j++)
  65. {
  66. oLink[aAttributes[j]] = $('#'+this.id+'_'+j).val();
  67. }
  68. this.aLinks.push(oLink);
  69. }
  70. this.Refresh();
  71. }
  72. this.OnLinkCancel = function()
  73. {
  74. // Restore the links to their previous value (just in case)
  75. this.aLinks = this.aPreviousLinks;
  76. }
  77. this.RemoveLink = function(index)
  78. {
  79. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  80. this.Refresh();
  81. }
  82. this.AddObject = function()
  83. {
  84. linkedObjId = $('#id_ac_'+this.id).val();
  85. // Clears the selection
  86. $('#id_ac_'+this.id).val('');
  87. $('#ac_'+this.id).val('');
  88. // Add the object to the list
  89. this.aObjectBeingLinked = new Array();
  90. this.aObjectBeingLinked[0] = linkedObjId;
  91. // Add the object to the list of links
  92. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  93. $('#LinkDlg_'+this.id).jqmShow();
  94. }
  95. this.ModifyLink = function(index)
  96. {
  97. this.aObjectBeingLinked = new Array();
  98. this.aObjectBeingLinked[0] = this.aLinks[index][this.sExtKeyToRemote];
  99. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  100. // Set the default values of the dialog to the current ones
  101. for(j=0; j<this.aAttributes.length; j++)
  102. {
  103. $('#'+this.id+'_'+j).val(aLinks[index][aAttributes[j]]);
  104. }
  105. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  106. $('#LinkDlg_'+this.id).jqmShow(); // And add it again
  107. }
  108. }