Commit 96637e9a by Valera Rozuvan Committed by Vasyl Nakvasiuk

Change some code styling. Added statistics display. Minor tweaks.

parent 6deeb602
...@@ -6,13 +6,10 @@ define('WordCloudMain', ['logme'], function (logme) { ...@@ -6,13 +6,10 @@ define('WordCloudMain', ['logme'], function (logme) {
WordCloudMain.prototype = { WordCloudMain.prototype = {
'submitAnswer': function () { 'submitAnswer': function () {
var _this, data; var _this = this,
data = {
data = { 'student_words': []
'student_words': [] };
};
_this = this;
this.wordCloudEl.find('input.input-cloud').each(function(index, value){ this.wordCloudEl.find('input.input-cloud').each(function(index, value){
data.student_words.push($(value).val()); data.student_words.push($(value).val());
...@@ -23,13 +20,12 @@ WordCloudMain.prototype = { ...@@ -23,13 +20,12 @@ WordCloudMain.prototype = {
$.postWithPrefix( $.postWithPrefix(
_this.ajax_url + '/' + 'submit', $.param(data), _this.ajax_url + '/' + 'submit', $.param(data),
function (response) { function (response) {
if ( if (response.status !== 'success') {
(response.hasOwnProperty('status') !== true) || logme('ERROR: ' + response.error);
(typeof response.status !== 'string') ||
(response.status.toLowerCase() !== 'success')) {
return; return;
} }
_this.showWordCloud(response); _this.showWordCloud(response);
} }
); );
...@@ -44,6 +40,8 @@ WordCloudMain.prototype = { ...@@ -44,6 +40,8 @@ WordCloudMain.prototype = {
this.wordCloudEl.find('#input-cloud-section').hide(); this.wordCloudEl.find('#input-cloud-section').hide();
console.log('response: ', response);
words = response.top_words; words = response.top_words;
maxSize = 0; maxSize = 0;
...@@ -67,7 +65,7 @@ WordCloudMain.prototype = { ...@@ -67,7 +65,7 @@ WordCloudMain.prototype = {
.fontSize(function (d) { .fontSize(function (d) {
var size; var size;
size = (d.size / maxSize) * 140; size = (d.size / maxSize) * 100;
if (size < 20) { if (size < 20) {
return 0; return 0;
...@@ -82,22 +80,32 @@ WordCloudMain.prototype = { ...@@ -82,22 +80,32 @@ WordCloudMain.prototype = {
return; return;
function draw(words) { function draw(words) {
var el; var el, firstWord = false;
$('#word_cloud_d3_' + _this.hash).remove(); $('#word_cloud_d3_' + _this.hash).remove();
el = $( el = $(
'<div ' + '<div ' +
'id="' + 'word_cloud_d3_' + _this.hash + '" ' + 'id="' + 'word_cloud_d3_' + _this.hash + '" ' +
'style="display: block; width: 500px; height: 500px; margin-left: auto; margin-right: auto;" ' + 'style="display: block; width: 500px; height: auto; margin-left: auto; margin-right: auto;" ' +
'></div>' '></div>'
); );
el.append('<h3>Your words</h3>');
$.each(response.student_words, function (index, value) {
if (firstWord === false) {
firstWord = true;
} else {
el.append(', ');
}
el.append(index + ': ' + (100.0 * (value / response.total_count)) + ' %');
});
el.append('<br /><br /><h3>Overall number of words: ' + response.total_count + '</h3><br />');
_this.wordCloudEl.append(el); _this.wordCloudEl.append(el);
d3.select('#word_cloud_d3_' + _this.hash).append('svg') d3.select('#word_cloud_d3_' + _this.hash).append('svg')
.attr('width', 500) .attr('width', 500)
.attr('height', 500) .attr('height', 500)
.attr('id', 'word_cloud_d3_' + _this.hash)
.append('g') .append('g')
.attr('transform', 'translate(190,250)') .attr('transform', 'translate(190,250)')
.selectAll('text') .selectAll('text')
...@@ -125,7 +133,7 @@ WordCloudMain.prototype = { ...@@ -125,7 +133,7 @@ WordCloudMain.prototype = {
return WordCloudMain; return WordCloudMain;
function WordCloudMain(el) { function WordCloudMain(el) {
var _this; var _this = this;
this.wordCloudEl = $(el).find('.word_cloud'); this.wordCloudEl = $(el).find('.word_cloud');
if (this.wordCloudEl.length !== 1) { if (this.wordCloudEl.length !== 1) {
...@@ -135,9 +143,9 @@ function WordCloudMain(el) { ...@@ -135,9 +143,9 @@ function WordCloudMain(el) {
return; return;
} }
// Later on used to create a unique DOM element.
hash += 1; hash += 1;
this.hash = hash; this.hash = hash;
this.wordCloudEl.addClass('word_cloud_d3_' + this.hash);
this.configJson = null; this.configJson = null;
try { try {
...@@ -160,7 +168,6 @@ function WordCloudMain(el) { ...@@ -160,7 +168,6 @@ function WordCloudMain(el) {
// Get the URL to which we will post the users words. // Get the URL to which we will post the users words.
this.ajax_url = this.wordCloudEl.data('ajax-url'); this.ajax_url = this.wordCloudEl.data('ajax-url');
_this = this;
this.inputSaveEl.on('click', function () { this.inputSaveEl.on('click', function () {
_this.submitAnswer(); _this.submitAnswer();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment