linkswidget.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $('#v_'+this.id).html('<img src="../images/indicator.gif" />');
  22. sLinks = JSON.stringify(this.aLinks);
  23. if (this.aLinks.length == 0)
  24. {
  25. $('#'+this.id+'_values').empty();
  26. $('#'+this.id).val(sLinks);
  27. $('#'+this.id).trigger('validate');
  28. }
  29. else
  30. {
  31. $('#'+this.id).val(sLinks);
  32. $('#'+this.id+'_values').load('ajax.render.php?operation=ui.linkswidget.linkedset&sclass='+this.sLinkedClass+'&sextkeytome='+this.sExtKeyToMe+'&sextkeytoremote='+this.sExtKeyToRemote+'&myid='+this.id,
  33. {'sset' : sLinks}, function()
  34. {
  35. // Refresh the style of the loaded table
  36. $('#'+this.id+' table.listResults').tableHover();
  37. $('#'+this.id+' .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra', 'truncatedList']} ); // sortable and zebra tables
  38. }
  39. );
  40. }
  41. }
  42. this.OnOk = function()
  43. {
  44. this.aObjectBeingLinked = new Array();
  45. sSelected = 'selected_objects_'+this.id;
  46. oSelected = document.getElementById(sSelected);
  47. for(i=0; i<oSelected.length; i++)
  48. {
  49. this.aObjectBeingLinked[i] = oSelected.options[i].value;
  50. }
  51. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  52. this.aLinks = new Array(); // rebuild the list of links from scratch
  53. if (oSelected.length > 0)
  54. {
  55. $('#LinkDlg_'+this.id).dialog('open');
  56. }
  57. else
  58. {
  59. this.Refresh();
  60. $('#ac_add_'+this.id).attr('disabled', 'disabled');
  61. }
  62. }
  63. this.OnCancel = function()
  64. {
  65. // Restore the links to their previous value (just in case)
  66. this.aLinks = this.aPreviousLinks;
  67. }
  68. this.OnLinkOk = function()
  69. {
  70. $('#LinkDlg_'+this.id).dialog('close');
  71. for(i=0; i<this.aObjectBeingLinked.length; i++)
  72. {
  73. oLink = {};
  74. oLink[this.sExtKeyToRemote] = this.aObjectBeingLinked[i];
  75. for(j=0; j<this.aAttributes.length; j++)
  76. {
  77. oLink[aAttributes[j]] = $('#'+this.id+'_'+j).val();
  78. }
  79. this.aLinks.push(oLink);
  80. }
  81. this.Refresh();
  82. // Grey out the 'Add...' button
  83. $('#ac_add_'+this.id).attr('disabled', 'disabled');
  84. return false;
  85. }
  86. this.OnLinkCancel = function()
  87. {
  88. $('#LinkDlg_'+this.id).dialog('close');
  89. // Restore the links to their previous value (just in case)
  90. this.aLinks = this.aPreviousLinks;
  91. // Grey out the 'Add...' button
  92. $('#ac_add_'+this.id).attr('disabled', 'disabled');
  93. return false;
  94. }
  95. this.RemoveLink = function(index)
  96. {
  97. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  98. this.Refresh();
  99. }
  100. this.AddObject = function()
  101. {
  102. linkedObjId = $('#id_ac_'+this.id).val();
  103. // Clears the selection
  104. $('#id_ac_'+this.id).val('');
  105. $('#ac_'+this.id).val('');
  106. // Add the object to the list
  107. this.aObjectBeingLinked = new Array();
  108. this.aObjectBeingLinked[0] = linkedObjId;
  109. // Add the object to the list of links
  110. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  111. $('#LinkDlg_'+this.id).dialog('open');
  112. }
  113. this.ModifyLink = function(index)
  114. {
  115. this.aObjectBeingLinked = new Array();
  116. this.aObjectBeingLinked[0] = this.aLinks[index][this.sExtKeyToRemote];
  117. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  118. // Set the default values of the dialog to the current ones
  119. for(j=0; j<this.aAttributes.length; j++)
  120. {
  121. $('#'+this.id+'_'+j).val(aLinks[index][aAttributes[j]]);
  122. }
  123. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  124. $('#LinkDlg_'+this.id).dialog('open'); // And add it again
  125. }
  126. }