utils.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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']} ).tablesorterPager({container: $("#pager")}); // 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']} ).tablesorterPager({container: $("#pager"), totalRows:97, filter: sSerializedFilter, extra_params: sExtraParams }); // 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. }
  85. );
  86. }
  87. /**
  88. * Update the display and value of a file input widget when the user picks a new file
  89. */
  90. function UpdateFileName(id, sNewFileName)
  91. {
  92. var aPath = sNewFileName.split('\\');
  93. var sNewFileName = aPath[aPath.length-1];
  94. $('#'+id).val(sNewFileName);
  95. $('#'+id).trigger('validate');
  96. $('#name_'+id).text(sNewFileName);
  97. return true;
  98. }
  99. /**
  100. * Reload a search form for the specified class
  101. */
  102. function ReloadSearchForm(divId, sClassName, sBaseClass, sContext)
  103. {
  104. var oDiv = $('#ds_'+divId);
  105. oDiv.block();
  106. var oFormEvents = $('#ds_'+divId+' form').data('events');
  107. // Save the submit handlers
  108. aSubmit = new Array();
  109. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  110. {
  111. for(index = 0; index < oFormEvents.submit.length; index++)
  112. {
  113. aSubmit [index ] = { data:oFormEvents.submit[index].data, namespace:oFormEvents.submit[index].namespace, handler: oFormEvents.submit[index].handler};
  114. }
  115. }
  116. sAction = $('#ds_'+divId+' form').attr('action');
  117. $.post('../pages/ajax.render.php?'+sContext,
  118. { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId, action: sAction },
  119. function(data) {
  120. oDiv.empty();
  121. oDiv.append(data);
  122. if (aSubmit.length > 0)
  123. {
  124. var oForm = $('#ds_'+divId+' form'); // Form was reloaded, recompute it
  125. for(index = 0; index < aSubmit.length; index++)
  126. {
  127. // Restore the previously bound submit handlers
  128. if (aSubmit[index].data != undefined)
  129. {
  130. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  131. }
  132. else
  133. {
  134. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  135. }
  136. }
  137. }
  138. oDiv.unblock();
  139. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  140. }
  141. );
  142. }
  143. /**
  144. * Stores - in a persistent way - user specific preferences
  145. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  146. * that acts as a local -write through- cache
  147. */
  148. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  149. {
  150. sPreviousValue = undefined;
  151. try
  152. {
  153. sPreviousValue = oUserPreferences[sPreferenceCode];
  154. }
  155. catch(err)
  156. {
  157. sPreviousValue = undefined;
  158. }
  159. oUserPreferences[sPreferenceCode] = sPrefValue;
  160. if (bPersistent && (sPrefValue != sPreviousValue))
  161. {
  162. ajax_request = $.post('../pages/ajax.render.php',
  163. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  164. }
  165. }
  166. /**
  167. * Get user specific preferences
  168. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  169. * that acts as a local -write through- cache
  170. */
  171. function GetUserPreference(sPreferenceCode, sDefaultValue)
  172. {
  173. var value = sDefaultValue;
  174. if ( oUserPreferences[sPreferenceCode] != undefined)
  175. {
  176. value = oUserPreferences[sPreferenceCode];
  177. }
  178. return value;
  179. }
  180. /**
  181. * Check/uncheck a whole list of checkboxes
  182. */
  183. function CheckAll(sSelector, bValue)
  184. {
  185. var value = bValue;
  186. $(sSelector).each( function() {
  187. if (this.checked != value)
  188. {
  189. this.checked = value;
  190. $(this).trigger('change');
  191. }
  192. });
  193. }
  194. /**
  195. * Toggle (enabled/disabled) the specified field of a form
  196. */
  197. function ToogleField(value, field_id)
  198. {
  199. if (value)
  200. {
  201. $('#'+field_id).removeAttr('disabled');
  202. }
  203. else
  204. {
  205. $('#'+field_id).attr('disabled', 'disabled');
  206. }
  207. $('#'+field_id).trigger('update');
  208. $('#'+field_id).trigger('validate');
  209. }
  210. /**
  211. * For the fields that cannot be visually disabled, they can be blocked
  212. * @return
  213. */
  214. function BlockField(field_id, bBlocked)
  215. {
  216. if (bBlocked)
  217. {
  218. $('#'+field_id).block({ message: ' ** disabled ** '});
  219. }
  220. else
  221. {
  222. $('#'+field_id).unblock();
  223. }
  224. }
  225. /**
  226. * Updates (enables/disables) a "duration" field
  227. */
  228. function ToggleDurationField(field_id)
  229. {
  230. // Toggle all the subfields that compose the "duration" input
  231. aSubFields = new Array('d', 'h', 'm', 's');
  232. if ($('#'+field_id).attr('disabled'))
  233. {
  234. for(var i=0; i<aSubFields.length; i++)
  235. {
  236. $('#'+field_id+'_'+aSubFields[i]).attr('disabled', 'disabled');
  237. }
  238. }
  239. else
  240. {
  241. for(var i=0; i<aSubFields.length; i++)
  242. {
  243. $('#'+field_id+'_'+aSubFields[i]).removeAttr('disabled');
  244. }
  245. }
  246. }
  247. /**
  248. * PropagateCheckBox
  249. */
  250. function PropagateCheckBox(bCurrValue, aFieldsList, bCheck)
  251. {
  252. if (bCurrValue == bCheck)
  253. {
  254. for(var i=0;i<aFieldsList.length;i++)
  255. {
  256. $('#enable_'+aFieldsList[i]).attr('checked', bCheck);
  257. ToogleField(bCheck, aFieldsList[i]);
  258. }
  259. }
  260. }
  261. function FixTableSorter(table)
  262. {
  263. if ($('th.header', table).length == 0)
  264. {
  265. // Table is not sort-able, let's fix it
  266. var checkbox = (table.find('th:first :checkbox').length > 0);
  267. if (checkbox)
  268. {
  269. // There is a checkbox in the first column, don't make it sort-able
  270. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  271. }
  272. else
  273. {
  274. // There is NO checkbox in the first column, all columns are considered sort-able
  275. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  276. }
  277. }
  278. }