Changeset 514
add CSS.preloadImages
Feb 08 2008 * 14:27 (11 months ago)
Committed by seb
Affected files:
trunk/CHANGELOG
trunk/src/util/css.js (Quick diff)
trunk/test/functional/window/test.html (Quick diff)
trunk/test/functional/window/test_theme.html (Quick diff)
trunk/src/util/css.js (Unified diff)
| r500 | r514 | |
|---|---|---|
| 12 | 12 | // Code based on: |
| 13 | 13 | // - IE5.5+ PNG Alpha Fix v1.0RC4 (c) 2004-2005 Angus Turnbull http://www.twinhelix.com |
| 14 | 14 | // - 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 | ||
| 17 | 23 | // To avoid flicking background |
| 18 | document.execCommand("BackgroundImageCache", false, true); | |
| 24 | //document.execCommand("BackgroundImageCache", false, true); | |
| 19 | 25 | // Parse all document stylesheets |
| 20 | 26 | 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 | }); | |
| 24 | 32 | }); |
| 25 | } | |
| 26 | styleSheets.each(fixStylesheet); | |
| 33 | } | |
| 34 | styleSheets.each(function(styleSheet) {fixStylesheet.call(this, styleSheet, method)}); | |
| 27 | 35 | }; |
| 28 | ||
| 36 | ||
| 29 | 37 | // Fixes a stylesheet |
| 30 | function fixStylesheet(stylesheet) { | |
| 38 | function fixStylesheet(stylesheet, method) { | |
| 31 | 39 | // Parse import files |
| 32 | 40 | if (stylesheet.imports) |
| 33 | 41 | $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('/')); | |
| 36 | 45 | // 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) }); | |
| 38 | 47 | }; |
| 39 | ||
| 48 | ||
| 40 | 49 | var filterPattern = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{src}",sizingMethod="#{method}")'; |
| 41 | ||
| 50 | ||
| 42 | 51 | // 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; | |
| 45 | 54 | // 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)) { | |
| 47 | 56 | var src = RegExp.$1; |
| 48 | 57 | var bgRepeat = rule.style.backgroundRepeat; |
| 49 | 58 | // Relative path |
| 50 | 59 | if (src[0] != '/') |
| 51 | 60 | src = docPath + "/" + src; |
| 52 | 61 | // Apply filter |
| 53 | rule.style.filter = filterPattern.interpolate({ | |
| 54 | src: src, | |
| 62 | rule.style.filter = filterPattern.interpolate({ | |
| 63 | src: src, | |
| 55 | 64 | method: bgRepeat == "no-repeat" ? "crop" : "scale" }); |
| 56 | 65 | rule.style.backgroundImage = "none"; |
| 57 | } | |
| 66 | } | |
| 58 | 67 | }; |
| 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 | ||
| 60 | 87 | return { |
| 61 | 88 | /* |
| 62 | 89 | Method: fixPNG |
| --- | --- | |
| 70 | 97 | |
| 71 | 98 | Examples: |
| 72 | 99 | > CSS.fixPNG() // To fix all css |
| 73 | > | |
| 100 | > | |
| 74 | 101 | > CSS.fixPNG("mac_shadow.css") // to fix all css files with mac_shadow.css so mainly only on file |
| 75 | 102 | > |
| 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 | |
| 77 | 104 | |
| 78 | 105 | Parameters |
| 79 | 106 | patterns: (optional) list of pattern to filter css files |
| 80 | 107 | */ |
| 81 | 108 | fixPNG: (Prototype.Browser.IE && Prototype.Browser.IEVersion < 7) ? fixPNG : Prototype.emptyFunction, |
| 82 | ||
| 109 | ||
| 83 | 110 | // By Tobie Langel (http://tobielangel.com) |
| 84 | 111 | // inspired by http://yuiblog.com/blog/2007/06/07/style/ |
| 85 | 112 | addRule: function(css, backwardCompatibility) { |
| --- | --- | |
| 89 | 116 | if (style.styleSheet) style.styleSheet.cssText = css; |
| 90 | 117 | else style.appendText(css); |
| 91 | 118 | return style; |
| 119 | }, | |
| 120 | ||
| 121 | preloadImages: function() { | |
| 122 | parseStylesheet.apply(this, $A(arguments).concat(preloadRule)); | |
| 92 | 123 | } |
| 93 | 124 | }; |
| 94 | 125 | })(); |
trunk/test/functional/window/test.html (Unified diff)
| r508 | r514 | |
|---|---|---|
| 23 | 23 | |
| 24 | 24 | <script type="text/javascript"> |
| 25 | 25 | function runTest() { |
| 26 | CSS.preloadImages(); | |
| 26 | 27 | openWindow(); |
| 27 | 28 | } |
| 28 | 29 |
trunk/test/functional/window/test_theme.html (Unified diff)
| r492 | r514 | |
|---|---|---|
| 33 | 33 | |
| 34 | 34 | function runTest() { |
| 35 | 35 | var width = 200, height = 300, spacing = 20; |
| 36 | CSS.preloadImages(); | |
| 36 | 37 | |
| 37 | 38 | UI.Window.setOptions({ |
| 38 | 39 | top: 200, |


RSS feeds