-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.mtgreeting.js
193 lines (192 loc) · 7.85 KB
/
jquery.mtgreeting.js
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
(function($){
/*
* MT Greeting Plugin for jQuery v.0.9.2
*
* Copyright (c) 2009-2010 Byrne Reese (endevver.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
*/
$.fn.greet = function(options) {
var defaults = {
/* Messages */
loggedInMessage: 'Welcome, %p! %o',
loggedOutMessage: '%i or %r',
noPermissionMessage: 'Welcome, %p! %o',
loginText: 'Sign In',
logoutText: 'Sign Out',
registerText: 'Sign Up',
editProfileText: '%u',
indicator: mt.blog.staticWebPath + 'images/indicator.white.gif',
linkClass: 'button',
loginClass: 'login',
logoutClass: 'logout',
registerClass: 'register',
mode: 'mtos',
greetingContainerTag: 'div',
isPreview: false,
ajaxLogin: true,
returnToURL: null,
/* Events and Callbacks */
onSignInClick: function(e){ return signInClickHandler( $(e) ); },
onSignOutClick: function(e){ return signOutClickHandler( $(e) ); },
onSignUpClick: function(e){ return signUpClickHandler( $(e) ); }
};
var self;
var settings = $.extend( {}, defaults, options);
return this.each(function() {
obj = $(this);
self = obj;
if (jQuery.needsAuth) {
$(this).onauthchange( function(e, u) {
_insertText( $(this) );
});
} else {
_insertText( $(this) );
$(this).onauthchange( function() { _insertText( obj ); return false; });
}
});
function _insertText(obj) {
var phrase = compileGreetingText();
obj.empty().append( jQuery("<" + settings.greetingContainerTag +">" + phrase + "</" + settings.greetingContainerTag +">") );
obj.children().children('a.'+settings.loginClass).click( function() { return settings.onSignInClick($(this)); });
obj.children().children('a.'+settings.logoutClass).click( function() { return settings.onSignOutClick.call($(this)); });
obj.children().children('a.'+settings.registerClass).click( function() { return settings.onSignUpClick.call($(this)); });
};
function _signIn() {
var url = mt.links.signIn;
if (url.match(/\?/)) { url += '&'; } else { url += '?'; }
if (settings.isPreview) {
if ( mt.entry && mt.entry.id ) {
url += 'entry_id=' + mt.entry.id;
} else {
url += 'return_url=' + settings.returnToURL;
}
} else {
var doc_url = document.URL;
doc_url = doc_url.replace(/#.+/, '');
url += 'return_url=' + encodeURIComponent(doc_url);
}
$.fn.movabletype.clearUser();
location.href = url;
};
// To make a static function:
// * pass in returnToURL
// * pass in isPreview
function signOutClickHandler(e) {
$.fn.movabletype.clearUser();
var doc_url = document.URL;
doc_url = doc_url.replace(/#.+/, '');
// TODO - error check: signouturl not set?
var url = mt.links.signOut;
if (url.match(/\?/)) { url += '&'; } else { url += '?'; }
if (settings.isPreview) {
if ( mt.entry && mt.entry.id ) {
url += 'entry_id=' + mt.entry.id;
} else {
url += 'return_url=' + settings.returnToURL;
}
} else {
if (settings.returnToURL) {
url += 'return_url=' + settings.returnToURL;
} else {
url += 'return_url=' + encodeURIComponent(doc_url);
}
}
location.href = url;
return false;
};
// To make a static function:
// * pass in returnToURL
// * pass in isPreview
function signUpClickHandler(e) {
$.fn.movabletype.clearUser();
var doc_url = document.URL;
doc_url = doc_url.replace(/#.+/, '');
// TODO - error check: signupurl not set?
var url = mt.links.signUp;
if (url.match(/\?/)) { url += '&'; } else { url += '?'; }
if (settings.isPreview) {
if ( mt.entry && mt.entry.id ) {
url += 'entry_id=' + mt.entry.id;
} else {
url += 'return_url=' + settings.returnToURL;
}
} else {
url += 'return_url=' + encodeURIComponent(doc_url);
}
location.href = url;
return false;
};
function signInClickHandler(e) {
if (settings.ajaxLogin) {
var phrase = 'Signing in... <img src="'+settings.indicator+'" height="16" width="16" alt="" />';
var target = e.parent().parent();
target.html(phrase);
$.fn.movabletype.clearUser(); // clear any 'anonymous' user cookie to allow sign in
$.fn.movabletype.fetchUser(function(u) {
if (u && u.is_authenticated) {
$.fn.movabletype.setUser(u);
_insertText( $(target) );
} else {
// user really isn't logged in; so let's do this!
_signIn();
}
});
} else {
_signIn();
}
return false;
};
function compileGreetingText() {
var phrase;
var u = $.fn.movabletype.getUser();
var profile_link;
if ( u && u.is_authenticated ) {
if ( u.is_banned ) {
phrase = settings.noPermissionText;
} else {
if ( u.is_author ) {
if (mt.links.editProfile) {
profile_link = '<a href="'+mt.links.editProfile;
} else if (settings.mode == "mtpro") {
profile_link = '<a href="'+mt.blog.community.script+'?__mode=edit&blog_id=' + mt.blog.id;
} else {
profile_link = '<a href="'+mt.blog.comments.script+'?__mode=edit_profile&blog_id=' + mt.blog.id;
}
if (mt.entry && mt.entry.id)
profile_link += '&entry_id=' + mt.entry.id;
if (settings.returnToURL)
profile_link += '&return_to=' + encodeURI(settings.returnToURL);
profile_link += '">' + settings.editProfileText + '</a>';
} else {
// registered user, but not a user with posting rights
if (u.url)
profile_link = '<a href="' + u.url + '">' + u.name + '</a>';
else
profile_link = u.name;
}
phrase = settings.loggedInMessage;
}
} else {
// TODO - this obviously does that same thing.
if (mt.blog.registration.required) {
phrase = settings.loggedOutMessage;
} else {
phrase = settings.loggedOutMessage;
}
}
var login_link = '<a class="'+settings.loginClass+' '+settings.linkClass+'" href="'+mt.links.signIn+'">' + settings.loginText + '</a>';
var logout_link = '<a class="'+settings.logoutClass+' '+settings.linkClass+'" href="'+mt.links.signOut+'">' + settings.logoutText + '</a>';
var register_link = '<a class="'+settings.registerClass+' '+settings.linkClass+'" href="'+mt.links.signUp+'">' + settings.registerText + '</a>';
phrase = phrase.replace(/\%p/,profile_link);
phrase = phrase.replace(/\%i/,login_link);
phrase = phrase.replace(/\%o/,logout_link);
phrase = phrase.replace(/\%r/,register_link);
if ( u && u.is_authenticated ) {
phrase = phrase.replace(/\%u/,u.name);
}
return phrase;
};
};
})(jQuery);