Merge branch 'localfile'
[potlatch2.git] / net / systemeD / halcyon / styleparser / NestedCSSLoader.as
1 package net.systemeD.halcyon.styleparser {
2
3     import net.systemeD.halcyon.FileBank;
4
5     import flash.events.*;
6
7         /** A class permitting you to load CSS files containing 'import' rules, which will be 
8                 automatically replaced with the contents of the file.                                                                   <p>
9         
10                 Typical usage:                                                                                                                                                  </p><pre>
11         
12                         cssLoader=new NestedCSSLoader();
13                         cssLoader.addEventListener(Event.COMPLETE, doParseCSS);
14                         cssLoader.load("potlatch.css");                                                                                                         </pre><p>
15         
16                 doParseCSS can then access the CSS via event.target.css.                                                                </p>
17         */
18
19         public class NestedCSSLoader extends EventDispatcher {
20                 public var css:String;
21                 private var count:int;
22
23                 private static const IMPORT:RegExp=/@import\s*[^'"]*['"]([^'"]+)['"][^;]*;/g;           // '
24
25                 public function NestedCSSLoader() {
26                 }
27                 
28                 public function load(url:String):void {
29             FileBank.getInstance().addFromFile(url, fileLoaded);
30                 }
31                 
32                 private function fileLoaded(fileBank:FileBank, filename:String):void {
33                         css = fileBank.getAsString(filename);
34                         count = 1;
35
36                         var results:Array = css.match(IMPORT);
37             while (results.length > 0) {
38                 IMPORT.lastIndex = 0;
39                 var result:Object = IMPORT.exec(results.shift());
40                                 count++;
41                                 replaceCSS(result[1],result[0]);
42                         }
43             decreaseCount();
44                 }
45
46                 private function replaceCSS(filename:String, toReplace:String):void {
47                         var cssLoader:NestedCSSLoader=new NestedCSSLoader();
48                         var replaceText:String=toReplace;
49                         cssLoader.addEventListener(Event.COMPLETE, function(event:Event):void {
50                                 css=css.replace(replaceText,event.target.css);
51                                 decreaseCount();
52                         });
53                         cssLoader.load(filename);
54                 }
55                 
56                 private function decreaseCount():void {
57                         count--; if (count>0) return;
58                         fireComplete();
59                 }
60                 
61                 private function fireComplete():void {
62                         var event:Event=new Event(Event.COMPLETE);
63                         dispatchEvent(event);
64                 }
65         }
66 }