utils.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. // Check if the user is not editing the list properties right now
  77. var bDialogOpen = false;
  78. var oDataTable = $('#'+divId+' :itop-datatable');
  79. if (oDataTable.length > 0)
  80. {
  81. bDialogOpen = oDataTable.datatable('IsDialogOpen');
  82. }
  83. if (!bDialogOpen)
  84. {
  85. $('#'+divId).block();
  86. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?style='+sStyle,
  87. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  88. function(data){
  89. $('#'+divId).empty();
  90. $('#'+divId).append(data);
  91. $('#'+divId).removeClass('loading');
  92. }
  93. );
  94. }
  95. }
  96. /**
  97. * Update the display and value of a file input widget when the user picks a new file
  98. */
  99. function UpdateFileName(id, sNewFileName)
  100. {
  101. var aPath = sNewFileName.split('\\');
  102. var sNewFileName = aPath[aPath.length-1];
  103. $('#'+id).val(sNewFileName);
  104. $('#'+id).trigger('validate');
  105. $('#name_'+id).text(sNewFileName);
  106. return true;
  107. }
  108. /**
  109. * Reload a search form for the specified class
  110. */
  111. function ReloadSearchForm(divId, sClassName, sBaseClass, sContext)
  112. {
  113. var oDiv = $('#ds_'+divId);
  114. oDiv.block();
  115. var oFormEvents = $('#ds_'+divId+' form').data('events');
  116. // Save the submit handlers
  117. aSubmit = new Array();
  118. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  119. {
  120. for(index = 0; index < oFormEvents.submit.length; index++)
  121. {
  122. aSubmit [index ] = { data:oFormEvents.submit[index].data, namespace:oFormEvents.submit[index].namespace, handler: oFormEvents.submit[index].handler};
  123. }
  124. }
  125. sAction = $('#ds_'+divId+' form').attr('action');
  126. // Save the current values in the form
  127. var oMap = {};
  128. $('#ds_'+divId+" form :input[name!='']").each(function() {
  129. oMap[this.name] = this.value;
  130. });
  131. oMap.operation = 'search_form';
  132. oMap.className = sClassName;
  133. oMap.baseClass = sBaseClass;
  134. oMap.currentId = divId;
  135. oMap.action = sAction;
  136. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, oMap,
  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(GetAbsoluteUrlAppRoot()+'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. if (this.checked != value)
  206. {
  207. this.checked = value;
  208. $(this).trigger('change');
  209. }
  210. });
  211. }
  212. /**
  213. * Toggle (enabled/disabled) the specified field of a form
  214. */
  215. function ToogleField(value, field_id)
  216. {
  217. if (value)
  218. {
  219. $('#'+field_id).removeAttr('disabled');
  220. }
  221. else
  222. {
  223. $('#'+field_id).attr('disabled', 'disabled');
  224. }
  225. $('#'+field_id).trigger('update');
  226. $('#'+field_id).trigger('validate');
  227. }
  228. /**
  229. * For the fields that cannot be visually disabled, they can be blocked
  230. * @return
  231. */
  232. function BlockField(field_id, bBlocked)
  233. {
  234. if (bBlocked)
  235. {
  236. $('#'+field_id).block({ message: ' ** disabled ** '});
  237. }
  238. else
  239. {
  240. $('#'+field_id).unblock();
  241. }
  242. }
  243. /**
  244. * Updates (enables/disables) a "duration" field
  245. */
  246. function ToggleDurationField(field_id)
  247. {
  248. // Toggle all the subfields that compose the "duration" input
  249. aSubFields = new Array('d', 'h', 'm', 's');
  250. if ($('#'+field_id).attr('disabled'))
  251. {
  252. for(var i=0; i<aSubFields.length; i++)
  253. {
  254. $('#'+field_id+'_'+aSubFields[i]).attr('disabled', 'disabled');
  255. }
  256. }
  257. else
  258. {
  259. for(var i=0; i<aSubFields.length; i++)
  260. {
  261. $('#'+field_id+'_'+aSubFields[i]).removeAttr('disabled');
  262. }
  263. }
  264. }
  265. /**
  266. * PropagateCheckBox
  267. */
  268. function PropagateCheckBox(bCurrValue, aFieldsList, bCheck)
  269. {
  270. if (bCurrValue == bCheck)
  271. {
  272. for(var i=0;i<aFieldsList.length;i++)
  273. {
  274. $('#enable_'+aFieldsList[i]).attr('checked', bCheck);
  275. ToogleField(bCheck, aFieldsList[i]);
  276. }
  277. }
  278. }
  279. function FixTableSorter(table)
  280. {
  281. if ($('th.header', table).length == 0)
  282. {
  283. // Table is not sort-able, let's fix it
  284. var checkbox = (table.find('th:first :checkbox').length > 0);
  285. if (checkbox)
  286. {
  287. // There is a checkbox in the first column, don't make it sort-able
  288. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  289. }
  290. else
  291. {
  292. // There is NO checkbox in the first column, all columns are considered sort-able
  293. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  294. }
  295. }
  296. }
  297. function DashletCreationDlg(sOQL)
  298. {
  299. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashlet_creation_dlg', oql: sOQL}, function(data){
  300. $('body').append(data);
  301. });
  302. return false;
  303. }
  304. function ShortcutListDlg(sOQL, sContext)
  305. {
  306. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, {operation: 'shortcut_list_dlg', oql: sOQL}, function(data){
  307. $('body').append(data);
  308. });
  309. return false;
  310. }