placeholder.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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) {
  49. // set optional argument "text" to value of "seed" if undefined
  50. text = text || seed;
  51. var hash = md5(seed),
  52. maxRange = parseInt('ffffffffffffffffffffffffffffffff', 16),
  53. hue = parseInt(hash, 16) / maxRange * 256,
  54. height = this.height();
  55. this.css('background-color', 'hsl(' + hue + ', 90%, 65%)');
  56. // CSS rules
  57. this.css('color', '#fff');
  58. this.css('font-weight', 'bold');
  59. this.css('text-align', 'center');
  60. // calculate the height
  61. this.css('line-height', height + 'px');
  62. this.css('font-size', (height * 0.55) + 'px');
  63. if(seed !== null && seed.length) {
  64. this.html(text[0].toUpperCase());
  65. }
  66. };
  67. }(jQuery));