Skip to content

Instantly share code, notes, and snippets.

@level420
Created December 4, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save level420/e1d70de25889609c1f56 to your computer and use it in GitHub Desktop.
Save level420/e1d70de25889609c1f56 to your computer and use it in GitHub Desktop.
Preload image resources of type png and gif into qx.ui.basic.Image instances and add them to the application root offscreen at position -5000, -5000
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2015 Visionet GmbH, http://www.visionet.de
License:
LGPL: http://www.gnu.org/licenses/lgpl.html
EPL: http://www.eclipse.org/org/documents/epl-v10.php
See the LICENSE file in the project's top-level directory for details.
Authors:
* Dietrich Streifert (level420)
************************************************************************ */
/**
* Add a method to preload images
*
*/
qx.Mixin.define("visionet.application.MPreloadImageResources",
{
/*
*****************************************************************************
MEMBERS
*****************************************************************************
*/
members :
{
/**
* Preload image resources of type png and gif into qx.ui.basic.Image instances
* and add them to the application root offscreen at position -5000, -5000
*
* @param pathfragment {String|null} only preload if pathfragment is contained (via indexOf) in the resource name
*/
preloadImageResources : function(pathfragment) {
var resources = qx.$$resources;
if(!resources || typeof resources !== "object") {
this.warn("unable to preload images: resource hash qx.$$resources not initialized or not an object");
return;
}
var root = qx.core.Init.getApplication().getRoot();
if(!root) {
this.error("unable to preload images: qx.core.Init.getApplication().getRoot() not yet available!");
return;
}
var loadedCombined = {};
for (var resname in resources) {
if (resources.hasOwnProperty(resname)) {
if(pathfragment && resname.indexOf(pathfragment) == -1) {
continue;
}
var res = resources[resname];
if(qx.lang.Type.isArray(res) && (res[2] == "png" || res[2] == "gif")) {
// simple images
if(res.length == 4) {
if (qx.core.Environment.get("qx.debug")) {
this.debug("preloading image: " + resname);
}
}
// combined images
else if(res.length == 7) {
var combinedresname = res[4];
if(!combinedresname || loadedCombined[combinedresname]) {
// combined base image already loaded
continue;
}
loadedCombined[combinedresname] = true;
resname = combinedresname;
if (qx.core.Environment.get("qx.debug")) {
this.debug("preloading combined image: " + resname);
}
}
var image = new qx.ui.basic.Image(resname);
root.add(image, {top:-5000, left:-5000});
}
}
}
}
}
});
@level420
Copy link
Author

level420 commented Dec 4, 2015

I have another version of this mixin which uses the not yes implemented getIds method of qx.util.ResourceManager which hopefully gets soon merged from pull request qooxdoo/qooxdoo#163

Then we don't need the internal qx.$$resources anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment