source: trunk/fl5/src/com/longtailvideo/jwplayer/player/JavascriptAPI.as @ 400

Revision 400, 5.8 KB checked in by zach, 4 years ago (diff)
  • Restructured view
  • Added ExternalInterface Checks
Line 
1package com.longtailvideo.jwplayer.player {
2
3        import com.jeroenwijering.events.ControllerEvent;
4        import com.jeroenwijering.events.ModelEvent;
5        import com.jeroenwijering.events.ViewEvent;
6        import com.longtailvideo.jwplayer.events.PlayerEvent;
7       
8        import flash.external.ExternalInterface;
9
10        public class JavascriptAPI {
11                private var _player:Player;
12                private var _emu:PlayerV4Emulation;
13
14                private var controllerCallbacks:Object;         
15                private var modelCallbacks:Object;             
16                private var viewCallbacks:Object;               
17
18                public function JavascriptAPI(player:Player) {
19                        _player = player;
20                        _emu = PlayerV4Emulation.getInstance();
21
22                        _player.addEventListener(PlayerEvent.JWPLAYER_READY, playerReady);
23                       
24                        controllerCallbacks = {};
25                        modelCallbacks = {};
26                        viewCallbacks = {};
27                       
28                        setupListeners();
29                }
30               
31                private function playerReady(evt:PlayerEvent):void {
32                        var newEvt:PlayerEvent = new PlayerEvent("");
33                       
34                        var callbacks:String = _player.config.playerready ? _player.config.playerready + "," + "playerReady" : "playerReady"; 
35
36                        if (ExternalInterface.available) {
37                                for each (var callback:String in callbacks.replace(/\s/,"").split(",")) {
38                                        ExternalInterface.call(callback,{
39                                                id:newEvt.id,
40                                                client:newEvt.client,
41                                                version:newEvt.version
42                                        });
43                                }
44                        }                       
45                }
46               
47                private function setupListeners():void {
48                        if (ExternalInterface.available) {
49                                ExternalInterface.addCallback("addControllerListener",addJSControllerListener);
50                                ExternalInterface.addCallback("addModelListener",addJSModelListener);
51                                ExternalInterface.addCallback("addViewListener",addJSViewListener);
52                                ExternalInterface.addCallback("removeControllerListener",removeJSControllerListener);
53                                ExternalInterface.addCallback("removeModelListener",removeJSModelListener);
54                                ExternalInterface.addCallback("removeViewListener",removeJSViewListener);
55                                ExternalInterface.addCallback("getConfig",getConfig);
56                                ExternalInterface.addCallback("getPlaylist",getPlaylist);
57                                ExternalInterface.addCallback("getPluginConfig",getJSPluginConfig);
58                                ExternalInterface.addCallback("loadPlugin",loadPlugin);
59                                ExternalInterface.addCallback("sendEvent",sendEvent);
60                        }
61                }
62               
63
64                private function addJSControllerListener(type:String,callback:String):Boolean {
65                        type = type.toUpperCase();
66                        if (!controllerCallbacks.hasOwnProperty(type)) { controllerCallbacks[type] = []; }
67                        if ( (controllerCallbacks[type] as Array).indexOf(callback) < 0) {
68                                (controllerCallbacks[type] as Array).push(callback);
69                                _emu.addControllerListener(type, forwardControllerEvents);
70                        }
71                        return true;
72                }
73               
74                private function removeJSControllerListener(type:String,callback:String):Boolean {
75                        type = type.toUpperCase();
76                        var listeners:Array = (controllerCallbacks[type] as Array);
77                        var idx:Number = listeners ? listeners.indexOf(callback) : -1;
78                        if (idx >= 0) {
79                                listeners.splice(idx, 1);
80                                _emu.removeControllerListener(type.toUpperCase(), forwardControllerEvents);
81                                return true;
82                        }
83                        return false;
84                }
85
86
87                private function addJSModelListener(type:String,callback:String):Boolean {
88                        type = type.toUpperCase();
89                        if (!modelCallbacks.hasOwnProperty(type)) { modelCallbacks[type] = []; }
90                        if ( (modelCallbacks[type] as Array).indexOf(callback) < 0) {
91                                (modelCallbacks[type] as Array).push(callback);
92                                _emu.addModelListener(type, forwardModelEvents);
93                        }
94                        return true;
95                }
96               
97                private function removeJSModelListener(type:String,callback:String):Boolean {
98                        type = type.toUpperCase();
99                        var listeners:Array = (modelCallbacks[type] as Array);
100                        var idx:Number = listeners ? listeners.indexOf(callback) : -1;
101                        if (idx >= 0) {
102                                listeners.splice(idx, 1);
103                                _emu.removeModelListener(type.toUpperCase(), forwardModelEvents);
104                                return true;
105                        }
106                        return false;
107                }
108
109
110                private function addJSViewListener(type:String,callback:String):Boolean {
111                        type = type.toUpperCase();
112                        if (!viewCallbacks.hasOwnProperty(type)) { viewCallbacks[type] = []; }
113                        if ( (viewCallbacks[type] as Array).indexOf(callback) < 0) {
114                                (viewCallbacks[type] as Array).push(callback);
115                                _emu.addViewListener(type.toUpperCase(), forwardViewEvents);
116                        }
117                        return true;
118                }
119               
120                private function removeJSViewListener(type:String,callback:String):Boolean {
121                        type = type.toUpperCase();
122                        var listeners:Array = (viewCallbacks[type] as Array);
123                        var idx:Number = listeners ? listeners.indexOf(callback) : -1;
124                        if (idx >= 0) {
125                                listeners.splice(idx, 1);
126                                _emu.removeViewListener(type.toUpperCase(), forwardViewEvents);
127                                return true;
128                        }
129                        return false;
130                }
131
132                private function getConfig():Object {
133                        return _emu.config;
134                }
135               
136                private function getPlaylist():Object {
137                        return _emu.playlist;
138                }
139               
140                private function getJSPluginConfig(pluginName:String):Object {
141                        return _player.config.pluginConfig(pluginName);
142                }
143               
144                private function loadPlugin(plugin:String):Object {
145                        return {error:'This function is no longer supported.'}
146                }
147               
148                private function sendEvent(type:String, data:Object):void {
149                        _emu.sendEvent(type.toUpperCase(), data);
150                }
151               
152                private function forwardControllerEvents(evt:ControllerEvent):void {
153                        if (controllerCallbacks.hasOwnProperty(evt.type)) {
154                                for each (var callback:String in controllerCallbacks[evt.type]) {
155                                        if (ExternalInterface.available) {
156                                                ExternalInterface.call(callback, evt.data);
157                                        }
158                                }
159                        }
160                }
161
162                private function forwardModelEvents(evt:ModelEvent):void {
163                        if (modelCallbacks.hasOwnProperty(evt.type)) {
164                                for each (var callback:String in modelCallbacks[evt.type]) {
165                                        if (ExternalInterface.available) {
166                                                ExternalInterface.call(callback, evt.data);
167                                        }
168                                }
169                        }
170                }
171
172                private function forwardViewEvents(evt:ViewEvent):void {
173                        if (viewCallbacks.hasOwnProperty(evt.type)) {
174                                for each (var callback:String in viewCallbacks[evt.type]) {
175                                        if (ExternalInterface.available) {
176                                                ExternalInterface.call(callback, evt.data);
177                                        }
178                                }
179                        }
180                }
181
182        }
183
184}
Note: See TracBrowser for help on using the repository browser.