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

267 lines
7.1 KiB
JavaScript
Raw Normal View History

/**
* 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 14:10:41 +01:00
/**
* Copyright 2009 Google Inc.
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01: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 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01: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.
*/
2011-07-07 19:59:34 +02:00
var padimpexp = (function()
{
2011-03-26 14:10:41 +01:00
///// import
var currentImportTimer = null;
2011-07-07 19:59:34 +02:00
function addImportFrames()
{
2011-07-21 21:13:58 +02:00
$("#import .importframe").remove();
var iframe = $('<iframe style="display: none;" name="importiframe" class="importframe"></iframe>');
$('#import').append(iframe);
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
function fileInputUpdated()
{
2014-11-18 19:09:29 +01:00
$('#importsubmitinput').addClass('throbbold');
2011-03-26 14:10:41 +01:00
$('#importformfilediv').addClass('importformenabled');
$('#importsubmitinput').removeAttr('disabled');
2014-11-18 19:09:29 +01:00
$('#importmessagefail').fadeOut('fast');
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
function fileInputSubmit()
{
2011-03-26 14:10:41 +01:00
$('#importmessagefail').fadeOut("fast");
var ret = window.confirm(html10n.get("pad.impexp.confirmimport"));
2011-07-07 19:59:34 +02:00
if (ret)
2011-07-21 21:13:58 +02:00
{
2011-07-07 19:59:34 +02:00
currentImportTimer = window.setTimeout(function()
{
if (!currentImportTimer)
{
2011-03-26 14:10:41 +01:00
return;
}
currentImportTimer = null;
importFailed("Request timed out.");
importDone();
2011-03-26 14:10:41 +01:00
}, 25000); // time out after some number of seconds
2011-07-07 19:59:34 +02:00
$('#importsubmitinput').attr(
{
disabled: true
}).val(html10n.get("pad.impexp.importing"));
2011-07-07 19:59:34 +02:00
window.setTimeout(function()
{
$('#importfileinput').attr(
{
disabled: true
});
}, 0);
2011-03-26 14:10:41 +01:00
$('#importarrow').stop(true, true).hide();
$('#importstatusball').show();
}
return ret;
}
2011-07-07 19:59:34 +02:00
function importFailed(msg)
{
2011-03-26 14:10:41 +01:00
importErrorMessage(msg);
}
2011-07-07 19:59:34 +02:00
function importDone()
{
$('#importsubmitinput').removeAttr('disabled').val(html10n.get("pad.impexp.importbutton"));
2011-07-07 19:59:34 +02:00
window.setTimeout(function()
{
$('#importfileinput').removeAttr('disabled');
}, 0);
2011-03-26 14:10:41 +01:00
$('#importstatusball').hide();
importClearTimeout();
addImportFrames();
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
function importClearTimeout()
{
if (currentImportTimer)
{
2011-03-26 14:10:41 +01:00
window.clearTimeout(currentImportTimer);
currentImportTimer = null;
}
}
2011-07-07 19:59:34 +02:00
function importErrorMessage(status)
2011-07-07 19:59:34 +02:00
{
var msg="";
if(status === "convertFailed"){
msg = html10n.get("pad.impexp.convertFailed");
} else if(status === "uploadFailed"){
msg = html10n.get("pad.impexp.uploadFailed");
2014-12-29 22:05:14 +01:00
} else if(status === "padHasData"){
msg = html10n.get("pad.impexp.padHasData");
}
2011-07-07 19:59:34 +02:00
function showError(fade)
{
$('#importmessagefail').html('<strong style="color: red">'+html10n.get('pad.impexp.importfailed')+':</strong> ' + (msg || html10n.get('pad.impexp.copypaste','')))[(fade ? "fadeIn" : "show")]();
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
if ($('#importexport .importmessage').is(':visible'))
{
$('#importmessagesuccess').fadeOut("fast");
$('#importmessagefail').fadeOut("fast", function()
{
showError(true);
});
}
else
{
2011-03-26 14:10:41 +01:00
showError();
}
}
2011-07-07 19:59:34 +02:00
function importSuccessful(token)
{
$.ajax(
{
2011-03-26 14:10:41 +01:00
type: 'post',
url: '/ep/pad/impexp/import2',
2011-07-07 19:59:34 +02:00
data: {
token: token,
padId: pad.getPadId()
},
2011-03-26 14:10:41 +01:00
success: importApplicationSuccessful,
error: importApplicationFailed,
timeout: 25000
});
addImportFrames();
}
2011-07-07 19:59:34 +02:00
function importApplicationFailed(xhr, textStatus, errorThrown)
{
2011-03-26 14:10:41 +01:00
importErrorMessage("Error during conversion.");
importDone();
}
2011-07-07 19:59:34 +02:00
2011-03-26 14:10:41 +01:00
///// export
2011-07-07 19:59:34 +02:00
function cantExport()
{
2011-03-26 14:10:41 +01:00
var type = $(this);
2011-07-07 19:59:34 +02:00
if (type.hasClass("exporthrefpdf"))
{
2011-03-26 14:10:41 +01:00
type = "PDF";
2011-07-07 19:59:34 +02:00
}
else if (type.hasClass("exporthrefdoc"))
{
2011-03-26 14:10:41 +01:00
type = "Microsoft Word";
2011-07-07 19:59:34 +02:00
}
else if (type.hasClass("exporthrefodt"))
{
2011-03-26 14:10:41 +01:00
type = "OpenDocument";
2011-07-07 19:59:34 +02:00
}
else
{
2011-03-26 14:10:41 +01:00
type = "this file";
}
alert(html10n.get("pad.impexp.exportdisabled", {type:type}));
2011-03-26 14:10:41 +01:00
return false;
}
/////
var pad = undefined;
2011-03-26 14:10:41 +01:00
var self = {
init: function(_pad)
2011-07-07 19:59:34 +02:00
{
pad = _pad;
//get /p/padname
2015-02-11 03:07:20 +01:00
// if /p/ isn't available due to a rewrite we use the clientVars padId
var pad_root_path = new RegExp(/.*\/p\/[^\/]+/).exec(document.location.pathname) || clientVars.padId;
2013-01-22 19:17:15 +01:00
//get http://example.com/p/padname without Params
var pad_root_url = document.location.protocol + '//' + document.location.host + document.location.pathname;
//i10l buttom import
$('#importsubmitinput').val(html10n.get("pad.impexp.importbutton"));
html10n.bind('localized', function() {
$('#importsubmitinput').val(html10n.get("pad.impexp.importbutton"));
})
2011-07-22 15:07:13 +02:00
// build the export links
$("#exporthtmla").attr("href", pad_root_path + "/export/html");
2014-12-29 20:57:58 +01:00
$("#exportetherpada").attr("href", pad_root_path + "/export/etherpad");
$("#exportplaina").attr("href", pad_root_path + "/export/txt");
// activate action to import in the form
$("#importform").attr('action', pad_root_url + "/import");
//hide stuff thats not avaible if abiword is disabled
2011-08-21 21:09:25 +02:00
if(clientVars.abiwordAvailable == "no")
{
$("#exportworda").remove();
$("#exportpdfa").remove();
$("#exportopena").remove();
$("#importmessageabiword").show();
}
2011-08-21 21:09:25 +02:00
else if(clientVars.abiwordAvailable == "withoutPDF")
{
$("#exportpdfa").remove();
$("#exportworda").attr("href", pad_root_path + "/export/doc");
$("#exportopena").attr("href", pad_root_path + "/export/odt");
2011-08-21 21:09:25 +02:00
$("#importexport").css({"height":"142px"});
$("#importexportline").css({"height":"142px"});
}
else
{
$("#exportworda").attr("href", pad_root_path + "/export/doc");
$("#exportpdfa").attr("href", pad_root_path + "/export/pdf");
$("#exportopena").attr("href", pad_root_path + "/export/odt");
}
2011-07-21 21:13:58 +02:00
2011-03-26 14:10:41 +01:00
addImportFrames();
$("#importfileinput").change(fileInputUpdated);
$('#importform').submit(fileInputSubmit);
$('.disabledexport').click(cantExport);
},
2014-12-30 12:12:24 +01:00
handleFrameCall: function(directDatabaseAccess, status)
2011-07-07 19:59:34 +02:00
{
if (status !== "ok")
2011-07-07 19:59:34 +02:00
{
importFailed(status);
2011-03-26 14:10:41 +01:00
}
2014-12-30 12:12:24 +01:00
if(directDatabaseAccess) pad.switchToPad(clientVars.padId);
importDone();
2011-03-26 14:10:41 +01:00
},
2011-07-07 19:59:34 +02:00
disable: function()
{
2011-03-26 14:10:41 +01:00
$("#impexp-disabled-clickcatcher").show();
2011-07-21 21:13:58 +02:00
$("#import").css('opacity', 0.5);
2011-03-26 14:10:41 +01:00
$("#impexp-export").css('opacity', 0.5);
},
2011-07-07 19:59:34 +02:00
enable: function()
{
2011-03-26 14:10:41 +01:00
$("#impexp-disabled-clickcatcher").hide();
2011-07-21 21:13:58 +02:00
$("#import").css('opacity', 1);
2011-03-26 14:10:41 +01:00
$("#impexp-export").css('opacity', 1);
}
};
return self;
}());
exports.padimpexp = padimpexp;