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 07:10:07 -0700 2008
commit  684dbc312be55e83d39966db83a793be04bbf1e0
tree    68242330872baa45dcb641f71109f3f313ce3d11
parent  4a9fa306c7620fbddafd2f7e6cc1dbe20fc78e7d
badjo / src / GithubBadge.as
100644 142 lines (116 sloc) 3.904 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
139
140
141
142
package {
  import flash.display.Loader;
  import flash.display.Sprite;
  import flash.display.DisplayObject;
  import flash.display.LoaderInfo;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  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;
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
 
      addExternalInterface();
 
      try {
        user = getParams().gitUser;
      } catch (e:Error) {
        user = 'evilchelu';
      }
 
      _gw = new Gateway();
      
      this.requestData(user || 'defunkt');
    }
 
    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({ width: stage.stageWidth, height: stage.stageHeight })
 
      _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 || 'No 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;
    }
  }
}