1/** 2 * Copyright (c) 2013 Christopher Schäpers <[email protected]> 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/** 9 * This plugin inserts the right avatar for the user, depending on, whether a 10 * custom avatar is uploaded - which it uses then - or not, and display a 11 * placeholder with the first letter of the users name instead. 12 * For this it queries the core_avatar_get route, thus this plugin is fit very 13 * tightly for owncloud, and it may not work anywhere else. 14 * 15 * You may use this on any <div></div> 16 * Here I'm using <div class="avatardiv"></div> as an example. 17 * 18 * There are 5 ways to call this: 19 * 20 * 1. $('.avatardiv').avatar('jdoe', 128); 21 * This will make the div to jdoe's fitting avatar, with a size of 128px. 22 * 23 * 2. $('.avatardiv').avatar('jdoe'); 24 * This will make the div to jdoe's fitting avatar. If the div already has a 25 * height, it will be used for the avatars size. Otherwise this plugin will 26 * search for 'size' DOM data, to use for avatar size. If neither are available 27 * it will default to 64px. 28 * 29 * 3. $('.avatardiv').avatar(); 30 * This will search the DOM for 'user' data, to use as the username. If there 31 * is no username available it will default to a placeholder with the value of 32 * "x". The size will be determined the same way, as the second example. 33 * 34 * 4. $('.avatardiv').avatar('jdoe', 128, true); 35 * This will behave like the first example, except it will also append random 36 * hashes to the custom avatar images, to force image reloading in IE8. 37 * 38 * 5. $('.avatardiv').avatar('jdoe', 128, undefined, true); 39 * This will behave like the first example, but it will hide the avatardiv, if 40 * it will display the default placeholder. undefined is the ie8fix from 41 * example 4 and can be either true, or false/undefined, to be ignored. 42 * 43 * 6. $('.avatardiv').avatar('jdoe', 128, undefined, true, callback); 44 * This will behave like the above example, but it will call the function 45 * defined in callback after the avatar is placed into the DOM. 46 * 47 */ 48 49(function ($) { 50 $.fn.avatar = function(user, size, ie8fix, hidedefault, callback, displayname) { 51 if (typeof(size) === 'undefined') { 52 if (this.height() > 0) { 53 size = this.height(); 54 } else if (this.data('size') > 0) { 55 size = this.data('size'); 56 } else { 57 size = 64; 58 } 59 } 60 61 this.height(size); 62 this.width(size); 63 64 if (typeof(user) === 'undefined') { 65 if (typeof(this.data('user')) !== 'undefined') { 66 user = this.data('user'); 67 } else { 68 this.imageplaceholder('x'); 69 return; 70 } 71 } 72 73 // sanitize 74 user = String(user).replace(/\//g,''); 75 76 var $div = this; 77 78 var url = OC.generateUrl( 79 '/avatar/{user}/{size}', 80 {user: user, size: Math.ceil(size * window.devicePixelRatio)}); 81 82 // If the displayname is not defined we use the old code path 83 if (typeof(displayname) === 'undefined') { 84 $.get(url).always(function(result, status) { 85 // if there is an error or an object returned (contains user information): 86 // -> show the fallback placeholder 87 if (typeof(result) === 'object' || status === 'error') { 88 if (!hidedefault) { 89 if (result.data && result.data.displayname) { 90 $div.imageplaceholder(user, result.data.displayname); 91 } else { 92 // User does not exist 93 $div.imageplaceholder(user, 'X'); 94 $div.css('background-color', '#b9b9b9'); 95 } 96 } else { 97 $div.hide(); 98 } 99 // else an image is transferred and should be shown 100 } else { 101 $div.show(); 102 if (ie8fix === true) { 103 $div.html('<img width="' + size + '" height="' + size + '" src="'+url+'#'+Math.floor(Math.random()*1000)+'">'); 104 } else { 105 $div.html('<img width="' + size + '" height="' + size + '" src="'+url+'">'); 106 } 107 } 108 if(typeof callback === 'function') { 109 callback(); 110 } 111 }); 112 } else { 113 // We already have the displayname so set the placeholder (to show at least something) 114 if (!hidedefault) { 115 $div.imageplaceholder(displayname); 116 } 117 118 var img = new Image(); 119 120 // If the new image loads successfully set it. 121 img.onload = function() { 122 $div.show(); 123 $div.text(''); 124 $div.append(img); 125 } 126 127 img.width = size; 128 img.height = size; 129 img.src = url; 130 } 131 }; 132}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.