utils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Some general purpose JS functions for the iTop application
  2. /**
  3. * Reload a truncated list
  4. */
  5. function ReloadTruncatedList(divId, sSerializedFilter, sExtraParams)
  6. {
  7. $('#'+divId).addClass('loading');
  8. //$('#'+divId).blockUI();
  9. $.post('ajax.render.php?style=list',
  10. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  11. function(data){
  12. $('#'+divId).empty();
  13. $('#'+divId).append(data);
  14. $('#'+divId).removeClass('loading');
  15. $('#'+divId+' .listResults').tableHover(); // hover tables
  16. $('#'+divId+' .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
  17. //$('#'+divId).unblockUI();
  18. }
  19. );
  20. }
  21. /**
  22. * Truncate a previously expanded list !
  23. */
  24. function TruncateList(divId, iLimit, sNewLabel, sLinkLabel)
  25. {
  26. var iCount = 0;
  27. $('#'+divId+' table.listResults tr').each( function(){
  28. if (iCount > iLimit)
  29. {
  30. $(this).remove();
  31. }
  32. iCount++;
  33. });
  34. $('#lbl_'+divId).html(sNewLabel);
  35. $('#'+divId+' table.listResults tr:last td').addClass('truncated');
  36. $('#'+divId+' table.listResults').addClass('truncated');
  37. $('#trc_'+divId).html(sLinkLabel);
  38. }
  39. /**
  40. * Reload any block -- used for periodic auto-reload
  41. */
  42. function ReloadBlock(divId, sStyle, sSerializedFilter, sExtraParams)
  43. {
  44. $('#'+divId).addClass('loading');
  45. //$('#'+divId).blockUI();
  46. $.post('ajax.render.php?style='+sStyle,
  47. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  48. function(data){
  49. $('#'+divId).empty();
  50. $('#'+divId).append(data);
  51. $('#'+divId).removeClass('loading');
  52. $('#'+divId+' .listResults').tableHover(); // hover tables
  53. $('#'+divId+' .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
  54. //$('#'+divId).unblockUI();
  55. }
  56. );
  57. }
  58. /**
  59. * Update the display and value of a file input widget when the user picks a new file
  60. */
  61. function UpdateFileName(id, sNewFileName)
  62. {
  63. var aPath = sNewFileName.split('\\');
  64. var sNewFileName = aPath[aPath.length-1];
  65. $('#'+id).val(sNewFileName);
  66. $('#'+id).trigger('validate');
  67. $('#name_'+id).text(sNewFileName);
  68. return true;
  69. }
  70. /**
  71. * Reload a search form for the specified class
  72. */
  73. function ReloadSearchForm(divId, sClassName, sBaseClass)
  74. {
  75. var oDiv = $('#'+divId);
  76. oDiv.block();
  77. var oFormEvents = $('#'+divId+' form').data('events');
  78. // Save the submit handlers
  79. aSubmit = new Array();
  80. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  81. {
  82. aSubmit = oFormEvents.submit;
  83. }
  84. $.post('ajax.render.php',
  85. { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId },
  86. function(data) {
  87. oDiv.empty();
  88. oDiv.append(data);
  89. if (aSubmit.length > 0)
  90. {
  91. var oForm = $('#'+divId+' form'); // Form was reloaded, recompute it
  92. for(index = 0; index < aSubmit.length; index++)
  93. {
  94. // Restore the previously bound submit handlers
  95. if (aSubmit[index].data != undefined)
  96. {
  97. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  98. }
  99. else
  100. {
  101. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  102. }
  103. }
  104. }
  105. oDiv.unblock();
  106. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  107. }
  108. );
  109. }
  110. /**
  111. * Stores - in a persistent way - user specific preferences
  112. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  113. * that acts as a local -write through- cache
  114. */
  115. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  116. {
  117. sPreviousValue = undefined;
  118. try
  119. {
  120. sPreviousValue = oUserPreferences[sPreferenceCode];
  121. }
  122. catch(err)
  123. {
  124. sPreviousValue = undefined;
  125. }
  126. oUserPreferences[sPreferenceCode] = sPrefValue;
  127. if (bPersistent && (sPrefValue != sPreviousValue))
  128. {
  129. ajax_request = $.post('ajax.render.php',
  130. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  131. }
  132. }
  133. /**
  134. * Get user specific preferences
  135. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  136. * that acts as a local -write through- cache
  137. */
  138. function GetUserPreference(sPreferenceCode, sDefaultValue)
  139. {
  140. var value = sDefaultValue;
  141. if ( oUserPreferences[sPreferenceCode] != undefined)
  142. {
  143. value = oUserPreferences[sPreferenceCode];
  144. }
  145. return value;
  146. }