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.

117 lines
4.5 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(openerp) {
  22. _t = openerp.web._t;
  23. openerp.web.Sidebar = openerp.web.Sidebar.extend({
  24. add_default_sections: function() {
  25. // IMHO sections should be registered objects
  26. // as views and retrieved using a specific registry
  27. // so that we don't have to override this
  28. var self = this,
  29. view = this.widget_parent,
  30. view_manager = view.widget_parent,
  31. action = view_manager.action;
  32. if (this.session.uid === 1) {
  33. this.add_section(_t('Customize'), 'customize');
  34. this.add_items('customize', [{
  35. label: _t("Translate"),
  36. callback: view.on_sidebar_translate,
  37. title: _t("Technical translation")
  38. }]);
  39. }
  40. this.add_section(_t('Other Options'), 'other');
  41. this.add_items('other', [
  42. {
  43. label: _t("Import"),
  44. callback: view.on_sidebar_import
  45. }, {
  46. label: _t("Export"),
  47. callback: view.on_sidebar_export
  48. },
  49. {
  50. label: _t("Export current view"),
  51. callback: this.on_sidebar_export_view
  52. }
  53. ]);
  54. },
  55. on_sidebar_export_view: function() {
  56. // Select the first list of the current (form) view
  57. // or assume the main view is a list view and use that
  58. var self = this,
  59. view = this.widget_parent; // valid for list view
  60. if (view.widget_children) {
  61. view.widget_children.every(function(child) {
  62. if (child.field && (
  63. child.field.type == 'many2many'
  64. || child.field.type == 'one2many')) {
  65. view = child.viewmanager.views.list.controller;
  66. return false; // break out of the loop
  67. }
  68. return true;
  69. });
  70. }
  71. var columns = view.visible_columns;
  72. export_columns_keys = [];
  73. export_columns_names = [];
  74. $.each(columns,function(){
  75. if(this.tag=='field'){
  76. // non-fields like `_group` or buttons
  77. export_columns_keys.push(this.id);
  78. export_columns_names.push(this.string);
  79. }
  80. });
  81. rows = view.$element.find('.ui-widget-content tr');
  82. export_rows = [];
  83. $.each(rows,function(){
  84. $row = $(this);
  85. // find only rows with data
  86. if($row.attr('data-id')){
  87. export_row = [];
  88. $.each(export_columns_keys,function(){
  89. cell = $row.find('td[data-field="'+this+'"]').get(0);
  90. text = cell.text || cell.textContent || cell.innerHTML || "";
  91. export_row.push(text.trim());
  92. });
  93. export_rows.push(export_row);
  94. }
  95. });
  96. $.blockUI();
  97. view.session.get_file({
  98. url: '/web/export/xls_view',
  99. data: {data: JSON.stringify({
  100. model : view.model,
  101. headers : export_columns_names,
  102. rows : export_rows,
  103. })},
  104. complete: $.unblockUI
  105. });
  106. },
  107. });
  108. }