utils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. /**
  142. * Update the display and value of a file input widget when the user picks a new file
  143. */
  144. function UpdateFileName(id, sNewFileName)
  145. {
  146. var aPath = sNewFileName.split('\\');
  147. var sNewFileName = aPath[aPath.length-1];
  148. $('#'+id).val(sNewFileName);
  149. $('#'+id).trigger('validate');
  150. $('#name_'+id).text(sNewFileName);
  151. return true;
  152. }
  153. /**
  154. * Reload a search form for the specified class
  155. */
  156. function ReloadSearchForm(divId, sClassName, sBaseClass, sContext)
  157. {
  158. var oDiv = $('#ds_'+divId);
  159. oDiv.block();
  160. // deprecated in jQuery 1.8
  161. //var oFormEvents = $('#ds_'+divId+' form').data('events');
  162. var oForm = $('#ds_'+divId+' form');
  163. var oFormEvents = $._data(oForm[0], "events");
  164. // Save the submit handlers
  165. aSubmit = new Array();
  166. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  167. {
  168. for(var index = 0; index < oFormEvents.submit.length; index++)
  169. {
  170. aSubmit [index ] = { data:oFormEvents.submit[index].data, namespace:oFormEvents.submit[index].namespace, handler: oFormEvents.submit[index].handler};
  171. }
  172. }
  173. sAction = $('#ds_'+divId+' form').attr('action');
  174. // Save the current values in the form
  175. var oMap = {};
  176. $('#ds_'+divId+" form :input[name!='']").each(function() {
  177. oMap[this.name] = this.value;
  178. });
  179. oMap.operation = 'search_form';
  180. oMap.className = sClassName;
  181. oMap.baseClass = sBaseClass;
  182. oMap.currentId = divId;
  183. oMap.action = sAction;
  184. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, oMap,
  185. function(data) {
  186. oDiv.empty();
  187. oDiv.append(data);
  188. if (aSubmit.length > 0)
  189. {
  190. var oForm = $('#ds_'+divId+' form'); // Form was reloaded, recompute it
  191. for(var index = 0; index < aSubmit.length; index++)
  192. {
  193. // Restore the previously bound submit handlers
  194. if (aSubmit[index].data != undefined)
  195. {
  196. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  197. }
  198. else
  199. {
  200. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  201. }
  202. }
  203. }
  204. FixSearchFormsDisposition();
  205. oDiv.unblock();
  206. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  207. }
  208. );
  209. }
  210. function FixSearchFormsDisposition()
  211. {
  212. // Fix search forms
  213. $('.SearchDrawer').each(function() {
  214. var colWidth = 0;
  215. var labelWidth = 0;
  216. $('label:visible', $(this)).each( function() {
  217. var l = $(this).parent().width() - $(this).width();
  218. colWidth = Math.max(l, colWidth);
  219. labelWidth = Math.max($(this).width(), labelWidth);
  220. });
  221. $('label:visible', $(this)).each( function() {
  222. if($(this).data('resized') != true)
  223. {
  224. $(this).parent().width(colWidth + labelWidth);
  225. $(this).width(labelWidth).css({display: 'inline-block'}).data('resized', true);
  226. }
  227. });
  228. });
  229. }
  230. /**
  231. * Stores - in a persistent way - user specific preferences
  232. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  233. * that acts as a local -write through- cache
  234. */
  235. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  236. {
  237. sPreviousValue = undefined;
  238. try
  239. {
  240. sPreviousValue = oUserPreferences[sPreferenceCode];
  241. }
  242. catch(err)
  243. {
  244. sPreviousValue = undefined;
  245. }
  246. oUserPreferences[sPreferenceCode] = sPrefValue;
  247. if (bPersistent && (sPrefValue != sPreviousValue))
  248. {
  249. ajax_request = $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  250. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  251. }
  252. }
  253. /**
  254. * Get user specific preferences
  255. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  256. * that acts as a local -write through- cache
  257. */
  258. function GetUserPreference(sPreferenceCode, sDefaultValue)
  259. {
  260. var value = sDefaultValue;
  261. if ( oUserPreferences[sPreferenceCode] != undefined)
  262. {
  263. value = oUserPreferences[sPreferenceCode];
  264. }
  265. return value;
  266. }
  267. /**
  268. * Check/uncheck a whole list of checkboxes
  269. */
  270. function CheckAll(sSelector, bValue)
  271. {
  272. var value = bValue;
  273. $(sSelector).each( function() {
  274. if (this.checked != value)
  275. {
  276. this.checked = value;
  277. $(this).trigger('change');
  278. }
  279. });
  280. }
  281. /**
  282. * Toggle (enabled/disabled) the specified field of a form
  283. */
  284. function ToogleField(value, field_id)
  285. {
  286. if (value)
  287. {
  288. $('#'+field_id).removeAttr('disabled');
  289. // In case the field is rendered as a div containing several inputs (e.g. RedundancySettings)
  290. $('#'+field_id+' :input').removeAttr('disabled');
  291. }
  292. else
  293. {
  294. $('#'+field_id).attr('disabled', 'disabled');
  295. // In case the field is rendered as a div containing several inputs (e.g. RedundancySettings)
  296. $('#'+field_id+' :input').attr('disabled', 'disabled');
  297. }
  298. $('#'+field_id).trigger('update');
  299. $('#'+field_id).trigger('validate');
  300. }
  301. /**
  302. * For the fields that cannot be visually disabled, they can be blocked
  303. * @return
  304. */
  305. function BlockField(field_id, bBlocked)
  306. {
  307. if (bBlocked)
  308. {
  309. $('#'+field_id).block({ message: ' ** disabled ** '});
  310. }
  311. else
  312. {
  313. $('#'+field_id).unblock();
  314. }
  315. }
  316. /**
  317. * Updates (enables/disables) a "duration" field
  318. */
  319. function ToggleDurationField(field_id)
  320. {
  321. // Toggle all the subfields that compose the "duration" input
  322. aSubFields = new Array('d', 'h', 'm', 's');
  323. if ($('#'+field_id).attr('disabled'))
  324. {
  325. for(var i=0; i<aSubFields.length; i++)
  326. {
  327. $('#'+field_id+'_'+aSubFields[i]).attr('disabled', 'disabled');
  328. }
  329. }
  330. else
  331. {
  332. for(var i=0; i<aSubFields.length; i++)
  333. {
  334. $('#'+field_id+'_'+aSubFields[i]).removeAttr('disabled');
  335. }
  336. }
  337. }
  338. /**
  339. * PropagateCheckBox
  340. */
  341. function PropagateCheckBox(bCurrValue, aFieldsList, bCheck)
  342. {
  343. if (bCurrValue == bCheck)
  344. {
  345. for(var i=0;i<aFieldsList.length;i++)
  346. {
  347. $('#enable_'+aFieldsList[i]).attr('checked', bCheck);
  348. ToogleField(bCheck, aFieldsList[i]);
  349. }
  350. }
  351. }
  352. function FixTableSorter(table)
  353. {
  354. if ($('th.header', table).length == 0)
  355. {
  356. // Table is not sort-able, let's fix it
  357. var checkbox = (table.find('th:first :checkbox').length > 0);
  358. if (checkbox)
  359. {
  360. // There is a checkbox in the first column, don't make it sort-able
  361. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  362. }
  363. else
  364. {
  365. // There is NO checkbox in the first column, all columns are considered sort-able
  366. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sort-able and zebra tables
  367. }
  368. }
  369. }
  370. function DashletCreationDlg(sOQL)
  371. {
  372. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashlet_creation_dlg', oql: sOQL}, function(data){
  373. $('body').append(data);
  374. });
  375. return false;
  376. }
  377. function ShortcutListDlg(sOQL, sDataTableId, sContext)
  378. {
  379. var sDataTableName = 'datatable_'+sDataTableId;
  380. var oTableSettings = {
  381. oColumns: $('#'+sDataTableName).datatable('option', 'oColumns'),
  382. iPageSize: $('#'+sDataTableName).datatable('option', 'iPageSize')
  383. };
  384. var sTableSettings = JSON.stringify(oTableSettings);
  385. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?'+sContext, {operation: 'shortcut_list_dlg', oql: sOQL, table_settings: sTableSettings}, function(data){
  386. $('body').append(data);
  387. });
  388. return false;
  389. }
  390. function ExportListDlg(sOQL, sDataTableId, sFormat, sDlgTitle)
  391. {
  392. var aFields = [];
  393. if (sDataTableId != '')
  394. {
  395. var sDataTableName = 'datatable_'+sDataTableId;
  396. var oColumns = $('#'+sDataTableName).datatable('option', 'oColumns');
  397. for(var j in oColumns)
  398. {
  399. for(var k in oColumns[j])
  400. {
  401. if (oColumns[j][k].checked)
  402. {
  403. var sCode = oColumns[j][k].code;
  404. if (sCode == '_key_')
  405. {
  406. sCode = 'id';
  407. }
  408. aFields.push(j+'.'+sCode);
  409. }
  410. }
  411. }
  412. }
  413. $.post(GetAbsoluteUrlAppRoot()+'webservices/export-v2.php', {interactive: 1, advanced: 1, mode: 'dialog', format: sFormat, expression: sOQL, suggested_fields: aFields.join(','), dialog_title: sDlgTitle}, function(data) {
  414. $('body').append(data);
  415. });
  416. return false;
  417. }
  418. function ExportToggleFormat(sFormat)
  419. {
  420. $('.form_part').hide();
  421. for(k in window.aFormParts[sFormat])
  422. {
  423. $('#form_part_'+window.aFormParts[sFormat][k]).show().trigger('form-part-activate');
  424. }
  425. }
  426. function ExportStartExport()
  427. {
  428. var oParams = {};
  429. $('.form_part:visible :input').each(function() {
  430. if (this.name != '')
  431. {
  432. if ((this.type == 'radio') || (this.type == 'checkbox'))
  433. {
  434. if (this.checked)
  435. {
  436. oParams[this.name] = $(this).val();
  437. }
  438. }
  439. else
  440. {
  441. oParams[this.name] = $(this).val();
  442. }
  443. }
  444. });
  445. $('#export-form').hide();
  446. $('#export-feedback').show();
  447. oParams.operation = 'export_build';
  448. oParams.format = $('#export-form :input[name=format]').val();
  449. var sQueryMode = $(':input[name=query_mode]:checked').val();
  450. if($(':input[name=query_mode]:checked').length > 0)
  451. {
  452. if (sQueryMode == 'oql')
  453. {
  454. oParams.expression = $('#export-form :input[name=expression]').val();
  455. }
  456. else
  457. {
  458. oParams.query = $('#export-form :input[name=query]').val();
  459. }
  460. }
  461. else
  462. {
  463. oParams.expression = $('#export-form :input[name=expression]').val();
  464. oParams.query = $('#export-form :input[name=query]').val();
  465. }
  466. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  467. ExportRun(data);
  468. }, 'json');
  469. }
  470. function ExportRun(data)
  471. {
  472. switch(data.code)
  473. {
  474. case 'run':
  475. // Continue
  476. $('.export-progress-bar').progressbar({value: data.percentage });
  477. $('.export-message').html(data.message);
  478. oParams = {};
  479. oParams.token = data.token;
  480. var sDataState = $('#export-form').attr('data-state');
  481. if (sDataState == 'cancelled')
  482. {
  483. oParams.operation = 'export_cancel';
  484. }
  485. else
  486. {
  487. oParams.operation = 'export_build';
  488. }
  489. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  490. ExportRun(data);
  491. },
  492. 'json');
  493. break;
  494. case 'done':
  495. $('#export-btn').hide();
  496. sMessage = '<a href="'+GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=export_download&token='+data.token+'" target="_blank">'+data.message+'</a>';
  497. $('.export-message').html(sMessage);
  498. $('.export-progress-bar').hide();
  499. $('#export-btn').hide();
  500. $('#export-form').attr('data-state', 'done');
  501. if(data.text_result != undefined)
  502. {
  503. if (data.mime_type == 'text/html')
  504. {
  505. $('#export_content').parent().html(data.text_result);
  506. $('#export_text_result').show();
  507. $('#export_text_result .listResults').tableHover();
  508. $('#export_text_result .listResults').tablesorter( { widgets: ['myZebra']} );
  509. }
  510. else
  511. {
  512. if ($('#export_text_result').closest('ui-dialog').length == 0)
  513. {
  514. // not inside a dialog box, adjust the height... approximately
  515. var jPane = $('#export_text_result').closest('.ui-layout-content');
  516. var iTotalHeight = jPane.height();
  517. jPane.children(':visible').each(function() {
  518. if ($(this).attr('id') != '')
  519. {
  520. iTotalHeight -= $(this).height();
  521. }
  522. });
  523. $('#export_content').height(iTotalHeight - 80);
  524. }
  525. $('#export_content').val(data.text_result);
  526. $('#export_text_result').show();
  527. }
  528. }
  529. $('#export-dlg-submit').button('option', 'label', Dict.S('UI:Button:Done')).button('enable');
  530. break;
  531. case 'error':
  532. $('#export-form').attr('data-state', 'error');
  533. $('.export-progress-bar').progressbar({value: data.percentage });
  534. $('.export-message').html(data.message);
  535. $('#export-dlg-submit').button('option', 'label', Dict.S('UI:Button:Done')).button('enable');
  536. $('#export-btn').hide();
  537. default:
  538. }
  539. }
  540. function ExportInitButton(sSelector)
  541. {
  542. $(sSelector).on('click', function() {
  543. var sDataState = $('#export-form').attr('data-state');
  544. switch(sDataState)
  545. {
  546. case 'not-yet-started':
  547. $('.form_part:visible').each(function() {
  548. $('#export-form').data('validation_messages', []);
  549. var ret = $(this).trigger('validate');
  550. });
  551. var aMessages = $('#export-form').data('validation_messages');
  552. if(aMessages.length > 0)
  553. {
  554. alert(aMessages.join(''));
  555. return;
  556. }
  557. if ($(this).hasClass('ui-button'))
  558. {
  559. $(this).button('option', 'label', Dict.S('UI:Button:Cancel'));
  560. }
  561. else
  562. {
  563. $(this).html(Dict.S('UI:Button:Cancel'));
  564. }
  565. $('#export-form').attr('data-state', 'running');
  566. ExportStartExport();
  567. break;
  568. case 'running':
  569. if ($(this).hasClass('ui-button'))
  570. {
  571. $(this).button('disable');
  572. }
  573. else
  574. {
  575. $(this).attr('disabled', 'disabled');
  576. }
  577. $('#export-form').attr('data-state', 'cancelled');
  578. break;
  579. case 'done':
  580. case 'error':
  581. $('#interactive_export_dlg').dialog('close');
  582. break;
  583. default:
  584. // Do nothing
  585. }
  586. });
  587. }
  588. function DisplayHistory(sSelector, sFilter, iCount, iStart)
  589. {
  590. $(sSelector).block();
  591. var oParams = { operation: 'history_from_filter', filter: sFilter, start: iStart, count: iCount };
  592. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  593. $(sSelector).html(data).unblock();
  594. }
  595. );
  596. }