tabularfieldsselector.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. var sContent = '';
  38. for(var i in this.options.fields)
  39. {
  40. sContent += '<fieldset><legend>'+this._format(this.options.labels.columns_selection, i)+'</legend>';
  41. 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>';
  42. for(var j in this.options.fields[i])
  43. {
  44. 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);
  45. }
  46. sContent += '</fieldset>';
  47. this.element.append(sContent);
  48. }
  49. sContent = '<fieldset><legend>'+this.options.labels.columns_order+'</legend>';
  50. 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>';
  51. sContent += '<div class="table_preview"></div>';
  52. sContent += '</fieldset>';
  53. this.element.append(sContent);
  54. this._update_from_holder();
  55. $('body').on('click change', '.tfs_checkbox', function() {
  56. var sInstanceId = $(this).attr('data-instance-id');
  57. if (sInstanceId != me.sId) return;
  58. me._on_click($(this));
  59. });
  60. var maxWidth = 0;
  61. $('#'+this.sId+' .tfs_checkbox, #'+this.sId+' .tfs_checkbox_multi').each(function() {
  62. maxWidth = Math.max(maxWidth, $(this).parent().width());
  63. });
  64. $('#'+this.sId+' .tfs_checkbox, #'+this.sId+' .tfs_checkbox_multi').each(function() {
  65. $(this).parent().parent().width(maxWidth).css({display: 'inline-block'});
  66. });
  67. $('#'+this.sId+' .tfs_checkbox_multi').click(function() {
  68. me._on_multi_click($(this).val(), this.checked);
  69. });
  70. $('#'+this.sId+' .check_all').click(function() {
  71. me._on_check_all($(this).closest('fieldset'), true);
  72. });
  73. $('#'+this.sId+' .uncheck_all').click(function() {
  74. me._on_check_all($(this).closest('fieldset'), false);
  75. });
  76. this._update_preview();
  77. this._make_tooltips();
  78. },
  79. _on_click: function(jItemClicked)
  80. {
  81. var bChecked = jItemClicked.prop('checked');
  82. var sValue = jItemClicked.val();
  83. this._mark_as_selected(sValue, bChecked);
  84. this._update_holder();
  85. this._update_preview();
  86. var sDataParent = jItemClicked.attr('data-parent');
  87. if (sDataParent != '')
  88. {
  89. this._update_tristate(sDataParent+'_multi');
  90. }
  91. },
  92. _on_multi_click: function(sMultiFieldCode, bChecked)
  93. {
  94. var oField = this._get_main_field_by_code(sMultiFieldCode);
  95. if (oField != null)
  96. {
  97. var sPrefix = '#tfs_'+this.sId+'_';
  98. for(var k in oField.subattr)
  99. {
  100. this._mark_as_selected(oField.subattr[k].code, bChecked);
  101. // In case the tooltip is visible, also update the checkboxes
  102. sElementId = (sPrefix+oField.subattr[k].code).replace('.', '_');
  103. $(sElementId).prop('checked', bChecked);
  104. }
  105. this._update_holder();
  106. this._update_preview();
  107. }
  108. },
  109. _on_check_all: function(jSelector, bChecked)
  110. {
  111. var me = this;
  112. jSelector.find('.tfs_checkbox').each(function() {
  113. $(this).prop('checked', bChecked);
  114. me._mark_as_selected($(this).val(), bChecked);
  115. });
  116. jSelector.find('.tfs_checkbox_multi').each(function() {
  117. var oField = me._get_main_field_by_code($(this).val());
  118. if (oField != null)
  119. {
  120. $(this).prop('checked', bChecked);
  121. $(this).prop('indeterminate', false);
  122. var sPrefix = '#tfs_'+this.sId+'_';
  123. for(var k in oField.subattr)
  124. {
  125. me._mark_as_selected(oField.subattr[k].code, bChecked);
  126. // In case the tooltip is visible, also update the checkboxes
  127. sElementId = (sPrefix+oField.subattr[k].code).replace('.', '_');
  128. $(sElementId).prop('checked', bChecked);
  129. }
  130. }
  131. });
  132. this._update_holder();
  133. this._update_preview();
  134. },
  135. _update_tristate: function(sParentId)
  136. {
  137. // Check if the parent is checked, unchecked or indeterminate
  138. var sParentId = sParentId.replace('.', '_');
  139. var sAttCode = $('#'+sParentId).val();
  140. var oField = this._get_main_field_by_code(sAttCode);
  141. if (oField != null)
  142. {
  143. var iNbChecked = 0;
  144. var aDebug = [];
  145. for(var j in oField.subattr)
  146. {
  147. if ($.inArray(oField.subattr[j].code, this.aSelected) != -1)
  148. {
  149. aDebug.push(oField.subattr[j].code);
  150. iNbChecked++;
  151. }
  152. }
  153. if (iNbChecked == oField.subattr.length)
  154. {
  155. $('#'+sParentId).prop('checked', true);
  156. $('#'+sParentId).prop('indeterminate', false);
  157. }
  158. else if (iNbChecked == 0)
  159. {
  160. $('#'+sParentId).prop('checked', false);
  161. $('#'+sParentId).prop('indeterminate', false);
  162. }
  163. else
  164. {
  165. $('#'+sParentId).prop('checked', false);
  166. $('#'+sParentId).prop('indeterminate', true);
  167. }
  168. }
  169. },
  170. _mark_as_selected: function(sValue, bSelected)
  171. {
  172. if(bSelected)
  173. {
  174. if ($.inArray(sValue, this.aSelected) == -1)
  175. {
  176. this.aSelected.push(sValue);
  177. }
  178. }
  179. else
  180. {
  181. aSelected = [];
  182. for(var k in this.aSelected)
  183. {
  184. if (this.aSelected[k] != sValue)
  185. {
  186. aSelected.push(this.aSelected[k]);
  187. }
  188. }
  189. this.aSelected = aSelected;
  190. }
  191. },
  192. _update_holder: function()
  193. {
  194. $(this.options.value_holder).val(this.aSelected.join(','));
  195. },
  196. _update_from_holder: function()
  197. {
  198. var sFields = $(this.options.value_holder).val();
  199. var bAdvanced = parseInt($(this.options.advanced_holder).val(), 10);
  200. if (sFields != '')
  201. {
  202. this.aSelected = sFields.split(',');
  203. var safeSelected = [];
  204. var me = this;
  205. var bModified = false;
  206. for(var k in this.aSelected)
  207. {
  208. var oField = this._get_field_by_code(this.aSelected[k])
  209. if (oField == null)
  210. {
  211. // Invalid field code supplied, don't copy it
  212. bModified = true;
  213. }
  214. else
  215. {
  216. safeSelected.push(this.aSelected[k]);
  217. }
  218. }
  219. if (bModified)
  220. {
  221. this.aSelected = safeSelected;
  222. this._update_holder();
  223. }
  224. $('#'+this.sId+' .tfs_checkbox').each(function() {
  225. if ($.inArray($(this).val(), me.aSelected) != -1)
  226. {
  227. $(this).prop('checked', true);
  228. }
  229. else
  230. {
  231. $(this).prop('checked', false);
  232. }
  233. });
  234. }
  235. var me = this;
  236. $('#'+this.sId+' .tfs_checkbox_multi').each(function() {
  237. me._update_tristate($(this).attr('id'));
  238. });
  239. },
  240. _update_preview: function()
  241. {
  242. var sHtml = '';
  243. if(this.aSelected.length > 0)
  244. {
  245. sHtml += '<table><thead><tr>';
  246. for(var k in this.aSelected)
  247. {
  248. var sField = this.aSelected[k];
  249. if ($.inArray(sField, this.aSelected) != -1)
  250. {
  251. var sRemoveBtn = '&nbsp;<span style="display:inline-block;float:right;cursor:pointer;" class="export-field-close" data-attcode="'+sField+'">×</span>';
  252. sHtml += '<th data-attcode="'+sField+'"><span class="drag-handle">'+this.aFieldsByCode[sField].unique_label+'</span>'+sRemoveBtn+'</th>';
  253. }
  254. }
  255. sHtml += '</tr></thead><tbody>';
  256. for(var i=0; i<Math.min(this.options.preview_limit, this.options.total_count); i++)
  257. {
  258. sHtml += '<tr>';
  259. for(var k in this.aSelected)
  260. {
  261. var sField = this.aSelected[k];
  262. sHtml += '<td>'+this.options.sample_data[i][sField]+'</td>';
  263. }
  264. sHtml += '</tr>';
  265. }
  266. sHtml += '</tbody></table>';
  267. $('#'+this.sId+' .preview_header').show();
  268. $('#'+this.sId+' .table_preview').html(sHtml);
  269. var me = this;
  270. $('#'+this.sId+' .table_preview table').dragtable({persistState: function(table) { me._on_drag_columns(table); }, dragHandle: '.drag-handle'});
  271. $('#'+this.sId+' .table_preview table .export-field-close').click( function(event) { me._on_remove_column($(this).attr('data-attcode')); event.preventDefault(); return false; } );
  272. }
  273. else
  274. {
  275. $('#'+this.sId+' .preview_header').hide();
  276. $('#'+this.sId+' .table_preview').html('<div class="export_empty_preview">'+this.options.labels.empty_preview+'</div>');
  277. }
  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. 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. },
  443. _get_tooltip_content: function(sDataAttCode)
  444. {
  445. var oField = this._get_main_field_by_code(sDataAttCode);
  446. var sContent = '';
  447. if (oField != null)
  448. {
  449. 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>';
  450. for(var k in oField.subattr)
  451. {
  452. bChecked = ($.inArray(oField.subattr[k].code, this.aSelected) != -1);
  453. sContent += this._get_field_checkbox(oField.subattr[k].code, oField.subattr[k].label, false, bChecked, sDataAttCode);
  454. }
  455. }
  456. return sContent;
  457. },
  458. _get_field_checkbox: function(sCode, sLabel, bHasTooltip, bChecked, sParentId)
  459. {
  460. var sPrefix = 'tfs_'+this.sId+'_';
  461. sParentId = (sPrefix+sParentId).replace('.', '_');
  462. sElementId = (sPrefix+sCode).replace('.', '_');
  463. var aClasses = [];
  464. if (bHasTooltip)
  465. {
  466. aClasses.push('tfs_advanced');
  467. sLabel += ' [+]';
  468. }
  469. var sChecked = '';
  470. if (bChecked)
  471. {
  472. sChecked = ' checked ';
  473. }
  474. var sDataParent = '';
  475. if (sParentId != null)
  476. {
  477. sDataParent = ' data-parent="'+sParentId+'" ';
  478. }
  479. if (bHasTooltip)
  480. {
  481. 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>';
  482. }
  483. else
  484. {
  485. 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>';
  486. }
  487. return sContent;
  488. },
  489. _close_all_tooltips: function()
  490. {
  491. this.element.find('.tfs_item').tooltip('close');
  492. }
  493. });
  494. });