2013-08-29 16:26:11 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-09-01 01:41:49 +04:00
|
|
|
/**
|
2013-09-06 01:12:52 +04:00
|
|
|
* 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.
|
2013-09-01 01:41:49 +04:00
|
|
|
*
|
|
|
|
* You may use this on any <div></div>
|
|
|
|
* Here I'm using <div class="avatardiv"></div> as an example.
|
|
|
|
*
|
2013-10-04 04:27:53 +04:00
|
|
|
* There are 5 ways to call this:
|
2013-09-01 01:41:49 +04:00
|
|
|
*
|
|
|
|
* 1. $('.avatardiv').avatar('jdoe', 128);
|
2013-09-06 01:12:52 +04:00
|
|
|
* This will make the div to jdoe's fitting avatar, with a size of 128px.
|
2013-09-01 01:41:49 +04:00
|
|
|
*
|
|
|
|
* 2. $('.avatardiv').avatar('jdoe');
|
2016-04-04 11:57:17 +03:00
|
|
|
* This will make the div to jdoe's fitting avatar. If the div already has a
|
2013-09-01 01:41:49 +04:00
|
|
|
* height, it will be used for the avatars size. Otherwise this plugin will
|
2013-09-06 01:12:52 +04:00
|
|
|
* search for 'size' DOM data, to use for avatar size. If neither are available
|
|
|
|
* it will default to 64px.
|
2013-09-01 01:41:49 +04:00
|
|
|
*
|
|
|
|
* 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
|
2016-11-22 19:08:28 +03:00
|
|
|
* "?". The size will be determined the same way, as the second example.
|
2013-09-03 20:34:40 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2013-10-04 04:27:53 +04:00
|
|
|
*
|
|
|
|
* 5. $('.avatardiv').avatar('jdoe', 128, undefined, true);
|
|
|
|
* This will behave like the first example, but it will hide the avatardiv, if
|
|
|
|
* it will display the default placeholder. undefined is the ie8fix from
|
|
|
|
* example 4 and can be either true, or false/undefined, to be ignored.
|
2014-02-20 16:37:23 +04:00
|
|
|
*
|
|
|
|
* 6. $('.avatardiv').avatar('jdoe', 128, undefined, true, callback);
|
|
|
|
* This will behave like the above example, but it will call the function
|
|
|
|
* defined in callback after the avatar is placed into the DOM.
|
|
|
|
*
|
2013-09-01 01:41:49 +04:00
|
|
|
*/
|
|
|
|
|
2013-08-29 16:26:11 +04:00
|
|
|
(function ($) {
|
2015-12-04 12:42:11 +03:00
|
|
|
$.fn.avatar = function(user, size, ie8fix, hidedefault, callback, displayname) {
|
2017-09-20 19:17:08 +03:00
|
|
|
var setAvatarForUnknownUser = function(target) {
|
|
|
|
target.imageplaceholder('?');
|
|
|
|
target.css('background-color', '#b9b9b9');
|
|
|
|
};
|
|
|
|
|
2017-03-29 23:54:08 +03:00
|
|
|
if (typeof(user) !== 'undefined') {
|
|
|
|
user = String(user);
|
|
|
|
}
|
|
|
|
if (typeof(displayname) !== 'undefined') {
|
|
|
|
displayname = String(displayname);
|
|
|
|
}
|
2017-03-29 14:39:48 +03:00
|
|
|
|
2013-08-30 11:00:37 +04:00
|
|
|
if (typeof(size) === 'undefined') {
|
|
|
|
if (this.height() > 0) {
|
|
|
|
size = this.height();
|
2013-08-31 20:27:28 +04:00
|
|
|
} else if (this.data('size') > 0) {
|
|
|
|
size = this.data('size');
|
2013-08-30 11:00:37 +04:00
|
|
|
} else {
|
|
|
|
size = 64;
|
|
|
|
}
|
2013-08-29 16:26:11 +04:00
|
|
|
}
|
|
|
|
|
2013-08-30 11:00:37 +04:00
|
|
|
this.height(size);
|
|
|
|
this.width(size);
|
2013-08-29 16:26:11 +04:00
|
|
|
|
|
|
|
if (typeof(user) === 'undefined') {
|
2013-08-31 20:27:28 +04:00
|
|
|
if (typeof(this.data('user')) !== 'undefined') {
|
|
|
|
user = this.data('user');
|
|
|
|
} else {
|
2017-09-20 19:17:08 +03:00
|
|
|
setAvatarForUnknownUser(this);
|
2013-08-31 20:27:28 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-29 16:26:11 +04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:27:28 +04:00
|
|
|
// sanitize
|
2013-10-26 00:00:32 +04:00
|
|
|
user = String(user).replace(/\//g,'');
|
2013-08-31 20:27:28 +04:00
|
|
|
|
2013-08-29 16:26:11 +04:00
|
|
|
var $div = this;
|
2016-08-29 15:55:23 +03:00
|
|
|
var url;
|
2013-08-29 16:26:11 +04:00
|
|
|
|
2016-08-29 15:55:23 +03:00
|
|
|
// If this is our own avatar we have to use the version attribute
|
|
|
|
if (user === OC.getCurrentUser().uid) {
|
|
|
|
url = OC.generateUrl(
|
|
|
|
'/avatar/{user}/{size}?v={version}',
|
|
|
|
{
|
|
|
|
user: user,
|
|
|
|
size: Math.ceil(size * window.devicePixelRatio),
|
|
|
|
version: oc_userconfig.avatar.version
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
url = OC.generateUrl(
|
|
|
|
'/avatar/{user}/{size}',
|
|
|
|
{
|
|
|
|
user: user,
|
|
|
|
size: Math.ceil(size * window.devicePixelRatio)
|
|
|
|
});
|
|
|
|
}
|
2014-03-03 01:30:24 +04:00
|
|
|
|
2017-12-16 16:13:22 +03:00
|
|
|
var img = new Image();
|
2015-12-04 12:42:11 +03:00
|
|
|
|
2017-12-16 16:13:22 +03:00
|
|
|
// If the new image loads successfully set it.
|
|
|
|
img.onload = function() {
|
|
|
|
$div.clearimageplaceholder();
|
2018-01-05 13:47:03 +03:00
|
|
|
$div.append(img);
|
2017-12-14 12:10:11 +03:00
|
|
|
|
2017-12-16 16:13:22 +03:00
|
|
|
if(typeof callback === 'function') {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Fallback when avatar loading fails:
|
|
|
|
// Use old placeholder when a displayname attribute is defined,
|
|
|
|
// otherwise show the unknown user placeholder.
|
|
|
|
img.onerror = function () {
|
|
|
|
$div.clearimageplaceholder();
|
|
|
|
if (typeof(displayname) !== 'undefined') {
|
|
|
|
$div.imageplaceholder(user, displayname);
|
|
|
|
} else {
|
|
|
|
setAvatarForUnknownUser($div);
|
|
|
|
}
|
2017-12-19 11:10:13 +03:00
|
|
|
|
|
|
|
if(typeof callback === 'function') {
|
|
|
|
callback();
|
|
|
|
}
|
2017-12-16 16:13:22 +03:00
|
|
|
};
|
2015-12-04 12:42:11 +03:00
|
|
|
|
2018-07-25 15:26:27 +03:00
|
|
|
if (size < 32) {
|
|
|
|
$div.addClass('icon-loading-small');
|
|
|
|
} else {
|
|
|
|
$div.addClass('icon-loading');
|
|
|
|
}
|
2017-12-16 16:13:22 +03:00
|
|
|
img.width = size;
|
|
|
|
img.height = size;
|
|
|
|
img.src = url;
|
2018-08-26 19:29:02 +03:00
|
|
|
img.alt = '';
|
2013-08-31 21:05:53 +04:00
|
|
|
};
|
2013-08-29 16:26:11 +04:00
|
|
|
}(jQuery));
|