forked from benoitc/couchapp
/
jquery.couchProfile.js
85 lines (80 loc) · 3.66 KB
/
jquery.couchProfile.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
// Copyright Chris Anderson 2011
// Apache 2.0 License
// jquery.couchProfile.js
// depends on md5,
// jquery.couchLogin.js and requires.js
//
// Example Usage (loggedIn and loggedOut callbacks are optional):
// $("#myprofilediv").couchProfile({
// profileReady : function(profile) {
// alert("hello, do you look like this? "+profile.gravatar_url);
// }
// });
(function($) {
$.couchProfile = {};
$.couchProfile.templates = {
profileReady : '<div class="avatar">{{#gravatar_url}}<img src="{{gravatar_url}}"/>{{/gravatar_url}}<div class="name">{{nickname}}</div></div><p>Hello {{nickname}}!</p><div style="clear:left;"></div>',
newProfile : '<form><p>Hello {{name}}, Please setup your user profile.</p><label for="nickname">Nickname <input type="text" name="nickname" value=""></label><label for="email">Email (<em>for <a href="http://gravatar.com">Gravatar</a></em>) <input type="text" name="email" value=""></label><label for="url">URL <input type="text" name="url" value=""></label><input type="submit" value="Go →"><input type="hidden" name="userCtxName" value="{{name}}" id="userCtxName"></form>'
};
$.fn.couchProfile = function(session, opts) {
opts = opts || {};
var templates = $.couchProfile.templates;
var userCtx = session.userCtx;
var widget = $(this);
// load the profile from the user doc
var db = $.couch.db(session.info.authentication_db);
var userDocId = "org.couchdb.user:"+userCtx.name;
db.openDoc(userDocId, {
success : function(userDoc) {
var profile = userDoc["couch.app.profile"];
if (profile) {
profile.name = userDoc.name;
profileReady(profile);
} else {
newProfile(userCtx)
}
}
});
function profileReady(profile) {
widget.html($.mustache(templates.profileReady, profile));
if (opts.profileReady) {opts.profileReady(profile)};
};
function storeProfileOnUserDoc(newProfile) {
// store the user profile on the user account document
$.couch.userDb(function(db) {
var userDocId = "org.couchdb.user:"+userCtx.name;
db.openDoc(userDocId, {
success : function(userDoc) {
userDoc["couch.app.profile"] = newProfile;
db.saveDoc(userDoc, {
success : function() {
newProfile.name = userDoc.name;
profileReady(newProfile);
}
});
}
});
});
};
function newProfile(userCtx) {
widget.html($.mustache(templates.newProfile, userCtx));
widget.find("form").submit(function(e) {
e.preventDefault();
var form = this;
var name = $("input[name=userCtxName]",form).val();
var newProfile = {
rand : Math.random().toString(),
nickname : $("input[name=nickname]",form).val(),
email : $("input[name=email]",form).val(),
url : $("input[name=url]",form).val()
};
// setup gravatar_url if md5.js is loaded
if (hex_md5) {
newProfile.gravatar_url = 'http://www.gravatar.com/avatar/'+hex_md5(newProfile.email || newProfile.rand)+'.jpg?s=40&d=identicon';
}
storeProfileOnUserDoc(newProfile);
return false;
});
};
}
})(jQuery);