Skip to content

Commit

Permalink
Number format for CSV output is now adjustable through config.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael committed Jun 20, 2011
1 parent a1629cb commit 1441071
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
10 changes: 5 additions & 5 deletions public/javascripts/views/sheet.js
Expand Up @@ -265,24 +265,24 @@ var Sheet = Backbone.View.extend({
toCSV: function() {
var that = this;
function formatValue(text) {
return /[";\n]/.test(text)
return new RegExp("[\";"+config.csv_separator+"\\n]").test(text)
? "\"" + text.replace(/\"/g, "\"\"") + "\""
: text;
}

var res = "";
// Headers
properties = this.groupedItems.properties().map(function(p) { return formatValue(p.name); }).values();
res += properties.join(';')+"\n";
res += properties.join(config.csv_separator)+"\n";

// Items
this.groupedItems.items().each(function(item) {
var values = that.groupedItems.properties().map(function(p) {
return formatValue(item.get(p.key));
return _.include(p.expectedTypes, "number") ? formatValue((item.get(p.key)+"").replace(".", ","))
: formatValue(item.get(p.key));
}).values();
res += values.join(';')+"\n";
res += values.join(config.csv_separator)+"\n";
});

return res;
},

Expand Down
9 changes: 9 additions & 0 deletions server.js
Expand Up @@ -284,12 +284,21 @@ function findDatasources(req, callback) {
}


function clientConfig() {
return {
"number_format": config.number_format,
"csv_separator": config.csv_separator
};
}


// Routes
// -----------

app.get('/', function(req, res) {
html = fs.readFileSync(__dirname+ '/templates/app.html', 'utf-8');
res.send(html.replace('{{{{seed}}}}', JSON.stringify(seed))
.replace('{{{{config}}}}', JSON.stringify(clientConfig()))
.replace('{{{{session}}}}', JSON.stringify(req.session)));
});

Expand Down
2 changes: 1 addition & 1 deletion templates/app.html
Expand Up @@ -7,6 +7,7 @@
<script>
var seed = {{{{seed}}}};
var session = {{{{session}}}};
var config = {{{{config}}}};
</script>

<script type="text/x-ejs-template" name="header">
Expand Down Expand Up @@ -54,7 +55,6 @@
<div class="user-name">
<%= username %> / <b><%= project_name %></b>
</div>
<!--<input type="text" id="project_name" name="project_name" value="foo"/>-->
</script>

<script type="text/x-ejs-template" name="browser">
Expand Down

0 comments on commit 1441071

Please sign in to comment.