linkswidget.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. sLinks = JSON.stringify(this.aLinks);
  22. if (this.aLinks.length == 0)
  23. {
  24. $('#'+this.id+'_values').empty();
  25. $('#'+this.id).val(sLinks);
  26. $('#'+this.id).trigger('validate');
  27. }
  28. else
  29. {
  30. $('#'+this.id).val(sLinks);
  31. $('#'+this.id).trigger('validate');
  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']} ); // 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).jqmShow();
  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).jqmHide();
  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. }
  85. this.OnLinkCancel = function()
  86. {
  87. // Restore the links to their previous value (just in case)
  88. this.aLinks = this.aPreviousLinks;
  89. // Grey out the 'Add...' button
  90. $('#ac_add_'+this.id).attr('disabled', 'disabled');
  91. }
  92. this.RemoveLink = function(index)
  93. {
  94. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  95. this.Refresh();
  96. }
  97. this.AddObject = function()
  98. {
  99. linkedObjId = $('#id_ac_'+this.id).val();
  100. // Clears the selection
  101. $('#id_ac_'+this.id).val('');
  102. $('#ac_'+this.id).val('');
  103. // Add the object to the list
  104. this.aObjectBeingLinked = new Array();
  105. this.aObjectBeingLinked[0] = linkedObjId;
  106. // Add the object to the list of links
  107. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  108. $('#LinkDlg_'+this.id).jqmShow();
  109. }
  110. this.ModifyLink = function(index)
  111. {
  112. this.aObjectBeingLinked = new Array();
  113. this.aObjectBeingLinked[0] = this.aLinks[index][this.sExtKeyToRemote];
  114. this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
  115. // Set the default values of the dialog to the current ones
  116. for(j=0; j<this.aAttributes.length; j++)
  117. {
  118. $('#'+this.id+'_'+j).val(aLinks[index][aAttributes[j]]);
  119. }
  120. this.aLinks.splice(index, 1); // Remove the element at position 'index'
  121. $('#LinkDlg_'+this.id).jqmShow(); // And add it again
  122. }
  123. }