diff --git a/js/apps/system/aardvark/frontend/js/collections/arangoDocument.js b/js/apps/system/aardvark/frontend/js/collections/arangoDocument.js index 1bb9f4919e..95ac58ba13 100644 --- a/js/apps/system/aardvark/frontend/js/collections/arangoDocument.js +++ b/js/apps/system/aardvark/frontend/js/collections/arangoDocument.js @@ -50,18 +50,29 @@ window.arangoDocument = Backbone.Collection.extend({ } return returnval; }, - addDocument: function (collectionID) { + addDocument: function (collectionID, key) { + console.log("adding document"); var self = this; - self.createTypeDocument(collectionID); + self.createTypeDocument(collectionID, key); }, - createTypeEdge: function (collectionID, from, to) { - var result = false; + createTypeEdge: function (collectionID, from, to, key) { + var result = false, newEdge; + + if (key) { + newEdge = JSON.stringify({ + _key: key + }); + } + else { + newEdge = JSON.stringify({}); + } + $.ajax({ cache: false, type: "POST", async: false, url: "/_api/edge?collection=" + collectionID + "&from=" + from + "&to=" + to, - data: JSON.stringify({}), + data: newEdge, contentType: "application/json", processData: false, success: function(data) { @@ -73,14 +84,24 @@ window.arangoDocument = Backbone.Collection.extend({ }); return result; }, - createTypeDocument: function (collectionID) { - var result = false; + createTypeDocument: function (collectionID, key) { + var result = false, newDocument; + + if (key) { + newDocument = JSON.stringify({ + _key: key + }); + } + else { + newDocument = JSON.stringify({}); + } + $.ajax({ cache: false, type: "POST", async: false, - url: "/_api/document?collection=" + collectionID, - data: JSON.stringify({}), + url: "/_api/document?collection=" + encodeURIComponent(collectionID), + data: newDocument, contentType: "application/json", processData: false, success: function(data) { diff --git a/js/apps/system/aardvark/frontend/js/templates/tableView.ejs b/js/apps/system/aardvark/frontend/js/templates/tableView.ejs index 0a0c9bbfb8..d1e6d16d9b 100644 --- a/js/apps/system/aardvark/frontend/js/templates/tableView.ejs +++ b/js/apps/system/aardvark/frontend/js/templates/tableView.ejs @@ -15,7 +15,7 @@ var cutByResolution = function (str) { Content - _key + diff --git a/js/apps/system/aardvark/frontend/js/views/documentsView.js b/js/apps/system/aardvark/frontend/js/views/documentsView.js index fecdee5a9c..51bf1a5f9a 100644 --- a/js/apps/system/aardvark/frontend/js/views/documentsView.js +++ b/js/apps/system/aardvark/frontend/js/views/documentsView.js @@ -75,7 +75,7 @@ "click .removeFilterItem" : "removeFilterItem", "click #deleteSelected" : "deleteSelectedDocs", "click #moveSelected" : "moveSelectedDocs", - "click #addDocumentButton" : "addDocument", + "click #addDocumentButton" : "addDocumentModal", "click #documents_first" : "firstDocuments", "click #documents_last" : "lastDocuments", "click #documents_prev" : "prevDocuments", @@ -94,10 +94,10 @@ "click .deleteIndex" : "prepDeleteIndex", "click #confirmDeleteIndexBtn" : "deleteIndex", "click #documentsToolbar ul" : "resetIndexForms", - "click #indexHeader #addIndex" : "toggleNewIndexView", - "click #indexHeader #cancelIndex" : "toggleNewIndexView", - "change #documentSize" : "setPagesize", - "change #docsSort" : "setSorting" + "click #indexHeader #addIndex" : "toggleNewIndexView", + "click #indexHeader #cancelIndex" : "toggleNewIndexView", + "change #documentSize" : "setPagesize", + "change #docsSort" : "setSorting" }, showSpinner: function() { @@ -490,13 +490,11 @@ this.filterId = 0; }, - addDocument: function () { + addDocumentModal: function () { var collid = window.location.hash.split("/")[1], - // second parameter is "true" to disable caching of collection type - doctype = arangoHelper.collectionApiType(collid, true); + // second parameter is "true" to disable caching of collection type + doctype = arangoHelper.collectionApiType(collid, true); if (doctype === 'edge') { - //$('#edgeCreateModal').modal('show'); - //arangoHelper.fixTooltips(".modalTooltips", "left"); var buttons = [], tableContent = []; @@ -534,6 +532,20 @@ ) ); + tableContent.push( + window.modalView.createTextEntry( + 'new-edge-key-attr', + '_key', + undefined, + "the edges unique key(optional attribute, leave empty for autogenerated key", + 'is optional: leave empty for autogenerated key', + false, + [ + {/*optional validation rules for joi*/} + ] + ) + ); + buttons.push( window.modalView.createSuccessButton('Create', this.addEdge.bind(this)) ); @@ -547,22 +559,49 @@ return; } + else { + var buttons = [], tableContent = []; - var result = this.documentStore.createTypeDocument(collid); - //Success - if (result !== false) { - window.location.hash = "collection/" + result; - return; + tableContent.push( + window.modalView.createTextEntry( + 'new-document-key-attr', + '_key', + undefined, + "the documents unique key(optional attribute, leave empty for autogenerated key", + 'is optional: leave empty for autogenerated key', + false, + [ + {/*optional validation rules for joi*/} + ] + ) + ); + + buttons.push( + window.modalView.createSuccessButton('Create', this.addDocument.bind(this)) + ); + + window.modalView.show( + 'modalTable.ejs', + 'Create document', + buttons, + tableContent + ); } - //Error - arangoHelper.arangoError('Creating document failed'); }, addEdge: function () { var collid = window.location.hash.split("/")[1]; var from = $('.modal-body #new-edge-from-attr').last().val(); var to = $('.modal-body #new-edge-to').last().val(); - var result = this.documentStore.createTypeEdge(collid, from, to); + var key = $('.modal-body #new-edge-key-attr').last().val(); + + var result; + if (key !== '' || key !== undefined) { + result = this.documentStore.createTypeEdge(collid, from, to, key); + } + else { + result = this.documentStore.createTypeEdge(collid, from, to); + } if (result !== false) { //$('#edgeCreateModal').modal('hide'); @@ -575,6 +614,26 @@ } }, + addDocument: function() { + var collid = window.location.hash.split("/")[1]; + var key = $('.modal-body #new-document-key-attr').last().val(); + var result; + if (key !== '' || key !== undefined) { + result = this.documentStore.createTypeDocument(collid, key); + } + else { + result = this.documentStore.createTypeDocument(collid); + } + //Success + if (result !== false) { + window.modalView.hide(); + window.location.hash = "collection/" + result; + } + else { + arangoHelper.arangoError('Creating document failed'); + } + }, + moveSelectedDocs: function() { var buttons = [], tableContent = []; var toDelete = this.getSelectedDocs();