/* DOKUWIKI:include jquery.filedownload.js */
/** global: DOKU_BASE */
/** global: LANG */
/** global: NS */
/** global: JSINFO */
/** global: opener */
// Siteexport Admin Plugin Script
(function($){
$(function(){
if ( !$('form#siteexport, form#siteexport_site_aggregator, form#siteexport_siteexporter').length ) {
return;
}
if (!window.location.origin) {
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}
var SiteexportAdmin = function() {
};
(function(_){
_.url = DOKU_BASE + 'lib/exe/ajax.php';
_.aggregateForm = $('form#siteexport_site_aggregator, form#siteexport_siteexporter');
_.suspendGenerate = _.aggregateForm.length > 0;
_.allElements = 'form#siteexport :input:not([readonly]):not([disabled]):not([type=submit]):not(button):not(.dummy), form#siteexport_site_aggregator :input:not([type=submit]):not(button), form#siteexport_siteexporter :input:not([type=submit]):not(button)';
_.isManager = $('div#siteexport__manager').length > 0;
_.forbidden_options = [ 'call', 'sectok' ];
_.generate = function() {
if ( _.suspendGenerate || _.isManager ) { return; }
this.resetDataForNewRequest();
_.throbber(true);
$.post( _.url, _.settings('__siteexport_generateurl'), function(data) {
data = data.split("\n");
$('#copyurl').val(data[0]);
$('#wgeturl').val(data[1]);
$('#curlurl').val(data[2]);
}).fail(function(jqXHR){
_.errorLog(jqXHR.responseText);
}).always(function(){
_.throbber(false);
});
};
_.run = function() {
this.resetDataForNewRequest();
if ( _.isManager && !(typeof opener === "undefined") ) {
var settings = $.param(_.cleanSettings()).split('&').join(' ');
if ( settings.length > 0 ) { settings = ' ' + settings; }
var edid = String.prototype.match.call(document.location, new RegExp("&edid=([^&]+)"));
opener.insertTags(edid ? edid[1] : 'wiki__text', '{{siteexportAGGREGATOR' + settings + '}}','','');
window.close();
opener.focus();
return;
}
_.throbber(true);
$.post( _.url, _.settings('__siteexport_getsitelist'), function(data) {
data = data.split("\n");
_.pattern = data.shift();
_.zipURL = data.shift();
_.pageCount = data.length - 1; // starting at 0
_.currentCount = 0;
_.allPages = data;
_.status(_.pages());
_.nextPage();
}).fail(function(jqXHR){
_.errorLog(jqXHR.responseText);
}).always(function(){
_.throbber(false);
});
};
_.aggregatorStatus = null;
_.runAggregator = function() {
this.resetDataForNewRequest();
if ( _.aggregatorStatus == null ) {
_.aggregatorStatus = $('').appendTo("form#siteexport_site_aggregator, form#siteexport_siteexporter");
}
_.status(LANG.plugins.siteexport.loadingpage);
_.aggregatorStatus.removeClass('error').show();
_.aggregateForm.addClass('loading');
var settings = _.settings('__siteexport_aggregate');
var throbber = $('form#siteexport_site_aggregator :input[name=baseID], form#siteexport_site_aggregator :input[type=submit], form#siteexport_siteexporter :input[type=submit]').prop('disabled', true);
$.post( _.url, settings, function(data) {
if ( data.match( new RegExp( 'mpdf error', 'i' ) ) ) {
_.aggregatorStatus.addClass('error');
_.status(data);
} else {
_.downloadFile({
id : 'siteexport_site_aggregator_downloader',
src: window.location.origin + data,
root: 'div.siteexporter',
timeout: function(){
_.aggregatorStatus.hide();
}
});
}
}).fail(function(jqXHR){
_.aggregatorStatus.addClass('error');
_.status(jqXHR.responseText.replace("\n", "
"));
}).always(function(){
throbber.prop('disabled', false);
_.aggregateForm.removeClass('loading');
});
};
_.downloadFile = function(iframeProps) {
_.status(LANG.plugins.siteexport.startdownload);
_.aggregateForm.addClass('download');
if ( $.fileDownload ) {
$.fileDownload(iframeProps.src).done(function(){
_.status(LANG.plugins.siteexport.downloadfinished);
if ( typeof iframeProps.timeout == 'function' ) {
window.setTimeout(iframeProps.timeout, 2000);
}
}).fail(function(){
_.error(LANG.plugins.siteexport.finishedbutdownloadfailed);
}).always(function(){
_.aggregateForm.removeClass('download');
});
return;
}
var frameQuery = "iframe#" + iframeProps.id;
var frame = $(frameQuery);
if ( frame.length == 0 ) {
frame = $('')
.hide()
.appendTo(iframeProps.root)
.prop({
type : 'application/octet-stream',
id : iframeProps.id
});
}
// Downloads do not generate a load event
frame.load(function(){
_.status(LANG.plugins.siteexport.downloadfinished);
// This must only happen when not downloading, meaning we have a PDF file.
// ENSURE THIS IS THE ONLY CASE!
// frame.remove();
if ( $.popupviewer ) {
var clone = frame.clone().css({
border: 'none'
}).show();
frame.remove();
_.aggregateForm.removeClass('download');
var viewer = new $.popupviewer();
viewer.showViewer();
$("div#popupviewer div.content").html(clone);
viewer.resizePopup($(window).width(), $(window).height(), null, clone, false, false);
} else {
// No Popup? Open right here
document.location.href = iframeProps.src;
}
});
frame.attr('src', iframeProps.src);
if ( typeof iframeProps.timeout == 'function' ) {
window.setTimeout(iframeProps.timeout, 2000);
}
$(window).unload(function(){
// last resort or the frame might reload
frame.remove();
});
};
_.addSite = function(site) {
var settings = _.settings('__siteexport_addsite');
settings.push({
name: 'site',
value: site
},{
name: 'pattern',
value: this.pattern
},{
name: 'base',
value: DOKU_BASE
});
_.throbber(true);
$.post( _.url, settings, function(data) {
_.zipURL = data.split("\n").pop();
_.nextPage();
}).fail(function(jqXHR){
_.errorLog(jqXHR.responseText);
_.errorCount++;
}).always(function(){
_.throbber(false);
});
};
_.nextPage = function() {
if (!this.allPages) {
return;
}
var page = this.allPages.shift();
if (!page) {
if (_.zipURL != "" && _.zipURL != 'undefined' && typeof _.zipURL != 'undefined' ) {
_.downloadFile({
id : 'siteexport_downloader',
src: window.location.origin + _.zipURL,
root: '#siteexport'
});
} else {
_.status(LANG.plugins.siteexport.finishedbutdownloadfailed);
_.errorLog(_.zipURL);
}
return;
}
if (!page) {
this.nextPage();
}
_.status('Adding "' + page + '" ' + this.pages(this.currentCount++));
_.addSite(page);
};
_.pages = function() {
return '( '
+ _.currentCount
+ ' / '
+ _.pageCount
+ (_.errorCount && _.errorCount != 0 ? ' / ' + _.errorCount + ''
: '') + ' )';
};
_.status = function(text) {
$('#siteexport__out').html(text).removeClass('error');
};
_.error = function(text) {
$('#siteexport__out').html(text).addClass('error');
};
_.settings = function(call) {
var settings = $(_.allElements).serializeArray();
/* Because serializeArray() ignores unset checkboxes and radio buttons: */
settings = settings.concat(
$('form#siteexport label.sendIfNotSet input[type=checkbox]:not(:checked)').map(function() {
return { "name": this.name, "value": '0' /* false */ }
}).get()
);
if (call) { settings.push({ name: 'call', value: call}); }
if ( $('input#pdfExport:checked').length > 0 ) { settings.push({ name: 'renderer', value: 'siteexport_pdf'}); } // is disabled and would not get pushed
return settings;
};
_.cleanSettings = function(call) {
return _.settings(call).filter(function(element){
if ( element.value == NS || element.value == JSINFO.id || element.value == JSINFO.namespace ) { element.name = null; }
if ( !isNaN(element.value) ) { element.value = parseInt(element.value); }
return element.name && _.forbidden_options.indexOf(element.name) < 0 && (element.value.length > 0 || (!isNaN(element.value) && element.value > 0));
});
};
/**
* Display the loading gif
*/
_.throbberCount = 0;
_.throbber = function(on) {
_.throbberCount += (on?1:-1);
$('#siteexport__throbber').css('visibility', _.throbberCount>0 ? 'visible' : 'hidden');
};
_.resetDataForNewRequest = function() {
this.pageCount = 0;
this.currentCount = 0;
this.errorCount = 0;
this.allPages = [];
this.zipURL = '';
this.pattern = '';
this.status('');
this.resetErrorLog();
};
/**
* Log errors into container
*/
_.errorLog = function(text) {
if (!text) {
return;
}
if (!$('#siteexport__errorlog').length) {
$('#siteexport__out').parent().append($('