Caged / gitnub

A Gitk-like application written in RubyCocoa that looks like it belongs on a Mac. See the wiki for downloads and screenshots.

This URL has Read+Write access

gitnub / NetworkController.rb
100644 108 lines (94 sloc) 3.14 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
#
# NetworkController.rb
# GitNub
#
# Created by Justin Palmer on 6/5/08.
# Copyright (c) 2008 Active Reload, LLC. All rights reserved.
#
 
require 'osx/cocoa'
 
class NetworkController < OSX::NSObject
  ib_outlet :application_controller
  ib_outlet :network_view
  
  def awakeFromNib
    if @application_controller.repo
      @github_user = @application_controller.repo.config['github.user']
      @github_repo = @application_controller.repo.config['github.repo']
      setup_network_visualization_view
    end
  end
  
  def has_github_credentials?
     @github_user && @github_repo
  end
  
  def setup_network_visualization_view
    github_network_url = NSURL.URLWithString("http://github.com/#{@github_user}/#{@github_repo}/network")
    @network_view.setFrameLoadDelegate(self)
    if has_github_credentials?
      @network_view.mainFrame.loadRequest(NSURLRequest.requestWithURL(github_network_url))
    else
      @network_view.mainFrame.loadHTMLString_baseURL(%(
<html>
<head>
<style type="text/css">
body {
font: normal 12px/1.5em "Helvetica Neue", Helvetica, Arial, sans-serif;
color:#333;
}
</style>
</head>
<body style="padding:30px">
<h2>If this is a Github repository you can set your credentials to view your network</h2>
<pre>
git config github.user REPO_USER
git config githubV.repo REPO_NAME
</pre>
</body>
</html>
), nil)
    end
  end
 
  def webView_didFinishLoadForFrame(view, frame)
    @document = @network_view.mainFrame.DOMDocument
    setup_document if has_github_credentials?
  end
  
  def setup_document
    unless @github_user.nil? || @github_repo.nil?
      hide_github_shell
      setup_network_viewer
      replace_github_header
    end
  end
  
  def setup_network_viewer
    network = @document.getElementById('network')
    network.setAttribute_value('style', "margin-top:60px")
    h2 = @document.getElementsByTagName('h2').item(0)
    p1 = @document.getElementById('network').children.item(1)
    p2 = @document.getElementById('network').children.item(2)
    
    [h2, p1, p2].each do |element|
      element.style.setProperty_value_priority("display", "none", nil)
    end
  end
  
  def replace_github_header
    header = @document.createElement('div')
    old_header = @document.getElementById('header')
  
    style = %(
font-size: 150%;
line-height: 130%;
color: #222;
text-align:left;
padding:9px;
font-family:"Helvetica Neue" !important;
font-weight:bold;
border-bottom:1px solid #ccc;
)
  
    header.setAttribute_value('style', style)
    header.setInnerHTML(%(Github Network))
    old_header.parentNode.insertBefore_refChild(header, old_header)
  end
  
  # Because we're only interested in the network visualizer
  def hide_github_shell
    %w(header repo_menu repo_sub_menu repos footer triangle).each do |element|
      element = @document.getElementById(element)
      element.style.setProperty_value_priority("display", "none", nil)
    end
  end
end