nextcloud/core/js/placeholder.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2013-08-28 18:25:14 +04:00
/**
* ownCloud
*
* @author Morris Jobke
* @copyright 2013 Morris Jobke <morris.jobke@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* Adds a background color to the element called on and adds the first charater
* of the passed in string. This string is also the seed for the generation of
* the background color.
*
* You have following HTML:
*
* <div id="albumart"></div>
*
* And call this from Javascript:
*
2013-08-28 18:52:12 +04:00
* $('#albumart').placeholder('The Album Title');
2013-08-28 18:25:14 +04:00
*
* Which will result in:
*
2013-08-28 23:54:20 +04:00
* <div id="albumart" style="background-color: rgb(123, 123, 123); ... ">T</div>
2013-08-28 18:25:14 +04:00
*
*/
(function ($) {
2013-08-28 18:52:12 +04:00
$.fn.placeholder = function(seed) {
2013-08-28 18:25:14 +04:00
var hash = md5(seed),
maxRange = parseInt('ffffffffff', 16),
red = parseInt(hash.substr(0,10), 16) / maxRange * 256,
green = parseInt(hash.substr(10,10), 16) / maxRange * 256,
2013-08-28 18:52:12 +04:00
blue = parseInt(hash.substr(20,10), 16) / maxRange * 256,
2013-08-28 23:54:20 +04:00
rgb = [Math.floor(red), Math.floor(green), Math.floor(blue)],
height = this.height();
2013-08-28 18:25:14 +04:00
this.css('background-color', 'rgb(' + rgb.join(',') + ')');
2013-08-28 18:52:12 +04:00
// CSS rules
this.css('color', 'rgb(255, 255, 255)');
this.css('font-weight', 'bold');
this.css('text-align', 'center');
2013-08-28 23:54:20 +04:00
// calculate the height
this.css('line-height', height + 'px');
this.css('font-size', (height * 0.55) + 'px');
2013-08-28 18:52:12 +04:00
if(seed !== null && seed.length) {
this.html(seed[0].toUpperCase());
}
2013-08-28 18:25:14 +04:00
};
2013-08-28 18:52:12 +04:00
}(jQuery));