utils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. $.post('../pages/ajax.render.php?'+sContext,
  135. { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId },
  136. function(data) {
  137. oDiv.empty();
  138. oDiv.append(data);
  139. if (aSubmit.length > 0)
  140. {
  141. var oForm = $('#ds_'+divId+' form'); // Form was reloaded, recompute it
  142. for(index = 0; index < aSubmit.length; index++)
  143. {
  144. // Restore the previously bound submit handlers
  145. if (aSubmit[index].data != undefined)
  146. {
  147. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  148. }
  149. else
  150. {
  151. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  152. }
  153. }
  154. }
  155. oDiv.unblock();
  156. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  157. }
  158. );
  159. }
  160. /**
  161. * Stores - in a persistent way - user specific preferences
  162. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  163. * that acts as a local -write through- cache
  164. */
  165. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  166. {
  167. sPreviousValue = undefined;
  168. try
  169. {
  170. sPreviousValue = oUserPreferences[sPreferenceCode];
  171. }
  172. catch(err)
  173. {
  174. sPreviousValue = undefined;
  175. }
  176. oUserPreferences[sPreferenceCode] = sPrefValue;
  177. if (bPersistent && (sPrefValue != sPreviousValue))
  178. {
  179. ajax_request = $.post('../pages/ajax.render.php',
  180. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  181. }
  182. }
  183. /**
  184. * Get user specific preferences
  185. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  186. * that acts as a local -write through- cache
  187. */
  188. function GetUserPreference(sPreferenceCode, sDefaultValue)
  189. {
  190. var value = sDefaultValue;
  191. if ( oUserPreferences[sPreferenceCode] != undefined)
  192. {
  193. value = oUserPreferences[sPreferenceCode];
  194. }
  195. return value;
  196. }
  197. /**
  198. * Check/uncheck a whole list of checkboxes
  199. */
  200. function CheckAll(sSelector, bValue)
  201. {
  202. var value = bValue;
  203. $(sSelector).each( function() {
  204. this.checked = value;
  205. });
  206. }
  207. /**
  208. * Toggle (enabled/disabled) the specified field of a form
  209. */
  210. function ToogleField(value, field_id)
  211. {
  212. if (value)
  213. {
  214. $('#'+field_id).removeAttr('disabled');
  215. }
  216. else
  217. {
  218. $('#'+field_id).attr('disabled', 'disabled');
  219. }
  220. $('#'+field_id).trigger('update');
  221. $('#'+field_id).trigger('validate');
  222. }
  223. /**
  224. * For the fields that cannot be visually disabled, they can be blocked
  225. * @return
  226. */
  227. function BlockField(field_id, bBlocked)
  228. {
  229. if (bBlocked)
  230. {
  231. $('#'+field_id).block({ message: ' ** disabled ** '});
  232. }
  233. else
  234. {
  235. $('#'+field_id).unblock();
  236. }
  237. }
  238. /**
  239. * Updates (enables/disables) a "duration" field
  240. */
  241. function ToggleDurationField(field_id)
  242. {
  243. // Toggle all the subfields that compose the "duration" input
  244. aSubFields = new Array('d', 'h', 'm', 's');
  245. if ($('#'+field_id).attr('disabled'))
  246. {
  247. for(var i=0; i<aSubFields.length; i++)
  248. {
  249. $('#'+field_id+'_'+aSubFields[i]).attr('disabled', 'disabled');
  250. }
  251. }
  252. else
  253. {
  254. for(var i=0; i<aSubFields.length; i++)
  255. {
  256. $('#'+field_id+'_'+aSubFields[i]).removeAttr('disabled');
  257. }
  258. }
  259. }
  260. /**
  261. * PropagateCheckBox
  262. */
  263. function PropagateCheckBox(bCurrValue, aFieldsList, bCheck)
  264. {
  265. if (bCurrValue == bCheck)
  266. {
  267. for(var i=0;i<aFieldsList.length;i++)
  268. {
  269. $('#enable_'+aFieldsList[i]).attr('checked', bCheck);
  270. ToogleField(bCheck, aFieldsList[i]);
  271. }
  272. }
  273. }