Changeset View
Changeset View
Standalone View
Standalone View
assets/src/bundles/save/index.js
/** | /** | ||||
* Copyright (C) 2018-2021 The Software Heritage developers | * Copyright (C) 2018-2021 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
import {csrfPost, handleFetchError, isGitRepoUrl, htmlAlert, removeUrlFragment} from 'utils/functions'; | import {csrfPost, handleFetchError, isGitRepoUrl, htmlAlert, removeUrlFragment} from 'utils/functions'; | ||||
import {swhSpinnerSrc} from 'utils/constants'; | import {swhSpinnerSrc} from 'utils/constants'; | ||||
import artifactFormRowTemplate from './artifact-form-row.ejs'; | |||||
let saveRequestsTable; | let saveRequestsTable; | ||||
function originSaveRequest( | function originSaveRequest( | ||||
originType, originUrl, extraData, | originType, originUrl, extraData, | ||||
acceptedCallback, pendingCallback, errorCallback | acceptedCallback, pendingCallback, errorCallback | ||||
) { | ) { | ||||
// Actually trigger the origin save request | // Actually trigger the origin save request | ||||
Show All 26 Lines | .catch(response => { | ||||
}); | }); | ||||
}); | }); | ||||
} | } | ||||
export function maybeRequireExtraInputs() { | export function maybeRequireExtraInputs() { | ||||
// Read the actual selected value and depending on the origin type, display some extra | // Read the actual selected value and depending on the origin type, display some extra | ||||
// inputs or hide them. This makes the extra inputs disabled when not displayed. | // inputs or hide them. This makes the extra inputs disabled when not displayed. | ||||
const originType = $('#swh-input-visit-type').val(); | const originType = $('#swh-input-visit-type').val(); | ||||
let cssDisplay; | let display = 'none'; | ||||
let disabled; | let disabled = true; | ||||
if (originType === 'archives') { | if (originType === 'archives') { | ||||
display = 'flex'; | |||||
disabled = false; | disabled = false; | ||||
cssDisplay = 'flex'; | |||||
} else { | |||||
cssDisplay = 'none'; | |||||
disabled = true; | |||||
} | } | ||||
$('#optional-origin-forms').css('display', cssDisplay); | $('.swh-save-origin-archives-form').css('display', display); | ||||
$('#swh-input-artifact-url').prop('disabled', disabled); | if (!disabled) { | ||||
$('#swh-input-artifact-version').prop('disabled', disabled); | // help paragraph must have block display for proper rendering | ||||
$('#swh-save-origin-archives-help').css('display', 'block'); | |||||
} | |||||
$('.swh-save-origin-archives-form .form-control').prop('disabled', disabled); | |||||
if (originType === 'archives' && $('.swh-save-origin-archives-form').length === 1) { | |||||
// insert first artifact row when the archives visit type is selected for the first time | |||||
$('.swh-save-origin-archives-form').last().after( | |||||
artifactFormRowTemplate({deletableRow: false, formId: 0})); | |||||
} | |||||
} | |||||
export function addArtifactFormRow() { | |||||
$('.swh-save-origin-artifact-form').last().after( | |||||
artifactFormRowTemplate({ | |||||
deletableRow: true, | |||||
formId: $('.swh-save-origin-artifact-form').length | |||||
}) | |||||
); | |||||
} | |||||
export function deleteArtifactFormRow(event) { | |||||
$(event.target).closest('.swh-save-origin-artifact-form').remove(); | |||||
} | } | ||||
const userRequestsFilterCheckbox = ` | const userRequestsFilterCheckbox = ` | ||||
<div class="custom-control custom-checkbox swhid-option"> | <div class="custom-control custom-checkbox swhid-option"> | ||||
<input class="custom-control-input" value="option-user-requests-filter" type="checkbox" | <input class="custom-control-input" value="option-user-requests-filter" type="checkbox" | ||||
id="swh-save-requests-user-filter"> | id="swh-save-requests-user-filter"> | ||||
<label class="custom-control-label font-weight-normal" for="swh-save-requests-user-filter"> | <label class="custom-control-label font-weight-normal" for="swh-save-requests-user-filter"> | ||||
show only your own requests | show only your own requests | ||||
▲ Show 20 Lines • Show All 176 Lines • ▼ Show 20 Lines | $('#swh-save-origin-form').submit(event => { | ||||
event.stopPropagation(); | event.stopPropagation(); | ||||
$('.alert').alert('close'); | $('.alert').alert('close'); | ||||
if (event.target.checkValidity()) { | if (event.target.checkValidity()) { | ||||
$(event.target).removeClass('was-validated'); | $(event.target).removeClass('was-validated'); | ||||
let originType = $('#swh-input-visit-type').val(); | let originType = $('#swh-input-visit-type').val(); | ||||
let originUrl = $('#swh-input-origin-url').val(); | let originUrl = $('#swh-input-origin-url').val(); | ||||
// read the extra inputs for the 'archives' type | // read the extra inputs for the 'archives' type | ||||
let extraData = originType !== 'archives' ? {} : { | let extraData = {}; | ||||
'artifact_url': $('#swh-input-artifact-url').val(), | if (originType === 'archives') { | ||||
'artifact_version': $('#swh-input-artifact-version').val() | extraData['archives_data'] = []; | ||||
}; | for (let i = 0; i < $('.swh-save-origin-artifact-form').length; ++i) { | ||||
extraData['archives_data'].push({ | |||||
'artifact_url': $(`#swh-input-artifact-url-${i}`).val(), | |||||
'artifact_version': $(`#swh-input-artifact-version-${i}`).val() | |||||
}); | |||||
} | |||||
} | |||||
originSaveRequest(originType, originUrl, extraData, | originSaveRequest(originType, originUrl, extraData, | ||||
() => $('#swh-origin-save-request-status').html(saveRequestAcceptedAlert), | () => $('#swh-origin-save-request-status').html(saveRequestAcceptedAlert), | ||||
() => $('#swh-origin-save-request-status').html(saveRequestPendingAlert), | () => $('#swh-origin-save-request-status').html(saveRequestPendingAlert), | ||||
(statusCode, errorData) => { | (statusCode, errorData) => { | ||||
$('#swh-origin-save-request-status').css('color', 'red'); | $('#swh-origin-save-request-status').css('color', 'red'); | ||||
if (statusCode === 403) { | if (statusCode === 403) { | ||||
const errorAlert = htmlAlert('danger', `Error: ${errorData['reason']}`); | const errorAlert = htmlAlert('danger', `Error: ${errorData['reason']}`); | ||||
▲ Show 20 Lines • Show All 253 Lines • Show Last 20 Lines |