visual import and bulk upload docs from an api
This commit is contained in:
parent
227ef010a4
commit
2d07653c09
@ -277,7 +277,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-footer">
|
||||
<button class="okButton button"> Import </button>
|
||||
<button class="okButton button"> Fetch </button>
|
||||
<button class="cancelButton button">Cancel</button>
|
||||
</div>
|
||||
</script>
|
||||
@ -389,12 +389,18 @@
|
||||
</script>
|
||||
|
||||
<script type='text/mustache' class="jsonTreeTemplate">
|
||||
<p class="info">
|
||||
<div class="dialog-header">
|
||||
Please highlight the array of JSON objects to convert to documents.
|
||||
</p>
|
||||
<div id="document-container">
|
||||
<div id="document-editor"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-body">
|
||||
<div id="document-container">
|
||||
<div id="document-editor"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-footer">
|
||||
<button class="okButton button"> Import </button>
|
||||
<button class="cancelButton button">Cancel</button>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type='text/mustache' class="editPreviewTemplate">
|
||||
|
||||
@ -82,7 +82,7 @@ var costco = function() {
|
||||
|
||||
function uploadDocs(docs) {
|
||||
var dfd = $.Deferred();
|
||||
if(!docs.length) dfd.resolve("Failed to update");
|
||||
if(!docs.length) dfd.resolve("Failed: No docs specified");
|
||||
couch.request({url: app.baseURL + "api/_bulk_docs", type: "POST", data: JSON.stringify({docs: docs})})
|
||||
.then(
|
||||
dfd.resolve,
|
||||
|
||||
@ -171,13 +171,13 @@ app.after = {
|
||||
},
|
||||
urlImport: function() {
|
||||
$('.dialog-content .okButton').click(function(e) {
|
||||
var apiURL = $('#url-input').val().trim();
|
||||
app.apiURL = $('#url-input').val().trim();
|
||||
util.notify("Fetching data...", {persist: true, loader: true});
|
||||
$.getJSON(apiURL + "?callback=?").then(
|
||||
$.getJSON(app.apiURL + "?callback=?").then(
|
||||
function(docs) {
|
||||
app.apiDocs = docs;
|
||||
util.notify("Data fetched successfully!");
|
||||
util.render('jsonTree', 'dialog-body');
|
||||
util.renderTree(docs);
|
||||
recline.showDialog('jsonTree');
|
||||
},
|
||||
function (err) {
|
||||
util.hide('dialog');
|
||||
@ -186,6 +186,17 @@ app.after = {
|
||||
);
|
||||
})
|
||||
},
|
||||
jsonTree: function() {
|
||||
util.renderTree(app.apiDocs);
|
||||
$('.dialog-content .okButton').click(function(e) {
|
||||
util.hide('dialog');
|
||||
util.notify("Saving documents...", {persist: true, loader: true});
|
||||
costco.uploadDocs(util.lookupPath(util.selectedTreePath())).then(function(msg) {
|
||||
util.notify("Docs saved successfully!");
|
||||
recline.fetchRows(false, app.offset);
|
||||
});
|
||||
})
|
||||
},
|
||||
pasteImport: function() {
|
||||
$('.dialog-content .okButton').click(function(e) {
|
||||
util.notify("Uploading documents...", {persist: true, loader: true});
|
||||
|
||||
@ -208,9 +208,40 @@ var util = function() {
|
||||
}
|
||||
}
|
||||
|
||||
function lookupPath(path) {
|
||||
var docs = app.apiDocs;
|
||||
try {
|
||||
_.each(path, function(node) {
|
||||
docs = docs[node];
|
||||
})
|
||||
} catch(e) {
|
||||
util.notify("Error selecting documents" + e);
|
||||
docs = [];
|
||||
}
|
||||
return docs;
|
||||
}
|
||||
|
||||
function nodePath(docField) {
|
||||
if (docField.children('.object-key').length > 0) return docField.children('.object-key').text();
|
||||
if (docField.children('.array-key').length > 0) return docField.children('.array-key').text();
|
||||
if (docField.children('.doc-key').length > 0) return docField.children('.doc-key').text();
|
||||
return "";
|
||||
}
|
||||
|
||||
function selectedTreePath() {
|
||||
var nodes = []
|
||||
, parent = $('.chosen');
|
||||
while (parent.length > 0) {
|
||||
nodes.push(nodePath(parent));
|
||||
parent = parent.parents('.doc-field:first');
|
||||
}
|
||||
return _.compact(nodes).reverse();
|
||||
}
|
||||
|
||||
// TODO refactor handlers so that they dont stack up as the tree gets bigger
|
||||
function handleTreeClick(e) {
|
||||
var clicked = $(e.target);
|
||||
if(clicked.hasClass('expand')) return;
|
||||
if (clicked.children('.array').length > 0) {
|
||||
var field = clicked;
|
||||
} else if (clicked.siblings('.array').length > 0) {
|
||||
@ -341,6 +372,8 @@ var util = function() {
|
||||
resetForm: resetForm,
|
||||
delay: delay,
|
||||
persist: persist,
|
||||
lookupPath: lookupPath,
|
||||
selectedTreePath: selectedTreePath,
|
||||
renderTree: renderTree
|
||||
};
|
||||
}();
|
||||
Loading…
x
Reference in New Issue
Block a user