/*! * jQCloud * Copyright 2011 Luca Ongaro (http://www.lucaongaro.eu) * Copyright 2013 Daniel White (http://www.developerdan.com) * Copyright 2014 Damien "Mistic" Sorel (http://www.strangeplanet.fr) * Licensed under MIT (http://opensource.org/licenses/MIT) */ /*jshint -W055 *//* non standard constructor name */ (function($) { "use strict"; /* * Plugin class */ var jQCloud = function (element, word_array, options) { this.$element = $(element); this.word_array = word_array || []; this.options = options; this.sizeGenerator = null; this.colorGenerator = null; // Data used internally this.data = { placed_words: [], timeouts: {}, namespace: null, step: null, angle: null, aspect_ratio: null, max_weight: null, min_weight: null, sizes: [], colors: [] }; this.initialize(); }; jQCloud.DEFAULTS = { width: 100, height: 100, center: { x: 0.5, y: 0.5 }, steps: 10, delay: null, shape: 'elliptic', classPattern: 'w{n}', encodeURI: true, removeOverflowing: true, afterCloudRender: null, autoResize: false, colors: null, fontSize: null, template: null }; jQCloud.prototype = { initialize: function() { // Set/Get dimensions if (this.options.width) { this.$element.width(this.options.width); } else { this.options.width = this.$element.width(); } if (this.options.height) { this.$element.height(this.options.height); } else { this.options.height = this.$element.height(); } // Default options value this.options = $.extend(true, {}, jQCloud.DEFAULTS, this.options); // Ensure delay if (this.options.delay === null) { this.options.delay = this.word_array.length > 50 ? 10 : 0; } // Backward compatibility if (this.options.center.x > 1) { this.options.center.x = this.options.center.x / this.options.width; this.options.center.y = this.options.center.y / this.options.height; } // Create colorGenerator function from options // Direct function if (typeof this.options.colors == 'function') { this.colorGenerator = this.options.colors; } // Array of sizes else if ($.isArray(this.options.colors)) { var cl = this.options.colors.length; if (cl > 0) { // Fill the sizes array to X items if (cl < this.options.steps) { for (var i=cl; i 0) { // Fill the sizes array to X items if (sl < this.options.steps) { for (var j=sl; j 0){ this.drawOneWordDelayed(); } else { for (i=0, l=this.word_array.length; i').attr(word.attr); // Apply class if (this.options.classPattern) { word_span.addClass(this.options.classPattern.replace('{n}', weight)); } // Apply color if (this.data.colors.length) { word_span.css('color', this.data.colors[weight-1]); } // Apply size if (this.data.sizes.length) { word_span.css('font-size', this.data.sizes[weight-1]); } //Render using template function if provided. if (this.options.template) { word_span.html(this.options.template(word)); } else if (word.link) { // Append link if word.link attribute was set // If link is a string, then use it as the link href if (typeof word.link === 'string') { word.link = { href: word.link }; } if (this.options.encodeURI) { word.link.href = encodeURI(word.link.href).replace(/'/g, '%27'); } word_span.append($('').attr(word.link).text(word.text)); } else { word_span.text(word.text); } // Bind handlers to words if (word.handlers) { word_span.on(word.handlers); } this.$element.append(word_span); word_size = { width: word_span.outerWidth(), height: word_span.outerHeight() }; word_size.left = this.options.center.x*this.options.width - word_size.width / 2.0; word_size.top = this.options.center.y*this.options.height - word_size.height / 2.0; // Save a reference to the style property, for better performance word_style = word_span[0].style; word_style.position = 'absolute'; word_style.left = word_size.left + 'px'; word_style.top = word_size.top + 'px'; while(this.hitTest(word_size)) { // option shape is 'rectangular' so move the word in a rectangular spiral if (this.options.shape === 'rectangular') { steps_in_direction++; if (steps_in_direction * this.data.step > (1 + Math.floor(quarter_turns / 2.0)) * this.data.step * ((quarter_turns % 4 % 2) === 0 ? 1 : this.data.aspect_ratio)) { steps_in_direction = 0.0; quarter_turns++; } switch(quarter_turns % 4) { case 1: word_size.left += this.data.step * this.data.aspect_ratio + Math.random() * 2.0; break; case 2: word_size.top -= this.data.step + Math.random() * 2.0; break; case 3: word_size.left -= this.data.step * this.data.aspect_ratio + Math.random() * 2.0; break; case 0: word_size.top += this.data.step + Math.random() * 2.0; break; } } // Default settings: elliptic spiral shape else { radius += this.data.step; angle += (index % 2 === 0 ? 1 : -1) * this.data.step; word_size.left = this.options.center.x*this.options.width - (word_size.width / 2.0) + (radius*Math.cos(angle)) * this.data.aspect_ratio; word_size.top = this.options.center.y*this.options.height + radius*Math.sin(angle) - (word_size.height / 2.0); } word_style.left = word_size.left + 'px'; word_style.top = word_size.top + 'px'; } // Don't render word if part of it would be outside the container if (this.options.removeOverflowing && ( word_size.left < 0 || word_size.top < 0 || (word_size.left + word_size.width) > this.options.width || (word_size.top + word_size.height) > this.options.height ) ) { word_span.remove(); return; } // Save position for further usage this.data.placed_words.push(word_size); if (typeof word.afterWordRender === 'function') { word.afterWordRender.call(word_span); } }, // Draw one word then recall the function after a delay drawOneWordDelayed: function(index) { index = index || 0; // if not visible then do not attempt to draw if (!this.$element.is(':visible')) { this.createTimeout($.proxy(function(){ this.drawOneWordDelayed(index); }, this), 10); return; } if (index < this.word_array.length) { this.drawOneWord(index, this.word_array[index]); this.createTimeout($.proxy(function(){ this.drawOneWordDelayed(index + 1); }, this), this.options.delay); } else { if (typeof this.options.afterCloudRender == 'function') { this.options.afterCloudRender.call(this.$element); } } }, // Destroy any data and objects added by the plugin destroy: function() { this.clearTimeouts(); this.$element.removeClass('jqcloud'); this.$element.removeData('jqcloud'); this.$element.children('[id^="' + this.data.namespace + '"]').remove(); }, // Update the list of words update: function(word_array) { this.word_array = word_array; this.data.placed_words = []; this.clearTimeouts(); this.drawWordCloud(); }, resize: function() { var new_size = { width: this.$element.width(), height: this.$element.height() }; if (new_size.width != this.options.width || new_size.height != this.options.height) { this.options.width = new_size.width; this.options.height = new_size.height; this.data.aspect_ratio = this.options.width / this.options.height; this.update(this.word_array); } } }; /* * Apply throttling to a callback * @param callback {function} * @param delay {int} milliseconds * @param context {object|null} * @return {function} */ function throttle(callback, delay, context) { var state = { pid: null, last: 0 }; return function() { var elapsed = new Date().getTime() - state.last, args = arguments, that = this; function exec() { state.last = new Date().getTime(); return callback.apply(context || that, Array.prototype.slice.call(args)); } if (elapsed > delay) { return exec(); } else { clearTimeout(state.pid); state.pid = setTimeout(exec, delay - elapsed); } }; } /* * jQuery plugin */ $.fn.jQCloud = function(word_array, option) { var args = arguments; return this.each(function () { var $this = $(this), data = $this.data('jqcloud'); if (!data && word_array === 'destroy') { // Don't even try to initialize when called with 'destroy' return; } if (!data) { var options = typeof option === 'object' ? option : {}; $this.data('jqcloud', (data = new jQCloud(this, word_array, options))); } else if (typeof word_array === 'string') { data[word_array].apply(data, Array.prototype.slice.call(args, 1)); } }); }; $.fn.jQCloud.defaults = { set: function(options) { $.extend(true, jQCloud.DEFAULTS, options); }, get: function(key) { var options = jQCloud.DEFAULTS; if (key) { options = options[key]; } return $.extend(true, {}, options); } }; })(jQuery);