Skip to content

Commit

Permalink
Added ability to edit individual hash keys for issue joeferner#80
Browse files Browse the repository at this point in the history
  • Loading branch information
tvernon committed Mar 11, 2013
1 parent 606244a commit 10bfd87
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 11 deletions.
44 changes: 39 additions & 5 deletions lib/routes/apiv1.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = function (app) {
app.post('/apiv1/listvalue', postAddListValue);
app.post('/apiv1/editListRow', postEditListRow);
app.post('/apiv1/editZSetRow', postEditZSetRow);
app.post('/apiv1/editHashRow', postEditHashRow);
app.get('/apiv1/keystree/:keyPrefix', getKeysTree);
app.get('/apiv1/keystree', getKeysTree);
app.get('/apiv1/keys/:keyPrefix', getKeys);
Expand Down Expand Up @@ -256,6 +257,14 @@ function postEditZSetRow(req, res, next) {
editZSetRow(key, score, value, oldValue, req, res, next);
}

function postEditHashRow(req, res, next) {
var key = req.body.hashKey;
var field = req.body.hashField;
var value = req.body.hashFieldValue;
console.log("hset",key,field,value);
editHashRow(key, field, value, req, res, next);
}

function addSetValue(key, value, req, res, next) {
return req.redisConnection.sadd(key, value, function (err) {
if (err) {
Expand All @@ -280,8 +289,8 @@ function addListValue(key, value, type, req, res, next) {
console.error('addListValue', err);
return next(err);
}
res.send('ok');
}
return res.send('ok');
};
myutil.decodeHTMLEntities(value, function(decodedString){
value = decodedString;
});
Expand All @@ -291,7 +300,7 @@ function addListValue(key, value, type, req, res, next) {
case 'rpush':
return req.redisConnection.rpush(key, value, callback);
default:
err = "invalid type";
var err = new Error("invalid type");
console.error('addListValue', err);
return next(err);
}
Expand Down Expand Up @@ -330,7 +339,7 @@ function editZSetRow(key, score, value, oldValue, req, res, next) {
return next(err);
}
if (value === "REDISCOMMANDERTOMBSTONE") {
res.send('ok');
return res.send('ok');
} else {
myutil.decodeHTMLEntities(value, function(decodedString){
value = decodedString;
Expand All @@ -339,14 +348,39 @@ function editZSetRow(key, score, value, oldValue, req, res, next) {
console.error('editZSetRow', err);
return next(err);
}
res.send('ok');
return res.send('ok');
});
});
}
});
});
}

function editHashRow(key, field, value, req, res, next) {
myutil.decodeHTMLEntities(field, function(decodedField){
myutil.decodeHTMLEntities(value, function(decodedValue){
if (value === "REDISCOMMANDERTOMBSTONE") {
req.redisConnection.hdel(key, decodedField, function(err, result) {
if (err) {
console.error('editHashRow', err);
return next(err);
}
return res.send('ok');
});
} else {
req.redisConnection.hset(key, decodedField, decodedValue, function (err, result) {
if (err) {
console.error('editHashRow', err);
return next(err);
}
return res.send('ok');
})
}
});
});
}


function postKey(req, res, next) {
var key = req.params.key;
if (req.query.action === 'delete') {
Expand Down
39 changes: 39 additions & 0 deletions web/static/scripts/redisCommander.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,34 @@ function setupAddKeyButton() {
}, 500);
}
}

function setupEditHashButton() {
$('#editHashFieldForm').ajaxForm({
beforeSubmit: function () {
console.log('saving');
$('#editHashFieldButton').button('loading');
},
error: function (err) {
console.log('save error', arguments);
alert("Could not save '" + err.statusText + "'");
saveComplete();
},
success: function () {
console.log('saved', arguments);
$('#editHashFieldButton').button('reset');
saveComplete();
}
});

function saveComplete() {
setTimeout(function () {
refreshTree();
getKeyTree().select_node(0);
$('#editHashRowModal').modal('hide');
}, 500);
}
}

function selectTreeNodeString(data) {
var html = new EJS({ url: '/templates/editString.ejs' }).render(data);
$('#body').html(html);
Expand Down Expand Up @@ -418,6 +446,13 @@ function editZSetRow(key, score, value){
$('#editZSetRowModal').modal('show');
setupEditZSetButton();
}
function editHashRow(key, field, value){
$('#hashKey').val(key);
$('#hashField').val(field);
$('#hashFieldValue').val(value);
$('#editHashRowModal').modal('show');
setupEditHashButton();
}
function removeListElement() {
$('#listValue').val('REDISCOMMANDERTOMBSTONE');
$('#editListRowForm').submit();
Expand All @@ -426,6 +461,10 @@ function removeZSetElement() {
$('#zSetValue').val('REDISCOMMANDERTOMBSTONE');
$('#editZSetRowForm').submit();
}
function removeHashField() {
$('#hashFieldValue').val('REDISCOMMANDERTOMBSTONE');
$('#editHashFieldForm').submit();
}

function deleteBranch(branchPrefix) {
var query = branchPrefix + ':*';
Expand Down
26 changes: 20 additions & 6 deletions web/static/templates/editHash.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@
<div id="itemData">
<table class="table">
<thead>
<th>Key</th>
<th>Field</th>
<th>Value</th>
</thead>
<tbody>
<% for(var key in data) { %>
<tr>
<td><%= key %></td>
<td><%= data[key] %></td>
<% for(var field in data) { %>
<tr>
<td><%= field %></td>
<% console.log(field);%>
<% console.log(data[field]);%>
<td><%= data[field] %></td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.table tbody tr').dblclick(function(event) {
var key = "<%= key %>";
var row = event.currentTarget;
var field = row.children[0].innerHTML.toString();
var value = row.children[1].innerHTML.toString();
editHashRow(key, field, value);
});
});
</script>
1 change: 1 addition & 0 deletions web/views/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
<% include modals/editZSetRowModal %>
<% include modals/editListRowModal %>
<% include modals/addKeyModal %>
<% include modals/editHashRowModal %>
</body>
</html>
22 changes: 22 additions & 0 deletions web/views/modals/editHashRowModal.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div id="editHashRowModal" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Edit Hash Field</h3>
</div>
<div class="modal-body">
<form id="editHashFieldForm" action="/apiv1/editHashRow/" method="POST" class="form-vertical">
<h3>Key</h3>
<input id="hashKey" name="hashKey" readonly/>
<h3>Field</h3>
<input id="hashField" name="hashField" readonly/>
<h3>Value</h3>
<textarea id="hashFieldValue" name="hashFieldValue" style="width: 500px; height: 250px;"></textarea>
<br>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">Close</button>
<button id="removeHashFieldButton" class="btn btn-danger" data-loading-text="<i class='icon-refresh'></i> Removing..." onclick="removeHashField()">Remove Field</button>
<button id="editHashFieldButton" class="btn btn-primary" data-loading-text="<i class='icon-refresh'></i> Saving..." onclick="$('#editHashFieldForm').submit()">Save</button>
</div>
</div>

0 comments on commit 10bfd87

Please sign in to comment.