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

Revision 1958, 12.3 KB checked in by jeroen, 21 months ago (diff)

some small updates to sharing plugin: API, Fade, playlistItem.link and close button

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.system.System;
15    import flash.text.TextField;
16    import flash.text.TextFormat;
17
18
19    /** This plugin displays an embed code and video link for viral sharing. **/
20    public class Sharing extends Sprite implements IPlugin {
21
22
23        /** Embedding the image assets. **/
24        [Embed(source="../../assets/controlbar.png")]
25        private const ControlbarIcon:Class;
26        [Embed(source="../../assets/copy.png")]
27        private const CopyButton:Class;
28        [Embed(source="../../assets/close.png")]
29        private const CloseButton:Class;
30        [Embed(source="../../assets/facebook.png")]
31        private const FacebookButton:Class;
32        [Embed(source="../../assets/icon.png")]
33        private const DockIcon:Class;
34        [Embed(source="../../assets/input.png")]
35        private const InputBackground:Class;
36        [Embed(source="../../assets/sheet.png")]
37        private const BackSheet:Class;
38        [Embed(source="../../assets/twitter.png")]
39        private const TwitterButton:Class;
40
41
42        /** URL for Facebook and Twitter sharing dialogues. **/
43        public const FACEBOOK_URL:String = 'http://www.facebook.com/sharer/sharer.php?u=';
44        public const TWITTER_URL:String = 'http://twitter.com/intent/tweet?url=';
45
46
47        /** Clip with all graphics. **/
48        private var _container:MovieClip;
49        /** Reference to the background sheet. **/
50        private var _back:Sprite;
51        /** Reference to the close button. **/
52        private var _close:Sprite;
53        /** The current embed code. **/
54        private var _code:String;
55        /** Reference to the code field background. **/
56        private var _codeBack:DisplayObject;
57        /** Reference to the code copy button. **/
58        private var _codeButton:Sprite;
59        /** Reference to the code text field. **/
60        private var _codeField:TextField;
61        /** The form with all code elements. **/
62        private var _codeForm:Sprite;
63        /** Reference to the code label. **/
64        private var _codeLabel:TextField;
65        /** The plugin configuration options.**/
66        private var _config:Object;
67        /** The form with all buttons. **/
68        private var _form:Sprite;
69        /** The current video link. **/
70        private var _link:String;
71        /** Reference to the link field background. **/
72        private var _linkBack:DisplayObject;
73        /** Reference to the link copy button. **/
74        private var _linkButton:Sprite;
75        /** Reference to the link text field. **/
76        private var _linkField:TextField;
77        /** Reference to the link form. **/
78        private var _linkForm:Sprite;
79        /** Reference to the link label. **/
80        private var _linkLabel:TextField;
81        /** Reference to the player. **/
82        private var _player:IPlayer;
83        /** Reference to the facebook button. **/
84        private var _facebookButton:Sprite;
85        /** Reference to the twitter button. **/
86        private var _twitterButton:Sprite;
87
88
89        /** The background screen was clicked. **/
90        private function _backHandler(evt:MouseEvent):void {
91            hide();
92        };
93
94
95        /** The controlbar/dock button was clicked. **/
96        private function _buttonHandler(evt:MouseEvent):void {
97            if(_container.alpha) { hide(); } else { show(); }
98        };
99
100
101        /** The code copy button is clicked. **/
102        private function _codeHandler(evt:MouseEvent):void {
103            stage.focus = _codeField;
104            _codeField.setSelection(0,999999);
105            System.setClipboard(_code);
106        };
107
108
109        /** The facebook button is clicked. **/
110        private function _facebookHandler(evt:MouseEvent):void {
111            navigateToURL(new URLRequest(FACEBOOK_URL+encodeURIComponent(_link)));
112        };
113
114
115        /** Grab the page URL with some javascript magic. **/
116        private function _getPageURL():String {
117            var url:String = '';
118            if(ExternalInterface.available) {
119                try {
120                    url =  ExternalInterface.call('function(){if(window.top==window)return window.location.toString();else return document.referrer;}');
121                } catch (err:Error) {}
122            }
123            return url;
124        };
125
126
127        /** Hide the sheet. **/
128        public function hide():void {
129            new Animations(_container).fade(0,0.2);
130            // Only 5.7+...
131            try {
132                (_player.controls.display as Object).show();
133                (_player.controls.dock as Object).show();
134            } catch (error:Error) {}
135        };
136
137
138        /** Returns the plugin name. **/
139        public function get id():String {
140            return "sharing";
141        };
142
143
144        /** Called by the player to initialize; setup events and dock buttons.  */
145        public function initPlugin(player:IPlayer, config:PluginConfig):void {
146            _player = player;
147            _config = config;
148            _player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, _itemHandler);
149            // Setup the dock button
150            if(_player.config.dock === false) {
151                _player.controls.controlbar.addButton(new ControlbarIcon(), "share", _buttonHandler);
152            } else {
153                _player.controls.dock.addButton(new DockIcon(), "share", _buttonHandler);
154            }
155            // Register the show / hide calls with javascript.
156            if(ExternalInterface.available) {
157                try {
158                    ExternalInterface.addCallback('sharingHide',hide);
159                    ExternalInterface.addCallback('sharingShow',show);
160                } catch (e:Error) {}
161            }
162            // Add the background and form.
163            _container = new MovieClip();
164            _container.visible = false;
165            _container.alpha = 0;
166            addChild(_container);
167            _back = new Sprite();
168            _back.buttonMode = true;
169            _back.addChild(new BackSheet());
170            _back.addEventListener(MouseEvent.CLICK,_backHandler);
171            _container.addChild(_back);
172            _close = new Sprite();
173            _close.buttonMode = true;
174            _close.addChild(new CloseButton());
175            _close.addEventListener(MouseEvent.CLICK,_backHandler);
176            _container.addChild(_close);
177            _form = new Sprite();
178            _container.addChild(_form);
179
180            // Add the embed code fields
181            _codeForm = new Sprite();
182            _form.addChild(_codeForm);
183            _codeLabel = new TextField();
184            _codeLabel.defaultTextFormat = new TextFormat('Arial', 13, 0xFFFFFF, true);
185            _codeLabel.text = "Embed code";
186            _codeLabel.x = -5;
187            _codeLabel.y = 3;
188            _codeLabel.filters = new Array(new DropShadowFilter(1,45,0,1,0,0,1));
189            _codeForm.addChild(_codeLabel);
190            _codeBack = new InputBackground();
191            _codeForm.addChild(_codeBack);
192            _codeBack.x = 80;
193            _codeField = new TextField();
194            _codeField.defaultTextFormat = new TextFormat('Arial',11,0x000000);
195            _codeForm.addChild(_codeField);
196            _codeField.x = 84;
197            _codeField.y = 5;
198            _codeField.width = 178;
199            _codeField.height = 20;
200            _codeButton = new Sprite();
201            _codeForm.addChild(_codeButton);
202            _codeButton.buttonMode = true;
203            _codeButton.addChild(new CopyButton());
204            _codeButton.addEventListener(MouseEvent.CLICK,_codeHandler);
205            _codeButton.x = 255;
206
207            // Add the video link fields
208            _linkForm = new Sprite();
209            _form.addChild(_linkForm);
210            _linkForm.y = 40;
211            _linkLabel = new TextField();
212            _linkLabel.defaultTextFormat = new TextFormat('Arial', 13, 0xFFFFFF, true);
213            _linkLabel.text = "Video link";
214            _linkLabel.x = 10;
215            _linkLabel.y = 3;
216            _linkLabel.filters = new Array(new DropShadowFilter(1,45,0,1,0,0,1));
217            _linkForm.addChild(_linkLabel);
218            _linkBack = new InputBackground();
219            _linkForm.addChild(_linkBack);
220            _linkBack.x = 80;
221            _linkField = new TextField();
222            _linkField.defaultTextFormat = new TextFormat('Arial',11,0x000000);
223            _linkForm.addChild(_linkField);
224            _linkField.x = 84;
225            _linkField.y = 5;
226            _linkField.width = 178;
227            _linkField.height = 20;
228            _linkButton = new Sprite();
229            _linkForm.addChild(_linkButton);
230            _linkButton.buttonMode = true;
231            _linkButton.addChild(new CopyButton());
232            _linkButton.addEventListener(MouseEvent.CLICK,_linkHandler);
233            _linkButton.x = 255;
234
235            // Add facebook and twitter buttons.
236            _facebookButton = new Sprite();
237            _facebookButton.addChild(new FacebookButton());
238            _facebookButton.buttonMode = true;
239            _facebookButton.addEventListener(MouseEvent.CLICK,_facebookHandler);
240            _facebookButton.x = 80;
241            _facebookButton.y = 40;
242            _linkForm.addChild(_facebookButton);
243            _twitterButton = new Sprite();
244            _twitterButton.addChild(new TwitterButton());
245            _twitterButton.buttonMode = true;
246            _twitterButton.addEventListener(MouseEvent.CLICK,_twitterHandler);
247            _twitterButton.x = 190;
248            _twitterButton.y = 40;
249            _linkForm.addChild(_twitterButton);
250        };
251
252
253        /** Change the embed code when an item changes.  */
254        public function _itemHandler(evt:PlaylistEvent):void {
255            // Set the embed code.
256            var item:Object = _player.playlist.currentItem;
257            _code = '';
258            if(item['sharing.code']) {
259                _code = item['sharing.code'];
260            } else if(_config.code) {
261                _code = _config.code;
262            }
263            if(_code.substr(0,3) == '%3C') {
264                _code = decodeURIComponent(_code);
265            }
266            _codeField.text = _code;
267            if(_code == '') {
268                _codeForm.visible = false;
269                _linkForm.y = 20;
270            } else {
271                _codeForm.visible = true;
272                _linkForm.y = 40;
273            }
274            // Set the video link.
275            _link = _getPageURL();
276            if(item['sharing.link']) {
277                _link = item['sharing.link'];
278            } else if(_config.link) {
279                _link = _config.link;
280            } else if(item.link) {
281                _link = item.link;
282            }
283            _linkField.text = _link;
284        };
285
286
287        /** The link copy button is clicked. **/
288        private function _linkHandler(evt:MouseEvent):void {
289            stage.focus = _linkField;
290            _linkField.setSelection(0,999999);
291            System.setClipboard(_link);
292        };
293
294
295        /** Reposition the screens when the player resizes itself **/
296        public function resize(wid:Number, hei:Number):void {
297            _back.width = wid;
298            _back.height = hei;
299            _close.x = wid - 50;
300            _form.x = Math.round(wid/2 - 160);
301            _form.y = Math.round(hei/2 - 54);
302        };
303
304
305        /** Show the sheet. **/
306        public function show():void {
307            new Animations(_container).fade(1,0.2);
308            // Only 5.7+...
309            try {
310                (_player.controls.display as Object).hide();
311                (_player.controls.dock as Object).hide();
312            } catch (error:Error) {}
313        };
314
315
316        /** The twitter button is clicked. **/
317        private function _twitterHandler(evt:MouseEvent):void {
318            navigateToURL(new URLRequest(TWITTER_URL+encodeURIComponent(_link)));
319        };
320
321
322    }
323}
Note: See TracBrowser for help on using the repository browser.