Skip to content

Commit

Permalink
app: Fix a bug where Rollback feature does not work.
Browse files Browse the repository at this point in the history
  • Loading branch information
eungjun-yi committed Jun 27, 2012
1 parent 5aeb102 commit 06e1932
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ noop = ->
process.env.uploadDir = uploadDir = __dirname + '/public/attachment'
WIKINAME = 'note'
ROOT_PATH = '/wikis/' + WIKINAME
API_ROOT_PATH = '/apis/' + WIKINAME
API_ROOT_PATH = '/api/' + WIKINAME

app = express()
server = http.createServer app
Expand Down
11 changes: 10 additions & 1 deletion lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ if (isServer) {
return path.normalize(directory + '/' + locale + ext);
};
} else {
var i18n = {};
var i18n = {state: 'uninitialized'};

var read = function(locale) {
$.get('/locales/' + locale + '.js', null, function(data) {
locales[locale] = data;
i18n.state = 'ready';
i18n.emit('ready');
}, 'json');
};
Expand All @@ -118,6 +119,14 @@ if (isServer) {
this.handlers[ev].push(fn);
};

i18n.onReady = function(fn) {
if (i18n.state === 'ready') {
fn();
} else {
i18n.on('ready', fn);
}
}

i18n.emit = function(ev) {
var fn;

Expand Down
74 changes: 44 additions & 30 deletions public/scripts/rollback.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
var rollback_handler = function(e) {
$.post('/api/note/pages/' + this.name, {
id: this.id,
action: 'rollback'
}, function(data) {
var commits = data.commits;
var ids = data.ids;
var name = data.name;
var tbody = '<tbody id="commits">';
for (var i = 0; i < commits.length; i++) {
var date = new Date(commits[i].author.unixtime * 1000)
tbody += '<tr>';
tbody += '<td>' + commits[i].author.name + '</td>';
tbody += '<td>' + date + '</td>';
tbody += '<td>';
tbody += '<input type="radio" name="a" value="' + ids[i] + '"/>';
tbody += '<input type="radio" name="b" value="' + ids[i] + '"/>';
tbody += '</td>';
tbody += '<td>' + commits[i].message + '</td>';
tbody += '<td><a href="#" class="rollback-button" name=' + name + ' id=' + ids[i] + '>Rollback</a></td>';
tbody += '</tr>';
var rollback = {
init: function(wikiName) {
var rollback_handler = function(e) {
$.post('/api/' + wikiName + '/pages/' + this.name, {
id: this.id,
action: 'rollback'
}, function(data) {
var commits = data.commits;
var ids = data.ids;
var name = data.name;
var tbody = '<tbody id="commits">';
var urlToPages = '/wikis/' + wikiName + '/pages';
for (var i = 0; i < commits.length; i++) {
var date = new Date(commits[i].author.unixtime * 1000)
tbody += '<tr>';
tbody += '<td>' + commits[i].author.name + '</td>';
tbody += '<td>' + date + '</td>';
tbody += '<td>';
tbody += '<input type="radio" name="a" value="' + ids[i] + '"/>';
tbody += '<input type="radio" name="b" value="' + ids[i] + '"/>';
tbody += '</td>';
tbody += '<td>' + commits[i].message + '</td>';
tbody += '<td><a href="' + urlToPages + '/' + this.name + '">';
tbody += i18n.__('Show') + '</a></td>';
tbody += '<td><a';
tbody += ' href="#"';
tbody += ' class="rollback-button"';
tbody += ' name=' + name + ';
tbody += ' id=' + ids[i] + '>';
tbody += i18n.__('Rollback') + '</a></td>';
tbody += '</tr>';
}
tbody += '</tbody>';
$('#commits').replaceWith(tbody);
$('.rollback-button').unbind('click');
$('.rollback-button').click(rollback_handler);
}, 'json');
}
tbody += '</tbody>';
$('#commits').replaceWith(tbody);
$('.rollback-button').unbind('click');
$('.rollback-button').click(rollback_handler);
}, 'json');
}

window.addEventListener('load', function init() {
$('.rollback-button').click(rollback_handler);
});
$(function() {
i18n.onReady(function() {
$('.rollback-button').click(rollback_handler);
});
});
}
}
8 changes: 6 additions & 2 deletions views/history.jade
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends layout

block content
urlToPage = joinPath('wikis', wikiName, 'pages', title)
urlToPage = joinPath('/wikis', wikiName, 'pages', title)

h1= __('History')
form(method="get", action=urlToPage)
Expand All @@ -11,7 +11,8 @@ block content
td= __('Author')
td= __('Date')
td
input(type="submit", value=__('Diff'), name="action")
input(type="hidden", value='diff', name="action")
input(type="submit", value=__('Diff'))
td= __('Message')
tbody(id="commits")
- var first = commits.ids[0];
Expand Down Expand Up @@ -49,4 +50,7 @@ block content
input(type="submit", value=__('Delete'), name="submit")

script(type="text/javascript", src="/scripts/jquery.js")
script(type="text/javascript", src="/scripts/i18n.js")
script(type="text/javascript", src="/scripts/rollback.js")
script(type="text/javascript")
rollback.init('#{wikiName}');

0 comments on commit 06e1932

Please sign in to comment.