etherpad-lite/static/js/pad_impexp.js

328 lines
8.6 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;
var hidePanelCall = 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()
{
2011-03-26 14:10:41 +01:00
$('#importformfilediv').addClass('importformenabled');
$('#importsubmitinput').removeAttr('disabled');
$('#importmessagefail').fadeOut("fast");
$('#importarrow').show();
2011-07-07 19:59:34 +02:00
$('#importarrow').animate(
{
paddingLeft: "0px"
}, 500).animate(
{
paddingLeft: "10px"
}, 150, 'swing').animate(
{
paddingLeft: "0px"
}, 150, 'swing').animate(
{
paddingLeft: "10px"
}, 150, 'swing').animate(
{
paddingLeft: "0px"
}, 150, 'swing').animate(
{
paddingLeft: "10px"
}, 150, 'swing').animate(
{
paddingLeft: "0px"
}, 150, 'swing');
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");
2011-07-07 19:59:34 +02:00
var ret = window.confirm("Importing a file will overwrite the current text of the pad." + " Are you sure you want to proceed?");
if (ret)
2011-07-21 21:13:58 +02:00
{
2011-03-26 14:10:41 +01:00
hidePanelCall = paddocbar.hideLaterIfNoOtherInteraction();
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.");
}, 25000); // time out after some number of seconds
2011-07-07 19:59:34 +02:00
$('#importsubmitinput').attr(
{
disabled: true
}).val("Importing...");
window.setTimeout(function()
{
$('#importfileinput').attr(
{
disabled: true
});
}, 0);
2011-03-26 14:10:41 +01:00
$('#importarrow').stop(true, true).hide();
$('#importstatusball').show();
2011-07-21 21:13:58 +02:00
$("#import .importframe").load(function()
{
importDone();
});
2011-03-26 14:10:41 +01:00
}
return ret;
}
2011-07-07 19:59:34 +02:00
function importFailed(msg)
{
2011-03-26 14:10:41 +01:00
importErrorMessage(msg);
importDone();
addImportFrames();
}
2011-07-07 19:59:34 +02:00
function importDone()
{
2011-03-26 14:10:41 +01:00
$('#importsubmitinput').removeAttr('disabled').val("Import Now");
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();
}
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(msg)
{
function showError(fade)
{
$('#importmessagefail').html('<strong style="color: red">Import failed:</strong> ' + (msg || 'Please try a different file.'))[(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
function importApplicationSuccessful(data, textStatus)
{
if (data.substr(0, 2) == "ok")
{
if ($('#importexport .importmessage').is(':visible'))
{
2011-03-26 14:10:41 +01:00
$('#importexport .importmessage').hide();
}
2011-07-07 19:59:34 +02:00
$('#importmessagesuccess').html('<strong style="color: green">Import successful!</strong>').show();
2011-03-26 14:10:41 +01:00
$('#importformfilediv').hide();
2011-07-07 19:59:34 +02:00
window.setTimeout(function()
{
$('#importmessagesuccess').fadeOut("slow", function()
{
2011-03-26 14:10:41 +01:00
$('#importformfilediv').show();
});
2011-07-07 19:59:34 +02:00
if (hidePanelCall)
{
2011-03-26 14:10:41 +01:00
hidePanelCall();
}
}, 3000);
2011-07-07 19:59:34 +02:00
}
else if (data.substr(0, 4) == "fail")
{
importErrorMessage("Couldn't update pad contents. This can happen if your web browser has \"cookies\" disabled.");
}
else if (data.substr(0, 4) == "msg:")
{
2011-03-26 14:10:41 +01:00
importErrorMessage(data.substr(4));
}
importDone();
}
///// 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";
}
2011-07-07 19:59:34 +02:00
alert("Exporting as " + type + " format is disabled. Please contact your" + " system administrator for details.");
2011-03-26 14:10:41 +01:00
return false;
}
/////
var self = {
2011-07-07 19:59:34 +02:00
init: function()
{
//get /p/padname
var pad_root_path = new RegExp(/.*\/p\/[^\/]+/).exec(document.location.pathname)
//get http://example.com/p/padname
var pad_root_url = document.location.href.replace(document.location.pathname, pad_root_path)
2011-07-22 15:07:13 +02:00
// build the export links
$("#exporthtmla").attr("href", pad_root_path + "/export/html");
$("#exportplaina").attr("href", pad_root_path + "/export/txt");
$("#exportwordlea").attr("href", pad_root_path + "/export/wordle");
$("#exportdokuwikia").attr("href", pad_root_path + "/export/dokuwiki");
//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();
2011-08-25 09:52:19 +02:00
$("#importexport").css({"height":"115px"});
$("#importexportline").css({"height":"115px"});
$("#import").html("Import is not available");
}
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"});
$("#importform").attr('action', pad_root_url + "/import");
2011-08-21 21:09:25 +02:00
}
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");
$("#importform").attr('action', pad_root_path + "/import");
}
2011-07-21 21:13:58 +02:00
2011-07-07 19:59:34 +02:00
$("#impexp-close").click(function()
{
paddocbar.setShownPanel(null);
});
2011-03-26 14:10:41 +01:00
addImportFrames();
$("#importfileinput").change(fileInputUpdated);
$('#importform').submit(fileInputSubmit);
$('.disabledexport').click(cantExport);
},
2011-07-07 19:59:34 +02:00
handleFrameCall: function(callName, argsArray)
{
if (callName == 'importFailed')
{
2011-03-26 14:10:41 +01:00
importFailed(argsArray[0]);
}
2011-07-07 19:59:34 +02:00
else if (callName == 'importSuccessful')
{
2011-03-26 14:10:41 +01:00
importSuccessful(argsArray[0]);
}
},
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);
2011-07-22 15:07:13 +02:00
},
export2Wordle: function()
{
var padUrl = $('#exportwordlea').attr('href').replace(/\/wordle$/, '/txt')
2011-07-22 15:07:13 +02:00
$.get(padUrl, function(data)
{
$('.result').html(data);
$('#text').html(data);
$('#wordlepost').submit();
});
2011-03-26 14:10:41 +01:00
}
};
return self;
}());