visual import and bulk upload docs from an api
This commit is contained in:
@@ -277,7 +277,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="dialog-footer">
|
<div class="dialog-footer">
|
||||||
<button class="okButton button"> Import </button>
|
<button class="okButton button"> Fetch </button>
|
||||||
<button class="cancelButton button">Cancel</button>
|
<button class="cancelButton button">Cancel</button>
|
||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
@@ -389,12 +389,18 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type='text/mustache' class="jsonTreeTemplate">
|
<script type='text/mustache' class="jsonTreeTemplate">
|
||||||
<p class="info">
|
<div class="dialog-header">
|
||||||
Please highlight the array of JSON objects to convert to documents.
|
Please highlight the array of JSON objects to convert to documents.
|
||||||
</p>
|
</div>
|
||||||
<div id="document-container">
|
<div class="dialog-body">
|
||||||
<div id="document-editor"></div>
|
<div id="document-container">
|
||||||
</div>
|
<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>
|
||||||
|
|
||||||
<script type='text/mustache' class="editPreviewTemplate">
|
<script type='text/mustache' class="editPreviewTemplate">
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ var costco = function() {
|
|||||||
|
|
||||||
function uploadDocs(docs) {
|
function uploadDocs(docs) {
|
||||||
var dfd = $.Deferred();
|
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})})
|
couch.request({url: app.baseURL + "api/_bulk_docs", type: "POST", data: JSON.stringify({docs: docs})})
|
||||||
.then(
|
.then(
|
||||||
dfd.resolve,
|
dfd.resolve,
|
||||||
|
|||||||
@@ -171,13 +171,13 @@ app.after = {
|
|||||||
},
|
},
|
||||||
urlImport: function() {
|
urlImport: function() {
|
||||||
$('.dialog-content .okButton').click(function(e) {
|
$('.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});
|
util.notify("Fetching data...", {persist: true, loader: true});
|
||||||
$.getJSON(apiURL + "?callback=?").then(
|
$.getJSON(app.apiURL + "?callback=?").then(
|
||||||
function(docs) {
|
function(docs) {
|
||||||
|
app.apiDocs = docs;
|
||||||
util.notify("Data fetched successfully!");
|
util.notify("Data fetched successfully!");
|
||||||
util.render('jsonTree', 'dialog-body');
|
recline.showDialog('jsonTree');
|
||||||
util.renderTree(docs);
|
|
||||||
},
|
},
|
||||||
function (err) {
|
function (err) {
|
||||||
util.hide('dialog');
|
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() {
|
pasteImport: function() {
|
||||||
$('.dialog-content .okButton').click(function(e) {
|
$('.dialog-content .okButton').click(function(e) {
|
||||||
util.notify("Uploading documents...", {persist: true, loader: true});
|
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
|
// TODO refactor handlers so that they dont stack up as the tree gets bigger
|
||||||
function handleTreeClick(e) {
|
function handleTreeClick(e) {
|
||||||
var clicked = $(e.target);
|
var clicked = $(e.target);
|
||||||
|
if(clicked.hasClass('expand')) return;
|
||||||
if (clicked.children('.array').length > 0) {
|
if (clicked.children('.array').length > 0) {
|
||||||
var field = clicked;
|
var field = clicked;
|
||||||
} else if (clicked.siblings('.array').length > 0) {
|
} else if (clicked.siblings('.array').length > 0) {
|
||||||
@@ -341,6 +372,8 @@ var util = function() {
|
|||||||
resetForm: resetForm,
|
resetForm: resetForm,
|
||||||
delay: delay,
|
delay: delay,
|
||||||
persist: persist,
|
persist: persist,
|
||||||
|
lookupPath: lookupPath,
|
||||||
|
selectedTreePath: selectedTreePath,
|
||||||
renderTree: renderTree
|
renderTree: renderTree
|
||||||
};
|
};
|
||||||
}();
|
}();
|
||||||
Reference in New Issue
Block a user