Skip to content

Commit 5f4895c

Browse files
author
Luca Ongaro
committed
version 0.2.0 (add configuration options)
1 parent 0fe5132 commit 5f4895c

File tree

6 files changed

+91
-34
lines changed

6 files changed

+91
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.textile

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ h2. Installation
99
Installing jQCloud is extremely simple:
1010

1111
# Make sure to import the jQuery library in your project.
12-
# Download the jQCloud files. Place jqcloud-0.1.8.js (or the minified version jqcloud-0.1.8.min.js) and jqcloud.css somewhere in your project and import both of them in your HTML code.
12+
# Download the jQCloud files. Place jqcloud-0.2.0.js (or the minified version jqcloud-0.2.0.min.js) and jqcloud.css somewhere in your project and import both of them in your HTML code.
1313

1414
You can easily substitute jqcloud.css with a custom CSS stylesheet following the guidelines explained later.
1515

@@ -23,7 +23,7 @@ bc. <!DOCTYPE html>
2323
<title>jQCloud Example</title>
2424
<link rel="stylesheet" type="text/css" href="jqcloud.css" />
2525
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
26-
<script type="text/javascript" src="jqcloud-0.1.8.js"></script>
26+
<script type="text/javascript" src="jqcloud-0.2.0.js"></script>
2727
<script type="text/javascript">
2828
/*!
2929
* Create an array of objects to be passed to jQCloud, each representing a word in the cloud and specifying
@@ -59,14 +59,37 @@ bc. <!DOCTYPE html>
5959
</body>
6060
</html>
6161

62+
h3. Options:
63+
64+
Since version 0.2.0, jQCloud accepts an object containing configuration options as the second argument:
65+
66+
bc. $("#example").jQCloud(word_list, {
67+
width: 300,
68+
height: 200
69+
});
70+
71+
The full list of available options is the following:
72+
73+
* *width* (number): The width of the word cloud. Defaults to the width of the container element.
74+
* *height* (number): The height of the word cloud. Defaults to the height of the container element.
75+
* *center* (object): The x and y coordinates of the center of the word cloud (e.g.: {x: 300, y: 150}). Defaults to the center of the container element.
76+
* *callback* (function): A callback function to be called after the cloud is fully rendered. Undefined by default.
77+
* *delayed_mode* (boolean): If true, words are rendered one after another with a tiny delay between each one. This prevents freezing of the browser when there are many words to be rendered. If false, the cloud will be rendered in one single shot. By default, delayed_mode is true when there are more than 50 words.
78+
6279
h3. Note:
6380

64-
Since drawing the cloud is rather computationally intensive, cloud rendering isn't instantaneous. If you want to make sure that some code executes after the cloud is rendered, you can pass to jQCloud a callback function:
81+
Since drawing the cloud is rather computationally intensive, cloud rendering isn't instantaneous. If you want to make sure that some code executes after the cloud is rendered, you can specify in the options a callback function:
6582

66-
bc. $("#example").jQCloud(word_list, function(){
67-
// This code executes after the cloud is fully rendered
83+
bc. $("#example").jQCloud(word_list, {
84+
callback: function() {
85+
// This code executes after the cloud is fully rendered
86+
}
6887
});
6988

89+
h4. Deprecation warning:
90+
91+
Before version 0.2.0 jQCloud used to accept a callback function as the second argument. This way of specifying the callback function is deprecated and, although version 0.2.0 maintains backward compatibility, it will be removed in newer versions. If you need a callback function, use the 'callback' configuration option instead.
92+
7093
h2. Custom CSS guidelines
7194

7295
The word cloud produced by jQCloud is made of pure HTML, so you can style it using CSS. When you call $("#example").jQCloud(...), the containing element is given a CSS class of "jqcloud", allowing for easy CSS targeting. The included CSS file jqcloud.css is intended as an example and as a base on which to develop your own custom style, defining dimensions and appearance of words in the cloud. When writing your custom CSS, just follow these guidelines:
@@ -83,6 +106,8 @@ If you happen to use jQCloud in your projects, you can make me know (just contac
83106

84107
h2. Changelog
85108

109+
0.2.0 Add configuration options, passed as the second argument of jQCloud (include ideas proposed by "mindscratch":https://github.com/mindscratch and "aaaa0441":https://github.com/aaaa0441)
110+
86111
0.1.8 Fix bug in the algorithm causing sometimes unbalanced layouts (thanks to "isamochernov":https://github.com/isamochernov)
87112

88113
0.1.7 Remove duplicated @</span>@ when word has an URL (thanks to "rbrancher":https://github.com/rbrancher)

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>jQCloud Example</title>
55
<link rel="stylesheet" type="text/css" href="../jqcloud/jqcloud.css" />
66
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
7-
<script type="text/javascript" src="../jqcloud/jqcloud-0.1.8.js"></script>
7+
<script type="text/javascript" src="../jqcloud/jqcloud-0.2.0.js"></script>
88
<script type="text/javascript">
99
var word_list = [
1010
{text: "Lorem", weight: 13, url: "https://github.com/DukeLeNoir/jQCloud"},

jqcloud/jqcloud-0.1.8.min.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

jqcloud/jqcloud-0.1.8.js renamed to jqcloud/jqcloud-0.2.0.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
/*!
22
* jQCloud Plugin for jQuery
33
*
4-
* Version 0.1.8
4+
* Version 0.2.0
55
*
66
* Copyright 2011, Luca Ongaro
77
* Licensed under the MIT license.
88
*
9-
* Date: Fri Apr 8 10:24:15 +0100 2011
9+
* Date: Wed Jul 13 13:48:05 +0100 2011
1010
*/
1111

1212
(function( $ ){
13-
$.fn.jQCloud = function(word_array, callback_function) {
13+
$.fn.jQCloud = function(word_array, options) {
1414
// Reference to the container element
1515
var $this = this;
1616
// Reference to the ID of the container element
1717
var container_id = $this.attr('id');
1818

19+
// Default options value
20+
var default_options = {
21+
width: $this.width(),
22+
height: $this.height(),
23+
center: {
24+
x: $this.width() / 2.0,
25+
y: $this.height() / 2.0
26+
},
27+
delayed_mode: word_array.length > 50
28+
};
29+
30+
// Maintain backward compatibility with old API, where the second argument of jQCloud was a callback function
31+
if (typeof options === 'function') {
32+
options = { callback: options }
33+
}
34+
35+
options = $.extend(default_options, options || {});
36+
1937
// Add the "jqcloud" class to the container for easy CSS styling
2038
$this.addClass("jqcloud");
2139

@@ -51,13 +69,10 @@
5169

5270
var step = 2.0;
5371
var already_placed_words = [];
54-
var aspect_ratio = $this.width() / $this.height();
55-
var origin_x = $this.width() / 2.0;
56-
var origin_y = $this.height() / 2.0;
57-
58-
// Move each word in spiral until it finds a suitable empty place
59-
$.each(word_array, function(index, word) {
72+
var aspect_ratio = options.width / options.height;
6073

74+
// Function to draw a word, by moving it in spiral until it finds a suitable empty place. This will be iterated on each word.
75+
var drawOneWord = function(index, word) {
6176
// Define the ID attribute of the span that will wrap the word, and the associated jQuery selector string
6277
var word_id = container_id + "_word_" + index;
6378
var word_selector = "#" + word_id;
@@ -73,8 +88,8 @@
7388

7489
var width = $(word_selector).width();
7590
var height = $(word_selector).height();
76-
var left = origin_x - width / 2.0;
77-
var top = origin_y - height / 2.0;
91+
var left = options.center.x - width / 2.0;
92+
var top = options.center.y - height / 2.0;
7893
$(word_selector).css("position", "absolute");
7994
$(word_selector).css("left", left + "px");
8095
$(word_selector).css("top", top + "px");
@@ -83,22 +98,38 @@
8398
radius += step;
8499
angle += (index % 2 === 0 ? 1 : -1)*step;
85100

86-
left = origin_x - (width / 2.0) + (radius*Math.cos(angle)) * aspect_ratio;
87-
top = origin_y + radius*Math.sin(angle) - (height / 2.0);
101+
left = options.center.x - (width / 2.0) + (radius*Math.cos(angle)) * aspect_ratio;
102+
top = options.center.y + radius*Math.sin(angle) - (height / 2.0);
88103

89104
$(word_selector).css('left', left + "px");
90105
$(word_selector).css('top', top + "px");
91106
}
92107
already_placed_words.push(document.getElementById(word_id));
93-
});
108+
}
109+
110+
var drawOneWordDelayed = function(index) {
111+
index = index || 0;
112+
if (index < word_array.length) {
113+
drawOneWord(index, word_array[index]);
114+
setTimeout(function(){drawOneWordDelayed(index + 1);}, 10);
115+
}
116+
}
117+
118+
// Iterate drawOneWord on every word. The way the iteration is done depends on the drawing mode (delayed_mode is true or false)
119+
if (options.delayed_mode){
120+
drawOneWordDelayed();
121+
}
122+
else {
123+
$.each(word_array, drawOneWord);
124+
}
94125

95-
if (typeof callback_function === 'function') {
96-
callback_function.call(this);
126+
if (typeof options.callback === 'function') {
127+
options.callback.call(this);
97128
}
98129
};
99130

100131
// Delay execution so that the browser can render the page before the computatively intensive word cloud drawing
101-
setTimeout(function(){drawWordCloud();}, 100);
132+
setTimeout(function(){drawWordCloud();}, 10);
102133
return this;
103134
};
104135
})(jQuery);

jqcloud/jqcloud-0.2.0.min.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)