Skip to content

Commit 1ef684a

Browse files
committed
update mini-excel
1 parent 3c192da commit 1ef684a

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

samples/node/web/vue/mini-excel/app.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const Koa = require('koa');
22

3+
const bodyParser = require('koa-bodyparser');
4+
35
const controller = require('./controller');
46

7+
const rest = require('./rest');
8+
59
const app = new Koa();
610

711
const isProduction = process.env.NODE_ENV === 'production';
@@ -21,11 +25,20 @@ app.use(async (ctx, next) => {
2125
let staticFiles = require('./static-files');
2226
app.use(staticFiles('/static/', __dirname + '/static'));
2327

24-
// redirect to /static/index.html:
2528
app.use(async (ctx, next) => {
26-
ctx.response.redirect('/static/index.html');
29+
if (ctx.request.path === '/') {
30+
ctx.response.redirect('/static/index.html');
31+
} else {
32+
await next();
33+
}
2734
});
2835

36+
// parse request body:
37+
app.use(bodyParser());
38+
39+
// bind .rest() for ctx:
40+
app.use(rest.restify());
41+
2942
// add controllers:
3043
app.use(controller());
3144

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
const APIError = require('../rest').APIError;
3+
4+
const path = require('path');
5+
6+
const fs = require('mz/fs');
7+
8+
const saved_dir = path.normalize(__dirname + path.sep + '..' + path.sep + 'saved-docs');
9+
10+
console.log(`documents will be saved in ${saved_dir}.`);
11+
12+
module.exports = {
13+
'GET /api/sheets/:id': async (ctx, next) => {
14+
var s, fp = path.join(saved_dir, '.' + ctx.params.id);
15+
console.log(`load from file ${fp}...`);
16+
s = await fs.readFile(fp, 'utf8');
17+
ctx.rest(JSON.parse(s));
18+
},
19+
'PUT /api/sheets/:id': async (ctx, next) => {
20+
var
21+
fp = path.join(saved_dir, '.' + ctx.params.id),
22+
title = ctx.request.body.title,
23+
rows = ctx.request.body.rows,
24+
data;
25+
if (!title) {
26+
throw new APIError('invalid_data', 'invalid title');
27+
}
28+
if (!Array.isArray(rows)){
29+
throw new APIError('invalid_data', 'invalid rows');
30+
}
31+
data = {
32+
title: title,
33+
rows: rows
34+
};
35+
await fs.writeFile(fp, JSON.stringify({
36+
title: title,
37+
rows: rows
38+
}), 'utf8');
39+
console.log(`wrote to file ${fp}.`);
40+
ctx.rest({
41+
id: ctx.params.id
42+
});
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# documents will be saved in this dir.

samples/node/web/vue/mini-excel/static/index.html

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<script src="/static/js/jquery.min.js"></script>
1111
<script src="/static/js/bootstrap.min.js"></script>
1212
<script src="/static/js/vue.js"></script>
13+
<script src="https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>
1314
<script src="/static/js/excel.js"></script>
1415
<style>
1516

@@ -45,6 +46,7 @@
4546
</style>
4647
<script>
4748

49+
var ID = 'S-001';
4850
var COLUMNS = 10;
4951

5052
function createHeader() {
@@ -102,6 +104,27 @@
102104
selectedColIndex: 0
103105
},
104106
methods: {
107+
open: function () {
108+
var that = this;
109+
that.$resource('/api/sheets/' + ID).get().then(function (resp) {
110+
resp.json().then(function (result) {
111+
that.title = result.title;
112+
that.rows = result.rows;
113+
});
114+
}, function (resp) {
115+
alert('Failed to load.');
116+
});
117+
},
118+
save: function () {
119+
this.$resource('/api/sheets/' + ID).update({
120+
title: this.title,
121+
rows: this.rows
122+
}).then(function (resp) {
123+
console.log('saved ok.');
124+
}, function (resp) {
125+
alert('failed to save.');
126+
});
127+
},
105128
focus: function (cell) {
106129
this.selectedRowIndex = cell.row;
107130
this.selectedColIndex = cell.col;
@@ -132,6 +155,14 @@
132155
}
133156
};
134157

158+
$('#cmd-open').click(function () {
159+
vm.open();
160+
});
161+
162+
$('#cmd-save').click(function () {
163+
vm.save();
164+
});
165+
135166
$('#cmd-left').click(function () {
136167
setAlign('left');
137168
});
@@ -171,6 +202,7 @@
171202
</div>
172203
<nav id="toolbar" class="collapse navbar-collapse">
173204
<div class="btn-group">
205+
<button id="cmd-open" type="button" class="btn btn-default navbar-btn"><i class="glyphicon glyphicon-folder-open"></i> Open</button>
174206
<button id="cmd-save" type="button" class="btn btn-default navbar-btn"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
175207
<button id="cmd-download" type="button" class="btn btn-default navbar-btn"><i class="glyphicon glyphicon-save"></i> Download</button>
176208
</div>
@@ -198,7 +230,7 @@
198230
</table>
199231
</div>
200232

201-
<footer class="navbar navbar-fixed-bottom" style="height:35px; min-height:35px; overflow:hidden;">
233+
<footer class="navbar navbar-fixed-bottom" style="background-color:#e7e7e7; height:35px; min-height:35px; overflow:hidden;">
202234
<div class="container-fluid">
203235
<nav class="collapse navbar-collapse">
204236
<p class="text-right" style="padding-top:5px">

0 commit comments

Comments
 (0)