etherpad-lite/src/static/js/pad_impexp.ts

188 lines
6.2 KiB
TypeScript
Raw Normal View History

'use strict';
/**
* This code is mostly from the old Etherpad. Please help us to comment this code.
* This helps other people to understand this code better and helps them to improve it.
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
*/
2011-03-26 13:10:41 +00:00
/**
* Copyright 2009 Google Inc.
2011-07-07 18:59:34 +01:00
*
2011-03-26 13:10:41 +00:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-07-07 18:59:34 +01:00
*
2011-03-26 13:10:41 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 18:59:34 +01:00
*
2011-03-26 13:10:41 +00:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import html10n from './vendors/html10n';
2024-07-19 19:22:04 +02:00
import {Pad} from "./pad";
2024-07-19 19:22:04 +02:00
class PadImpExp {
private pad?: Pad;
2011-03-26 13:10:41 +00:00
// /// import
2024-07-19 19:22:04 +02:00
addImportFrames = () => {
2020-11-23 13:24:19 -05:00
$('#import .importframe').remove();
const iframe = $('<iframe>')
2024-07-19 19:22:04 +02:00
.css('display', 'none')
.attr('name', 'importiframe')
.addClass('importframe');
2011-07-21 20:13:58 +01:00
$('#import').append(iframe);
2024-07-19 19:22:04 +02:00
}
fileInputUpdated = () => {
2014-11-18 18:09:29 +00:00
$('#importsubmitinput').addClass('throbbold');
2011-03-26 13:10:41 +00:00
$('#importformfilediv').addClass('importformenabled');
$('#importsubmitinput').prop('disabled', false);
2014-11-18 18:09:29 +00:00
$('#importmessagefail').fadeOut('fast');
2024-07-19 19:22:04 +02:00
}
fileInputSubmit = (e: Event) => {
e.preventDefault();
2020-11-23 13:24:19 -05:00
$('#importmessagefail').fadeOut('fast');
if (!window.confirm(html10n.get('pad.impexp.confirmimport'))) return;
$('#importsubmitinput').attr({disabled: true}).val(html10n.get('pad.impexp.importing'));
window.setTimeout(() => $('#importfileinput').attr({disabled: true}), 0);
$('#importarrow').stop(true, true).hide();
$('#importstatusball').show();
(async () => {
2024-07-19 19:22:04 +02:00
// @ts-ignore
const {code, message, data: {directDatabaseAccess} = {}} = await $.ajax({
url: `${window.location.href.split('?')[0].split('#')[0]}/import`,
method: 'POST',
2024-07-19 19:22:04 +02:00
// FIXME is this correct
// @ts-ignore
data: new FormData(this.fileInputSubmit),
processData: false,
contentType: false,
dataType: 'json',
timeout: 25000,
}).catch((err) => {
if (err.responseJSON) return err.responseJSON;
return {code: 2, message: 'Unknown import error'};
});
if (code !== 0) {
2024-07-19 19:22:04 +02:00
this.importErrorMessage(message);
} else {
$('#import_export').removeClass('popup-show');
if (directDatabaseAccess) window.location.reload();
}
$('#importsubmitinput').prop('disabled', false).val(html10n.get('pad.impexp.importbutton'));
window.setTimeout(() => $('#importfileinput').prop('disabled', false), 0);
$('#importstatusball').hide();
2024-07-19 19:22:04 +02:00
this.addImportFrames();
})();
2024-07-19 19:22:04 +02:00
}
2011-07-07 18:59:34 +01:00
2024-07-19 19:22:04 +02:00
importErrorMessage = (status: string) => {
const known = [
'convertFailed',
'uploadFailed',
'padHasData',
'maxFileSize',
'permission',
];
const msg = html10n.get(`pad.impexp.${known.indexOf(status) !== -1 ? status : 'copypaste'}`);
2024-07-19 19:22:04 +02:00
const showError = (fade?: boolean) => {
const popup = $('#importmessagefail').empty()
2024-07-19 19:22:04 +02:00
.append($('<strong>')
.css('color', 'red')
.text(`${html10n.get('pad.impexp.importfailed')}: `))
.append(document.createTextNode(msg));
popup[(fade ? 'fadeIn' : 'show')]();
};
2011-03-26 13:10:41 +00:00
2020-11-23 13:24:19 -05:00
if ($('#importexport .importmessage').is(':visible')) {
$('#importmessagesuccess').fadeOut('fast');
$('#importmessagefail').fadeOut('fast', () => showError(true));
2020-11-23 13:24:19 -05:00
} else {
2011-03-26 13:10:41 +00:00
showError();
}
2024-07-19 19:22:04 +02:00
}
2011-07-07 18:59:34 +01:00
2020-11-23 13:24:19 -05:00
// /// export
2024-07-19 19:22:04 +02:00
cantExport = () => {
2020-11-23 13:24:19 -05:00
let type = $(this);
if (type.hasClass('exporthrefpdf')) {
2024-07-19 19:22:04 +02:00
// @ts-ignore
2020-11-23 13:24:19 -05:00
type = 'PDF';
} else if (type.hasClass('exporthrefdoc')) {
2024-07-19 19:22:04 +02:00
// @ts-ignore
2020-11-23 13:24:19 -05:00
type = 'Microsoft Word';
} else if (type.hasClass('exporthrefodt')) {
2024-07-19 19:22:04 +02:00
// @ts-ignore
2020-11-23 13:24:19 -05:00
type = 'OpenDocument';
} else {
2024-07-19 19:22:04 +02:00
// @ts-ignore
2020-11-23 13:24:19 -05:00
type = 'this file';
2011-03-26 13:10:41 +00:00
}
2020-11-23 13:24:19 -05:00
alert(html10n.get('pad.impexp.exportdisabled', {type}));
2011-03-26 13:10:41 +00:00
return false;
}
2024-07-19 19:22:04 +02:00
init = (_pad: Pad) => {
this.pad = _pad;
2024-07-19 19:22:04 +02:00
// get /p/padname
// if /p/ isn't available due to a rewrite we use the clientVars padId
const padRootPath = /.*\/p\/[^/]+/.exec(document.location.pathname) || window.clientVars.padId;
2020-11-23 13:24:19 -05:00
2024-07-19 19:22:04 +02:00
// i10l buttom import
$('#importsubmitinput').val(html10n.get('pad.impexp.importbutton'));
html10n.bind('localized', () => {
2020-11-23 13:24:19 -05:00
$('#importsubmitinput').val(html10n.get('pad.impexp.importbutton'));
2024-07-19 19:22:04 +02:00
});
2024-07-19 19:22:04 +02:00
// build the export links
$('#exporthtmla').attr('href', `${padRootPath}/export/html`);
$('#exportetherpada').attr('href', `${padRootPath}/export/etherpad`);
$('#exportplaina').attr('href', `${padRootPath}/export/txt`);
2024-07-19 19:22:04 +02:00
// hide stuff thats not avaible if abiword/soffice is disabled
if (window.clientVars.exportAvailable === 'no') {
$('#exportworda').remove();
$('#exportpdfa').remove();
$('#exportopena').remove();
2020-11-23 13:24:19 -05:00
2024-07-19 19:22:04 +02:00
$('#importmessageabiword').show();
} else if (window.clientVars.exportAvailable === 'withoutPDF') {
$('#exportpdfa').remove();
2020-11-23 13:24:19 -05:00
2024-07-19 19:22:04 +02:00
$('#exportworda').attr('href', `${padRootPath}/export/doc`);
$('#exportopena').attr('href', `${padRootPath}/export/odt`);
2020-11-23 13:24:19 -05:00
2024-07-19 19:22:04 +02:00
$('#importexport').css({height: '142px'});
$('#importexportline').css({height: '142px'});
} else {
$('#exportworda').attr('href', `${padRootPath}/export/doc`);
$('#exportpdfa').attr('href', `${padRootPath}/export/pdf`);
$('#exportopena').attr('href', `${padRootPath}/export/odt`);
}
this.addImportFrames();
$('#importfileinput').on('change', this.fileInputUpdated);
$('#importform').off('submit').on('submit', this.fileInputSubmit);
$('.disabledexport').on('click', this.cantExport);
}
disable= () => {
$('#impexp-disabled-clickcatcher').show();
$('#import').css('opacity', 0.5);
$('#impexp-export').css('opacity', 0.5);
}
enable= () => {
$('#impexp-disabled-clickcatcher').hide();
$('#import').css('opacity', 1);
$('#impexp-export').css('opacity', 1);
}
}
2024-07-19 19:22:04 +02:00
export const padImpExp = new PadImpExp();