source: plugins/sharing/src/as/Sharing.as @ 2059

Revision 2059, 10.0 KB checked in by jeroen, 17 months ago (diff)

added OVA support, removed shortcuts from docs

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
8    import flash.display.*;
9    import flash.events.MouseEvent;
10    import flash.external.ExternalInterface;
11    import flash.filters.DropShadowFilter;
12    import flash.net.URLRequest;
13    import flash.net.navigateToURL;
14    import flash.text.TextField;
15    import flash.text.TextFormat;
16
17
18    /**
19    * This plugin displays a dialog with embed code, video link and Facebook/Twitter icons.
20    **/
21    public class Sharing extends Sprite implements IPlugin {
22
23
24        /** Embedding the image assets. **/
25        [Embed(source="../../assets/closeButton.png")]
26        private const CloseButton:Class;
27        [Embed(source="../../assets/dialogIcon.png")]
28        private const DialogIcon:Class;
29        [Embed(source="../../assets/dialogBack.png")]
30        private const DialogBack:Class;
31        [Embed(source="../../assets/facebookIcon.png")]
32        private const FacebookIcon:Class;
33        [Embed(source="../../assets/shareIcon.png")]
34        private const ShareIcon:Class;
35        [Embed(source="../../assets/tweetIcon.png")]
36        private const TweetIcon:Class;
37        [Embed(source="../../assets/twitterIcon.png")]
38        private const TwitterIcon:Class;
39
40
41        /** URL for Facebook and Twitter sharing dialogues. **/
42        public const FACEBOOK_URL:String = 'http://www.facebook.com/sharer/sharer.php?u=';
43        public const TWITTER_URL:String = 'http://twitter.com/intent/tweet?url=';
44
45
46        /** Reference to the background sheet. **/
47        private var _back:Sprite;
48        /** Reference to the close button. **/
49        private var _close:Sprite;
50        /** The current embed code. **/
51        private var _code:String;
52        /** The complete form row with embed code. **/
53        private var _codeRow:SharingRow;
54        /** The plugin configuration options.**/
55        private var _config:Object;
56        /** Clip with all graphics. **/
57        private var _container:MovieClip;
58        /** Dock button list, which are hidden/restored for OVA. **/
59        private var _dockButtons:Array;
60        /** Reference to the facebook button. **/
61        private var _facebookButton:SharingButton;
62        /** The form with all buttons. **/
63        private var _form:Sprite;
64        /** The heading text. **/
65        private var _headingText:TextField;
66        /** The current video link. **/
67        private var _link:String;
68        /** The complete form row with video link. **/
69        private var _linkRow:SharingRow;
70        /** Reference to the player. **/
71        private var _player:IPlayer;
72        /** Reference to the twitter button. **/
73        private var _twitterButton:SharingButton;
74
75
76        /** Hide the dialog when clicking its background. **/
77        private function _backHandler(evt:MouseEvent):void {
78            new Animations(_container).fade(0,0.2);
79            // Only 5.7+...
80            try {
81                (_player.controls.display as Object).show();
82                (_player.controls.dock as Object).show();
83            } catch (error:Error) {}
84        };
85
86
87        /** Show the dialog when clicking its dock icon. **/
88        private function _dialogHandler(evt:MouseEvent):void {
89            _player.pause();
90            new Animations(_container).fade(1,0.2);
91            // Only 5.7+...
92            try {
93                (_player.controls.display as Object).hide();
94                (_player.controls.dock as Object).hide();
95            } catch (error:Error) {}
96        };
97
98
99        /** The facebook button is clicked. **/
100        private function _facebookHandler(evt:MouseEvent):void {
101            navigateToURL(new URLRequest(FACEBOOK_URL+encodeURIComponent(_link)));
102        };
103
104
105        /** Grab the page URL with some javascript magic. **/
106        private function _getPageURL():String {
107            var url:String = '';
108            if(ExternalInterface.available) {
109                try {
110                    url =  ExternalInterface.call('function(){if(window.top==window)return window.location.toString();else return document.referrer;}');
111                } catch (err:Error) {}
112            }
113            return url;
114        };
115
116
117        /** Returns the plugin name. **/
118        public function get id():String {
119            return "sharing";
120        };
121
122
123        /** Called by the player to initialize; setup events and dock buttons.  **/
124        public function initPlugin(player:IPlayer, config:PluginConfig):void {
125            _player = player;
126            _config = config;
127            _player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, _itemHandler);
128            // Setup the dock buttons
129            _dockButtons = new Array();
130            if(_config.shortcuts) {
131                _dockButtons.push(_player.controls.dock.addButton(new ShareIcon(), "share", _facebookHandler));
132                _dockButtons.push(_player.controls.dock.addButton(new TweetIcon(), "tweet", _twitterHandler));
133            } else {
134                _dockButtons.push(_player.controls.dock.addButton(new DialogIcon(), "share", _dialogHandler));
135                _renderDialog();
136            }
137        };
138
139
140        /** Change the embed code when an item changes.  */
141        public function _itemHandler(evt:PlaylistEvent):void {
142            // Set the embed code.
143            var item:Object = _player.playlist.currentItem;
144            _code = '';
145            if(item['sharing.code']) {
146                _code = item['sharing.code'];
147            } else if(_config.code) {
148                _code = _config.code.replace('MEDIAID',item['mediaid']);;
149            }
150            if(_code.substr(0,3) == '%3C') {
151                _code = decodeURIComponent(_code);
152            }
153            // Set the video link.
154            _link = _getPageURL();
155            if(item['sharing.link']) {
156                _link = item['sharing.link'];
157            } else if(item.link) {
158                _link = item.link;
159            } else if(_config.link) {
160                _link = _config.link.replace('MEDIAID',item['mediaid']);
161            }
162            // Update the form elements only if the dialog is available.
163            if(_container) {
164                _codeRow.setText(_code);
165                _linkRow.setText(_link);
166                // Show/hide the embed dialog per entry.
167                if(_code == '') {
168                    _codeRow.visible = false;
169                    _linkRow.y = 32;
170                    _facebookButton.y = _twitterButton.y = 74;
171                } else {
172                    _codeRow.visible = true;
173                    _codeRow.y = 32;
174                    _linkRow.y = 74;
175                    _facebookButton.y = _twitterButton.y = 116;
176                }
177                resize(_back.width,_back.height);
178            }
179            // Hide the dock button(s) if OVA is used.
180            if(item['ova.hidden']) {
181                _dockButtons[0].visible = false;
182                if(_dockButtons[1]) { _dockButtons[1].visible = false; }
183            } else {
184                _dockButtons[0].visible = true;
185                if(_dockButtons[1]) { _dockButtons[1].visible = true; }
186            }
187        };
188
189
190        /** The dialog is only rendered when the shortcuts are not set. **/
191        private function _renderDialog():void {
192            // Draw the background and close button.
193            _container = new MovieClip();
194            _container.visible = false;
195            _container.alpha = 0;
196            addChild(_container);
197            _back = new Sprite();
198            _back.buttonMode = true;
199            _back.addChild(new DialogBack());
200            _back.addEventListener(MouseEvent.CLICK,_backHandler);
201            _container.addChild(_back);
202            _close = new Sprite();
203            _close.buttonMode = true;
204            _close.addChild(new CloseButton());
205            _close.addEventListener(MouseEvent.CLICK,_backHandler);
206            _container.addChild(_close);
207
208            // Draw the form wrapper and heading.
209            _form = new Sprite();
210            _container.addChild(_form);
211            _headingText = new TextField();
212            _headingText.width = 240;
213            _headingText.height = 24;
214            _headingText.defaultTextFormat = new TextFormat('Arial', 16, 0xFFFFFF, true);
215            if(_config.heading is String) {
216                _headingText.text = _config.heading;
217            } else {
218                _headingText.text = "Share this video";
219            }
220            _headingText.x = 80;
221            _headingText.filters = new Array(new DropShadowFilter(1,45,0,1,0,0,2));
222            _form.addChild(_headingText);
223
224
225            // Draw the rows and buttons
226            _codeRow = new SharingRow('Embed code');
227            _form.addChild(_codeRow);
228            _linkRow = new SharingRow('Video link');
229            _form.addChild(_linkRow);
230            _facebookButton = new SharingButton(new FacebookIcon(),'Facebook',_facebookHandler);
231            _facebookButton.x = 80;
232            _form.addChild(_facebookButton);
233            _twitterButton = new SharingButton(new TwitterIcon(),'Twitter',_twitterHandler);
234            _twitterButton.x = 200;
235            _form.addChild(_twitterButton);
236        };
237
238
239        /** Reposition the screens when the player resizes itself **/
240        public function resize(wid:Number, hei:Number):void {
241            if(_container) {
242                _back.width = wid;
243                _back.height = hei;
244                _close.x = wid - 50;
245                _form.x = Math.round(wid/2 - 165);
246                _form.y = Math.round(hei/2 - _form.height/2);
247            }
248        };
249
250
251        /** The twitter button is clicked. **/
252        private function _twitterHandler(evt:MouseEvent):void {
253            navigateToURL(new URLRequest(TWITTER_URL+encodeURIComponent(_link)));
254        };
255
256
257    }
258}
Note: See TracBrowser for help on using the repository browser.