etherpad-lite/static/js/pad_impexp.js

316 lines
8.2 KiB
JavaScript
Raw Normal View History

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()
{
2011-07-22 15:07:13 +02:00
// build the export links
$("#exporthtmla").attr("href", document.location.pathname + "/export/html");
$("#exportplaina").attr("href", document.location.pathname + "/export/txt");
$("#exportwordlea").attr("href", document.location.pathname + "/export/wordle");
//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();
$("#importexport").css({"height":"95px"});
$("#importexportline").css({"height":"95px"});
$("#import").html("Import is not available");
}
2011-08-21 21:09:25 +02:00
else if(clientVars.abiwordAvailable == "withoutPDF")
{
$("#exportpdfa").remove();
$("#exportworda").attr("href", document.location.pathname + "/export/doc");
$("#exportopena").attr("href", document.location.pathname + "/export/odt");
2011-08-21 21:09:25 +02:00
$("#importexport").css({"height":"142px"});
$("#importexportline").css({"height":"142px"});
$("#importform").get(0).setAttribute('action', document.location.href + "/import");
}
else
{
$("#exportworda").attr("href", document.location.pathname + "/export/doc");
$("#exportpdfa").attr("href", document.location.pathname + "/export/pdf");
$("#exportopena").attr("href", document.location.pathname + "/export/odt");
$("#importform").get(0).setAttribute('action', document.location.pathname + "/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()
{
2011-07-27 14:35:22 +02:00
var padUrl = document.location.href + "/export/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;
}());