diff --git a/attachments/pages/index.html b/attachments/pages/index.html index 111d606c..00e94255 100644 --- a/attachments/pages/index.html +++ b/attachments/pages/index.html @@ -32,7 +32,7 @@
- Loading... + Loading...
@@ -192,7 +192,6 @@ Preview
-
diff --git a/attachments/script/costco.js b/attachments/script/costco.js index 5383f0bd..7c9946da 100644 --- a/attachments/script/costco.js +++ b/attachments/script/costco.js @@ -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("About to add " + docs.length - + " docs to " + getDb().name + ""); - $("#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("About to edit " + edited - // + " docs and delete " + deleted + " docs from " - // + getDb().name + ""); - // 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("error updating docs: " - + err + ""); - } - }); + 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 }; diff --git a/attachments/script/jquery.couch2.js b/attachments/script/jquery.couch2.js index 49683af6..15b14520 100644 --- a/attachments/script/jquery.couch2.js +++ b/attachments/script/jquery.couch2.js @@ -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() { diff --git a/attachments/script/removalist.js b/attachments/script/removalist.js index dfef2c11..dfc3f1b9 100644 --- a/attachments/script/removalist.js +++ b/attachments/script/removalist.js @@ -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'); } diff --git a/attachments/script/site.js b/attachments/script/site.js index 8e58173e..b4ee471e 100644 --- a/attachments/script/site.js +++ b/attachments/script/site.js @@ -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); diff --git a/attachments/script/util.js b/attachments/script/util.js index 04117de5..0c99142d 100644 --- a/attachments/script/util.js +++ b/attachments/script/util.js @@ -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 = '
'; @@ -149,6 +158,7 @@ var util = function() { hide: hide, position: position, render: render, + notify: notify, formatMetadata:formatMetadata, getBaseURL:getBaseURL, resetForm: resetForm,