source: plugins/sharing/v4/com/jeroenwijering/plugins/Sharing.as @ 1087

Revision 1087, 7.7 KB checked in by jeroen, 3 years ago (diff)

fixed #941 (sharing plugin shows page link before play)

Line 
1package com.jeroenwijering.plugins {
2
3
4import com.jeroenwijering.events.*;
5
6import flash.display.MovieClip;
7import flash.display.Sprite;
8import flash.events.MouseEvent;
9import flash.external.ExternalInterface;
10import flash.net.navigateToURL;
11import flash.net.URLRequest;
12import flash.text.TextField;
13import flash.text.TextFormat;
14import flash.system.System;
15import flash.utils.setTimeout;
16
17
18/**
19* Sharing plugin; shows a "share" and "embed" button in the (4.5+) dock or (4.3+) controlbar.
20**/
21public class Sharing extends MovieClip implements PluginInterface {
22
23
24        [Embed(source="../../../embedButton.png")]
25        private const EmbedButton:Class;
26        [Embed(source="../../../embedIcon.png")]
27        private const EmbedIcon:Class;
28        [Embed(source="../../../embedScreen.png")]
29        private const EmbedScreen:Class;
30        [Embed(source="../../../shareButton.png")]
31        private const ShareButton:Class;
32        [Embed(source="../../../shareIcon.png")]
33        private const ShareIcon:Class;
34        [Embed(source="../../../shareScreen.png")]
35        private const ShareScreen:Class;
36
37
38        /** List with configuration settings. **/
39        public var config:Object = {
40                code:undefined,
41                link:'',
42                title:'Great%20video'
43        };
44        /** Reference to the View of the player. **/
45        private var view:AbstractView;
46        /** Reference to the embed container. **/
47        private var embed:Sprite;
48        /** Reference to the embed graphics. **/
49        private var embedScreen:Sprite;
50        /** Reference to the embed textfield. **/
51        private var embedField:TextField;
52        /** Reference to the share graphics. **/
53        private var share:Sprite;
54        /** Reference to the share graphics. **/
55        private var shareScreen:Sprite;
56        /** Reference to the share textfield. **/
57        private var shareField:TextField;
58        /** Reference to the transparent bg area. **/
59        private var back:Sprite;
60        /** Remenber which item is visible. **/
61        private var showing:String;
62
63
64        /** Constructor; builds the background area. **/
65        public function Sharing():void {
66                back = new Sprite();
67                embed = new Sprite();
68                share = new Sprite();
69                back.graphics.beginFill(0x000000,0);
70                back.graphics.drawRect(0,0,320,240);
71                back.buttonMode = true;
72                back.addEventListener(MouseEvent.CLICK,backHandler);
73                addChild(back);
74        };
75
76
77        /** Screenground clicked; hide the screen. **/
78        private function backHandler(evt:MouseEvent=null):void {
79                showHide('none');
80        };
81
82
83        /** Embed area is clicked; select the code and copy to clipboard.. **/
84        private function codeHandler(evt:MouseEvent):void {
85                stage.focus = embedField;
86                embedField.setSelection(0,999999);
87                System.setClipboard(config['code']);
88        };
89
90
91        /** Set the embed buttons and graphics. **/
92        private function drawEmbed():void {
93                if(view.getPlugin('dock') && view.config['dock']) {
94                        view.getPlugin('dock').addButton(new EmbedIcon(),'embed',embedHandler);
95                } else if (view.getPlugin('controlbar')) {
96                        view.getPlugin('controlbar').addButton(new EmbedButton(),'embed',embedHandler);
97                }
98                embedScreen = new Sprite();
99                embedScreen.addChild(new EmbedScreen());
100                embedScreen.buttonMode = true;
101                embedScreen.addEventListener(MouseEvent.CLICK,codeHandler);
102                embed.addChild(embedScreen);
103                embedField = new TextField();
104                embedField.defaultTextFormat = new TextFormat('_sans',11);
105                embedField.multiline = true;
106                embedField.wordWrap = true;
107                embedField.x = 22;
108                embedField.y = 32;
109                embedField.text = config['code'];
110                embedField.width = 258;
111                embedField.height = 64;
112                embed.addChild(embedField);
113                addChild(embed);
114        };
115
116
117        /** Set the embed buttons and graphics. **/
118        private function drawShare():void {
119                if(view.getPlugin('dock') && view.config['dock']) {
120                        view.getPlugin('dock').addButton(new ShareIcon(),'share',shareHandler);
121                } else if (view.getPlugin('controlbar')) {
122                        view.getPlugin('controlbar').addButton(new ShareButton(),'share',shareHandler);
123                }
124                shareScreen = new Sprite();
125                shareScreen.addChild(new ShareScreen());
126                shareScreen.buttonMode = true;
127                shareScreen.addEventListener(MouseEvent.CLICK,linkHandler);
128                share.addChild(shareScreen);
129                shareField = new TextField();
130                shareField.defaultTextFormat = new TextFormat('_sans',11);
131                shareField.x = 22;
132                shareField.y = 32;
133                shareField.text = config['link'];
134                shareField.width = 258;
135                shareField.height = 20;
136                share.addChild(shareField);
137                addChild(share);
138        };
139
140
141
142        /** Embed icon is clicked; toggle the embed screen. **/
143        private function embedHandler(evt:MouseEvent):void {
144                showHide('embed');
145        };
146
147
148        /** The initialize call is invoked by the player View. **/
149        public function initializePlugin(vie:AbstractView):void {
150                view = vie;
151                view.addControllerListener(ControllerEvent.RESIZE,resizeHandler);
152                if(view.config['sharing.code']) {
153                        config['code'] = view.config['sharing.code'];
154                        drawEmbed();
155                }
156                if(view.config['sharing.link']) {
157                        if(view.config['sharing.link'] != 'none') {
158                                config['link'] = view.config['sharing.link'];
159                                drawShare();
160                        }
161                } else {
162                        setTimeout(pageLink,1000);
163                        drawShare();
164                        view.addControllerListener(ControllerEvent.ITEM,itemHandler);
165                        view.addControllerListener(ControllerEvent.PLAYLIST,itemHandler);
166                }
167                showHide('none');
168        };
169
170
171        /** Share area is clicked; select the link and copy to clipboard or follow a shortcut. **/
172        private function linkHandler(evt:MouseEvent):void {
173                if(shareScreen.mouseY < 80) {
174                        if(shareScreen.mouseX < 80) {
175                                navigateToURL(new URLRequest(config['link']),view.config['linktarget']);
176                        } else {
177                                stage.focus = shareField;
178                                shareField.setSelection(0,999999);
179                                System.setClipboard(config['link']);
180                        }
181                } else {
182                        var url:String;
183                        if (shareScreen.mouseX < 75) {
184                                url = 'http://www.facebook.com/share.php?u='+encodeURI(config['link']);
185                                navigateToURL(new URLRequest(url));
186                        } else if (shareScreen.mouseX < 150) {
187                                url = 'http://twitter.com/home?status='+config['title']+': '+encodeURI(config['link']);
188                                navigateToURL(new URLRequest(url));
189                        } else if (shareScreen.mouseX < 225) {
190                                if(config['code']) {
191                                        url = 'http://www.myspace.com/Modules/PostTo/Pages/?t='+config['title']+'&c='+encodeURI(config['code']);
192                                } else {
193                                        url = 'http://www.myspace.com/Modules/PostTo/Pages/?t='+config['title']+'&c='+encodeURI(config['link']);
194                                }
195                                navigateToURL(new URLRequest(url));
196                        } else {
197                                url = 'mailto:?subject='+config['title']+'&body='+encodeURI(config['link']);
198                                navigateToURL(new URLRequest(url),'_self');
199                        }
200                }
201        };
202
203
204        /** Save link from new item. **/
205        private function itemHandler(evt:ControllerEvent):void {
206                config['title'] = encodeURI(view.playlist[view.config['item']]['title']);
207                if (view.playlist[view.config['item']]['link']) {
208                        config['link'] = view.playlist[view.config['item']]['link'];
209                        shareField.text = config['link'];
210                }
211        };
212
213
214        /** Grab the page link. **/
215        private function pageLink():void {
216                if(ExternalInterface.available && !config['link']) {
217                        try {
218                                config['link'] = ExternalInterface.call("function(){return window.location.href;}");
219                                shareField.text = config['link'];
220                        } catch (err:Error) {}
221                }
222        };
223
224
225        /** Resize the clip when on screen. **/
226        private function resizeHandler(evt:ControllerEvent):void {
227                back.width = view.config['width'];
228                back.height = view.config['height'];
229                embed.x = share.x = view.config['width']/2-150
230                embed.y = share.y = view.config['height']/2-65
231        };
232
233        /** Share icon is clicked; toggle the share screen. **/
234        private function shareHandler(evt:MouseEvent):void {
235                showHide('share');
236        };
237
238
239        /** Show or hide the display icons. **/
240        private function showHide(stt:String):void {
241                if(stt == showing && stt != 'none') {
242                        stt = 'none';
243                }
244                switch (stt) {
245                        case 'none':
246                                back.visible = false;
247                                embed.visible = false;
248                                share.visible = false;
249                                break;
250                        case 'embed':
251                                back.visible = true;
252                                embed.visible = true;
253                                share.visible = false;
254                                break;
255                        case 'share':
256                                back.visible = true;
257                                embed.visible = false;
258                                share.visible = true;
259                                break;
260                }
261                showing = stt;
262        };
263
264
265};
266
267
268}
Note: See TracBrowser for help on using the repository browser.