106 lines
No EOL
2.8 KiB
JavaScript
106 lines
No EOL
2.8 KiB
JavaScript
wn.provide("wn.tools");
|
|
|
|
wn.tools.downloadify = function(data, roles, me) {
|
|
if(roles && roles.length && !has_common(roles, user_roles)) {
|
|
msgprint("Export not allowed. You need " + wn.utils.comma_or(roles)
|
|
+ " Role to export.");
|
|
return;
|
|
}
|
|
|
|
var _get_data = function(){
|
|
return wn.tools.to_csv(data);
|
|
};
|
|
|
|
// save file > abt 200 kb using server call
|
|
if(_get_data().length > 200000) {
|
|
open_url_post("server.py?cmd=webnotes.utils.datautils.send_csv_to_client",
|
|
{args: {data: data, filename: me.title}}, true);
|
|
} else {
|
|
wn.require("lib/js/lib/downloadify/downloadify.min.js");
|
|
wn.require("lib/js/lib/downloadify/swfobject.js");
|
|
|
|
var id = wn.dom.set_unique_id();
|
|
var msgobj = msgprint('<p id="'+ id +'">You must have Flash 10 installed to \
|
|
download this file.</p>');
|
|
|
|
Downloadify.create(id ,{
|
|
filename: function(){
|
|
return me.title + '.csv';
|
|
},
|
|
data: _get_data,
|
|
swf: 'lib/js/lib/downloadify/downloadify.swf',
|
|
downloadImage: 'lib/js/lib/downloadify/download.png',
|
|
onComplete: function(){
|
|
msgobj.hide();
|
|
msgprint("Saved.");
|
|
},
|
|
onCancel: function(){ msgobj.hide(); },
|
|
onError: function(){ msgobj.hide(); },
|
|
width: 100,
|
|
height: 30,
|
|
transparent: true,
|
|
append: false
|
|
});
|
|
}
|
|
};
|
|
|
|
wn.tools.to_csv = function(data) {
|
|
var res = [];
|
|
$.each(data, function(i, row) {
|
|
row = $.map(row, function(col) {
|
|
return typeof(col)==="string" ? ('"' + col.replace(/"/g, '""') + '"') : col;
|
|
});
|
|
res.push(row.join(","));
|
|
});
|
|
return res.join("\n");
|
|
};
|
|
|
|
wn.slickgrid_tools = {
|
|
get_view_data: function(columns, dataView, filter) {
|
|
var col_row = $.map(columns, function(v) { return v.name; });
|
|
var res = [];
|
|
var col_map = $.map(columns, function(v) { return v.field; });
|
|
|
|
for (var i=0, len=dataView.getLength(); i<len; i++) {
|
|
var d = dataView.getItem(i);
|
|
var row = [];
|
|
$.each(col_map, function(i, col) {
|
|
var val = d[col];
|
|
if(val===null || val===undefined) {
|
|
val = "";
|
|
}
|
|
row.push(val);
|
|
});
|
|
|
|
if(!filter || filter(row, d)) {
|
|
res.push(row);
|
|
}
|
|
}
|
|
return [col_row].concat(res);
|
|
},
|
|
add_property_setter_on_resize: function(grid) {
|
|
grid.onColumnsResized.subscribe(function(e, args) {
|
|
$.each(grid.getColumns(), function(i, col) {
|
|
if(col.docfield && col.previousWidth != col.width &&
|
|
!in_list(wn.model.std_fields_list, col.docfield.fieldname) ) {
|
|
wn.call({
|
|
method:"webnotes.client.make_width_property_setter",
|
|
args: {
|
|
doclist: [{
|
|
doctype:'Property Setter',
|
|
doctype_or_field: 'DocField',
|
|
doc_type: col.docfield.parent,
|
|
field_name: col.docfield.fieldname,
|
|
property: 'width',
|
|
value: col.width,
|
|
"__islocal": 1
|
|
}]
|
|
}
|
|
});
|
|
col.previousWidth = col.width;
|
|
col.docfield.width = col.width;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}; |