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

Revision 1910, 11.0 KB checked in by jeroen, 23 months ago (diff)

updated docs and tests of sharing plugin, fixed Flash controlbar button support

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