summaryrefslogtreecommitdiffstats
path: root/core/js/jquery.avatar.js
blob: dc73d8f0d91b3099d32f927704267b7e3adac8dd (plain)
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
/**
 * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

/**
 * This plugins inserts the right avatar for the user, depending on, whether
 * he has a custom uploaded avatar, or not and show a placeholder with the
 * first letter of the users displayname instead.
 * For this it asks the core_avatar_get route, thus this plugin is fit very
 * tightly fitted for owncloud. It may not work anywhere else.
 *
 * You may use this on any <div></div>
 * Here I'm using <div class="avatardiv"></div> as an example.
 *
 * There are 3 ways to call this:
 *
 * 1. $('.avatardiv').avatar('jdoe', 128);
 * This will make the div to jdoe's fitting avatar, with the size of 128px.
 *
 * 2. $('.avatardiv').avatar('jdoe');
 * This will make the div to jdoe's fitting avatar. If the div aready has a
 * height, it will be used for the avatars size. Otherwise this plugin will
 * search for 'size' DOM data, to use it for avatar size. If neither are
 * available it will default to 64px.
 *
 * 3. $('.avatardiv').avatar();
 * This will search the DOM for 'user' data, to use as the username. If there
 * is no username available it will default to a placeholder with the value of
 * "x". The size will be determined the same way, as the second example did.
 */

(function ($) {
	$.fn.avatar = function(user, size) {
		if (typeof(size) === 'undefined') {
			if (this.height() > 0) {
				size = this.height();
			} else if (this.data('size') > 0) {
				size = this.data('size');
			} else {
				size = 64;
			}
		}

		this.height(size);
		this.width(size);

		if (typeof(user) === 'undefined') {
			if (typeof(this.data('user')) !== 'undefined') {
				user = this.data('user');
			} else {
				this.placeholder('x');
				return;
			}
		}

		// sanitize
		user = user.replace(/\//g,'');

		var $div = this;

		OC.Router.registerLoadedCallback(function() {
			var url = OC.Router.generate('core_avatar_get', {user: user, size: size});
			$.get(url, function(result) {
				if (typeof(result) === 'object') {
					$div.placeholder(result.user);
				} else {
					$div.html('<img src="'+url+'">');
				}
			});
		});
	};
}(jQuery));