diff --git a/cms/static/client_templates/metadata_number_entry.html b/cms/static/client_templates/metadata_number_entry.html index b429ee8b9d..5f50eb2c85 100644 --- a/cms/static/client_templates/metadata_number_entry.html +++ b/cms/static/client_templates/metadata_number_entry.html @@ -1,6 +1,6 @@
- + diff --git a/cms/static/js/base.js b/cms/static/js/base.js index b5bbd8e99a..eb8209535a 100644 --- a/cms/static/js/base.js +++ b/cms/static/js/base.js @@ -897,9 +897,5 @@ function saveSetSectionScheduleDate(e) { }); } -function checkNumberValidity(e) { - //e.preventDefault; - //$(e).val($(e).val().match(/\d*\.?\d+/)); -} diff --git a/cms/static/js/views/metadata_editor_view.js b/cms/static/js/views/metadata_editor_view.js index 70244d2bd6..c2b354ed7f 100644 --- a/cms/static/js/views/metadata_editor_view.js +++ b/cms/static/js/views/metadata_editor_view.js @@ -150,7 +150,8 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({ events : { "change input" : "updateModel", - "keypress .setting-input" : "showClearButton" , + "keypress .setting-input" : "keyPressed", + "change .setting-input" : "changed", "click .setting-clear" : "clear" }, @@ -162,14 +163,17 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({ var step = "step"; var options = this.model.getOptions(); if (options.hasOwnProperty(min)) { - this.$el.find('input').attr(min, options[min].toString()); + this.min = Number(options[min]); + this.$el.find('input').attr(min, this.min.toFixed(4)); } if (options.hasOwnProperty(max)) { - this.$el.find('input').attr(max, options[max].toString()); + this.max = Number(options[max]); + this.$el.find('input').attr(max, this.max.toFixed(4)); } var stepValue = undefined; if (options.hasOwnProperty(step)) { - stepValue = options[step].toString(); + // Parse step and convert to String. Polyfill doesn't like float values like ".1" (expects "0.1"). + stepValue = Number(options[step]).toFixed(4); } else if (this.model.getType() === 'Integer') { stepValue = "1"; @@ -195,7 +199,35 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({ setValueInEditor : function (value) { this.$el.find('input').val(value); + }, + + keyPressed: function (e) { + this.showClearButton(); + // This first filtering if statement is take from polyfill to prevent + // non-numeric input (for browsers that don't use polyfill because they DO have a number input type). + var _ref, _ref1; + if (((_ref = e.keyCode) !== 8 && _ref !== 9 && _ref !== 35 && _ref !== 36 && _ref !== 37 && _ref !== 39) && + ((_ref1 = e.which) !== 45 && _ref1 !== 46 && _ref1 !== 48 && _ref1 !== 49 && _ref1 !== 50 && _ref1 !== 51 + && _ref1 !== 52 && _ref1 !== 53 && _ref1 !== 54 && _ref1 !== 55 && _ref1 !== 56 && _ref1 !== 57)) { + e.preventDefault(); + } + // For integers, prevent decimal points. + if (this.model.getType() === 'Integer' && e.keyCode === 46) { + e.preventDefault(); + } + }, + + changed: function () { + // Limit value to the range specified by min and max (necessary for browsers that aren't using polyfill). + var value = this.getValueFromEditor(); + if ((this.max !== undefined) && value > this.max) { + value = this.max; + } else if ((this.min != undefined) && value < this.min) { + value = this.min; + } + this.setValueInEditor(value); } + }); diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index 5d2fffe895..2ee0c6e699 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -92,7 +92,7 @@ class CapaFields(object): seed = StringyInteger(help="Random seed for this student", scope=Scope.user_state) weight = StringyFloat(display_name="Problem Weight", help="Specifies the number of points the problem is worth. If unset, each response field in the problem is worth one point.", - values = {"min" : 0 , "step": ".1"}, + values = {"min" : 0 , "step": .1}, scope=Scope.settings) markdown = String(help="Markdown source of this module", scope=Scope.settings) source_code = String(help="Source code for LaTeX and Word problems. This feature is not well-supported.",