You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

placeholder.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * ownCloud
  3. *
  4. * @author Morris Jobke
  5. * @copyright 2013 Morris Jobke <morris.jobke@gmail.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /*
  22. * Adds a background color to the element called on and adds the first character
  23. * of the passed in string. This string is also the seed for the generation of
  24. * the background color.
  25. *
  26. * You have following HTML:
  27. *
  28. * <div id="albumart"></div>
  29. *
  30. * And call this from Javascript:
  31. *
  32. * $('#albumart').imageplaceholder('The Album Title');
  33. *
  34. * Which will result in:
  35. *
  36. * <div id="albumart" style="background-color: hsl(123, 90%, 65%); ... ">T</div>
  37. *
  38. * You may also call it like this, to have a different background, than the seed:
  39. *
  40. * $('#albumart').imageplaceholder('The Album Title', 'Album Title');
  41. *
  42. * Resulting in:
  43. *
  44. * <div id="albumart" style="background-color: hsl(123, 90%, 65%); ... ">A</div>
  45. *
  46. */
  47. (function ($) {
  48. $.fn.imageplaceholder = function(seed, text, size) {
  49. // set optional argument "text" to value of "seed" if undefined
  50. text = text || seed;
  51. var hash = md5(seed).substring(0, 4),
  52. maxRange = parseInt('ffff', 16),
  53. hue = parseInt(hash, 16) / maxRange * 256,
  54. height = this.height() || size || 32;
  55. this.css('background-color', 'hsl(' + hue + ', 90%, 65%)');
  56. // Placeholders are square
  57. this.height(height);
  58. this.width(height);
  59. // CSS rules
  60. this.css('color', '#fff');
  61. this.css('font-weight', 'normal');
  62. this.css('text-align', 'center');
  63. // calculate the height
  64. this.css('line-height', height + 'px');
  65. this.css('font-size', (height * 0.55) + 'px');
  66. if(seed !== null && seed.length) {
  67. this.html(text[0].toUpperCase());
  68. }
  69. };
  70. }(jQuery));