tabularfieldsselector.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // jQuery UI style "widget" for managing the "xlsx-exporter"
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "tabularfieldsselector" the widget name
  6. $.widget( "itop.tabularfieldsselector",
  7. {
  8. // default options
  9. options:
  10. {
  11. fields: [],
  12. value_holder: '#tabular_fields',
  13. sample_data: [],
  14. total_count: 0,
  15. preview_limit: 3,
  16. labels: {
  17. preview_header: "Drag and drop the columns to change their order. Preview of %1$s lines. Total number of lines to export: %2$s",
  18. empty_preview: "Select the columns to be exported from the list above",
  19. columns_order: "Columns order",
  20. columns_selection: 'Available columns from %1$s',
  21. check_all: 'Check all',
  22. uncheck_all: 'Uncheck all',
  23. no_field_selected: 'Select at least one column to be exported'
  24. }
  25. },
  26. // the constructor
  27. _create: function()
  28. {
  29. var me = this;
  30. this._flatten_fields(this.options.fields);
  31. this.sId = this.element.attr('id');
  32. this.element
  33. .addClass('itop-tabularfieldsselector');
  34. this.element.parent().bind('form-part-activate', function() { me._update_from_holder(); me._update_preview(); });
  35. this.element.parent().bind('validate', function() { me.validate(); });
  36. this.aSelected = [];
  37. for(var i in this.options.fields)
  38. {
  39. var sContent = '<fieldset><legend>'+this._format(this.options.labels.columns_selection, i)+'</legend>';
  40. sContent += '<div style="text-align:right"><button class="check_all" type="button">'+this.options.labels.check_all+'</button>&nbsp;<button class="uncheck_all" type="button">'+this.options.labels.uncheck_all+'</button></div>';
  41. for(var j in this.options.fields[i])
  42. {
  43. sContent += this._get_field_checkbox(this.options.fields[i][j].code, this.options.fields[i][j].label, (this.options.fields[i][j].subattr.length > 0), false, null);
  44. }
  45. sContent += '</fieldset>';
  46. this.element.append(sContent);
  47. }
  48. sContent = '<fieldset><legend>'+this.options.labels.columns_order+'</legend>';
  49. sContent += '<div class="preview_header">'+this._format(this.options.labels.preview_header, Math.min(this.options.preview_limit, this.options.total_count), this.options.total_count)+'</div>';
  50. sContent += '<div class="table_preview"></div>';
  51. sContent += '</fieldset>';
  52. this.element.append(sContent);
  53. this._update_from_holder();
  54. $('body').on('click change', '.tfs_checkbox', function() {
  55. var sInstanceId = $(this).attr('data-instance-id');
  56. if (sInstanceId != me.sId) return;
  57. me._on_click($(this));
  58. });
  59. var maxWidth = 0;
  60. $('#'+this.sId+' .tfs_checkbox, #'+this.sId+' .tfs_checkbox_multi').each(function() {
  61. maxWidth = Math.max(maxWidth, $(this).parent().width());
  62. });
  63. $('#'+this.sId+' .tfs_checkbox, #'+this.sId+' .tfs_checkbox_multi').each(function() {
  64. $(this).parent().parent().width(maxWidth).css({display: 'inline-block'});
  65. });
  66. $('#'+this.sId+' .tfs_checkbox_multi').click(function() {
  67. me._on_multi_click($(this).val(), this.checked);
  68. });
  69. $('#'+this.sId+' .check_all').click(function() {
  70. me._on_check_all($(this).closest('fieldset'), true);
  71. });
  72. $('#'+this.sId+' .uncheck_all').click(function() {
  73. me._on_check_all($(this).closest('fieldset'), false);
  74. });
  75. this._update_preview();
  76. this._make_tooltips();
  77. },
  78. _on_click: function(jItemClicked)
  79. {
  80. var bChecked = jItemClicked.prop('checked');
  81. var sValue = jItemClicked.val();
  82. this._mark_as_selected(sValue, bChecked);
  83. this._update_holder();
  84. this._update_preview();
  85. var sDataParent = jItemClicked.attr('data-parent');
  86. if (sDataParent != '')
  87. {
  88. this._update_tristate(sDataParent+'_multi');
  89. }
  90. },
  91. _on_multi_click: function(sMultiFieldCode, bChecked)
  92. {
  93. var oField = this._get_main_field_by_code(sMultiFieldCode);
  94. if (oField != null)
  95. {
  96. var sPrefix = '#tfs_'+this.sId+'_';
  97. for(var k in oField.subattr)
  98. {
  99. this._mark_as_selected(oField.subattr[k].code, bChecked);
  100. // In case the tooltip is visible, also update the checkboxes
  101. sElementId = (sPrefix+oField.subattr[k].code).replace('.', '_');
  102. $(sElementId).prop('checked', bChecked);
  103. }
  104. this._update_holder();
  105. this._update_preview();
  106. }
  107. },
  108. _on_check_all: function(jSelector, bChecked)
  109. {
  110. var me = this;
  111. jSelector.find('.tfs_checkbox').each(function() {
  112. $(this).prop('checked', bChecked);
  113. me._mark_as_selected($(this).val(), bChecked);
  114. });
  115. jSelector.find('.tfs_checkbox_multi').each(function() {
  116. var oField = me._get_main_field_by_code($(this).val());
  117. if (oField != null)
  118. {
  119. $(this).prop('checked', bChecked);
  120. $(this).prop('indeterminate', false);
  121. var sPrefix = '#tfs_'+this.sId+'_';
  122. for(var k in oField.subattr)
  123. {
  124. me._mark_as_selected(oField.subattr[k].code, bChecked);
  125. // In case the tooltip is visible, also update the checkboxes
  126. sElementId = (sPrefix+oField.subattr[k].code).replace('.', '_');
  127. $(sElementId).prop('checked', bChecked);
  128. }
  129. }
  130. });
  131. this._update_holder();
  132. this._update_preview();
  133. },
  134. _update_tristate: function(sParentId)
  135. {
  136. // Check if the parent is checked, unchecked or indeterminate
  137. var sParentId = sParentId.replace('.', '_');
  138. var sAttCode = $('#'+sParentId).val();
  139. var oField = this._get_main_field_by_code(sAttCode);
  140. if (oField != null)
  141. {
  142. var iNbChecked = 0;
  143. var aDebug = [];
  144. for(var j in oField.subattr)
  145. {
  146. if ($.inArray(oField.subattr[j].code, this.aSelected) != -1)
  147. {
  148. aDebug.push(oField.subattr[j].code);
  149. iNbChecked++;
  150. }
  151. }
  152. if (iNbChecked == oField.subattr.length)
  153. {
  154. $('#'+sParentId).prop('checked', true);
  155. $('#'+sParentId).prop('indeterminate', false);
  156. }
  157. else if (iNbChecked == 0)
  158. {
  159. $('#'+sParentId).prop('checked', false);
  160. $('#'+sParentId).prop('indeterminate', false);
  161. }
  162. else
  163. {
  164. $('#'+sParentId).prop('checked', false);
  165. $('#'+sParentId).prop('indeterminate', true);
  166. }
  167. }
  168. },
  169. _mark_as_selected: function(sValue, bSelected)
  170. {
  171. if(bSelected)
  172. {
  173. if ($.inArray(sValue, this.aSelected) == -1)
  174. {
  175. this.aSelected.push(sValue);
  176. }
  177. }
  178. else
  179. {
  180. aSelected = [];
  181. for(var k in this.aSelected)
  182. {
  183. if (this.aSelected[k] != sValue)
  184. {
  185. aSelected.push(this.aSelected[k]);
  186. }
  187. }
  188. this.aSelected = aSelected;
  189. }
  190. },
  191. _update_holder: function()
  192. {
  193. $(this.options.value_holder).val(this.aSelected.join(','));
  194. },
  195. _update_from_holder: function()
  196. {
  197. var sFields = $(this.options.value_holder).val();
  198. var bAdvanced = parseInt($(this.options.advanced_holder).val(), 10);
  199. if (sFields != '')
  200. {
  201. this.aSelected = sFields.split(',');
  202. var safeSelected = [];
  203. var me = this;
  204. var bModified = false;
  205. for(var k in this.aSelected)
  206. {
  207. var oField = this._get_field_by_code(this.aSelected[k])
  208. if (oField == null)
  209. {
  210. // Invalid field code supplied, don't copy it
  211. bModified = true;
  212. }
  213. else
  214. {
  215. safeSelected.push(this.aSelected[k]);
  216. }
  217. }
  218. if (bModified)
  219. {
  220. this.aSelected = safeSelected;
  221. this._update_holder();
  222. }
  223. $('#'+this.sId+' .tfs_checkbox').each(function() {
  224. if ($.inArray($(this).val(), me.aSelected) != -1)
  225. {
  226. $(this).prop('checked', true);
  227. }
  228. else
  229. {
  230. $(this).prop('checked', false);
  231. }
  232. });
  233. }
  234. var me = this;
  235. $('#'+this.sId+' .tfs_checkbox_multi').each(function() {
  236. me._update_tristate($(this).attr('id'));
  237. });
  238. },
  239. _update_preview: function()
  240. {
  241. var sHtml = '';
  242. if(this.aSelected.length > 0)
  243. {
  244. sHtml += '<table><thead><tr>';
  245. for(var k in this.aSelected)
  246. {
  247. var sField = this.aSelected[k];
  248. if ($.inArray(sField, this.aSelected) != -1)
  249. {
  250. var sRemoveBtn = '&nbsp;<span style="display:inline-block;float:right;cursor:pointer;" class="export-field-close" data-attcode="'+sField+'">×</span>';
  251. sHtml += '<th data-attcode="'+sField+'"><span class="drag-handle">'+this.aFieldsByCode[sField].unique_label+'</span>'+sRemoveBtn+'</th>';
  252. }
  253. }
  254. sHtml += '</tr></thead><tbody>';
  255. for(var i=0; i<Math.min(this.options.preview_limit, this.options.total_count); i++)
  256. {
  257. sHtml += '<tr>';
  258. for(var k in this.aSelected)
  259. {
  260. var sField = this.aSelected[k];
  261. sHtml += '<td>'+this.options.sample_data[i][sField]+'</td>';
  262. }
  263. sHtml += '</tr>';
  264. }
  265. sHtml += '</tbody></table>';
  266. $('#'+this.sId+' .preview_header').show();
  267. $('#'+this.sId+' .table_preview').html(sHtml);
  268. var me = this;
  269. $('#'+this.sId+' .table_preview table').dragtable({persistState: function(table) { me._on_drag_columns(table); }, dragHandle: '.drag-handle'});
  270. $('#'+this.sId+' .table_preview table .export-field-close').click( function(event) { me._on_remove_column($(this).attr('data-attcode')); event.preventDefault(); return false; } );
  271. }
  272. else
  273. {
  274. $('#'+this.sId+' .preview_header').hide();
  275. $('#'+this.sId+' .table_preview').html('<div class="export_empty_preview">'+this.options.labels.empty_preview+'</div>');
  276. }
  277. },
  278. _get_field_by_code: function(sFieldCode)
  279. {
  280. for(var k in this.aFieldsByCode)
  281. {
  282. if (k == sFieldCode)
  283. {
  284. return this.aFieldsByCode[k];
  285. }
  286. }
  287. return null;
  288. },
  289. _get_main_field_by_code: function(sFieldCode)
  290. {
  291. for(var i in this.options.fields)
  292. {
  293. for(var j in this.options.fields[i])
  294. {
  295. if (this.options.fields[i][j].code == sFieldCode)
  296. {
  297. return this.options.fields[i][j];
  298. }
  299. }
  300. }
  301. return null;
  302. },
  303. _on_drag_columns: function(table)
  304. {
  305. var me = this;
  306. me.aSelected = [];
  307. table.el.find('th').each(function(i) {
  308. me.aSelected.push($(this).attr('data-attcode'));
  309. });
  310. this._update_holder();
  311. },
  312. _on_remove_column: function(sField)
  313. {
  314. var sElementId = this.sId+'_'+sField;
  315. sElementId = '#tfs_'+sElementId.replace('.', '_');
  316. $(sElementId).prop('checked', false);
  317. this._mark_as_selected(sField, false);
  318. this._update_holder();
  319. this._update_preview();
  320. var me = this;
  321. $('#'+this.sId+' .tfs_checkbox_multi').each(function() {
  322. me._update_tristate($(this).attr('id'));
  323. });
  324. },
  325. _format: function()
  326. {
  327. var s = arguments[0];
  328. for (var i = 0; i < arguments.length - 1; i++) {
  329. var reg = new RegExp("%" + (i+1) + "\\$s", "gm");
  330. s = s.replace(reg, arguments[i+1]);
  331. }
  332. return s;
  333. },
  334. validate: function()
  335. {
  336. if (this.aSelected.length == 0)
  337. {
  338. var aMessages = $('#export-form').data('validation_messages');
  339. aMessages.push(this.options.labels.no_field_selected);
  340. $('#export-form').data('validation_messages', aMessages);
  341. }
  342. },
  343. // events bound via _bind are removed automatically
  344. // revert other modifications here
  345. destroy: function()
  346. {
  347. this.element
  348. .removeClass('itop-tabularfieldsselector');
  349. this.element.parent().unbind('activate');
  350. this.element.parent().unbind('validate');
  351. },
  352. // _setOptions is called with a hash of all options that are changing
  353. _setOptions: function()
  354. {
  355. this._superApply(arguments);
  356. },
  357. // _setOption is called for each individual option that is changing
  358. _setOption: function( key, value )
  359. {
  360. if (key == 'fields')
  361. {
  362. this._flatten_fields(value);
  363. }
  364. this._superApply(arguments);
  365. },
  366. _flatten_fields: function(aFields)
  367. {
  368. // Update the "flattened" via of the fields
  369. this.aFieldsByCode = [];
  370. for(var k in aFields)
  371. {
  372. for(var i in aFields[k])
  373. {
  374. this.aFieldsByCode[aFields[k][i].code] = aFields[k][i];
  375. for(var j in aFields[k][i].subattr)
  376. {
  377. this.aFieldsByCode[aFields[k][i].subattr[j].code] = aFields[k][i].subattr[j];
  378. }
  379. }
  380. }
  381. },
  382. _make_tooltips: function()
  383. {
  384. var me = this;
  385. $('#'+this.sId+' .tfs_advanced').tooltip({
  386. content: function() {
  387. var sDataAttcode = $(this).attr('data-attcode');
  388. var sTooltipContent = '';
  389. sTooltipContent += me._get_tooltip_content(sDataAttcode);
  390. return sTooltipContent;
  391. },
  392. items: '.tfs_advanced',
  393. tooltipClass: 'tooltip-tfs',
  394. position: {
  395. my: "center bottom-10",
  396. at: "center top",
  397. using: function( position, feedback ) {
  398. $(this).css( position );
  399. $( "<div>" )
  400. .addClass( "arrow" )
  401. .addClass( feedback.vertical )
  402. .addClass( feedback.horizontal )
  403. .appendTo( this );
  404. }
  405. }
  406. })
  407. .off( "mouseover mouseout" )
  408. .on( "mouseover", function(event){
  409. event.stopImmediatePropagation();
  410. var jMe = $(this);
  411. $(this).data('openTimeoutId', setTimeout(function() {
  412. var sDataId = jMe.attr('data-attcode');
  413. if ($('.tooltip-close-button[data-attcode="'+sDataId+'"]').length == 0)
  414. {
  415. jMe.tooltip('open');
  416. }
  417. }, 500));
  418. })
  419. .on( "mouseout", function(event){
  420. event.stopImmediatePropagation();
  421. clearTimeout($(this).data('openTimeoutId'));
  422. });
  423. /*
  424. .on( "click", function(){
  425. var sDataId = $(this).attr('data-attcode');
  426. if ($('.tooltip-close-button[data-attcode="'+sDataId+'"]').length == 0)
  427. {
  428. $(this).tooltip( 'open' );
  429. }
  430. else
  431. {
  432. $(this).tooltip( 'close' );
  433. }
  434. $( this ).unbind( "mouseleave" );
  435. return false;
  436. });
  437. */
  438. $('body').on('click', '.tooltip-close-button', function() {
  439. var sDataId = $(this).attr('data-attcode');
  440. $('#'+me.sId+' .tfs_advanced[data-attcode="'+sDataId+'"]').tooltip('close');
  441. });
  442. this.element.parent().on("click", ":not(.tooltip-tfs *,.tooltip-tfs)", function(){
  443. me.close_all_tooltips();
  444. });
  445. },
  446. _get_tooltip_content: function(sDataAttCode)
  447. {
  448. var oField = this._get_main_field_by_code(sDataAttCode);
  449. var sContent = '';
  450. if (oField != null)
  451. {
  452. sContent += '<div display:block;">'+oField.label+'<div class="tooltip-close-button" data-attcode="'+sDataAttCode+'" style="display:inline-block; float:right; cursor:pointer; padding-left:0.25em; padding-bottom:0.25em;">×</div></div>';
  453. for(var k in oField.subattr)
  454. {
  455. bChecked = ($.inArray(oField.subattr[k].code, this.aSelected) != -1);
  456. sContent += this._get_field_checkbox(oField.subattr[k].code, oField.subattr[k].label, false, bChecked, sDataAttCode);
  457. }
  458. }
  459. return sContent;
  460. },
  461. _get_field_checkbox: function(sCode, sLabel, bHasTooltip, bChecked, sParentId)
  462. {
  463. var sPrefix = 'tfs_'+this.sId+'_';
  464. sParentId = (sPrefix+sParentId).replace('.', '_');
  465. sElementId = (sPrefix+sCode).replace('.', '_');
  466. var aClasses = [];
  467. if (bHasTooltip)
  468. {
  469. aClasses.push('tfs_advanced');
  470. sLabel += ' [+]';
  471. }
  472. var sChecked = '';
  473. if (bChecked)
  474. {
  475. sChecked = ' checked ';
  476. }
  477. var sDataParent = '';
  478. if (sParentId != null)
  479. {
  480. sDataParent = ' data-parent="'+sParentId+'" ';
  481. }
  482. if (bHasTooltip)
  483. {
  484. sContent = '<div style="display:block; clear:both;"><span style="white-space: nowrap;"><input data-instance-id="'+this.sId+'" class="tfs_checkbox_multi" type="checkbox" id="'+sElementId+'_multi" value="'+sCode+'"'+sChecked+sDataParent+'><label data-attcode="'+sCode+'" class="'+aClasses.join(' ')+'" title="'+sCode+'">&nbsp;'+sLabel+'</label></div>';
  485. }
  486. else
  487. {
  488. sContent = '<div style="display:block; clear:both;"><span style="white-space: nowrap;"><input data-instance-id="'+this.sId+'" class="tfs_checkbox" type="checkbox" id="'+sElementId+'" value="'+sCode+'"'+sChecked+sDataParent+'><label data-attcode="'+sCode+'" class="'+aClasses.join(' ')+'" title="'+sCode+'" for="'+sElementId+'">&nbsp;'+sLabel+'</label></div>';
  489. }
  490. return sContent;
  491. },
  492. close_all_tooltips: function()
  493. {
  494. $('.tfs_advanced').each(function (i) {
  495. $(this).tooltip("close");
  496. });
  497. }
  498. });
  499. });