/
LoadablesLoader.as
97 lines (85 loc) · 3.49 KB
/
LoadablesLoader.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright 2011 Marek Standio.
This file is part of SaladoPlayer.
SaladoPlayer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
SaladoPlayer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SaladoPlayer. If not, see <http://www.gnu.org/licenses/>.
*/
package com.panozona.player.manager.utils.loading{
import com.panozona.player.manager.utils.loading.*;
import com.panozona.player.manager.events.*;
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.system.*;
public class LoadablesLoader extends EventDispatcher{
private var loaders:Vector.<Loader>;
private var loadables:Vector.<ILoadable>;
public function load(loadables:Vector.<ILoadable>):void {
this.loadables = loadables;
loaders = new Vector.<Loader>();
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
context.checkPolicyFile = true;
for (var i:int = 0; i < loadables.length; i++) {
if (loadables[i].path == null || !loadables[i].path.match(/^.+(.jpg|.jpeg|.png|.gif|.swf)$/i)) {
dispatchEvent(new LoadLoadableEvent(LoadLoadableEvent.LOST, loadables[i]));
loaders[i] = null;
continue;
}
loaders[i] = new Loader();
loaders[i].contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadableLost);
loaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, loadableLoaded);
loaders[i].load(new URLRequest(loadables[i].path), context);
}
checkLoadingState(); // perhaps there is nothing to load
}
public function abort():void{
for (var i:int = 0; i < loaders.length; i++) {
if (loaders[i] != null) {
loaders[i].unload();
loaders[i] = null;
}
}
dispatchEvent(new LoadLoadableEvent(LoadLoadableEvent.ABORTED));
}
private function loadableLost(e:IOErrorEvent):void {
for(var i:int = 0; i < loaders.length; i++) {
if (loaders[i] != null && loaders[i].contentLoaderInfo === e.target) {
dispatchEvent(new LoadLoadableEvent(LoadLoadableEvent.LOST, loadables[i]));
loaders[i].contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loadableLost);
loaders[i].contentLoaderInfo.removeEventListener(Event.COMPLETE, loadableLoaded);
loaders[i] = null;
checkLoadingState();
return;
}
}
}
private function loadableLoaded(e:Event):void {
for(var i:int = 0; i < loaders.length; i++){
if (loaders[i] != null && loaders[i].contentLoaderInfo === e.target) {
dispatchEvent(new LoadLoadableEvent(LoadLoadableEvent.LOADED, loadables[i], loaders[i].content));
loaders[i].contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loadableLost);
loaders[i].contentLoaderInfo.removeEventListener(Event.COMPLETE, loadableLoaded);
loaders[i] = null;
checkLoadingState();
return;
}
}
}
private function checkLoadingState():void {
for (var i:int = 0; i < loaders.length; i++) {
if (loaders[i] != null) {
return;
}
}
dispatchEvent(new LoadLoadableEvent(LoadLoadableEvent.FINISHED));
}
}
}