utils.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. if (false) // deactivated since it causes troubles: for(k in aData) => returns the indexOf function as first element on empty arrays !
  5. {
  6. Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
  7. "use strict";
  8. if (this == null) {
  9. throw new TypeError();
  10. }
  11. var t = Object(this);
  12. var len = t.length >>> 0;
  13. if (len === 0) {
  14. return -1;
  15. }
  16. var n = 0;
  17. if (arguments.length > 1) {
  18. n = Number(arguments[1]);
  19. if (n != n) { // shortcut for verifying if it's NaN
  20. n = 0;
  21. } else if (n != 0 && n != Infinity && n != -Infinity) {
  22. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  23. }
  24. }
  25. if (n >= len) {
  26. return -1;
  27. }
  28. var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
  29. for (; k < len; k++) {
  30. if (k in t && t[k] === searchElement) {
  31. return k;
  32. }
  33. }
  34. return -1;
  35. }
  36. }
  37. }
  38. /**
  39. * Reload a truncated list
  40. */
  41. aTruncatedLists = {}; // To keep track of the list being loaded, each member is an ajaxRequest object
  42. function ReloadTruncatedList(divId, sSerializedFilter, sExtraParams)
  43. {
  44. $('#'+divId).block();
  45. //$('#'+divId).blockUI();
  46. if (aTruncatedLists[divId] != undefined)
  47. {
  48. try
  49. {
  50. aAjaxRequest = aTruncatedLists[divId];
  51. aAjaxRequest.abort();
  52. }
  53. catch(e)
  54. {
  55. // Do nothing special, just continue
  56. console.log('Uh,uh, exception !');
  57. }
  58. }
  59. aTruncatedLists[divId] = $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?style=list',
  60. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  61. function(data)
  62. {
  63. aTruncatedLists[divId] = undefined;
  64. if (data.length > 0)
  65. {
  66. $('#'+divId).html(data);
  67. $('#'+divId+' .listResults').tableHover(); // hover tables
  68. $('#'+divId+' .listResults').each( function()
  69. {
  70. var table = $(this);
  71. var id = $(this).parent();
  72. aTruncatedLists[divId] = undefined;
  73. var checkbox = (table.find('th:first :checkbox').length > 0);
  74. if (checkbox)
  75. {
  76. // There is a checkbox in the first column, don't make it sortable
  77. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ).tablesorterPager({container: $("#pager")}); // sortable and zebra tables
  78. }
  79. else
  80. {
  81. // There is NO checkbox in the first column, all columns are considered sortable
  82. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ).tablesorterPager({container: $("#pager"), totalRows:97, filter: sSerializedFilter, extra_params: sExtraParams }); // sortable and zebra tables
  83. }
  84. });
  85. $('#'+divId).unblock();
  86. }
  87. }
  88. );
  89. }
  90. /**
  91. * Truncate a previously expanded list !
  92. */
  93. function TruncateList(divId, iLimit, sNewLabel, sLinkLabel)
  94. {
  95. $('#'+divId).block();
  96. var iCount = 0;
  97. $('#'+divId+' table.listResults tr:gt('+iLimit+')').each( function(){
  98. $(this).remove();
  99. });
  100. $('#lbl_'+divId).html(sNewLabel);
  101. $('#'+divId+' table.listResults tr:last td').addClass('truncated');
  102. $('#'+divId+' table.listResults').addClass('truncated');
  103. $('#trc_'+divId).html(sLinkLabel);
  104. $('#'+divId+' .listResults').trigger("update"); // Reset the cache
  105. $('#'+divId).unblock();
  106. }
  107. /**
  108. * Reload any block -- used for periodic auto-reload
  109. */
  110. function ReloadBlock(divId, sStyle, sSerializedFilter, sExtraParams)
  111. {
  112. // Check if the user is not editing the list properties right now
  113. var bDialogOpen = false;
  114. var oDataTable = $('#'+divId+' :itop-datatable');
  115. var bIsDataTable = false;
  116. if (oDataTable.length > 0)
  117. {
  118. bDialogOpen = oDataTable.datatable('IsDialogOpen');
  119. bIsDataTable = true;
  120. }
  121. if (!bDialogOpen)
  122. {
  123. if (bIsDataTable)
  124. {
  125. oDataTable.datatable('DoRefresh');
  126. }
  127. else
  128. {
  129. $('#'+divId).block();
  130. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?style='+sStyle,
  131. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  132. function(data){
  133. $('#'+divId).empty();
  134. $('#'+divId).append(data);
  135. $('#'+divId).removeClass('loading');
  136. }
  137. );
  138. }
  139. }
  140. }
  141. function SaveGroupBySortOrder(sTableId, aValues)
  142. {
  143. var sDashboardId = $('#'+sTableId).closest('.dashboard_contents').attr('id');
  144. var sPrefKey = 'GroupBy_'+sDashboardId+'_'+sTableId;
  145. if (aValues.length != 0)
  146. {
  147. $sValue = JSON.stringify(aValues);
  148. if (GetUserPreference(sPrefKey, null) != $sValue)
  149. {
  150. SetUserPreference(sPrefKey, $sValue, true);
  151. }
  152. }
  153. }
  154. function LoadGroupBySortOrder(sTableId)
  155. {
  156. var sDashboardId = $('#'+sTableId).closest('.dashboard_contents').attr('id');
  157. var sPrefKey = 'GroupBy_'+sDashboardId+'_'+sTableId;
  158. var sValues = GetUserPreference(sPrefKey, null);
  159. if (sValues != null)
  160. {
  161. aValues = JSON.parse(sValues);
  162. window.setTimeout(function () { $('#'+sTableId+' table.listResults').trigger('sorton', [aValues]); }, 50);
  163. }
  164. }
  165. /**
  166. * Update the display and value of a file input widget when the user picks a new file
  167. */
  168. function UpdateFileName(id, sNewFileName)
  169. {
  170. var aPath = sNewFileName.split('\\');
  171. var sNewFileName = aPath[aPath.length-1];
  172. $('#'+id).val(sNewFileName);
  173. $('#'+id).trigger('validate');
  174. $('#name_'+id).text(sNewFileName);
  175. return true;
  176. }
  177. /**
  178. * Reload a search form for the specified class
  179. */
  180. function ReloadSearchForm(divId, sClassName, sBaseClass, sContext)
  181. {
  182. var oDiv = $('#ds_'+divId);
  183. oDiv.block();
  184. // deprecated in jQuery 1.8
  185. //var oFormEvents = $('#ds_'+divId+' form').data('events');
  186. var oForm = $('#ds_'+divId+' form');
  187. var oFormEvents = $._data(oForm[0], "events");
  188. // Save the submit handlers
  189. aSubmit = new Array();
  190. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  191. {
  192. for(var index = 0; index < oFormEvents.submit.length; index++)
  193. {
  194. aSubmit [index ] = { data:oFormEvents.submit[index].data, namespace:oFormEvents.submit[index].namespace, handler: oFormEvents.submit[index].handler};
  195. }
  196. }
  197. sAction = $('#ds_'+divId+' form').attr('action');
  198. // Save the current values in the form
  199. var oMap = {};
  200. $('#ds_'+divId+" form :input[name!='']").each(function() {
  201. oMap[this.name] = this.value;
  202. });
  203. oMap.operation = 'search_form';
  204. oMap.className = sClassName;
  205. oMap.baseClass = sBaseClass;
  206. oMap.currentId = divId;
  207. oMap.action = sAction;
  208. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, oMap,
  209. function(data) {
  210. oDiv.empty();
  211. oDiv.append(data);
  212. if (aSubmit.length > 0)
  213. {
  214. var oForm = $('#ds_'+divId+' form'); // Form was reloaded, recompute it
  215. for(var index = 0; index < aSubmit.length; index++)
  216. {
  217. // Restore the previously bound submit handlers
  218. if (aSubmit[index].data != undefined)
  219. {
  220. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  221. }
  222. else
  223. {
  224. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  225. }
  226. }
  227. }
  228. FixSearchFormsDisposition();
  229. oDiv.unblock();
  230. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  231. }
  232. );
  233. }
  234. function FixSearchFormsDisposition()
  235. {
  236. // Fix search forms
  237. $('.SearchDrawer').each(function() {
  238. var colWidth = 0;
  239. var labelWidth = 0;
  240. $('label:visible', $(this)).each( function() {
  241. var l = $(this).parent().width() - $(this).width();
  242. colWidth = Math.max(l, colWidth);
  243. labelWidth = Math.max($(this).width(), labelWidth);
  244. });
  245. $('label:visible', $(this)).each( function() {
  246. if($(this).data('resized') != true)
  247. {
  248. $(this).parent().width(colWidth + labelWidth);
  249. $(this).width(labelWidth).css({display: 'inline-block'}).data('resized', true);
  250. }
  251. });
  252. });
  253. }
  254. /**
  255. * Stores - in a persistent way - user specific preferences
  256. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  257. * that acts as a local -write through- cache
  258. */
  259. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  260. {
  261. sPreviousValue = undefined;
  262. try
  263. {
  264. sPreviousValue = oUserPreferences[sPreferenceCode];
  265. }
  266. catch(err)
  267. {
  268. sPreviousValue = undefined;
  269. }
  270. oUserPreferences[sPreferenceCode] = sPrefValue;
  271. if (bPersistent && (sPrefValue != sPreviousValue))
  272. {
  273. ajax_request = $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  274. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  275. }
  276. }
  277. /**
  278. * Get user specific preferences
  279. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  280. * that acts as a local -write through- cache
  281. */
  282. function GetUserPreference(sPreferenceCode, sDefaultValue)
  283. {
  284. var value = sDefaultValue;
  285. if ( oUserPreferences[sPreferenceCode] != undefined)
  286. {
  287. value = oUserPreferences[sPreferenceCode];
  288. }
  289. return value;
  290. }
  291. /**
  292. * Check/uncheck a whole list of checkboxes
  293. */
  294. function CheckAll(sSelector, bValue)
  295. {
  296. var value = bValue;
  297. $(sSelector).each( function() {
  298. if (this.checked != value)
  299. {
  300. this.checked = value;
  301. $(this).trigger('change');
  302. }
  303. });
  304. }
  305. /**
  306. * Toggle (enabled/disabled) the specified field of a form
  307. */
  308. function ToogleField(value, field_id)
  309. {
  310. if (value)
  311. {
  312. $('#'+field_id).removeAttr('disabled');
  313. // In case the field is rendered as a div containing several inputs (e.g. RedundancySettings)
  314. $('#'+field_id+' :input').removeAttr('disabled');
  315. }
  316. else
  317. {
  318. $('#'+field_id).attr('disabled', 'disabled');
  319. // In case the field is rendered as a div containing several inputs (e.g. RedundancySettings)
  320. $('#'+field_id+' :input').attr('disabled', 'disabled');
  321. }
  322. $('#'+field_id).trigger('update');
  323. $('#'+field_id).trigger('validate');
  324. }
  325. /**
  326. * For the fields that cannot be visually disabled, they can be blocked
  327. * @return
  328. */
  329. function BlockField(field_id, bBlocked)
  330. {
  331. if (bBlocked)
  332. {
  333. $('#'+field_id).block({ message: ' ** disabled ** '});
  334. }
  335. else
  336. {
  337. $('#'+field_id).unblock();
  338. }
  339. }
  340. /**
  341. * Updates (enables/disables) a "duration" field
  342. */
  343. function ToggleDurationField(field_id)
  344. {
  345. // Toggle all the subfields that compose the "duration" input
  346. aSubFields = new Array('d', 'h', 'm', 's');
  347. if ($('#'+field_id).attr('disabled'))
  348. {
  349. for(var i=0; i<aSubFields.length; i++)
  350. {
  351. $('#'+field_id+'_'+aSubFields[i]).attr('disabled', 'disabled');
  352. }
  353. }
  354. else
  355. {
  356. for(var i=0; i<aSubFields.length; i++)
  357. {
  358. $('#'+field_id+'_'+aSubFields[i]).removeAttr('disabled');
  359. }
  360. }
  361. }
  362. /**
  363. * PropagateCheckBox
  364. */
  365. function PropagateCheckBox(bCurrValue, aFieldsList, bCheck)
  366. {
  367. if (bCurrValue == bCheck)
  368. {
  369. for(var i=0;i<aFieldsList.length;i++)
  370. {
  371. $('#enable_'+aFieldsList[i]).attr('checked', bCheck);
  372. ToogleField(bCheck, aFieldsList[i]);
  373. }
  374. }
  375. }
  376. function FixTableSorter(table)
  377. {
  378. if (table[0].config == undefined)
  379. {
  380. // Table is not sort-able, let's fix it
  381. var checkbox = (table.find('th:first :checkbox').length > 0);
  382. if (checkbox)
  383. {
  384. // There is a checkbox in the first column, don't make it sort-able
  385. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  386. }
  387. else
  388. {
  389. // There is NO checkbox in the first column, all columns are considered sort-able
  390. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  391. }
  392. }
  393. }
  394. function DashletCreationDlg(sOQL)
  395. {
  396. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashlet_creation_dlg', oql: sOQL}, function(data){
  397. $('body').append(data);
  398. });
  399. return false;
  400. }
  401. function ShortcutListDlg(sOQL, sDataTableId, sContext)
  402. {
  403. var sDataTableName = 'datatable_'+sDataTableId;
  404. var oTableSettings = {
  405. oColumns: $('#'+sDataTableName).datatable('option', 'oColumns'),
  406. iPageSize: $('#'+sDataTableName).datatable('option', 'iPageSize')
  407. };
  408. var sTableSettings = JSON.stringify(oTableSettings);
  409. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, {operation: 'shortcut_list_dlg', oql: sOQL, table_settings: sTableSettings}, function(data){
  410. $('body').append(data);
  411. });
  412. return false;
  413. }
  414. function ExportListDlg(sOQL, sDataTableId, sFormat, sDlgTitle)
  415. {
  416. var aFields = [];
  417. if (sDataTableId != '')
  418. {
  419. var sDataTableName = 'datatable_'+sDataTableId;
  420. var oColumns = $('#'+sDataTableName).datatable('option', 'oColumns');
  421. for(var j in oColumns)
  422. {
  423. for(var k in oColumns[j])
  424. {
  425. if (oColumns[j][k].checked)
  426. {
  427. var sCode = oColumns[j][k].code;
  428. if (sCode == '_key_')
  429. {
  430. sCode = 'id';
  431. }
  432. aFields.push(j+'.'+sCode);
  433. }
  434. }
  435. }
  436. }
  437. var oParams = {
  438. interactive: 1,
  439. mode: 'dialog',
  440. expression: sOQL,
  441. suggested_fields: aFields.join(','),
  442. dialog_title: sDlgTitle
  443. };
  444. if (sFormat !== null)
  445. {
  446. oParams.format = sFormat;
  447. }
  448. $.post(GetAbsoluteUrlAppRoot()+'webservices/export-v2.php', oParams, function(data) {
  449. $('body').append(data);
  450. });
  451. return false;
  452. }
  453. function ExportToggleFormat(sFormat)
  454. {
  455. $('.form_part').hide();
  456. for(k in window.aFormParts[sFormat])
  457. {
  458. $('#form_part_'+window.aFormParts[sFormat][k]).show().trigger('form-part-activate');
  459. }
  460. }
  461. function ExportStartExport()
  462. {
  463. var oParams = {};
  464. $('.form_part:visible :input').each(function() {
  465. if (this.name != '')
  466. {
  467. if ((this.type == 'radio') || (this.type == 'checkbox'))
  468. {
  469. if (this.checked)
  470. {
  471. oParams[this.name] = $(this).val();
  472. }
  473. }
  474. else
  475. {
  476. oParams[this.name] = $(this).val();
  477. }
  478. }
  479. });
  480. $(':itop-tabularfieldsselector:visible').tabularfieldsselector('close_all_tooltips');
  481. $('#export-form').hide();
  482. $('#export-feedback').show();
  483. oParams.operation = 'export_build';
  484. oParams.format = $('#export-form :input[name=format]').val();
  485. var sQueryMode = $(':input[name=query_mode]:checked').val();
  486. if($(':input[name=query_mode]:checked').length > 0)
  487. {
  488. if (sQueryMode == 'oql')
  489. {
  490. oParams.expression = $('#export-form :input[name=expression]').val();
  491. }
  492. else
  493. {
  494. oParams.query = $('#export-form :input[name=query]').val();
  495. }
  496. }
  497. else
  498. {
  499. oParams.expression = $('#export-form :input[name=expression]').val();
  500. oParams.query = $('#export-form :input[name=query]').val();
  501. }
  502. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  503. ExportRun(data);
  504. }, 'json');
  505. }
  506. function ExportRun(data)
  507. {
  508. switch(data.code)
  509. {
  510. case 'run':
  511. // Continue
  512. $('.export-progress-bar').progressbar({value: data.percentage });
  513. $('.export-message').html(data.message);
  514. oParams = {};
  515. oParams.token = data.token;
  516. var sDataState = $('#export-form').attr('data-state');
  517. if (sDataState == 'cancelled')
  518. {
  519. oParams.operation = 'export_cancel';
  520. }
  521. else
  522. {
  523. oParams.operation = 'export_build';
  524. }
  525. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  526. ExportRun(data);
  527. },
  528. 'json');
  529. break;
  530. case 'done':
  531. $('#export-btn').hide();
  532. sMessage = '<a href="'+GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=export_download&token='+data.token+'" target="_blank">'+data.message+'</a>';
  533. $('.export-message').html(sMessage);
  534. $('.export-progress-bar').hide();
  535. $('#export-btn').hide();
  536. $('#export-form').attr('data-state', 'done');
  537. if(data.text_result != undefined)
  538. {
  539. if (data.mime_type == 'text/html')
  540. {
  541. $('#export_content').parent().html(data.text_result);
  542. $('#export_text_result').show();
  543. $('#export_text_result .listResults').tableHover();
  544. $('#export_text_result .listResults').tablesorter( { widgets: ['myZebra']} );
  545. }
  546. else
  547. {
  548. if ($('#export_text_result').closest('ui-dialog').length == 0)
  549. {
  550. // not inside a dialog box, adjust the height... approximately
  551. var jPane = $('#export_text_result').closest('.ui-layout-content');
  552. var iTotalHeight = jPane.height();
  553. jPane.children(':visible').each(function() {
  554. if ($(this).attr('id') != '')
  555. {
  556. iTotalHeight -= $(this).height();
  557. }
  558. });
  559. $('#export_content').height(iTotalHeight - 80);
  560. }
  561. $('#export_content').val(data.text_result);
  562. $('#export_text_result').show();
  563. }
  564. }
  565. $('#export-dlg-submit').button('option', 'label', Dict.S('UI:Button:Done')).button('enable');
  566. break;
  567. case 'error':
  568. $('#export-form').attr('data-state', 'error');
  569. $('.export-progress-bar').progressbar({value: data.percentage });
  570. $('.export-message').html(data.message);
  571. $('#export-dlg-submit').button('option', 'label', Dict.S('UI:Button:Done')).button('enable');
  572. $('#export-btn').hide();
  573. default:
  574. }
  575. }
  576. function ExportInitButton(sSelector)
  577. {
  578. $(sSelector).on('click', function() {
  579. var sDataState = $('#export-form').attr('data-state');
  580. switch(sDataState)
  581. {
  582. case 'not-yet-started':
  583. $('.form_part:visible').each(function() {
  584. $('#export-form').data('validation_messages', []);
  585. var ret = $(this).trigger('validate');
  586. });
  587. var aMessages = $('#export-form').data('validation_messages');
  588. if(aMessages.length > 0)
  589. {
  590. alert(aMessages.join(''));
  591. return;
  592. }
  593. if ($(this).hasClass('ui-button'))
  594. {
  595. $(this).button('option', 'label', Dict.S('UI:Button:Cancel'));
  596. }
  597. else
  598. {
  599. $(this).html(Dict.S('UI:Button:Cancel'));
  600. }
  601. $('#export-form').attr('data-state', 'running');
  602. ExportStartExport();
  603. break;
  604. case 'running':
  605. if ($(this).hasClass('ui-button'))
  606. {
  607. $(this).button('disable');
  608. }
  609. else
  610. {
  611. $(this).attr('disabled', 'disabled');
  612. }
  613. $('#export-form').attr('data-state', 'cancelled');
  614. break;
  615. case 'done':
  616. case 'error':
  617. $('#interactive_export_dlg').dialog('close');
  618. break;
  619. default:
  620. // Do nothing
  621. }
  622. });
  623. }
  624. function DisplayHistory(sSelector, sFilter, iCount, iStart)
  625. {
  626. $(sSelector).block();
  627. var oParams = { operation: 'history_from_filter', filter: sFilter, start: iStart, count: iCount };
  628. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  629. $(sSelector).html(data).unblock();
  630. }
  631. );
  632. }