You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.8 KiB

  1. // @@@ web_export_view custom JS @@@
  2. //#############################################################################
  3. //
  4. // Copyright (C) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
  5. // Copyright (C) 2012 Therp BV (<http://therp.nl>)
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU Affero General Public License as published
  9. // by the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU Affero General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Affero General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. //#############################################################################
  21. openerp.web_export_view = function(instance, m) {
  22. var _t = instance.web._t,
  23. QWeb = instance.web.qweb;
  24. instance.web.Sidebar.include({
  25. redraw: function() {
  26. var self = this;
  27. this._super.apply(this, arguments);
  28. self.$el.find('.oe_sidebar').append(QWeb.render('AddExportViewMain', {widget: self}));
  29. self.$el.find('.oe_sidebar_export_view_xls').on('click', self.on_sidebar_export_view_xls);
  30. },
  31. on_sidebar_export_view_xls: function() {
  32. // Select the first list of the current (form) view
  33. // or assume the main view is a list view and use that
  34. var self = this,
  35. view = this.getParent(),
  36. children = view.getChildren();
  37. if (children) {
  38. children.every(function(child) {
  39. if (child.field && child.field.type == 'one2many') {
  40. view = child.viewmanager.views.list.controller;
  41. return false; // break out of the loop
  42. }
  43. if (child.field && child.field.type == 'many2many') {
  44. view = child.list_view;
  45. return false; // break out of the loop
  46. }
  47. return true;
  48. });
  49. }
  50. export_columns_keys = [];
  51. export_columns_names = [];
  52. $.each(view.visible_columns, function(){
  53. if(this.tag=='field'){
  54. // non-fields like `_group` or buttons
  55. export_columns_keys.push(this.id);
  56. export_columns_names.push(this.string);
  57. }
  58. });
  59. rows = view.$el.find('.oe_list_content > tbody > tr');
  60. export_rows = [];
  61. $.each(rows,function(){
  62. $row = $(this);
  63. // find only rows with data
  64. if($row.attr('data-id')){
  65. export_row = [];
  66. checked = $row.find('th input[type=checkbox]').attr("checked");
  67. if (children && checked === "checked"){
  68. $.each(export_columns_keys,function(){
  69. cell = $row.find('td[data-field="'+this+'"]').get(0);
  70. text = cell.text || cell.textContent || cell.innerHTML || "";
  71. if (cell.classList.contains("oe_list_field_float")){
  72. export_row.push(instance.web.parse_value(text, {'type': "float"}));
  73. }
  74. else if (cell.classList.contains("oe_list_field_boolean")){
  75. var data_id = $( '<div>' + cell.innerHTML + '</div>');
  76. if(data_id.find('input').get(0).checked){
  77. export_row.push(_t("True"));
  78. }
  79. else {
  80. export_row.push(_t("False"));
  81. }
  82. }
  83. else if (cell.classList.contains("oe_list_field_integer")){
  84. export_row.push(parseInt(text));
  85. }
  86. else{
  87. export_row.push(text.trim());
  88. }
  89. });
  90. export_rows.push(export_row);
  91. };
  92. }
  93. });
  94. $.blockUI();
  95. view.session.get_file({
  96. url: '/web/export/xls_view',
  97. data: {data: JSON.stringify({
  98. model : view.model,
  99. headers : export_columns_names,
  100. rows : export_rows,
  101. })},
  102. complete: $.unblockUI
  103. });
  104. },
  105. });
  106. };