fully implementing bulk functional transform
This commit is contained in:
parent
64ef71fdd4
commit
ba9264b212
@ -32,7 +32,7 @@
|
||||
|
||||
<div id="notification-container">
|
||||
<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>
|
||||
|
||||
@ -192,7 +192,6 @@
|
||||
<span>Preview</span>
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -2,58 +2,29 @@
|
||||
|
||||
var costco = function() {
|
||||
|
||||
var toUpdate = [];
|
||||
|
||||
function handleEditorChange(e) {
|
||||
mapDocs(e.target.value);
|
||||
}
|
||||
|
||||
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();
|
||||
mapDocs(app.cache, e.target.value, true);
|
||||
}
|
||||
|
||||
function mapDocs(funcString) {
|
||||
var errors = $('.expression-preview-parsing-status');
|
||||
function mapDocs(docs, funcString, preview) {
|
||||
if(preview) var errors = $('.expression-preview-parsing-status');
|
||||
try {
|
||||
eval("var editFunc = " + funcString);
|
||||
errors.text('No syntax error.');
|
||||
if(preview) errors.text('No syntax error.');
|
||||
} catch(e) {
|
||||
errors.text(e+"");
|
||||
if(preview) errors.text(e+"");
|
||||
return;
|
||||
}
|
||||
|
||||
toUpdate = [];
|
||||
var deleted = 0
|
||||
var toUpdate = []
|
||||
, deleted = 0
|
||||
, edited = 0
|
||||
, failed = 0
|
||||
, count = 0
|
||||
, preview = [];
|
||||
;
|
||||
|
||||
if(preview) var preview = [];
|
||||
|
||||
_.each(app.cache, function(doc) {
|
||||
_.each(docs, function(doc) {
|
||||
try {
|
||||
var updated = editFunc(_.clone(doc));
|
||||
} catch(e) {
|
||||
@ -69,37 +40,21 @@ var costco = function() {
|
||||
toUpdate.push(updated);
|
||||
edited++;
|
||||
}
|
||||
preview.push({before: doc[app.currentColumn], after: updated[app.currentColumn]});
|
||||
count++;
|
||||
if(preview) preview.push({before: doc[app.currentColumn], after: updated[app.currentColumn]});
|
||||
});
|
||||
|
||||
util.render('editPreview', 'editPreview', {rows: preview});
|
||||
//
|
||||
// // 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();
|
||||
if(preview) util.render('editPreview', 'editPreview', {rows: preview});
|
||||
return toUpdate;
|
||||
}
|
||||
|
||||
function updateDocs(callback) {
|
||||
if(!toUpdate.length)
|
||||
return callback();
|
||||
|
||||
getDb().bulkSave({docs: toUpdate}, {
|
||||
success: callback,
|
||||
error: function(req, status, err) {
|
||||
$("#status").html("<span class='error'>error updating docs: "
|
||||
+ err + "</span>");
|
||||
}
|
||||
});
|
||||
function updateDocs(docs, callback) {
|
||||
if(!docs.length)
|
||||
return callback("Failed to update");
|
||||
couch.request({url: app.baseURL + "api/_bulk_docs", type: "POST", data: JSON.stringify({docs: docs})}).then(callback);
|
||||
}
|
||||
|
||||
return {
|
||||
handleEditorChange: handleEditorChange,
|
||||
computeChanges: computeChanges,
|
||||
mapDocs: mapDocs,
|
||||
updateDocs: updateDocs
|
||||
};
|
||||
|
||||
@ -13,19 +13,8 @@
|
||||
};
|
||||
|
||||
couch.request = function(opts) {
|
||||
var key = JSON.stringify(opts);
|
||||
if (cache[key]) {
|
||||
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();
|
||||
}
|
||||
var ajaxOpts = $.extend({}, defaults, opts);
|
||||
return $.ajax(ajaxOpts).promise();
|
||||
}
|
||||
|
||||
couch.clearCache = function() {
|
||||
|
||||
@ -9,9 +9,6 @@ var removalist = function() {
|
||||
if ($(e.target).hasClass('transform')) {
|
||||
util.show('dialog');
|
||||
util.render('bulkEdit', 'dialog-content', {name: app.currentColumn});
|
||||
$('.cancelButton').click(function(e) {
|
||||
util.hide('dialog');
|
||||
})
|
||||
util.hide('menu');
|
||||
}
|
||||
|
||||
|
||||
@ -41,6 +41,22 @@ app.after = {
|
||||
exportActions: removalist.handleMenuClick,
|
||||
columnActions: removalist.handleMenuClick,
|
||||
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');
|
||||
editor.val("function(doc) {\n doc['"+ app.currentColumn+"'] = doc['"+ app.currentColumn+"'];\n return doc;\n}");
|
||||
editor.focus().get(0).setSelectionRange(18, 18);
|
||||
|
||||
@ -59,6 +59,15 @@ var util = function() {
|
||||
}
|
||||
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) {
|
||||
out = '<dl>';
|
||||
@ -149,6 +158,7 @@ var util = function() {
|
||||
hide: hide,
|
||||
position: position,
|
||||
render: render,
|
||||
notify: notify,
|
||||
formatMetadata:formatMetadata,
|
||||
getBaseURL:getBaseURL,
|
||||
resetForm: resetForm,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user