',
'<%= date %>
',
' ',
'<%= document.title %>
',
''
].join('');
$documentIdsElt = $('#input-sync-import-couchdb-documentid');
var modalElt = document.querySelector('.modal-download-couchdb');
var $documentListElt = $(modalElt.querySelector('.document-list'));
var $selectedDocumentListElt = $(modalElt.querySelector('.selected-document-list'));
var $pleaseWaitElt = $(modalElt.querySelector('.please-wait'));
var $noDocumentElt = $(modalElt.querySelector('.no-document'));
var $moreDocumentsElt = $(modalElt.querySelector('.more-documents'));
var documentMap, lastDocument;
var selectedDocuments, $selectedElts;
function doSelect() {
$selectedElts = $documentListElt.children('.active').clone();
selectedDocuments = [];
var selectedDocumentIds = [];
$selectedElts.each(function() {
var documentId = $(this).data('documentId');
selectedDocumentIds.push(documentId);
selectedDocuments.push(documentMap[documentId]);
});
$documentIdsElt.val(selectedDocumentIds.join(' '));
$selectedDocumentListElt.empty().append($selectedElts);
$(modalElt.querySelectorAll('.action-delete-items')).parent().toggleClass('disabled', selectedDocuments.length === 0);
}
function clear() {
documentMap = {};
lastDocument = undefined;
$documentListElt.empty();
doSelect();
}
clear();
function setMode(mode) {
$(modalElt.querySelectorAll('.list-mode')).toggleClass('hide', mode != 'list');
$(modalElt.querySelectorAll('.delete-mode')).toggleClass('hide', mode != 'delete');
$(modalElt.querySelectorAll('.byid-mode')).toggleClass('hide', mode != 'byid');
}
var updateDocumentList = _.debounce(function() {
$pleaseWaitElt.removeClass('hide');
$noDocumentElt.addClass('hide');
$moreDocumentsElt.addClass('hide');
couchdbHelper.listDocuments($selectTagElt.val(), lastDocument && lastDocument.updated, function(err, result) {
$pleaseWaitElt.addClass('hide');
if(err) {
$moreDocumentsElt.removeClass('hide');
return;
}
if(result.length === 3) {
$moreDocumentsElt.removeClass('hide');
lastDocument = result.pop();
}
var documentListHtml = _.reduce(result, function(result, document) {
documentMap[document._id] = document;
return result + _.template(documentEltTmpl, {
document: document,
date: utils.formatDate(document.updated)
});
}, '');
$documentListElt.append(documentListHtml);
if($documentListElt.children().length === 0) {
$noDocumentElt.removeClass('hide');
}
});
setMode('list');
}, 10, true);
var tagList = utils.retrieveIgnoreError(PROVIDER_COUCHDB + '.tagList') || [];
var $selectTagElt = $('#input-sync-import-couchdb-tag')
.on('change', function() {
clear();
updateDocumentList();
});
function updateTagList() {
$selectTagElt.empty().append(crel('option', {
value: ''
}, 'none'));
_.sortBy(tagList, function(tag) {
return tag.toLowerCase();
}).forEach(function(tag) {
$selectTagElt.append(crel('option', {
value: tag
}, tag));
});
}
updateTagList();
$(modalElt)
.on('show.bs.modal', function() {
clear();
updateDocumentList();
})
.on('click', '.document-list .document', function() {
$(this).toggleClass('active');
doSelect();
})
.on('click', '.more-documents', updateDocumentList)
.on('click', '.action-unselect-all', function() {
$documentListElt.children().removeClass('active');
doSelect();
})
.on('click', '.action-byid-mode', function() {
setMode('byid');
})
.on('click', '.action-add-tag', function() {
alertify.prompt("Enter a tag (case sensitive):", function (e, tag) {
if(!e || !tag) {
return;
}
tagList.push(tag);
tagList = _.chain(tagList)
.sortBy(function(tag) {
return tag.toLowerCase();
})
.unique(true)
.value();
storage[PROVIDER_COUCHDB + '.tagList'] = JSON.stringify(tagList);
updateTagList();
$selectTagElt.val(tag);
}, "Tag");
})
.on('click', '.action-remove-tag', function() {
var tag = $selectTagElt.val();
tag && alertify.confirm('You are removing