Skip to content

Commit

Permalink
Add Facebook support (App ID removed)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurenju authored and timdream committed May 3, 2011
1 parent 356b5f6 commit 7cc2eee
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
9 changes: 9 additions & 0 deletions en/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<canvas id="canvas" class="toggleUI"></canvas>
<div id="fb-root"></div>
<div id="loading" class="center_msg">
<p>Loading ...</p>
</div>
Expand Down Expand Up @@ -71,6 +72,7 @@ <h2>Text Source for Word Clouds</h2>
<!--<label><input type="radio" name="source" value="wiki" /> Wikipedia</label>-->
<label><input type="radio" name="source" value="blog:blogger" /> Blogger</label>
<label><input type="radio" name="source" value="blog:twitter"/> Twitter</label>
<label><input type="radio" name="source" value="fbok"/> Facebook</label>
</p>
<hr />
<div id="demo_entry" class="entry">
Expand Down Expand Up @@ -105,6 +107,9 @@ <h2>Text Source for Word Clouds</h2>
<p><label><span>URL:</span><input type="url" size="40" id="html_url" /></label></p>
<p>Only public webpages that has been indexed by search engines can be accessed.</p>
</div>
<div id="fbok_entry" class="entry">
<p>Loading...</p>
</div>
<button type="submit" id="source_panel_finish">Start</button>
</form>
</div>
Expand Down Expand Up @@ -136,6 +141,7 @@ <h2>About</h2>
<script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../wordfreq.js"></script>
<script type="text/javascript" src="../wordcloud.js"></script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">

window.Worker.iframeURI = '../worker.iframe.html';
Expand All @@ -149,6 +155,9 @@ <h2>About</h2>
analyzing: 'Analyzing ...',
errorReading: 'Reading error, please try again later.',
errorWordCount: 'Not enough word/phrases to show.',
clickToAnalyzer: 'Click Start to execute analyzer',
needLogin: '<a id="fb_login" href="#">Login facebook</a> to get your feed',
needWaiting: 'Get facebook feeds require half minute, ',
startList_1: 'HTML5 Word Cloud',
startList_1C: 45,
startList_2: '文字雲',
Expand Down
20 changes: 19 additions & 1 deletion jquery.getcontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ $.getContent = function (source, options) {
}
);
},
getFbText = function () {
settings.beforeComplete();
FB.api('/me/feed', {limit: 250}, function(response) {
var text = [];
response.data.forEach(
function (entry) {
if (entry.message != undefined && entry.from.id == source) {
text.push(entry.message);
}
}
);
text = text.join('\n');
settings.complete(text);
});
},
parseYQLElementObject = function (text, obj) {
// TBD, properly exclude script
for (var key in obj) if (obj.hasOwnProperty(key)) {
Expand Down Expand Up @@ -144,6 +159,9 @@ $.getContent = function (source, options) {
case 'file.html':
return getFileText(true);
break;
case 'fbok':
return getFbText();
break;
}
};

Expand All @@ -156,4 +174,4 @@ $.getContentSupported = (
&& $.getContent.fileSupported
);

})(jQuery);
})(jQuery);
73 changes: 71 additions & 2 deletions wordcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ function t(id) {
return s;
}

var FB_app_id = '';

jQuery(function ($) {
var $c = $('#canvas'),
$toggleUI = $('.toggleUI'),
$loading = $('#loading'),
$error = $('#error'),
$loadingText = $('#loading > p'),
$errorText = $('#error > p'),
fbUser = null,
wordfreq = WordFreq({worker: '../wordfreq.worker.js'}),
theme = [
{
Expand Down Expand Up @@ -225,7 +228,23 @@ jQuery(function ($) {
}
);
}
return;
break;
case 'fbok':
if(fbUser) {
updateTitle('Facebook', 'facebook');
changeUIState.loading(t('downloading'));
$.getContent(
fbUser.id,
{
type: 'fbok',
beforeComplete: processingFb,
complete: handleText
}
);
}
return;
break;
//default:
//do nothing
//break;
Expand All @@ -235,6 +254,13 @@ jQuery(function ($) {
}
}

FB.init({
appId : FB_app_id,
status: true,
cookie: true,
xfbml: true
});

window.onhashchange = handleHash;
if (window.location.hash) handleHash(); // only load when hash exists
else changeUIState.start(); // show start otherwise
Expand Down Expand Up @@ -281,6 +307,17 @@ jQuery(function ($) {
$(this).parent().addClass('checked').siblings().removeClass('checked');
$('#' + type + '_entry').show();
$('.feed_type_name').text($(this).parent('label').text());

if (type == "fbok") {
FB.getLoginStatus(function(response) {
if (response.session) {
getFbUser();
}
else {
showFbLogin();
}
});
}
};

$s.bind(
Expand Down Expand Up @@ -345,6 +382,10 @@ jQuery(function ($) {
if (!$('#html_url').val()) return false;
window.location.hash = '#html:' + $('#html_url').val();
break;
case 'fbok':
if (!fbUser) return false;
window.location.hash = '#fbok';
break;
}
return false;
}
Expand All @@ -363,7 +404,7 @@ jQuery(function ($) {
function updateTitle(type, title) {
$('#title')
.empty()
.append('<span class="famfamfam_sprite ' + {feed:'feed', html:'drive_world', file:'drive'}[type] + '"></span>')
.append('<span class="famfamfam_sprite ' + {feed:'feed', html:'drive_world', file:'drive', facebook:'facebook'}[type] + '"></span>')
.append($('<span />').text(t('title', title)));
}

Expand All @@ -377,6 +418,11 @@ jQuery(function ($) {
changeUIState.loading(t('processing'));
};

function processingFb(title) {
updateTitle('facebook', 'facebook');
changeUIState.loading(t('reading'));
};

function readingFile() {
changeUIState.loading(t('reading'));
};
Expand Down Expand Up @@ -427,6 +473,29 @@ jQuery(function ($) {
);
};

// Facebook functions

function getFbUser() {
FB.api('/me', function(response) {
$('#fbok_entry').html("<p>" + t('needWaiting') + t('clickToAnalyzer') + "</p>");
fbUser = response;
});
};

function showFbLogin() {
$('#fbok_entry').html("<p>" + t('needLogin') + "</p>");
$('#fb_login').click(function(event) {
FB.login(function(response) {
if (response.session) {
getFbUser();
}
else {
}
}, {perms:'read_stream'});
});
};


// panel functions

$('.smaller').bind(
Expand Down Expand Up @@ -484,4 +553,4 @@ jQuery(function ($) {
return false;
}
);
});
});
9 changes: 9 additions & 0 deletions zh/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<canvas id="canvas" class="toggleUI"></canvas>
<div id="fb-root"></div>
<div id="loading" class="center_msg">
<p>載入中 ...</p>
</div>
Expand Down Expand Up @@ -74,6 +75,7 @@ <h2>詞彙分析文字來源</h2>
<label><input type="radio" name="source" value="blog:wretch"/> 無名小站部落格</label>
<label><input type="radio" name="source" value="blog:plurk"/> 噗浪</label>
<label><input type="radio" name="source" value="blog:twitter"/> Twitter</label>
<label><input type="radio" name="source" value="fbok"/> Facebook</label>
</p>
<hr />
<div id="demo_entry" class="entry">
Expand Down Expand Up @@ -108,6 +110,9 @@ <h2>詞彙分析文字來源</h2>
<p><label><span>網址:</span><input type="url" size="40" id="html_url" /></label></p>
<p>僅能處理被搜尋引擎分析過的公開網頁。</p>
</div>
<div id="fbok_entry" class="entry">
<p>讀取中…</p>
</div>
<button type="submit" id="source_panel_finish">開始</button>
</form>
</div>
Expand Down Expand Up @@ -139,6 +144,7 @@ <h2>關於</h2>
<script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../wordfreq.js"></script>
<script type="text/javascript" src="../wordcloud.js"></script>
<script src="http://connect.facebook.net/zh_TW/all.js"></script>
<script type="text/javascript">

window.Worker.iframeURI = '../worker.iframe.html';
Expand All @@ -152,6 +158,9 @@ <h2>關於</h2>
analyzing: '分析中 ...',
errorReading: '讀取失敗,請稍後再試。',
errorWordCount: '取得詞彙數量太少,請選其他內容。',
clickToAnalyzer: '按『開始』分析內容',
needLogin: '<a id="fb_login" href="#">登入 facebook</a>取得分析內容',
needWaiting: '取得 Facebook 留言約費時半分鐘,',
startList_1: 'HTML5 文字雲',
startList_1C: 60,
startList_2: 'Word Cloud',
Expand Down

0 comments on commit 7cc2eee

Please sign in to comment.