scinote-web/app/assets/javascripts/repositories/index.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-06-19 20:05:37 +08:00
//= require repositories/import/records_importer.js
2018-07-19 23:56:42 +08:00
/*
global animateSpinner repositoryRecordsImporter getParam RepositoryDatatable
*/
(function(global) {
'use strict';
global.pageReload = function() {
animateSpinner();
location.reload();
}
2018-07-19 23:56:42 +08:00
function handleErrorSubmit(XHR) {
var formGroup = $('#form-records-file').find('.form-group');
formGroup.addClass('has-error');
formGroup.find('.help-block').remove();
formGroup.append(
'<span class="help-block">' + XHR.responseJSON.message + '</span>'
);
}
function handleSuccessfulSubmit(data) {
$('#modal-import-records').modal('hide');
$(data.html).appendTo('body').promise().done(function() {
$('#parse-records-modal')
.modal('show')
.on('hidden.bs.modal', function() {
animateSpinner();
location.reload();
});
repositoryRecordsImporter();
});
}
2018-07-19 23:56:42 +08:00
function initParseRecordsModal() {
var form = $('#form-records-file');
var submitBtn = form.find('input[type="submit"]');
submitBtn.on('click', function(event) {
2018-07-19 23:56:42 +08:00
var data = new FormData();
event.preventDefault();
event.stopPropagation();
data.append('file', document.getElementById('file').files[0]);
data.append('team_id', document.getElementById('team_id').value);
$.ajax({
type: 'POST',
url: form.attr('action'),
data: data,
2018-07-19 23:56:42 +08:00
success: handleSuccessfulSubmit,
error: handleErrorSubmit,
processData: false,
2018-07-19 23:56:42 +08:00
contentType: false
});
});
}
2018-07-19 23:56:42 +08:00
function initImportRecordsModal() {
$('#importRecordsButton').off().on('click', function() {
$('#modal-import-records').modal('show');
initParseRecordsModal();
});
}
function loadRepositoryTab() {
2018-07-19 23:56:42 +08:00
var param = getParam('repository');
$('#repository-tabs a').on('click', function(e) {
var pane = $(this);
2018-07-19 23:56:42 +08:00
e.preventDefault();
$.ajax({
2018-07-19 23:56:42 +08:00
url: $(this).attr('data-url'),
type: 'GET',
dataType: 'json',
success: function(data) {
var tabBody = $(pane.context.hash).find('.tab-content-body');
tabBody.html(data.html);
pane.tab('show').promise().done(function(el) {
initImportRecordsModal();
2018-07-19 23:56:42 +08:00
RepositoryDatatable.destroy();
RepositoryDatatable.init(el.attr('data-repo-table'));
});
},
2018-07-19 23:56:42 +08:00
error: function() {
// TODO
}
});
});
// on page load
2018-07-19 23:56:42 +08:00
if (param) {
// load selected tab
2018-07-19 23:56:42 +08:00
$('a[href="#custom_repo_' + param + '"]').click();
} else {
// load first tab content
$('#repository-tabs a:first').click();
}
// clean tab content
2018-07-19 23:56:42 +08:00
$('a[data-toggle="tab"]').on('hide.bs.tab', function() {
$('.tab-content-body').html('');
});
}
2018-07-19 23:56:42 +08:00
$('#create-new-repository').initializeModal('#create-repo-modal');
loadRepositoryTab();
initImportRecordsModal();
}(window));