From 13bed39dde6eba010f266fb580a3e06f2f5e3a49 Mon Sep 17 00:00:00 2001 From: Valera Rozuvan Date: Mon, 22 Apr 2013 17:50:10 +0300 Subject: [PATCH] Refactoring after peer review. --- .../js/src/word_cloud/word_cloud_main.js | 92 ++++++++++++------- .../lib/xmodule/xmodule/word_cloud_module.py | 1 - lms/templates/word_cloud.html | 3 - 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js b/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js index 042fd6b09a..4a6dee77b0 100644 --- a/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js +++ b/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js @@ -15,6 +15,44 @@ (function (requirejs, require, define) { define('WordCloudMain', ['logme'], function (logme) { + + // To add compatible Object.keys support in older environments that do not natively support it. + // + // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys#Compatibility + if (!Object.keys) { + Object.keys = (function () { + var hasOwnProperty = Object.prototype.hasOwnProperty, + hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'), + dontEnums = [ + 'toString', + 'toLocaleString', + 'valueOf', + 'hasOwnProperty', + 'isPrototypeOf', + 'propertyIsEnumerable', + 'constructor' + ], + dontEnumsLength = dontEnums.length; + + return function (obj) { + if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object'); + + var result = []; + + for (var prop in obj) { + if (hasOwnProperty.call(obj, prop)) result.push(prop); + } + + if (hasDontEnumBug) { + for (var i=0; i < dontEnumsLength; i++) { + if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]); + } + } + return result; + } + }()); + }; + /** * @function WordCloudMain * @@ -61,16 +99,7 @@ define('WordCloudMain', ['logme'], function (logme) { // Show WordCloud container after Ajax request done _this.wordCloudEl.show(); - try { - _this.configJson = _this.configJson || JSON.parse(_this.wordCloudEl.find('.word_cloud_div').html()); - } catch (err) { - logme('ERROR: Incorrect JSON config was given.'); - logme(err.message); - - return; - } - - if (_this.configJson.submitted) { + if (_this.configJson && _this.configJson.submitted) { _this.showWordCloud(_this.configJson); return; @@ -149,8 +178,10 @@ define('WordCloudMain', ['logme'], function (logme) { }); // Find the longest word, and calculate the scale appropriately. This is - // required so that even long words fit into the drawing area and are - // not simply discarded. + // required so that even long words fit into the drawing area. + // + // This is a fix for: if the word is very long and/or big, it is discarded by + // for unknown reason. $.each(words, function (index, word) { var tempScaleFactor = 1.0, size = ((word.size / maxSize) * maxFontSize); @@ -207,30 +238,27 @@ define('WordCloudMain', ['logme'], function (logme) { * coordinate object contains two properties: 'x', and 'y'. */ WordCloudMain.prototype.drawWordCloud = function (response, words, bounds) { - var firstWord = false, // The first word in the list of user enetered words does not get a leading comma. - fill = d3.scale.category20(), // Color words in different colors. - scale = bounds ? Math.min( + // The first word in the list of user enetered words does not get a leading comma. + var firstWord = false, + + // Color words in different colors. + fill = d3.scale.category20(), + + // Comma separated string of user enetered words. + studentWordsStr = (Object.keys(response.student_words)).join(', '), + + // By default we do not scale. + scale = 1; + + // If bounding rectangle is given, scale based on the bounding box of all the words. + if (bounds) { + scale = 0.5 * Math.min( this.width / Math.abs(bounds[1].x - this.width / 2), this.width / Math.abs(bounds[0].x - this.width / 2), this.height / Math.abs(bounds[1].y - this.height / 2), this.height / Math.abs(bounds[0].y - this.height / 2) - ) / 2 : 1, // Scale based on the bounding box of all the words. - studentWordsStr = ''; - - // Get the user his entered words. - $.each(response.student_words, function (index, value) { - if (firstWord === false) { - firstWord = true; - } else { - studentWordsStr += ', '; - } - - // For now we do not show the percentages for each word the user has enetered. - // el.append(index + ': ' + (100.0 * (value / response.total_count)) + ' %'); - // - // Only show the words. - studentWordsStr += '"' + index + '"'; - }); + ); + } this.wordCloudEl.find('.result_cloud_section').addClass('active'); diff --git a/common/lib/xmodule/xmodule/word_cloud_module.py b/common/lib/xmodule/xmodule/word_cloud_module.py index 9bfc667ced..1a2c99f59a 100644 --- a/common/lib/xmodule/xmodule/word_cloud_module.py +++ b/common/lib/xmodule/xmodule/word_cloud_module.py @@ -148,7 +148,6 @@ class WordCloudModule(WordCloudFields, XModule): 'element_id': self.location.html_id(), 'element_class': self.location.category, 'ajax_url': self.system.ajax_url, - 'configuration_json': self.get_state(), 'num_inputs': int(self.num_inputs), 'submitted': self.submitted } diff --git a/lms/templates/word_cloud.html b/lms/templates/word_cloud.html index 66d98b4aa6..633e8bf168 100644 --- a/lms/templates/word_cloud.html +++ b/lms/templates/word_cloud.html @@ -24,7 +24,4 @@

Total number of words:

- - -