JustPaste.it

$(document).ready(function () {
if (!Gallery || !Gallery.config)
return;
var btn = $('<button style="200px;">Gallery preload</button>');
btn.click(function () {
(new preloader()).init();
});
$('#header').append(btn);
});
var preloader = function () {
this.media = 'mediatypes=image%2Fpng%3Bimage%2Fjpeg%3Bimage%2Fgif%3Bimage%2Fx-xbitmap%3Bimage%2Fbmp%3Bimage%2Ftiff%3Bapplication%2Fx-photoshop%3Bapplication%2Fillustrator%3Bapplication%2Fpostscript&features=&etag';
};
preloader.prototype.init = function () {
//this.startDir = 'Fotos/2010/2010-04';
this.startDir = '';
this.files = {
};
this.paths = {
};
this.currentFile = null;
this.pathScannerActive = false;
this.imageLoadActive = false;
this.imageRequestStart = 0;
// Build Area
this.area = $('<div id="preloadArea" style="height:210px;width:100%;padding:10px;position:absolute;bottom:0px;overflow:scroll"></div>');
$('body').append(this.area);
this.area.html('Starting');
this.pathScannerActive = true;
this.requestFiles();
};
preloader.prototype.requestFiles = function () {
this.area.html('Requesting');
this.dirRequest(this.startDir);
};
preloader.prototype.dirRequest = function (dir) {
var uri = window.location + 'files/list?location=' + dir + '&' + this.media;
var cbSuccess = function (res) {
for (var ix in res.files) {
var f = res.files[ix];
this.files[f.path] = f;
var parts = f.path.split('/');
var path = '';
for (var ix2 = 0; ix2 < parts.length - 1; ix2++) {
if (ix2 > 0)
path += '/'
path += parts[ix2];
}
if (!this.paths[path]) {
this.paths[path] = {
};
}
}
this.printState();
// call next
window.setTimeout(function () {
this.findUnrequestedPath()
}.bind(this), 1);
};
$.ajax({
url: uri,
context: this,
success: cbSuccess
});
};
preloader.prototype.findUnrequestedPath = function () {
for (var ix in this.paths) {
var path = this.paths[ix];
if (!path.requested) {
path.requested = true;
this.dirRequest(ix);
return;
}
}
this.pathScannerActive = false;
this.printState();
this.startImageloader();
};
preloader.prototype.startImageloader = function () {
//alert('startImageloader');
this.imageLoadActive = true;
for (var ix in this.files) {
var img = this.files[ix];
if (!img.requested) {
this.requestImage(img);
return;
}
}
this.imageLoadActive = false;
this.printState();
};
preloader.prototype.requestImage = function (img) {
img.requested = true;
this.currentFile = img;
var cbSuccess = function (res) {
this.lastResult = res;
this.lastDuration = new Date().getTime() - this.imageRequestStart;
this.printState();
//this.startImageloader();
}.bind(this);
var cbComplete = function () {
this.startImageloader();
}.bind(this);
var uri = 'thumbnails?ids=' + img.fileid + '&requesttoken=' + encodeURIComponent($('head').data('requesttoken')) + '&scale=1&square=0';
$.ajax({
url: uri,
context: this,
success: cbSuccess,
complete: cbComplete
});
this.imageRequestStart = new Date().getTime();
this.printState();
};
preloader.prototype.printState = function () {
var out = 'Paths: ';
var countPaths = 0;
var countIncomplete = 0;
for (var ix in this.paths) {
countPaths++;
if (!this.paths[ix].requested)
countIncomplete++;
}
out += countPaths;
out += '. Requested for Subdir: ';
out += countIncomplete;
out += '. Percent: ';
out += 100 - parseInt(countIncomplete / countPaths * 100);
out += '%';
if (this.pathScannerActive)
out += ' (Pathscanner active)';
out += '<br>';
// Files
var countFiles = 0;
var countRequested = 0;
for (var ix in this.files) {
countFiles++;
if (this.files[ix].requested)
countRequested++;
}
out += 'Images: ';
out += countFiles;
out += '. Requested Image: ';
out += countRequested;
out += '. Percent: ';
out += parseInt(countRequested / countFiles * 100);
out += '%';
if (this.imageLoadActive)
out += ' (Imageloader active)';
out += '<br>';
// Current File
if (this.currentFile) {
out += 'Current File: ID:';
out += this.currentFile.fileid;
out += '. Path: ';
out += this.currentFile.path;
out += '<br>';
}
// Last Result

if (this.lastResult) {
out += 'Last Result:';
out += this.lastResult.length;
out += ' bytes. Time: ';
out += this.lastDuration;
out += ' ms. ';
out += '<br>';
}
this.area.html(out);
};