-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfbconnect.js
More file actions
196 lines (151 loc) · 4.89 KB
/
fbconnect.js
File metadata and controls
196 lines (151 loc) · 4.89 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var FBConnect = {
/*
* If this value is true alert boxes will be raised when various
* types of configuration errors are detected.
*/
debugVerbose : (window.location.href.indexOf('fbc_verbose_debug') != -1),
initialized : false,
init : function(api_key, plugin_path,
home_url, wp_user, app_config) {
if (!api_key) {
FBConnect.error("api_key is not set");
}
if (!plugin_path) {
FBConnect.error("plugin path not provided");
}
// Check for properly configured template - note this test fails in IE!
if (this.debugVerbose) {
var html_tag = document.getElementsByTagName('html').item(0);
if (html_tag.getAttribute('xmlns:fb') === null) {
FBConnect.error('xmlns:fb not defined on html tag - check your templates');
}
}
FBConnect.home_url = home_url || "/";
FBConnect.plugin_path = plugin_path;
FBConnect.wp_user = wp_user;
FB.init(api_key, plugin_path + "xd_receiver.php", app_config);
FBConnect.initialized = true;
},
appconfig_reload : {
reloadIfSessionStateChanged: true
},
appconfig_none : {},
appconfig_ajaxy : {
ifUserConnected : fbc_onlogin_noauto,
ifUserNotConnected : fbc_onlogout_noauto
},
logout : function() {
FB.ensureInit(function() {
FB.Connect.logout();
});
},
redirect_home : function() {
window.location = FBConnect.home_url;
},
/*
wordpress specific functions
*/
setup_feedform : function() {
var comment_form = ge('commentform');
if (!comment_form) {
FBConnect.error('unable to locate id=commentform');
return;
}
var elem = comment_form.getElementsByTagName("a")[1];
elem.setAttribute("href","#");
elem.setAttribute("onclick", "FBConnect.logout(); return false");
return;
var orig_submit = ge('submit');
if (orig_submit && orig_submit.getAttribute('name') === 'submit') {
/* This is a bit of a hack. The default theme gives the submit
button an id and name of "submit". This causes it to overwrite
the .submit() function on the form. The solution is to delete
the submit button and recreate it with a different id. See
http://jibbering.com/faq/names/ for more info on why this is
bad. */
var subbutton = document.createElement('input');
subbutton.setAttribute('name', 'fbc_submit_hack');
subbutton.setAttribute('type', 'submit');
// restore the origional name
subbutton.setAttribute('value', 'Submit Comment');
comment_form.appendChild(subbutton);
orig_submit.parentNode.replaceChild(subbutton, orig_submit);
}
/*
* This is pretty terrible but I don't have a better option for
* detecting a callable on IE since typeof(comment_form.submit)
* returns object and 'call' in comment_form.submit is false.
*/
if (comment_form.submit.nodeType) {
FBConnect.error('unable to find .submit() on commentform');
return;
}
comment_form.onsubmit = function() {
return FBConnect.show_comment_feedform();
}
},
show_comment_feedform : function() {
var comment_text = '';
var commentform = ge('commentform');
if (commentform) {
// if this isn't present something is seriously wrong
var comment_box = commentform.comment;
if (comment_box) {
comment_text = comment_box.value;
} else {
FBConnect.error('unable to locate comment textarea');
return true;
}
} else {
FBConnect.error('unable to locate comment form, expected id=commentform');
return true;
}
if (comment_text.length === 0) {
// allow normal submit to complete
return true;
}
FB.Connect.streamPublish(
comment_text, // user_message
{
name: FBConnect.article_title,
href: window.location.href,
caption: 'A post on the blog ' + FBConnect.blog_name,
description: "\"" + FBConnect.excerpt + "\""
}, // attachment
null, // action links
null, // target_id
'My Comment', // prompt
function() {
// unconditional
commentform.submit();
}
);
// submit handled by streamPublish
return false;
},
error : function(msg) {
if (FBConnect.debugVerbose) {
var emsg = 'Error: ' + msg;
alert(emsg);
}
}
};
// end FBConnect
function fbc_onlogout_noauto() {
fbc_set_visibility_by_class('fbc_hide_on_login', '');
fbc_set_visibility_by_class('fbc_hide_on_logout', 'none');
}
function fbc_onlogin_noauto() {
fbc_set_visibility_by_class('fbc_hide_on_login', 'none');
fbc_set_visibility_by_class('fbc_hide_on_logout', '');
FBConnect.setup_feedform();
}
function fbc_set_visibility_by_class(cls, vis) {
var res = document.getElementsByClassName(cls);
for(var i = 0; i < res.length; ++i) {
res[i].style.visibility = vis;
}
}
function ge(elem) {
return document.getElementById(elem);
}