jquery.avatar.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  3. * This file is licensed under the Affero General Public License version 3 or
  4. * later.
  5. * See the COPYING-README file.
  6. */
  7. /**
  8. * This plugin inserts the right avatar for the user, depending on, whether a
  9. * custom avatar is uploaded - which it uses then - or not, and display a
  10. * placeholder with the first letter of the users name instead.
  11. * For this it queries the core_avatar_get route, thus this plugin is fit very
  12. * tightly for owncloud, and it may not work anywhere else.
  13. *
  14. * You may use this on any <div></div>
  15. * Here I'm using <div class="avatardiv"></div> as an example.
  16. *
  17. * There are 5 ways to call this:
  18. *
  19. * 1. $('.avatardiv').avatar('jdoe', 128);
  20. * This will make the div to jdoe's fitting avatar, with a size of 128px.
  21. *
  22. * 2. $('.avatardiv').avatar('jdoe');
  23. * This will make the div to jdoe's fitting avatar. If the div already has a
  24. * height, it will be used for the avatars size. Otherwise this plugin will
  25. * search for 'size' DOM data, to use for avatar size. If neither are available
  26. * it will default to 64px.
  27. *
  28. * 3. $('.avatardiv').avatar();
  29. * This will search the DOM for 'user' data, to use as the username. If there
  30. * is no username available it will default to a placeholder with the value of
  31. * "?". The size will be determined the same way, as the second example.
  32. *
  33. * 4. $('.avatardiv').avatar('jdoe', 128, true);
  34. * This will behave like the first example, except it will also append random
  35. * hashes to the custom avatar images, to force image reloading in IE8.
  36. *
  37. * 5. $('.avatardiv').avatar('jdoe', 128, undefined, true);
  38. * This will behave like the first example, but it will hide the avatardiv, if
  39. * it will display the default placeholder. undefined is the ie8fix from
  40. * example 4 and can be either true, or false/undefined, to be ignored.
  41. *
  42. * 6. $('.avatardiv').avatar('jdoe', 128, undefined, true, callback);
  43. * This will behave like the above example, but it will call the function
  44. * defined in callback after the avatar is placed into the DOM.
  45. *
  46. */
  47. (function ($) {
  48. $.fn.avatar = function(user, size, ie8fix, hidedefault, callback, displayname) {
  49. var setAvatarForUnknownUser = function(target) {
  50. target.imageplaceholder('?');
  51. target.css('background-color', '#b9b9b9');
  52. };
  53. if (typeof(user) !== 'undefined') {
  54. user = String(user);
  55. }
  56. if (typeof(displayname) !== 'undefined') {
  57. displayname = String(displayname);
  58. }
  59. if (typeof(size) === 'undefined') {
  60. if (this.height() > 0) {
  61. size = this.height();
  62. } else if (this.data('size') > 0) {
  63. size = this.data('size');
  64. } else {
  65. size = 64;
  66. }
  67. }
  68. this.height(size);
  69. this.width(size);
  70. if (typeof(user) === 'undefined') {
  71. if (typeof(this.data('user')) !== 'undefined') {
  72. user = this.data('user');
  73. } else {
  74. setAvatarForUnknownUser(this);
  75. return;
  76. }
  77. }
  78. // sanitize
  79. user = String(user).replace(/\//g,'');
  80. var $div = this;
  81. var url;
  82. // If this is our own avatar we have to use the version attribute
  83. if (user === OC.getCurrentUser().uid) {
  84. url = OC.generateUrl(
  85. '/avatar/{user}/{size}?v={version}',
  86. {
  87. user: user,
  88. size: Math.ceil(size * window.devicePixelRatio),
  89. version: oc_userconfig.avatar.version
  90. });
  91. } else {
  92. url = OC.generateUrl(
  93. '/avatar/{user}/{size}',
  94. {
  95. user: user,
  96. size: Math.ceil(size * window.devicePixelRatio)
  97. });
  98. }
  99. var img = new Image();
  100. // If the new image loads successfully set it.
  101. img.onload = function() {
  102. $div.clearimageplaceholder();
  103. $div.append(img);
  104. if(typeof callback === 'function') {
  105. callback();
  106. }
  107. };
  108. // Fallback when avatar loading fails:
  109. // Use old placeholder when a displayname attribute is defined,
  110. // otherwise show the unknown user placeholder.
  111. img.onerror = function () {
  112. $div.clearimageplaceholder();
  113. if (typeof(displayname) !== 'undefined') {
  114. $div.imageplaceholder(user, displayname);
  115. } else {
  116. setAvatarForUnknownUser($div);
  117. }
  118. if(typeof callback === 'function') {
  119. callback();
  120. }
  121. };
  122. if (size < 32) {
  123. $div.addClass('icon-loading-small');
  124. } else {
  125. $div.addClass('icon-loading');
  126. }
  127. img.width = size;
  128. img.height = size;
  129. img.src = url;
  130. img.alt = '';
  131. };
  132. }(jQuery));