utils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Some general purpose JS functions for the iTop application
  2. /**
  3. * Reload a truncated list
  4. */
  5. aTruncatedLists = {}; // To keep track of the list being loaded, each member is an ajaxRequest object
  6. function ReloadTruncatedList(divId, sSerializedFilter, sExtraParams)
  7. {
  8. $('#'+divId).block();
  9. //$('#'+divId).blockUI();
  10. if (aTruncatedLists[divId] != undefined)
  11. {
  12. try
  13. {
  14. aAjaxRequest = aTruncatedLists[divId];
  15. aAjaxRequest.abort();
  16. }
  17. catch(e)
  18. {
  19. // Do nothing special, just continue
  20. console.log('Uh,uh, exception !');
  21. }
  22. }
  23. aTruncatedLists[divId] = $.post('../pages/ajax.render.php?style=list',
  24. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  25. function(data)
  26. {
  27. aTruncatedLists[divId] = undefined;
  28. if (data.length > 0)
  29. {
  30. $('#'+divId).html(data);
  31. $('#'+divId+' .listResults').tableHover(); // hover tables
  32. $('#'+divId+' .listResults').each( function()
  33. {
  34. var table = $(this);
  35. var id = $(this).parent();
  36. aTruncatedLists[divId] = undefined;
  37. var checkbox = (table.find('th:first :checkbox').length > 0);
  38. if (checkbox)
  39. {
  40. // There is a checkbox in the first column, don't make it sortable
  41. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  42. }
  43. else
  44. {
  45. // There is NO checkbox in the first column, all columns are considered sortable
  46. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  47. }
  48. });
  49. $('#'+divId).unblock();
  50. }
  51. }
  52. );
  53. }
  54. /**
  55. * Truncate a previously expanded list !
  56. */
  57. function TruncateList(divId, iLimit, sNewLabel, sLinkLabel)
  58. {
  59. $('#'+divId).block();
  60. var iCount = 0;
  61. $('#'+divId+' table.listResults tr:gt('+iLimit+')').each( function(){
  62. $(this).remove();
  63. });
  64. $('#lbl_'+divId).html(sNewLabel);
  65. $('#'+divId+' table.listResults tr:last td').addClass('truncated');
  66. $('#'+divId+' table.listResults').addClass('truncated');
  67. $('#trc_'+divId).html(sLinkLabel);
  68. $('#'+divId+' .listResults').trigger("update"); // Reset the cache
  69. $('#'+divId).unblock();
  70. }
  71. /**
  72. * Reload any block -- used for periodic auto-reload
  73. */
  74. function ReloadBlock(divId, sStyle, sSerializedFilter, sExtraParams)
  75. {
  76. $('#'+divId).block();
  77. //$('#'+divId).blockUI();
  78. $.post('../pages/ajax.render.php?style='+sStyle,
  79. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  80. function(data){
  81. $('#'+divId).empty();
  82. $('#'+divId).append(data);
  83. $('#'+divId).removeClass('loading');
  84. $('#'+divId+' .listResults').tableHover(); // hover tables
  85. $('#'+divId+' .listResults').each( function()
  86. {
  87. var table = $(this);
  88. var id = $(this).parent();
  89. var checkbox = (table.find('th:first :checkbox').length > 0);
  90. if (checkbox)
  91. {
  92. // There is a checkbox in the first column, don't make it sortable
  93. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  94. }
  95. else
  96. {
  97. // There is NO checkbox in the first column, all columns are considered sortable
  98. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  99. }
  100. });
  101. //$('#'+divId).unblockUI();
  102. }
  103. );
  104. }
  105. /**
  106. * Update the display and value of a file input widget when the user picks a new file
  107. */
  108. function UpdateFileName(id, sNewFileName)
  109. {
  110. var aPath = sNewFileName.split('\\');
  111. var sNewFileName = aPath[aPath.length-1];
  112. $('#'+id).val(sNewFileName);
  113. $('#'+id).trigger('validate');
  114. $('#name_'+id).text(sNewFileName);
  115. return true;
  116. }
  117. /**
  118. * Reload a search form for the specified class
  119. */
  120. function ReloadSearchForm(divId, sClassName, sBaseClass, sContext)
  121. {
  122. var oDiv = $('#ds_'+divId);
  123. oDiv.block();
  124. var oFormEvents = $('#ds_'+divId+' form').data('events');
  125. // Save the submit handlers
  126. aSubmit = new Array();
  127. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  128. {
  129. for(index = 0; index < oFormEvents.submit.length; index++)
  130. {
  131. aSubmit [index ] = { data:oFormEvents.submit[index].data, namespace:oFormEvents.submit[index].namespace, handler: oFormEvents.submit[index].handler};
  132. }
  133. }
  134. sAction = $('#ds_'+divId+' form').attr('action');
  135. $.post('../pages/ajax.render.php?'+sContext,
  136. { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId, action: sAction },
  137. function(data) {
  138. oDiv.empty();
  139. oDiv.append(data);
  140. if (aSubmit.length > 0)
  141. {
  142. var oForm = $('#ds_'+divId+' form'); // Form was reloaded, recompute it
  143. for(index = 0; index < aSubmit.length; index++)
  144. {
  145. // Restore the previously bound submit handlers
  146. if (aSubmit[index].data != undefined)
  147. {
  148. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  149. }
  150. else
  151. {
  152. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  153. }
  154. }
  155. }
  156. oDiv.unblock();
  157. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  158. }
  159. );
  160. }
  161. /**
  162. * Stores - in a persistent way - user specific preferences
  163. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  164. * that acts as a local -write through- cache
  165. */
  166. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  167. {
  168. sPreviousValue = undefined;
  169. try
  170. {
  171. sPreviousValue = oUserPreferences[sPreferenceCode];
  172. }
  173. catch(err)
  174. {
  175. sPreviousValue = undefined;
  176. }
  177. oUserPreferences[sPreferenceCode] = sPrefValue;
  178. if (bPersistent && (sPrefValue != sPreviousValue))
  179. {
  180. ajax_request = $.post('../pages/ajax.render.php',
  181. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  182. }
  183. }
  184. /**
  185. * Get user specific preferences
  186. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  187. * that acts as a local -write through- cache
  188. */
  189. function GetUserPreference(sPreferenceCode, sDefaultValue)
  190. {
  191. var value = sDefaultValue;
  192. if ( oUserPreferences[sPreferenceCode] != undefined)
  193. {
  194. value = oUserPreferences[sPreferenceCode];
  195. }
  196. return value;
  197. }
  198. /**
  199. * Check/uncheck a whole list of checkboxes
  200. */
  201. function CheckAll(sSelector, bValue)
  202. {
  203. var value = bValue;
  204. $(sSelector).each( function() {
  205. this.checked = value;
  206. });
  207. }
  208. /**
  209. * Toggle (enabled/disabled) the specified field of a form
  210. */
  211. function ToogleField(value, field_id)
  212. {
  213. if (value)
  214. {
  215. $('#'+field_id).removeAttr('disabled');
  216. }
  217. else
  218. {
  219. $('#'+field_id).attr('disabled', 'disabled');
  220. }
  221. $('#'+field_id).trigger('update');
  222. $('#'+field_id).trigger('validate');
  223. }
  224. /**
  225. * For the fields that cannot be visually disabled, they can be blocked
  226. * @return
  227. */
  228. function BlockField(field_id, bBlocked)
  229. {
  230. if (bBlocked)
  231. {
  232. $('#'+field_id).block({ message: ' ** disabled ** '});
  233. }
  234. else
  235. {
  236. $('#'+field_id).unblock();
  237. }
  238. }
  239. /**
  240. * Updates (enables/disables) a "duration" field
  241. */
  242. function ToggleDurationField(field_id)
  243. {
  244. // Toggle all the subfields that compose the "duration" input
  245. aSubFields = new Array('d', 'h', 'm', 's');
  246. if ($('#'+field_id).attr('disabled'))
  247. {
  248. for(var i=0; i<aSubFields.length; i++)
  249. {
  250. $('#'+field_id+'_'+aSubFields[i]).attr('disabled', 'disabled');
  251. }
  252. }
  253. else
  254. {
  255. for(var i=0; i<aSubFields.length; i++)
  256. {
  257. $('#'+field_id+'_'+aSubFields[i]).removeAttr('disabled');
  258. }
  259. }
  260. }
  261. /**
  262. * PropagateCheckBox
  263. */
  264. function PropagateCheckBox(bCurrValue, aFieldsList, bCheck)
  265. {
  266. if (bCurrValue == bCheck)
  267. {
  268. for(var i=0;i<aFieldsList.length;i++)
  269. {
  270. $('#enable_'+aFieldsList[i]).attr('checked', bCheck);
  271. ToogleField(bCheck, aFieldsList[i]);
  272. }
  273. }
  274. }