fully implementing bulk functional transform

This commit is contained in:
Max Ogden 2011-07-02 23:55:25 -07:00
parent 64ef71fdd4
commit ba9264b212
6 changed files with 47 additions and 81 deletions

View File

@ -32,7 +32,7 @@
<div id="notification-container"> <div id="notification-container">
<div id="notification"> <div id="notification">
<img src="images/small-spinner.gif" class="notification-loader"><span class="notification-message">Loading...</span> <img src="images/small-spinner.gif" class="notification-loader"><span id="notification-message">Loading...</span>
</div> </div>
</div> </div>
@ -192,7 +192,6 @@
<span>Preview</span> <span>Preview</span>
<div id="expression-preview-tabs-preview" class="ui-tabs-panel ui-widget-content ui-corner-bottom"> <div id="expression-preview-tabs-preview" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
<div id="editPreview" class="expression-preview-container" style="width: 652px; "> <div id="editPreview" class="expression-preview-container" style="width: 652px; ">
</div> </div>
</div> </div>
</div> </div>

View File

@ -2,58 +2,29 @@
var costco = function() { var costco = function() {
var toUpdate = [];
function handleEditorChange(e) { function handleEditorChange(e) {
mapDocs(e.target.value); mapDocs(app.cache, e.target.value, true);
}
function computeChanges() {
$("#notification-message").text("Computing changes...");
var text = $("#map-function").val();
var docs;
try {
docs = JSON.parse(text);
}
catch(e) {
try {
docs = JSON.parse("[" + text + "]");
}
catch(e) {
// not JSON, must be an edit function
return mapDocs(text);
}
}
if(!docs.length) docs = [docs];
toUpdate = docs;
$("#notification-message").text("Computing changes...");
$("#status").html("<span class='warning'>About to add " + docs.length
+ " docs to " + getDb().name + "</span>");
$("#update-container").show();
} }
function mapDocs(funcString) { function mapDocs(docs, funcString, preview) {
var errors = $('.expression-preview-parsing-status'); if(preview) var errors = $('.expression-preview-parsing-status');
try { try {
eval("var editFunc = " + funcString); eval("var editFunc = " + funcString);
errors.text('No syntax error.'); if(preview) errors.text('No syntax error.');
} catch(e) { } catch(e) {
errors.text(e+""); if(preview) errors.text(e+"");
return; return;
} }
toUpdate = []; var toUpdate = []
var deleted = 0 , deleted = 0
, edited = 0 , edited = 0
, failed = 0 , failed = 0
, count = 0 ;
, preview = [];
if(preview) var preview = [];
_.each(app.cache, function(doc) { _.each(docs, function(doc) {
try { try {
var updated = editFunc(_.clone(doc)); var updated = editFunc(_.clone(doc));
} catch(e) { } catch(e) {
@ -69,37 +40,21 @@ var costco = function() {
toUpdate.push(updated); toUpdate.push(updated);
edited++; edited++;
} }
preview.push({before: doc[app.currentColumn], after: updated[app.currentColumn]}); if(preview) preview.push({before: doc[app.currentColumn], after: updated[app.currentColumn]});
count++;
}); });
util.render('editPreview', 'editPreview', {rows: preview}); if(preview) util.render('editPreview', 'editPreview', {rows: preview});
// return toUpdate;
// // todo: make template for this
// $("#status").html("<span class='warning'>About to edit " + edited
// + " docs and delete " + deleted + " docs from "
// + getDb().name + "</span>");
// if(failed)
// $("#status").append(". Edit function threw on " + failed + " docs");
// $("#update-container").show();
} }
function updateDocs(callback) { function updateDocs(docs, callback) {
if(!toUpdate.length) if(!docs.length)
return callback(); return callback("Failed to update");
couch.request({url: app.baseURL + "api/_bulk_docs", type: "POST", data: JSON.stringify({docs: docs})}).then(callback);
getDb().bulkSave({docs: toUpdate}, {
success: callback,
error: function(req, status, err) {
$("#status").html("<span class='error'>error updating docs: "
+ err + "</span>");
}
});
} }
return { return {
handleEditorChange: handleEditorChange, handleEditorChange: handleEditorChange,
computeChanges: computeChanges,
mapDocs: mapDocs, mapDocs: mapDocs,
updateDocs: updateDocs updateDocs: updateDocs
}; };

View File

@ -13,19 +13,8 @@
}; };
couch.request = function(opts) { couch.request = function(opts) {
var key = JSON.stringify(opts); var ajaxOpts = $.extend({}, defaults, opts);
if (cache[key]) { return $.ajax(ajaxOpts).promise();
var dfd = $.Deferred();
dfd.resolve(jQuery.extend(true, {}, cache[key]));
return dfd.promise();
} else {
var ajaxOpts = $.extend({}, defaults, opts);
ajaxOpts.dataFilter = function (data) {
cache[key] = JSON.parse(data);
return data;
};
return $.ajax(ajaxOpts).promise();
}
} }
couch.clearCache = function() { couch.clearCache = function() {

View File

@ -9,9 +9,6 @@ var removalist = function() {
if ($(e.target).hasClass('transform')) { if ($(e.target).hasClass('transform')) {
util.show('dialog'); util.show('dialog');
util.render('bulkEdit', 'dialog-content', {name: app.currentColumn}); util.render('bulkEdit', 'dialog-content', {name: app.currentColumn});
$('.cancelButton').click(function(e) {
util.hide('dialog');
})
util.hide('menu'); util.hide('menu');
} }

View File

@ -41,6 +41,22 @@ app.after = {
exportActions: removalist.handleMenuClick, exportActions: removalist.handleMenuClick,
columnActions: removalist.handleMenuClick, columnActions: removalist.handleMenuClick,
bulkEdit: function() { bulkEdit: function() {
$('.cancelButton').click(function(e) {
util.hide('dialog');
})
$('.okButton').click(function(e) {
var funcText = $('.expression-preview-code').val();
util.hide('dialog');
util.notify("Updating documents...", {persist: true, loader: true});
couch.request({url: app.baseURL + "api/json"}).then(function(docs) {
var toUpdate = costco.mapDocs(docs.docs, funcText);
costco.updateDocs(toUpdate, function(msg) {
util.notify(msg.length + " documents updated successfully");
removalist.fetchRows(false, app.offset);
});
});
})
var editor = $('.expression-preview-code'); var editor = $('.expression-preview-code');
editor.val("function(doc) {\n doc['"+ app.currentColumn+"'] = doc['"+ app.currentColumn+"'];\n return doc;\n}"); editor.val("function(doc) {\n doc['"+ app.currentColumn+"'] = doc['"+ app.currentColumn+"'];\n return doc;\n}");
editor.focus().get(0).setSelectionRange(18, 18); editor.focus().get(0).setSelectionRange(18, 18);

View File

@ -59,6 +59,15 @@ var util = function() {
} }
if (template in app.after) app.after[template](); if (template in app.after) app.after[template]();
} }
function notify( message, options ) {
if (!options) var options = {};
$('#notification-container').show();
$('#notification-message').text(message);
if (!options.loader) $('.notification-loader').hide();
if (options.loader) $('.notification-loader').show();
if (!options.persist) setTimeout(function() { $('#notification-container').hide() }, 3000);
}
function formatMetadata(data) { function formatMetadata(data) {
out = '<dl>'; out = '<dl>';
@ -149,6 +158,7 @@ var util = function() {
hide: hide, hide: hide,
position: position, position: position,
render: render, render: render,
notify: notify,
formatMetadata:formatMetadata, formatMetadata:formatMetadata,
getBaseURL:getBaseURL, getBaseURL:getBaseURL,
resetForm: resetForm, resetForm: resetForm,