utils.js 8.2 KB

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