diff options
author | Bernhard Posselt <Raydiation@users.noreply.github.com> | 2013-09-14 16:00:36 -0700 |
---|---|---|
committer | Bernhard Posselt <Raydiation@users.noreply.github.com> | 2013-09-14 16:00:36 -0700 |
commit | a58e176852d80dd23b9346b4f8bd676e9ca4d8ed (patch) | |
tree | 3f06e0ab511159221e915a6fdce8393b6192bfdb /core/js | |
parent | a0c2c2473a975893d590e779433091a3f7d6abb2 (diff) | |
parent | 86143beb6ae264e31e806dd195a04e5ed6f9f2d1 (diff) | |
download | nextcloud-server-a58e176852d80dd23b9346b4f8bd676e9ca4d8ed.tar.gz nextcloud-server-a58e176852d80dd23b9346b4f8bd676e9ca4d8ed.zip |
Merge pull request #4506 from owncloud/oc_avatars
OC Avatars
Diffstat (limited to 'core/js')
-rw-r--r-- | core/js/avatar.js | 9 | ||||
-rw-r--r-- | core/js/jquery.avatar.js | 83 |
2 files changed, 92 insertions, 0 deletions
diff --git a/core/js/avatar.js b/core/js/avatar.js new file mode 100644 index 00000000000..410182f01bf --- /dev/null +++ b/core/js/avatar.js @@ -0,0 +1,9 @@ +$(document).ready(function(){ + $('#header .avatardiv').avatar(OC.currentUser, 32); + // Personal settings + $('#avatar .avatardiv').avatar(OC.currentUser, 128); + // User settings + $.each($('td.avatar .avatardiv'), function(i, element) { + $(element).avatar($(element).parent().parent().data('uid'), 32); + }); +}); diff --git a/core/js/jquery.avatar.js b/core/js/jquery.avatar.js new file mode 100644 index 00000000000..f1382fd7d2d --- /dev/null +++ b/core/js/jquery.avatar.js @@ -0,0 +1,83 @@ +/** + * 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 plugin inserts the right avatar for the user, depending on, whether a + * custom avatar is uploaded - which it uses then - or not, and display a + * placeholder with the first letter of the users name instead. + * For this it queries the core_avatar_get route, thus this plugin is fit very + * tightly for owncloud, and 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 4 ways to call this: + * + * 1. $('.avatardiv').avatar('jdoe', 128); + * This will make the div to jdoe's fitting avatar, with a 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 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. + * + * 4. $('.avatardiv').avatar('jdoe', 128, true); + * This will behave like the first example, except it will also append random + * hashes to the custom avatar images, to force image reloading in IE8. + */ + +(function ($) { + $.fn.avatar = function(user, size, ie8fix) { + 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})+'?requesttoken='+oc_requesttoken; + $.get(url, function(result) { + if (typeof(result) === 'object') { + $div.placeholder(user); + } else { + if (ie8fix === true) { + $div.html('<img src="'+url+'#'+Math.floor(Math.random()*1000)+'">'); + } else { + $div.html('<img src="'+url+'">'); + } + } + }); + }); + }; +}(jQuery)); |