Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

134 řádky
3.5 KiB

  1. wn.provide("wn.tools");
  2. wn.tools.downloadify = function(data, roles, me) {
  3. if(roles && roles.length && !has_common(roles, user_roles)) {
  4. msgprint("Export not allowed. You need " + wn.utils.comma_or(roles)
  5. + " Role to export.");
  6. return;
  7. }
  8. var _get_data = function() { return wn.tools.to_csv(data); };
  9. var flash_disabled = (navigator.mimeTypes["application/x-shockwave-flash"] == undefined);
  10. // save file > abt 200 kb using server call
  11. if((_get_data().length > 200000) || flash_disabled) {
  12. open_url_post("server.py?cmd=webnotes.utils.datautils.send_csv_to_client",
  13. {args: {data: data, filename: me.title}}, true);
  14. } else {
  15. wn.require("lib/js/lib/downloadify/downloadify.min.js");
  16. wn.require("lib/js/lib/downloadify/swfobject.js");
  17. var id = wn.dom.set_unique_id();
  18. var msgobj = msgprint('<p id="'+ id +'"></p>');
  19. Downloadify.create(id ,{
  20. filename: function(){
  21. return me.title + '.csv';
  22. },
  23. data: _get_data,
  24. swf: 'lib/js/lib/downloadify/downloadify.swf',
  25. downloadImage: 'lib/js/lib/downloadify/download.png',
  26. onComplete: function(){
  27. $(msgobj.msg_area).html("<p>Saved</p>")
  28. },
  29. onCancel: function(){ msgobj.hide(); },
  30. onError: function(){ msgobj.hide(); },
  31. width: 100,
  32. height: 30,
  33. transparent: true,
  34. append: false
  35. });
  36. }
  37. };
  38. wn.markdown = function(txt) {
  39. if(!wn.md2html) {
  40. wn.require('lib/js/lib/showdown.js');
  41. wn.md2html = new Showdown.converter();
  42. }
  43. while(txt.substr(0,1)==="\n") {
  44. txt = txt.substr(1);
  45. }
  46. // remove leading tab (if they exist in the first line)
  47. var whitespace_len = 0,
  48. first_line = txt.split("\n")[0];
  49. while([" ", "\n", "\t"].indexOf(first_line.substr(0,1))!== -1) {
  50. whitespace_len++;
  51. first_line = first_line.substr(1);
  52. }
  53. if(whitespace_len && whitespace_len != first_line.length) {
  54. var txt1 = [];
  55. $.each(txt.split("\n"), function(i, t) {
  56. txt1.push(t.substr(whitespace_len));
  57. })
  58. txt = txt1.join("\n");
  59. }
  60. return wn.md2html.makeHtml(txt);
  61. }
  62. wn.tools.to_csv = function(data) {
  63. var res = [];
  64. $.each(data, function(i, row) {
  65. row = $.map(row, function(col) {
  66. return typeof(col)==="string" ? ('"' + col.replace(/"/g, '""') + '"') : col;
  67. });
  68. res.push(row.join(","));
  69. });
  70. return res.join("\n");
  71. };
  72. wn.slickgrid_tools = {
  73. get_view_data: function(columns, dataView, filter) {
  74. var col_row = $.map(columns, function(v) { return v.name; });
  75. var res = [];
  76. var col_map = $.map(columns, function(v) { return v.field; });
  77. for (var i=0, len=dataView.getLength(); i<len; i++) {
  78. var d = dataView.getItem(i);
  79. var row = [];
  80. $.each(col_map, function(i, col) {
  81. var val = d[col];
  82. if(val===null || val===undefined) {
  83. val = "";
  84. }
  85. row.push(val);
  86. });
  87. if(!filter || filter(row, d)) {
  88. res.push(row);
  89. }
  90. }
  91. return [col_row].concat(res);
  92. },
  93. add_property_setter_on_resize: function(grid) {
  94. grid.onColumnsResized.subscribe(function(e, args) {
  95. $.each(grid.getColumns(), function(i, col) {
  96. if(col.docfield && col.previousWidth != col.width &&
  97. !in_list(wn.model.std_fields_list, col.docfield.fieldname) ) {
  98. return wn.call({
  99. method:"webnotes.client.make_width_property_setter",
  100. args: {
  101. doclist: [{
  102. doctype:'Property Setter',
  103. doctype_or_field: 'DocField',
  104. doc_type: col.docfield.parent,
  105. field_name: col.docfield.fieldname,
  106. property: 'width',
  107. value: col.width,
  108. "__islocal": 1
  109. }]
  110. }
  111. });
  112. col.previousWidth = col.width;
  113. col.docfield.width = col.width;
  114. }
  115. });
  116. });
  117. }
  118. };