/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Pure javascript implementation of <a href="http://en.wikipedia.org/wiki/Tag_cloud">Tag cloud</a>
* @namespace jsbeans
* @class TagCloud
* @static
* */
jsbeans.TagCloud = {
/**
* Renders TagCloud
* @method render
* @param id {String} the id of the DOM container
* @param items {Array} an Array in the form "WORD:WEIGHT" (sample: <code>["Internet:11", "Facebook:42", "Twitter:2", "Google:40"]</code>)
* @param [func] {Function} an optional callback fired on the "onclick" event on each tag. As each tag is placed in an anchor, the anchor itself will be passed on the callback.
* @static
* */
render: function(id, items) {
var target = document.getElementById(id);
if (typeof target == "undefined" || typeof target.innerHTML == "undefined") {
return;
}
target.innerHTML = "";
// items are in the form ["<word_1>:<weight_1>", "<word_2>:<weight_2>"]
// e.g. ["Internet:11", "Facebook:42", "Twitter:2", "Google:40"]
if (!items.length) {
return;
}
var _items = new Array();
var max = 0;
var min = 0;
for (var i = 0, item; item = items[i]; i++) {
var sp = item.split(":");
_items[sp[0]] = sp[1];
var candidate = parseInt(sp[1]);
if (candidate > max) {
max = candidate;
}
if (candidate < min) {
min = candidate;
}
}
var fn = arguments[2];
var hasCallback = !!fn && typeof fn != "string" && !fn.nodeName && fn.constructor != Array && /function/i.test( fn + "" );
for (var word in _items) {
var weight = Math.floor((((_items[word]-min)*100/(max-min)) + 50));
var el = document.createElement("a");
el.setAttribute("weight", weight);
el.setAttribute("title", weight);
el.setAttribute("href", "javascript:void(0)");
el.setAttribute("id", "jsbeans_tagcloud_" + target.id + "_" + word.replace(" ", "_"));
el.styleClass = "tagcloud";
el.style.fontSize = weight + "%";
el.style.padding = "4px";
el.innerHTML = word;
if (hasCallback) {
el.onclick = function() {
fn(this);
};
}
target.appendChild(el);
el = null;
var blank = document.createTextNode(" ");
target.appendChild(blank);
blank = null;
}
}
};