mirror of https://gitee.com/bigwinds/arangodb
278 lines
8.6 KiB
JavaScript
278 lines
8.6 KiB
JavaScript
var documentsView = Backbone.View.extend({
|
|
collectionID: 0,
|
|
currentPage: 1,
|
|
documentsPerPage: 10,
|
|
totalPages: 1,
|
|
|
|
collectionContext : {
|
|
prev: null,
|
|
next: null
|
|
},
|
|
|
|
alreadyClicked: false,
|
|
|
|
el: '#content',
|
|
table: '#documentsTableID',
|
|
|
|
events: {
|
|
"click #collectionPrev" : "prevCollection",
|
|
"click #collectionNext" : "nextCollection",
|
|
"click #confirmCreateEdge" : "addEdge",
|
|
"click #documentsTableID tr" : "clicked",
|
|
"click #deleteDoc" : "remove",
|
|
"click #documents_first" : "firstDocuments",
|
|
"click #documents_last" : "lastDocuments",
|
|
"click #documents_prev" : "prevDocuments",
|
|
"click #documents_next" : "nextDocuments",
|
|
"click #confirmDeleteBtn" : "confirmDelete"
|
|
},
|
|
|
|
buildCollectionLink : function (collection) {
|
|
return "#collection/" + encodeURIComponent(collection.get('name')) + '/documents/1';
|
|
},
|
|
|
|
prevCollection : function () {
|
|
if (this.collectionContext.prev !== null) {
|
|
$('#collectionPrev').parent().removeClass('disabledPag');
|
|
window.location.hash = this.buildCollectionLink(this.collectionContext.prev);
|
|
}
|
|
else {
|
|
$('#collectionPrev').parent().addClass('disabledPag');
|
|
}
|
|
},
|
|
|
|
nextCollection : function () {
|
|
if (this.collectionContext.next !== null) {
|
|
$('#collectionNext').parent().removeClass('disabledPag');
|
|
window.location.hash = this.buildCollectionLink(this.collectionContext.next);
|
|
}
|
|
else {
|
|
$('#collectionNext').parent().addClass('disabledPag');
|
|
}
|
|
},
|
|
addDocument: function () {
|
|
var collid = window.location.hash.split("/")[1];
|
|
var doctype = arangoHelper.collectionApiType(collid);
|
|
|
|
if (doctype === 'edge') {
|
|
$('#edgeCreateModal').modal('show');
|
|
$('.modalTooltips').tooltip({
|
|
placement: "left"
|
|
});
|
|
}
|
|
else {
|
|
var result = window.arangoDocumentStore.createTypeDocument(collid);
|
|
//Success
|
|
if (result !== false) {
|
|
window.location.hash = "collection/"+result;
|
|
}
|
|
//Error
|
|
else {
|
|
alert("something wrong");
|
|
}
|
|
}
|
|
},
|
|
addEdge: function () {
|
|
var collid = window.location.hash.split("/")[1];
|
|
var from = $('#new-document-from').val();
|
|
var to = $('#new-document-to').val();
|
|
var result = window.arangoDocumentStore.createTypeEdge(collid, from, to);
|
|
|
|
if (result !== false) {
|
|
$('#edgeCreateModal').modal('hide');
|
|
window.location.hash = "collection/"+result;
|
|
}
|
|
//Error
|
|
else {
|
|
$('#edgeCreateModal').modal('hide');
|
|
alert("something wrong");
|
|
}
|
|
},
|
|
firstDocuments: function () {
|
|
window.arangoDocumentsStore.getFirstDocuments();
|
|
},
|
|
lastDocuments: function () {
|
|
window.arangoDocumentsStore.getLastDocuments();
|
|
},
|
|
prevDocuments: function () {
|
|
window.arangoDocumentsStore.getPrevDocuments();
|
|
},
|
|
nextDocuments: function () {
|
|
window.arangoDocumentsStore.getNextDocuments();
|
|
},
|
|
remove: function (a) {
|
|
this.target = a.currentTarget;
|
|
var thiselement = a.currentTarget.parentElement;
|
|
this.idelement = $(thiselement).prev().prev();
|
|
this.alreadyClicked = true;
|
|
|
|
$('#docDeleteModal').modal('show');
|
|
|
|
},
|
|
confirmDelete: function () {
|
|
this.reallyDelete();
|
|
},
|
|
reallyDelete: function () {
|
|
var self = this;
|
|
var row = $(self.target).closest("tr").get(0);
|
|
var hash = window.location.hash.split("/");
|
|
var page = hash[3];
|
|
var deleted = false;
|
|
this.docid = $(self.idelement).text();
|
|
|
|
if (this.type === 'document') {
|
|
var result = window.arangoDocumentStore.deleteDocument(this.colid, this.docid);
|
|
if (result === true) {
|
|
//on success
|
|
deleted = true;
|
|
}
|
|
else if (result === false) {
|
|
alert("error");
|
|
}
|
|
}
|
|
else if (this.type === 'edge') {
|
|
var result = window.arangoDocumentStore.deleteEdge(this.colid, this.docid);
|
|
if (result === true) {
|
|
//on success
|
|
deleted = true;
|
|
}
|
|
else if (result === false) {
|
|
alert("error");
|
|
}
|
|
}
|
|
|
|
if (deleted === true) {
|
|
$('#documentsTableID').dataTable().fnDeleteRow($('#documentsTableID').dataTable().fnGetPosition(row));
|
|
$('#documentsTableID').dataTable().fnClearTable();
|
|
window.arangoDocumentsStore.getDocuments(this.colid, page);
|
|
$('#docDeleteModal').modal('hide');
|
|
alert("deleted");
|
|
}
|
|
|
|
},
|
|
clicked: function (a) {
|
|
if (this.alreadyClicked == true) {
|
|
this.alreadyClicked = false;
|
|
return 0;
|
|
}
|
|
var self = a.currentTarget;
|
|
var aPos = $(this.table).dataTable().fnGetPosition(self);
|
|
|
|
var checkData = $(this.table).dataTable().fnGetData(self);
|
|
if (checkData[0] === '') {
|
|
this.addDocument();
|
|
return;
|
|
}
|
|
|
|
var rowContent = $(this.table).dataTable().fnGetData(aPos);
|
|
window.location.hash = "#collection/" + this.colid + "/" + rowContent[0];
|
|
},
|
|
|
|
initTable: function (colid, pageid) {
|
|
this.collectionID = colid;
|
|
this.currentPage = pageid;
|
|
var documentsTable = $('#documentsTableID').dataTable({
|
|
"aaSorting": [[ 1, "asc" ]],
|
|
"bFilter": false,
|
|
"bPaginate":false,
|
|
"bSortable": false,
|
|
"bSort": false,
|
|
"bLengthChange": false,
|
|
"bAutoWidth": false,
|
|
"iDisplayLength": -1,
|
|
"bJQueryUI": false,
|
|
"aoColumns": [
|
|
{ "sClass":"read_only leftCell docleftico", "bSortable": false, "sWidth":"30px"},
|
|
{ "sClass":"read_only arangoTooltip","bSortable": false},
|
|
{ "bSortable": false, "sClass": "cuttedContent rightCell"}
|
|
],
|
|
"oLanguage": { "sEmptyTable": "No documents"}
|
|
});
|
|
},
|
|
clearTable: function() {
|
|
$(this.table).dataTable().fnClearTable();
|
|
},
|
|
drawTable: function() {
|
|
var self = this;
|
|
$.each(window.arangoDocumentsStore.models, function(key, value) {
|
|
$(self.table).dataTable().fnAddData([
|
|
//value.attributes.id,
|
|
value.attributes.key,
|
|
//value.attributes.rev,
|
|
'<pre class=prettify title="'+self.escaped(JSON.stringify(value.attributes.content)) +'">' + self.cutByResolution(JSON.stringify(value.attributes.content)) + '</pre>',
|
|
'<button class="enabled" id="deleteDoc"><img src="/_admin/html/img/icon_delete.png" width="16" height="16"></button>'
|
|
]);
|
|
});
|
|
$(self.table).dataTable().fnAddData([
|
|
'',
|
|
'<a id="plusIconDoc" style="padding-left: 30px">Add document</a>',
|
|
'<img src="/_admin/html/img/plus_icon.png" id="documentAddBtn"></img>'
|
|
]);
|
|
$(".prettify").snippet("javascript", {style: "nedit", menu: false, startText: false, transparent: true, showNum: false});
|
|
$(".prettify").tooltip({
|
|
placement: "top"
|
|
});
|
|
this.totalPages = window.arangoDocumentsStore.totalPages;
|
|
this.currentPage = window.arangoDocumentsStore.currentPage;
|
|
this.documentsCount = window.arangoDocumentsStore.documentsCount;
|
|
|
|
if (this.documentsCount === 0) {
|
|
$('#documentsStatus').html('No documents');
|
|
}
|
|
else {
|
|
$('#documentsStatus').html(
|
|
'Showing Page '+this.currentPage+' of '+this.totalPages+
|
|
', '+this.documentsCount+' entries'
|
|
);
|
|
}
|
|
},
|
|
|
|
template: new EJS({url: '/_admin/html/js/templates/documentsView.ejs'}),
|
|
|
|
render: function() {
|
|
this.collectionContext = window.arangoCollectionsStore.getPosition(this.colid);
|
|
|
|
$(this.el).html(this.template.text);
|
|
this.breadcrumb();
|
|
if (this.collectionContext.prev === null) {
|
|
$('#collectionPrev').parent().addClass('disabledPag');
|
|
}
|
|
if (this.collectionContext.next === null) {
|
|
$('#collectionNext').parent().addClass('disabledPag');
|
|
}
|
|
|
|
return this;
|
|
},
|
|
renderPagination: function (totalPages) {
|
|
var currentPage = JSON.parse(this.pageid);
|
|
$('#testdiv').pagination({
|
|
link: '#collection/' + this.colid + '/documents/{p}',
|
|
count: totalPages,
|
|
current: currentPage
|
|
});
|
|
},
|
|
renderPaginationDummy: function () {
|
|
|
|
},
|
|
breadcrumb: function () {
|
|
var name = window.location.hash.split("/")[1];
|
|
$('#transparentHeader').append(
|
|
'<div class="breadcrumb">'+
|
|
'<a class="activeBread" href="#">Collections</a>'+
|
|
' > '+
|
|
'<a class="disabledBread">'+name+'</a>'+
|
|
'</div>'
|
|
);
|
|
},
|
|
cutByResolution: function (string) {
|
|
if (string.length > 1024) {
|
|
return this.escaped(string.substr(0, 1024)) + '...';
|
|
}
|
|
return this.escaped(string);
|
|
},
|
|
escaped: function (value) {
|
|
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "&");
|
|
}
|
|
|
|
});
|