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.
 
 
 
 
 
 

171 regels
5.3 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. wn.provide("wn.report_dump");
  23. $.extend(wn.report_dump, {
  24. data: {},
  25. with_data: function(doctypes, callback) {
  26. var missing = [];
  27. $.each(doctypes, function(i, v) {
  28. if(!wn.report_dump.data[v]) missing.push(v);
  29. })
  30. if(missing.length) {
  31. wn.call({
  32. method: "webnotes.widgets.report_dump.get_data",
  33. args: {doctypes: missing},
  34. callback: function(r) {
  35. // creating map of data from a list
  36. $.each(r.message, function(doctype, doctype_data) {
  37. var data = [];
  38. $.each(doctype_data.data, function(i, d) {
  39. var row = {};
  40. $.each(doctype_data.columns, function(idx, col) {
  41. row[col] = d[idx];
  42. });
  43. row.id = doctype + "-" + i;
  44. data.push(row);
  45. });
  46. wn.report_dump.data[doctype] = data;
  47. });
  48. callback();
  49. }
  50. })
  51. } else {
  52. callback();
  53. }
  54. }
  55. });
  56. wn.provide("wn.views");
  57. wn.views.GridReport = Class.extend({
  58. init: function(opts) {
  59. this.filter_inputs = {};
  60. $.extend(this, opts);
  61. this.wrapper = $("<div style='height: 500px; border: 1px solid #aaa;'>").appendTo(this.parent);
  62. this.id = wn.dom.set_unique_id(this.wrapper.get(0));
  63. if(this.filters) {
  64. this.make_filters();
  65. }
  66. this.make_waiting();
  67. this.import_slickgrid();
  68. var me = this;
  69. this.get_data();
  70. wn.cur_grid_report = this;
  71. },
  72. get_data: function() {
  73. var me = this;
  74. wn.report_dump.with_data(this.doctypes, function() {
  75. // setup filters
  76. $.each(me.filter_inputs, function(i, v) {
  77. var opts = v.get(0).opts;
  78. if (opts.fieldtype == "Select" && inList(me.doctypes, opts.options)) {
  79. $(v).add_options($.map(wn.report_dump.data[opts.options], function(d) {
  80. return d.name;
  81. }));
  82. }
  83. });
  84. me.setup();
  85. me.refresh();
  86. });
  87. },
  88. make_waiting: function() {
  89. $('<div class="well" style="width: 63%; margin: 30px auto;">\
  90. <p style="text-align: center;">Loading Report...</p>\
  91. <div class="progress progress-striped active">\
  92. <div class="bar" style="width: 100%"></div></div>')
  93. .appendTo(this.wrapper);
  94. },
  95. load_filters: function(callback) {
  96. // override
  97. callback();
  98. },
  99. make_filters: function() {
  100. var me = this;
  101. $.each(this.filters, function(i, v) {
  102. v.fieldname = v.fieldname || v.label.replace(/ /g, '_').toLowerCase();
  103. var input = null;
  104. if(v.fieldtype=='Select') {
  105. input = me.appframe.add_select(v.label, ["Select "+v.options]);
  106. } else if(v.fieldtype=='Button') {
  107. input = me.appframe.add_button(v.label);
  108. } else if(v.fieldtype=='Date') {
  109. input = me.appframe.add_date(v.label);
  110. } else if(v.fieldtype=='Label') {
  111. input = me.appframe.add_label(v.label);
  112. }
  113. input && (input.get(0).opts = v);
  114. me.filter_inputs[v.fieldname] = input;
  115. });
  116. },
  117. import_slickgrid: function() {
  118. wn.require('js/lib/slickgrid/slick.grid.css');
  119. wn.require('js/lib/slickgrid/slick-default-theme.css');
  120. wn.require('js/lib/slickgrid/jquery.event.drag.min.js');
  121. wn.require('js/lib/slickgrid/slick.core.js');
  122. wn.require('js/lib/slickgrid/slick.grid.js');
  123. wn.require('js/lib/slickgrid/slick.dataview.js');
  124. wn.dom.set_style('.slick-cell { font-size: 12px; }');
  125. },
  126. refresh: function() {
  127. this.prepare_data();
  128. this.render();
  129. },
  130. render: function() {
  131. // new slick grid
  132. this.grid = new Slick.Grid("#"+this.id, this.dataView, this.columns, this.options);
  133. // bind events
  134. this.dataView.onRowsChanged.subscribe(function (e, args) {
  135. grid.invalidateRows(args.rows);
  136. grid.render();
  137. });
  138. this.dataView.onRowCountChanged.subscribe(function (e, args) {
  139. grid.updateRowCount();
  140. grid.render();
  141. });
  142. },
  143. prepare_data_view: function(items) {
  144. // initialize the model
  145. this.dataView = new Slick.Data.DataView({ inlineFilters: true });
  146. this.dataView.beginUpdate();
  147. this.dataView.setItems(items);
  148. this.dataView.setFilter(this.dataview_filter);
  149. this.dataView.endUpdate();
  150. },
  151. options: {
  152. editable: false,
  153. enableColumnReorder: false
  154. },
  155. dataview_filter: function(item) {
  156. return true;
  157. },
  158. date_formatter: function(row, cell, value, columnDef, dataContext) {
  159. return dateutil.str_to_user(value);
  160. },
  161. currency_formatter: function(row, cell, value, columnDef, dataContext) {
  162. return "<div style='text-align: right;'>" + fmt_money(value) + "</div>";
  163. }
  164. })