tabularfieldsselector.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. $('.form_part:visible').trigger('preview_updated');
  278. },
  279. _get_field_by_code: function(sFieldCode)
  280. {
  281. for(var k in this.aFieldsByCode)
  282. {
  283. if (k == sFieldCode)
  284. {
  285. return this.aFieldsByCode[k];
  286. }
  287. }
  288. return null;
  289. },
  290. _get_main_field_by_code: function(sFieldCode)
  291. {
  292. for(var i in this.options.fields)
  293. {
  294. for(var j in this.options.fields[i])
  295. {
  296. if (this.options.fields[i][j].code == sFieldCode)
  297. {
  298. return this.options.fields[i][j];
  299. }
  300. }
  301. }
  302. return null;
  303. },
  304. _on_drag_columns: function(table)
  305. {
  306. var me = this;
  307. me.aSelected = [];
  308. table.el.find('th').each(function(i) {
  309. me.aSelected.push($(this).attr('data-attcode'));
  310. });
  311. this._update_holder();
  312. },
  313. _on_remove_column: function(sField)
  314. {
  315. var sElementId = this.sId+'_'+sField;
  316. sElementId = '#tfs_'+sElementId.replace('.', '_');
  317. $(sElementId).prop('checked', false);
  318. this._mark_as_selected(sField, false);
  319. this._update_holder();
  320. this._update_preview();
  321. var me = this;
  322. $('#'+this.sId+' .tfs_checkbox_multi').each(function() {
  323. me._update_tristate($(this).attr('id'));
  324. });
  325. },
  326. _format: function()
  327. {
  328. var s = arguments[0];
  329. for (var i = 0; i < arguments.length - 1; i++) {
  330. var reg = new RegExp("%" + (i+1) + "\\$s", "gm");
  331. s = s.replace(reg, arguments[i+1]);
  332. }
  333. return s;
  334. },
  335. validate: function()
  336. {
  337. if (this.aSelected.length == 0)
  338. {
  339. var aMessages = $('#export-form').data('validation_messages');
  340. aMessages.push(this.options.labels.no_field_selected);
  341. $('#export-form').data('validation_messages', aMessages);
  342. }
  343. },
  344. // events bound via _bind are removed automatically
  345. // revert other modifications here
  346. destroy: function()
  347. {
  348. this.element
  349. .removeClass('itop-tabularfieldsselector');
  350. this.element.parent().unbind('activate');
  351. this.element.parent().unbind('validate');
  352. },
  353. // _setOptions is called with a hash of all options that are changing
  354. _setOptions: function()
  355. {
  356. this._superApply(arguments);
  357. },
  358. // _setOption is called for each individual option that is changing
  359. _setOption: function( key, value )
  360. {
  361. if (key == 'fields')
  362. {
  363. this._flatten_fields(value);
  364. }
  365. this._superApply(arguments);
  366. },
  367. _flatten_fields: function(aFields)
  368. {
  369. // Update the "flattened" via of the fields
  370. this.aFieldsByCode = [];
  371. for(var k in aFields)
  372. {
  373. for(var i in aFields[k])
  374. {
  375. this.aFieldsByCode[aFields[k][i].code] = aFields[k][i];
  376. for(var j in aFields[k][i].subattr)
  377. {
  378. this.aFieldsByCode[aFields[k][i].subattr[j].code] = aFields[k][i].subattr[j];
  379. }
  380. }
  381. }
  382. },
  383. _make_tooltips: function()
  384. {
  385. var me = this;
  386. $('#'+this.sId+' .tfs_advanced').tooltip({
  387. content: function() {
  388. var sDataAttcode = $(this).attr('data-attcode');
  389. var sTooltipContent = '';
  390. sTooltipContent += me._get_tooltip_content(sDataAttcode);
  391. return sTooltipContent;
  392. },
  393. items: '.tfs_advanced',
  394. tooltipClass: 'tooltip-tfs',
  395. position: {
  396. my: "center bottom-10",
  397. at: "center top",
  398. using: function( position, feedback ) {
  399. $(this).css( position );
  400. $( "<div>" )
  401. .addClass( "arrow" )
  402. .addClass( feedback.vertical )
  403. .addClass( feedback.horizontal )
  404. .appendTo( this );
  405. }
  406. }
  407. })
  408. .off( "mouseover mouseout" )
  409. .on( "mouseover", function(event){
  410. event.stopImmediatePropagation();
  411. var jMe = $(this);
  412. $(this).data('openTimeoutId', setTimeout(function() {
  413. var sDataId = jMe.attr('data-attcode');
  414. if ($('.tooltip-close-button[data-attcode="'+sDataId+'"]').length == 0)
  415. {
  416. jMe.tooltip('open');
  417. }
  418. }, 500));
  419. })
  420. .on( "mouseout", function(event){
  421. event.stopImmediatePropagation();
  422. clearTimeout($(this).data('openTimeoutId'));
  423. });
  424. /*
  425. .on( "click", function(){
  426. var sDataId = $(this).attr('data-attcode');
  427. if ($('.tooltip-close-button[data-attcode="'+sDataId+'"]').length == 0)
  428. {
  429. $(this).tooltip( 'open' );
  430. }
  431. else
  432. {
  433. $(this).tooltip( 'close' );
  434. }
  435. $( this ).unbind( "mouseleave" );
  436. return false;
  437. });
  438. */
  439. $('body').on('click', '.tooltip-close-button', function() {
  440. var sDataId = $(this).attr('data-attcode');
  441. $('#'+me.sId+' .tfs_advanced[data-attcode="'+sDataId+'"]').tooltip('close');
  442. });
  443. this.element.parent().on("click", ":not(.tooltip-tfs *,.tooltip-tfs)", function(){
  444. me.close_all_tooltips();
  445. });
  446. },
  447. _get_tooltip_content: function(sDataAttCode)
  448. {
  449. var oField = this._get_main_field_by_code(sDataAttCode);
  450. var sContent = '';
  451. if (oField != null)
  452. {
  453. 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>';
  454. for(var k in oField.subattr)
  455. {
  456. bChecked = ($.inArray(oField.subattr[k].code, this.aSelected) != -1);
  457. sContent += this._get_field_checkbox(oField.subattr[k].code, oField.subattr[k].label, false, bChecked, sDataAttCode);
  458. }
  459. }
  460. return sContent;
  461. },
  462. _get_field_checkbox: function(sCode, sLabel, bHasTooltip, bChecked, sParentId)
  463. {
  464. var sPrefix = 'tfs_'+this.sId+'_';
  465. sParentId = (sPrefix+sParentId).replace('.', '_');
  466. sElementId = (sPrefix+sCode).replace('.', '_');
  467. var aClasses = [];
  468. if (bHasTooltip)
  469. {
  470. aClasses.push('tfs_advanced');
  471. sLabel += ' [+]';
  472. }
  473. var sChecked = '';
  474. if (bChecked)
  475. {
  476. sChecked = ' checked ';
  477. }
  478. var sDataParent = '';
  479. if (sParentId != null)
  480. {
  481. sDataParent = ' data-parent="'+sParentId+'" ';
  482. }
  483. if (bHasTooltip)
  484. {
  485. 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>';
  486. }
  487. else
  488. {
  489. 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>';
  490. }
  491. return sContent;
  492. },
  493. close_all_tooltips: function()
  494. {
  495. $('.tfs_advanced').each(function (i) {
  496. $(this).tooltip("close");
  497. });
  498. }
  499. });
  500. });