source: plugins/related/src/as/Related.as @ 1938

Revision 1938, 7.8 KB checked in by jeroen, 22 months ago (diff)

initial cleanup of Flash related. Most test working, but tweaks needed

Line 
1package {
2
3    import com.longtailvideo.jwplayer.events.*;
4    import com.longtailvideo.jwplayer.player.*;
5    import com.longtailvideo.jwplayer.plugins.*;
6    import com.longtailvideo.jwplayer.utils.*;
7    import com.longtailvideo.jwplayer.parsers.*;
8
9    import flash.display.*;
10    import flash.events.*;
11    import flash.filters.DropShadowFilter;
12    import flash.net.*;
13    import flash.text.*;
14
15
16    /** This plugin displays an overlay with related videos. **/
17    public class Related extends Sprite implements IPlugin {
18
19
20        /** Embedding the image assets. **/
21        [Embed(source="../../assets/icon.png")]
22        private const DockIcon:Class;
23        [Embed(source="../../assets/sheet.png")]
24        private const BackSheet:Class;
25
26
27        /** Reference to the background sheet. **/
28        private var _back:Sprite;
29        /** Reference to the dock button. **/
30        private var _button:MovieClip;
31        /** The plugin configuration options.**/
32        private var _config:Object;
33        /** Number of columns and rows in the grid. **/
34        private var _dimensions:Array;
35        /** Link to the mRSS file with related videos. **/
36        private var _file:String;
37        /** The grid with all thumbs. **/
38        private var _grid:Sprite;
39        /** The CTA text heading. **/
40        private var _heading:TextField;
41        /** Reference to the dock icon. **/
42        private var _icon:DisplayObject;
43        /** Component that loads the related videos. **/
44        private var _loader:URLLoader;
45        /** Component that parses the related videos. **/
46        private var _parser:RSSParser;
47        /** Reference to the player. **/
48        private var _player:IPlayer;
49
50
51        /** The background screen was clicked. **/
52        private function _backHandler(evt:MouseEvent):void { hide(); };
53
54
55        /** Display the related items on complete. **/
56        private function _completeHandler(evt:MediaEvent):void {
57            if(_config.oncomplete !== false) { show(); }
58        };
59
60
61        /** The controlbar/dock button was clicked. **/
62        private function _dockHandler(evt:MouseEvent):void { show(); };
63
64
65        /** Loading the RSS feed failed. **/
66        private function _errorHandler(evt:ErrorEvent):void {
67            Logger.log(evt.text,id);
68            _file = undefined;
69            if(_icon) {
70                _icon.alpha = 0.5;
71                _button.field.alpha = 0.5;
72                _button.field.text = 'not set';
73            }
74        };
75
76
77        /** Hide the list with related videos. **/
78        public function hide():void {
79            _back.visible = false;
80            _grid.visible = false;
81            _heading.visible = false;
82            // Only 5.7+...
83            try { (_player.controls.display as Object).show(); } catch (error:Error) {}
84        };
85
86
87        /** Returns the plugin name. **/
88        public function get id():String {
89            return "related";
90        };
91
92
93        /** Called by the player to initialize; setup events and dock buttons.  */
94        public function initPlugin(player:IPlayer, config:PluginConfig):void {
95            _player = player;
96            _config = config;
97            _player.addEventListener(MediaEvent.JWPLAYER_MEDIA_COMPLETE, _completeHandler);
98            _player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, _itemHandler);
99            _loader = new URLLoader();
100            _loader.addEventListener(Event.COMPLETE,_loaderHandler);
101            _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _errorHandler);
102            _loader.addEventListener(IOErrorEvent.IO_ERROR, _errorHandler);
103            _parser = new RSSParser();
104            if(_config.usedock !== false) {
105                _icon = new DockIcon()
106                _button = _player.controls.dock.addButton(_icon, "related", _dockHandler);
107            }
108            // Add the background sheet.
109            _back = new Sprite();
110            _back.visible = false;
111            _back.buttonMode = true;
112            _back.addChild(new BackSheet());
113            _back.addEventListener(MouseEvent.CLICK,_backHandler);
114            addChild(_back);
115            // Add the text heading.
116            _heading = new TextField();
117            _heading.height = 30;
118            _heading.defaultTextFormat = new TextFormat('Arial', 16, 0xFFFFFF, true);
119            _heading.autoSize = 'center';
120            _heading.multiline = false;
121            _heading.selectable = false;
122            _heading.filters = new Array(new DropShadowFilter(2,45,0,1,1,1,1));
123            if(_config.heading) {
124                _heading.htmlText = _config.heading;
125            } else {
126                _heading.htmlText = "Watch related videos";
127            }
128            addChild(_heading);
129            // Add the grid for thumbs
130            _grid = new Sprite();
131            addChild(_grid);
132            // Hide the graphics on startup.
133            hide();
134        };
135
136
137        /** Find the related thumbs feed when an item loads.  */
138        public function _itemHandler(evt:PlaylistEvent):void {
139            // Reset old data
140            _file = undefined;
141            // Check for new file
142            if(_player.playlist.currentItem['related.file']) {
143                _file = _player.playlist.currentItem['related.file'];
144            } else if (_config['file']) {
145                _file = _config['file'];
146            }
147            // Load the mRSS feed and set the dock icon
148            if(_file) {
149                _loader.load(new URLRequest(_file));
150                if(_icon) {
151                    _icon.alpha = 1;
152                    _button.field.alpha = 1;
153                    _button.field.text = 'related';
154                }
155            } else {
156                _errorHandler(new ErrorEvent(ErrorEvent.ERROR,false,false,
157                    "This playlist entry has no related videos."));
158            }
159        };
160
161
162        /** Loader has loaded the mRSS feed. **/
163        private function _loaderHandler(evt:Event):void {
164            var xml:XML = XML(evt.target.data);
165            var rss:Array = _parser.parse(xml);
166            var related:Array = new Array();
167            for (var i:Number = 0; i < rss.length; i++) {
168                if(rss[i].title && rss[i].image && rss[i].link) {
169                    related.push(rss[i]);
170                }
171            }
172            if(related.length) {
173                Logger.log("Found "+related.length+" related videos",id);
174                for(var j:Number = 0; j < related.length; j++) {
175                    var thumb:RelatedThumb = new RelatedThumb(related[j].image,related[j].link,related[j].title);
176                    thumb.x = 130*j;
177                    _grid.addChild(thumb);
178                    resize(_back.width,_back.height);
179                }
180            } else {
181                _errorHandler(new ErrorEvent(ErrorEvent.ERROR,false,false,
182                    "RSS feed has 0 entries that contain title,link and image."));
183            }
184        };
185
186
187        /** Reposition the screens when the player resizes itself **/
188        public function resize(width:Number, height:Number):void {
189            _back.width = width;
190            _back.height = height;
191            _grid.x = Math.round(width/2 - _grid.width/2);
192            _grid.y = Math.round(height/2 - _grid.height/2) + 10;
193            _heading.y = _grid.y - 30;
194            _heading.x = Math.round(width/2 - _heading.width/2);
195            _dimensions = new Array(
196                Math.floor(width/130),
197                Math.floor((height-30)/90)
198            );
199        };
200
201
202        /** Show the list with related videos. **/
203        public function show():void {
204            if(_file) {
205                _back.visible = true;
206                _grid.visible = true;
207                _heading.visible = true;
208                // Only 5.7+...
209                try { (_player.controls.display as Object).hide(); } catch (error:Error) {}
210            }
211        };
212
213
214    }
215}
Note: See TracBrowser for help on using the repository browser.