utils.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Some general purpose JS functions for the iTop application
  2. //IE 8 compatibility, copied from: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/IndexOf
  3. if (!Array.prototype.indexOf) {
  4. Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
  5. "use strict";
  6. if (this == null) {
  7. throw new TypeError();
  8. }
  9. var t = Object(this);
  10. var len = t.length >>> 0;
  11. if (len === 0) {
  12. return -1;
  13. }
  14. var n = 0;
  15. if (arguments.length > 1) {
  16. n = Number(arguments[1]);
  17. if (n != n) { // shortcut for verifying if it's NaN
  18. n = 0;
  19. } else if (n != 0 && n != Infinity && n != -Infinity) {
  20. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  21. }
  22. }
  23. if (n >= len) {
  24. return -1;
  25. }
  26. var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
  27. for (; k < len; k++) {
  28. if (k in t && t[k] === searchElement) {
  29. return k;
  30. }
  31. }
  32. return -1;
  33. }
  34. }
  35. /**
  36. * Reload a truncated list
  37. */
  38. aTruncatedLists = {}; // To keep track of the list being loaded, each member is an ajaxRequest object
  39. function ReloadTruncatedList(divId, sSerializedFilter, sExtraParams)
  40. {
  41. $('#'+divId).block();
  42. //$('#'+divId).blockUI();
  43. if (aTruncatedLists[divId] != undefined)
  44. {
  45. try
  46. {
  47. aAjaxRequest = aTruncatedLists[divId];
  48. aAjaxRequest.abort();
  49. }
  50. catch(e)
  51. {
  52. // Do nothing special, just continue
  53. console.log('Uh,uh, exception !');
  54. }
  55. }
  56. aTruncatedLists[divId] = $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?style=list',
  57. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  58. function(data)
  59. {
  60. aTruncatedLists[divId] = undefined;
  61. if (data.length > 0)
  62. {
  63. $('#'+divId).html(data);
  64. $('#'+divId+' .listResults').tableHover(); // hover tables
  65. $('#'+divId+' .listResults').each( function()
  66. {
  67. var table = $(this);
  68. var id = $(this).parent();
  69. aTruncatedLists[divId] = undefined;
  70. var checkbox = (table.find('th:first :checkbox').length > 0);
  71. if (checkbox)
  72. {
  73. // There is a checkbox in the first column, don't make it sortable
  74. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ).tablesorterPager({container: $("#pager")}); // sortable and zebra tables
  75. }
  76. else
  77. {
  78. // There is NO checkbox in the first column, all columns are considered sortable
  79. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ).tablesorterPager({container: $("#pager"), totalRows:97, filter: sSerializedFilter, extra_params: sExtraParams }); // sortable and zebra tables
  80. }
  81. });
  82. $('#'+divId).unblock();
  83. }
  84. }
  85. );
  86. }
  87. /**
  88. * Truncate a previously expanded list !
  89. */
  90. function TruncateList(divId, iLimit, sNewLabel, sLinkLabel)
  91. {
  92. $('#'+divId).block();
  93. var iCount = 0;
  94. $('#'+divId+' table.listResults tr:gt('+iLimit+')').each( function(){
  95. $(this).remove();
  96. });
  97. $('#lbl_'+divId).html(sNewLabel);
  98. $('#'+divId+' table.listResults tr:last td').addClass('truncated');
  99. $('#'+divId+' table.listResults').addClass('truncated');
  100. $('#trc_'+divId).html(sLinkLabel);
  101. $('#'+divId+' .listResults').trigger("update"); // Reset the cache
  102. $('#'+divId).unblock();
  103. }
  104. /**
  105. * Reload any block -- used for periodic auto-reload
  106. */
  107. function ReloadBlock(divId, sStyle, sSerializedFilter, sExtraParams)
  108. {
  109. // Check if the user is not editing the list properties right now
  110. var bDialogOpen = false;
  111. var oDataTable = $('#'+divId+' :itop-datatable');
  112. if (oDataTable.length > 0)
  113. {
  114. bDialogOpen = oDataTable.datatable('IsDialogOpen');
  115. }
  116. if (!bDialogOpen)
  117. {
  118. $('#'+divId).block();
  119. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?style='+sStyle,
  120. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  121. function(data){
  122. $('#'+divId).empty();
  123. $('#'+divId).append(data);
  124. $('#'+divId).removeClass('loading');
  125. }
  126. );
  127. }
  128. }
  129. /**
  130. * Update the display and value of a file input widget when the user picks a new file
  131. */
  132. function UpdateFileName(id, sNewFileName)
  133. {
  134. var aPath = sNewFileName.split('\\');
  135. var sNewFileName = aPath[aPath.length-1];
  136. $('#'+id).val(sNewFileName);
  137. $('#'+id).trigger('validate');
  138. $('#name_'+id).text(sNewFileName);
  139. return true;
  140. }
  141. /**
  142. * Reload a search form for the specified class
  143. */
  144. function ReloadSearchForm(divId, sClassName, sBaseClass, sContext)
  145. {
  146. var oDiv = $('#ds_'+divId);
  147. oDiv.block();
  148. var oFormEvents = $('#ds_'+divId+' form').data('events');
  149. // Save the submit handlers
  150. aSubmit = new Array();
  151. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  152. {
  153. for(var index = 0; index < oFormEvents.submit.length; index++)
  154. {
  155. aSubmit [index ] = { data:oFormEvents.submit[index].data, namespace:oFormEvents.submit[index].namespace, handler: oFormEvents.submit[index].handler};
  156. }
  157. }
  158. sAction = $('#ds_'+divId+' form').attr('action');
  159. // Save the current values in the form
  160. var oMap = {};
  161. $('#ds_'+divId+" form :input[name!='']").each(function() {
  162. oMap[this.name] = this.value;
  163. });
  164. oMap.operation = 'search_form';
  165. oMap.className = sClassName;
  166. oMap.baseClass = sBaseClass;
  167. oMap.currentId = divId;
  168. oMap.action = sAction;
  169. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, oMap,
  170. function(data) {
  171. oDiv.empty();
  172. oDiv.append(data);
  173. if (aSubmit.length > 0)
  174. {
  175. var oForm = $('#ds_'+divId+' form'); // Form was reloaded, recompute it
  176. for(var index = 0; index < aSubmit.length; index++)
  177. {
  178. // Restore the previously bound submit handlers
  179. if (aSubmit[index].data != undefined)
  180. {
  181. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  182. }
  183. else
  184. {
  185. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  186. }
  187. }
  188. }
  189. oDiv.unblock();
  190. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  191. }
  192. );
  193. }
  194. /**
  195. * Stores - in a persistent way - user specific preferences
  196. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  197. * that acts as a local -write through- cache
  198. */
  199. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  200. {
  201. sPreviousValue = undefined;
  202. try
  203. {
  204. sPreviousValue = oUserPreferences[sPreferenceCode];
  205. }
  206. catch(err)
  207. {
  208. sPreviousValue = undefined;
  209. }
  210. oUserPreferences[sPreferenceCode] = sPrefValue;
  211. if (bPersistent && (sPrefValue != sPreviousValue))
  212. {
  213. ajax_request = $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  214. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  215. }
  216. }
  217. /**
  218. * Get user specific preferences
  219. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  220. * that acts as a local -write through- cache
  221. */
  222. function GetUserPreference(sPreferenceCode, sDefaultValue)
  223. {
  224. var value = sDefaultValue;
  225. if ( oUserPreferences[sPreferenceCode] != undefined)
  226. {
  227. value = oUserPreferences[sPreferenceCode];
  228. }
  229. return value;
  230. }
  231. /**
  232. * Check/uncheck a whole list of checkboxes
  233. */
  234. function CheckAll(sSelector, bValue)
  235. {
  236. var value = bValue;
  237. $(sSelector).each( function() {
  238. if (this.checked != value)
  239. {
  240. this.checked = value;
  241. $(this).trigger('change');
  242. }
  243. });
  244. }
  245. /**
  246. * Toggle (enabled/disabled) the specified field of a form
  247. */
  248. function ToogleField(value, field_id)
  249. {
  250. if (value)
  251. {
  252. $('#'+field_id).removeAttr('disabled');
  253. }
  254. else
  255. {
  256. $('#'+field_id).attr('disabled', 'disabled');
  257. }
  258. $('#'+field_id).trigger('update');
  259. $('#'+field_id).trigger('validate');
  260. }
  261. /**
  262. * For the fields that cannot be visually disabled, they can be blocked
  263. * @return
  264. */
  265. function BlockField(field_id, bBlocked)
  266. {
  267. if (bBlocked)
  268. {
  269. $('#'+field_id).block({ message: ' ** disabled ** '});
  270. }
  271. else
  272. {
  273. $('#'+field_id).unblock();
  274. }
  275. }
  276. /**
  277. * Updates (enables/disables) a "duration" field
  278. */
  279. function ToggleDurationField(field_id)
  280. {
  281. // Toggle all the subfields that compose the "duration" input
  282. aSubFields = new Array('d', 'h', 'm', 's');
  283. if ($('#'+field_id).attr('disabled'))
  284. {
  285. for(var i=0; i<aSubFields.length; i++)
  286. {
  287. $('#'+field_id+'_'+aSubFields[i]).attr('disabled', 'disabled');
  288. }
  289. }
  290. else
  291. {
  292. for(var i=0; i<aSubFields.length; i++)
  293. {
  294. $('#'+field_id+'_'+aSubFields[i]).removeAttr('disabled');
  295. }
  296. }
  297. }
  298. /**
  299. * PropagateCheckBox
  300. */
  301. function PropagateCheckBox(bCurrValue, aFieldsList, bCheck)
  302. {
  303. if (bCurrValue == bCheck)
  304. {
  305. for(var i=0;i<aFieldsList.length;i++)
  306. {
  307. $('#enable_'+aFieldsList[i]).attr('checked', bCheck);
  308. ToogleField(bCheck, aFieldsList[i]);
  309. }
  310. }
  311. }
  312. function FixTableSorter(table)
  313. {
  314. if ($('th.header', table).length == 0)
  315. {
  316. // Table is not sort-able, let's fix it
  317. var checkbox = (table.find('th:first :checkbox').length > 0);
  318. if (checkbox)
  319. {
  320. // There is a checkbox in the first column, don't make it sort-able
  321. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  322. }
  323. else
  324. {
  325. // There is NO checkbox in the first column, all columns are considered sort-able
  326. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  327. }
  328. }
  329. }
  330. function DashletCreationDlg(sOQL)
  331. {
  332. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashlet_creation_dlg', oql: sOQL}, function(data){
  333. $('body').append(data);
  334. });
  335. return false;
  336. }
  337. function ShortcutListDlg(sOQL, sContext)
  338. {
  339. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, {operation: 'shortcut_list_dlg', oql: sOQL}, function(data){
  340. $('body').append(data);
  341. });
  342. return false;
  343. }