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.

124 lines
4.5 KiB

  1. // -*- coding: utf-8 -*-
  2. // © 2012 Agile Business Group
  3. // © 2012 Therp BV
  4. // License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. odoo.define('web_export_view.Sidebar', function (require) {
  6. "use strict";
  7. var core = require('web.core');
  8. var formats = require('web.formats');
  9. var Sidebar = require('web.Sidebar');
  10. var _t = core._t;
  11. Sidebar.include({
  12. init: function () {
  13. var self = this;
  14. this._super.apply(this, arguments);
  15. self.sections.push({
  16. name: 'export_current_view',
  17. label: _t('Export Current View')
  18. });
  19. self.items['export_current_view'] = [];
  20. var view = self.getParent();
  21. if (view.fields_view && view.fields_view.type === "tree") {
  22. self.web_export_add_items();
  23. }
  24. },
  25. web_export_add_items: function () {
  26. var self = this;
  27. self.add_items('export_current_view', [{
  28. label: 'Excel',
  29. callback: self.on_sidebar_export_view_xls,
  30. },]);
  31. },
  32. on_sidebar_export_view_xls: function () {
  33. // Select the first list of the current (form) view
  34. // or assume the main view is a list view and use that
  35. var self = this,
  36. view = this.getParent(),
  37. children = view.getChildren();
  38. if (children) {
  39. children.every(function (child) {
  40. if (child.field && child.field.type == 'one2many') {
  41. view = child.viewmanager.views.list.controller;
  42. return false; // break out of the loop
  43. }
  44. if (child.field && child.field.type == 'many2many') {
  45. view = child.list_view;
  46. return false; // break out of the loop
  47. }
  48. return true;
  49. });
  50. }
  51. var export_columns_keys = [];
  52. var export_columns_names = [];
  53. $.each(view.visible_columns, function () {
  54. if (this.tag == 'field') {
  55. // non-fields like `_group` or buttons
  56. export_columns_keys.push(this.id);
  57. export_columns_names.push(this.string);
  58. }
  59. });
  60. var rows = view.$el.find('tbody tr[data-id]');
  61. var export_rows = [];
  62. $.each(rows, function () {
  63. var $row = $(this);
  64. var export_row = [];
  65. var row_selector = '.o_list_record_selector input[type=checkbox],\
  66. .oe_list_record_selector input[type=checkbox]';
  67. var checked = $row.find(row_selector).is(':checked');
  68. if (children && checked === true) {
  69. $.each(export_columns_keys, function () {
  70. var $cell = $row.find('td[data-field="' + this + '"]');
  71. var text = $cell.text();
  72. if ($cell.hasClass("oe_list_field_monetary")) {
  73. // Remove all but digits, minus, dots and commas
  74. text = text.replace(/[^\d\.,-]/g, "");
  75. export_row.push(
  76. formats.parse_value(text, {"type": "monetary"})
  77. );
  78. } else if ($cell.hasClass("oe_list_field_float")) {
  79. export_row.push(
  80. formats.parse_value(text, {'type': "float"})
  81. );
  82. } else if ($cell.hasClass("oe_list_field_boolean")) {
  83. export_row.push(
  84. $cell.is(':has(input:checked)')
  85. ? _t("True") : _t("False")
  86. );
  87. } else if ($cell.hasClass("oe_list_field_integer")) {
  88. var tmp, tmp2 = text;
  89. do {
  90. tmp = tmp2;
  91. tmp2 = tmp.replace(
  92. _t.database.parameters.thousands_sep,
  93. ""
  94. );
  95. } while (tmp !== tmp2);
  96. export_row.push(parseInt(tmp2));
  97. } else {
  98. export_row.push(text.trim());
  99. }
  100. });
  101. export_rows.push(export_row);
  102. }
  103. });
  104. $.blockUI();
  105. view.session.get_file({
  106. url: '/web/export/xls_view',
  107. data: {data: JSON.stringify({
  108. model: view.model,
  109. headers: export_columns_names,
  110. rows: export_rows
  111. })},
  112. complete: $.unblockUI
  113. });
  114. },
  115. });
  116. });