Changeset 514

add CSS.preloadImages

Feb 08 2008 * 14:27 (11 months ago)
Committed by seb

Affected files:

trunk/src/util/css.js (Unified diff)

r500r514
1212 // Code based on:
1313 // - IE5.5+ PNG Alpha Fix v1.0RC4 (c) 2004-2005 Angus Turnbull http://www.twinhelix.com
1414 // - Whatever:hover - V2.02.060206 - hover, active & focus (c) 2005 - Peter Nederlof * Peterned - http://www.xs4all.nl/~peterned/
15 function fixPNG() {
16 var patterns = $A(arguments);
15 function fixPNG() {
16 parseStylesheet.apply(this, $A(arguments).concat(fixRule));
17 };
18
19 function parseStylesheet() {
20 var patterns = $A(arguments);
21 var method = patterns.pop();
22
1723 // To avoid flicking background
18 document.execCommand("BackgroundImageCache", false, true);
24 //document.execCommand("BackgroundImageCache", false, true);
1925 // Parse all document stylesheets
2026 var styleSheets = $A(document.styleSheets);
21 if (patterns.length) {
22 styleSheets = styleSheets.select(function(css) {
23 return patterns.any(function(pattern) { return css.href.match(pattern) });
27 if (patterns.length > 1) {
28 styleSheets = styleSheets.select(function(css) {
29 return patterns.any(function(pattern) {
30 return css.href && css.href.match(pattern)
31 });
2432 });
25 }
26 styleSheets.each(fixStylesheet);
33 }
34 styleSheets.each(function(styleSheet) {fixStylesheet.call(this, styleSheet, method)});
2735 };
28
36
2937 // Fixes a stylesheet
30 function fixStylesheet(stylesheet) {
38 function fixStylesheet(stylesheet, method) {
3139 // Parse import files
3240 if (stylesheet.imports)
3341 $A(stylesheet.imports).each(fixStylesheet);
34
35 var docPath = stylesheet.href.substr(0, stylesheet.href.lastIndexOf('/'));
42
43 var href = stylesheet.href || document.location.href;
44 var docPath = href.substr(0, href.lastIndexOf('/'));
3645 // Parse all CSS Rules
37 $A(stylesheet.rules).each(function(rule) { fixRule(rule, docPath) });
46 $A(stylesheet.rules || stylesheet.cssRules).each(function(rule) { method.call(this, rule, docPath) });
3847 };
39
48
4049 var filterPattern = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{src}",sizingMethod="#{method}")';
41
50
4251 // Fixes a rule if it has a PNG background
43 function fixRule(rule, docPath) {
44 var bgImg = rule.style.backgroundImage;
52 function fixRule(rule, docPath) {
53 var bgImg = rule.style.backgroundImage;
4554 // Rule with PNG background image
46 if (bgImg && bgImg != 'none' && bgImg.match(/^url[("']+(.*\.png)[)"']+$/i)) {
55 if (bgImg && bgImg != 'none' && bgImg.match(/^url[("']+(.*\.png)[)"']+$/i)) {
4756 var src = RegExp.$1;
4857 var bgRepeat = rule.style.backgroundRepeat;
4958 // Relative path
5059 if (src[0] != '/')
5160 src = docPath + "/" + src;
5261 // Apply filter
53 rule.style.filter = filterPattern.interpolate({
54 src: src,
62 rule.style.filter = filterPattern.interpolate({
63 src: src,
5564 method: bgRepeat == "no-repeat" ? "crop" : "scale" });
5665 rule.style.backgroundImage = "none";
57 }
66 }
5867 };
59
68
69 var preloadedImages = new Hash();
70
71 function preloadRule(rule, docPath) {
72 var bgImg = rule.style.backgroundImage;
73 if (bgImg && bgImg != 'none' && bgImg != 'initial' ) {
74 if (!preloadedImages.get(bgImg)) {
75 bgImg.match(/^url[("']+(.*)[)"']+$/i);
76 var src = RegExp.$1;
77 // Relative path
78 if (!(src[0] == '/' || src.match(/^file:/) || src.match(/^https?:/)))
79 src = docPath + "/" + src;
80 preloadedImages.set(bgImg, true);
81 var image = new Image();
82 image.src = src;
83 }
84 }
85 }
86
6087 return {
6188 /*
6289 Method: fixPNG
------
7097
7198 Examples:
7299 > CSS.fixPNG() // To fix all css
73 >
100 >
74101 > CSS.fixPNG("mac_shadow.css") // to fix all css files with mac_shadow.css so mainly only on file
75102 >
76 > CSS.fixPNG("shadow", "vista"); // To fix all css files with shadow or vista in their names
103 > CSS.fixPNG("shadow", "vista"); // To fix all css files with shadow or vista in their names
77104
78105 Parameters
79106 patterns: (optional) list of pattern to filter css files
80107 */
81108 fixPNG: (Prototype.Browser.IE && Prototype.Browser.IEVersion < 7) ? fixPNG : Prototype.emptyFunction,
82
109
83110 // By Tobie Langel (http://tobielangel.com)
84111 // inspired by http://yuiblog.com/blog/2007/06/07/style/
85112 addRule: function(css, backwardCompatibility) {
------
89116 if (style.styleSheet) style.styleSheet.cssText = css;
90117 else style.appendText(css);
91118 return style;
119 },
120
121 preloadImages: function() {
122 parseStylesheet.apply(this, $A(arguments).concat(preloadRule));
92123 }
93124 };
94125 })();

trunk/test/functional/window/test.html (Unified diff)

r508r514
2323
2424 <script type="text/javascript">
2525 function runTest() {
26 CSS.preloadImages();
2627 openWindow();
2728 }
2829

trunk/test/functional/window/test_theme.html (Unified diff)

r492r514
3333
3434 function runTest() {
3535 var width = 200, height = 300, spacing = 20;
36 CSS.preloadImages();
3637
3738 UI.Window.setOptions({
3839 top: 200,