Skip to content

Commit d4e625c

Browse files
author
dkuffner
committed
scalatron#1 Allow user to revert code to an older version
scalatron#2 Allow user to enter a label before document will be saved.
1 parent 27572d1 commit d4e625c

File tree

4 files changed

+195
-7
lines changed

4 files changed

+195
-7
lines changed

Scalatron/webui/client/API.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@
123123
enumVersions: function(param) {
124124
var p = this.applyDefault("GET", this.api.Versions, param);
125125
Ext.Ajax.request(p);
126+
},
127+
128+
createVersion: function(param) {
129+
var p = this.applyDefault("POST", this.api.Versions, param);
130+
Ext.Ajax.request(p);
126131
}
127132
});
128133

Scalatron/webui/client/EditorToolbar.js

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
(function () {
23
EditorToolBar = {
34
create:function () {
@@ -108,36 +109,102 @@
108109
handler:function (c) {
109110
disableActions(true);
110111
API.logout();
111-
window.location = "/"
112+
window.location = "/";
112113
}
113114
});
114115

115116
var saveAction = Ext.create('Ext.Action', {
116-
text: "Save",
117-
handler:function (c) {
117+
text: "Save...",
118+
119+
saveHandler: function(label) {
120+
var self = this;
121+
118122
disableActions(true);
119123
var botCode = Editor.getContent();
120124
if (botCode) {
121125
API.updateSourceFiles({
126+
// Create a version of the previouse content - if different.
122127
versionLabel: "before Save",
123128
versionPolicy: "ifDifferent",
129+
// --
130+
124131
jsonData:{ files: [ { filename: "Bot.scala", code: botCode} ] },
125132
success:function () {
126133
disableActions(false);
134+
Events.fireEvent("documentSaved");
127135

128-
Events.fireEvent("documentSaved")
129-
136+
self.saveVersion(label, botCode);
130137
},
138+
131139
failure:errorHandler
132140
});
133141
}
142+
},
143+
144+
saveVersion: function(label, botCode) {
145+
API.createVersion({
146+
jsonData:{
147+
label: label,
148+
files: [
149+
{ filename: "Bot.scala", code: botCode }
150+
]
151+
},
152+
failure: errorHandler
153+
});
154+
},
155+
156+
handler:function (c) {
157+
158+
Ext.MessageBox.prompt('Save...', 'Please enter label name:', function(btn, label) {
159+
if(btn = "ok") {
160+
c.saveHandler(label);
161+
} // else - user canceled operation.
162+
});
163+
164+
165+
}
166+
});
167+
168+
var revertAction = Ext.create('Ext.Action', {
169+
text:"Revert...",
170+
handler:function (c) {
171+
172+
var el = c.getEl();
173+
var xy = el.getXY();
174+
var h = el.getHeight();
175+
var menuPosY = xy[1] + h;
176+
177+
var pageDim = Ext.getBody().getViewSize();
178+
var maxHeight = pageDim.height - menuPosY - 20;
179+
180+
//maxHeight = maxHeight > 600 ? 600 : maxHeight;
181+
182+
var grid = VersionGrid.create(maxHeight);
183+
184+
grid.loadVersions(function (versions) {
185+
var p = Ext.create("Ext.menu.Menu", {
186+
items:[ grid ],
187+
listeners: {
188+
189+
}
190+
});
191+
192+
grid.on({
193+
itemclick:function () {
194+
p.destroy();
195+
}
196+
});
197+
198+
p.showAt(xy[0], menuPosY);
199+
});
134200
}
135201
});
136202

137203
actions.push(saveAction);
138204
actions.push(buildAction);
139205
actions.push(buildAndPubAction);
140206
actions.push(sandbox);
207+
actions.push(revertAction);
141208

142209
var spinner = {
143210
xtype:"panel",
@@ -166,10 +233,10 @@
166233
}
167234
})
168235
]
169-
}
236+
};
170237

171238

172-
return [ saveAction, "-", buildAction, "-", sandbox, "-", buildAndPubAction, "->", spinner, signOut]
239+
return [ saveAction, revertAction, "-", buildAction, sandbox, buildAndPubAction, "->", spinner, signOut]
173240
}
174241
};
175242

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
(function () {
2+
3+
function createStore() {
4+
return Ext.create('Ext.data.Store', {
5+
fields:[ 'date', 'label', 'id', 'url'],
6+
data:{},
7+
// Note: See columns: date is handled as int
8+
sorters:[{ property:'date', direction:'DESC' }]
9+
});
10+
}
11+
12+
VersionGrid = {
13+
create:function (maxHeight) {
14+
return Ext.create("Ext.grid.Panel", {
15+
store:createStore(),
16+
17+
width: "400",
18+
19+
// Has no effect!
20+
// maxHeight: maxHeight,
21+
22+
// Workaround: See afterrender below.
23+
height: maxHeight,
24+
25+
columns:[
26+
{ header:'ID', dataIndex:'id', width:40},
27+
{ header:'Date', dataIndex:'date', width:120, type:'int',
28+
renderer:function (value) {
29+
if(value) {
30+
return Ext.Date.format(new Date(parseInt(value)), "Y-m-d H:i:s");
31+
}
32+
return "";
33+
}
34+
},
35+
{ header:'Label', dataIndex:'label', flex:1,
36+
renderer: function(value) {
37+
// The string might contain html special chars.
38+
// Prevent that rendering get screwed up.
39+
return Ext.util.Format.htmlEncode(value);
40+
}
41+
}
42+
],
43+
44+
listeners:{
45+
itemclick:function (grid, record) {
46+
var data = record.data;
47+
48+
// It can be null in case no versions are available - fake message.
49+
if(data.url) {
50+
Ext.Ajax.request({
51+
url:data.url,
52+
method:"GET",
53+
headers:{
54+
"Accept":'application/json',
55+
"Content-Type":'application/json'
56+
},
57+
success:function (r) {
58+
if (r.responseText) {
59+
var result = Ext.JSON.decode(r.responseText);
60+
61+
// TODO: Why does a version deliver a list of files?
62+
var newCode = result.files[0].code;
63+
Editor.setContent(newCode);
64+
}
65+
},
66+
failure:function (r) {
67+
ErrorConsole.showError(r.responseText);
68+
}
69+
});
70+
}
71+
},
72+
73+
/**
74+
* Hack: Adapt the height after rendering when we know how big the actual content is.
75+
* @param grid this grid
76+
*/
77+
afterrender: function(grid) {
78+
// Must be delayed a bit because child components are might be not rendered.
79+
setTimeout(function() {
80+
var h1 = grid.getEl().down(".x-grid-table").getHeight();
81+
h1 = h1 + grid.getEl().down(".x-grid-header-ct").getHeight() + 3 /* Magic Number */;
82+
83+
var h2 = grid.getHeight();
84+
85+
if(h1 < h2) {
86+
grid.setHeight(h1);
87+
}
88+
}, 10);
89+
}
90+
},
91+
92+
loadVersions:function (fn) {
93+
var store = this.store;
94+
95+
API.enumVersions({
96+
success:function (e) {
97+
if(e.versions.length == 0) {
98+
e.versions.push({
99+
label: "No versions available"
100+
})
101+
}
102+
103+
store.loadRawData(e.versions);
104+
fn(e.versions);
105+
},
106+
failure:function (r) {
107+
ErrorConsole.showError(r.responseText);
108+
}
109+
});
110+
}
111+
})
112+
}
113+
}
114+
115+
})();

Scalatron/webui/webclient.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<script type="text/javascript" src="/client/DebuggerHelper.js"></script>
3535
<script type="text/javascript" src="/client/DebuggerInputGrid.js"></script>
3636
<script type="text/javascript" src="/client/DebuggerOutputGrid.js"></script>
37+
<script type="text/javascript" src="/client/VersionGrid.js"></script>
3738
<script type="text/javascript" src="/client/Debugger.js"></script>
3839
<script type="text/javascript" src="/client/ErrorConsole.js"></script>
3940
<script type="text/javascript" src="/client/Editor.js"></script>

0 commit comments

Comments
 (0)