GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Github flash badge
Homepage: http://spinach.andascarygoat.com/tags/badjo
Clone URL: git://github.com/zmack/badjo.git
zmack (author)
Sun May 04 06:16:39 -0700 2008
commit  4a9fa306c7620fbddafd2f7e6cc1dbe20fc78e7d
tree    f0d0abdc9538acb44414bbd0083ef215a5ca9e70
parent  02766cd496af4f22614c32df21f891c4331d11a5
badjo / src / GithubBadge.as
100644 138 lines (113 sloc) 3.718 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package {
  import flash.display.Loader;
  import flash.display.Sprite;
  import flash.display.DisplayObject;
  import flash.display.LoaderInfo;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.text.StyleSheet;
  import flash.net.URLRequest;
  import skins.GithubBadgeSkin;
  import dataexchange.*;
  import com.adobe.serialization.json.JSON;
  import com.adobe.crypto.MD5;
  import flash.external.ExternalInterface;  
 
  /*
  Data looks pretty much like this:
 
  {"user":
    {  "name": "Andrei Bocan",
      "repositories":
      [
        {"name": "badjo", "url": "http://github.com/zmack/badjo", "description": "Github flash badge ", "homepage": ""},
        {"name": "mephisto", "url": "http://github.com/zmack/mephisto", "description": "A mirror of the mephisto code-base", "homepage": "http://mephistoblog.com/"}
      ],
      "blog": "http://spinach.andascarygoat.com",
      "login": "zmack",
      "email": "zmaxor@gmail.com",
      "location": "Bucharest, Romania"
    }
  }
  */
 
  public class GithubBadge extends Sprite {
    private var _gw:Gateway;
    private var _pl:ProjectList;
    private var _user:String;
 
    public function GithubBadge() {
      var user:String;
      addExternalInterface();
      trace(stage.height + ' -- ' + stage.width);
 
      try {
        user = getParams().gitUser;
      } catch (e:Error) {
        user = 'evilchelu';
      }
 
      _gw = new Gateway();
      
      this.requestData(user || 'zmack');
    }
 
    private function addExternalInterface():void {
      if ( ExternalInterface.available ) {
        ExternalInterface.addCallback("requestData",requestData);
      }
    }
 
    private function getParams():Object {
      return LoaderInfo(this.loaderInfo).parameters;
    }
 
    private function onDataLoaded(e:GatewayEvent):void {
      trace(e);
    }
 
    private function displayProjectList(user:Object):void {
      if ( _pl != null ) _pl.parent.removeChild(_pl);
      _pl = new ProjectList()
 
      _pl.setHeader({image: SpriteWrapper(loadAvatar('http://www.gravatar.com/avatar/' + MD5.hash(user.email || '') + '?s=40')), text: user.name || user.login });
      user.repositories.forEach( function(repo:Object, index:uint, arr:Array):void {
        _pl.addButton({
          image: SpriteWrapper(new GithubBadgeSkin.PublicProject()),
          text: repo.name,
          extended_content: SpriteWrapper(this.createTextField(repo.description))
        });
      }, this);
 
      addChild(_pl);
    }
 
    private function displayError():void {
      _pl.addChild(SpriteWrapper(new GithubBadgeSkin.OctocatImage()));
    }
 
    private function requestData(user:String):void {
      _user = user;
      _gw.getUserInfo(user);
      _gw.addEventListener(GatewayEvent.DATA_RECEIVED, dataLoaded);
    }
 
    private function dataLoaded(e:GatewayEvent):void {
      if ( e.succeeded ) {
        displayProjectList(JSON.decode(e.data).user);
      } else {
        displayError();
      }
    }
 
    private function SpriteWrapper(d:DisplayObject):Sprite {
      var s:Sprite = new Sprite();
      s.addChild(d);
 
      return s;
    }
 
    private function loadAvatar(url:String):DisplayObject {
      var request:URLRequest = new URLRequest(url);
      var loader:Loader = new Loader();
      loader.load(request);
      return loader;
    }
 
    private function createStyleSheet():StyleSheet {
      var style:StyleSheet = new StyleSheet();
 
      style.parseCSS('p { font-family: "Trebuchet MS"; font-size: 10px; color: #000000; background-color: #FF00FF; }');
      return style;
    }
 
    private function createTextField(text:String):TextField {
      var textField:TextField = new TextField();
      
      textField.width = 200;
      textField.height = 0;
      textField.autoSize = TextFieldAutoSize.LEFT;
      textField.styleSheet = this.createStyleSheet();
      textField.selectable = false;
      textField.htmlText = '<p>' + text + '</p>';
 
      return textField;
    }
  }
}