diff --git a/common/lib/xmodule/xmodule/discussion_module.py b/common/lib/xmodule/xmodule/discussion_module.py index 8051cb23a3..86dd2fddee 100644 --- a/common/lib/xmodule/xmodule/discussion_module.py +++ b/common/lib/xmodule/xmodule/discussion_module.py @@ -36,10 +36,14 @@ class DiscussionFields(object): class DiscussionModule(DiscussionFields, XModule): - js = {'coffee': - [resource_string(__name__, 'js/src/time.coffee'), - resource_string(__name__, 'js/src/discussion/display.coffee')] - } + js = { + 'coffee': [ + resource_string(__name__, 'js/src/discussion/display.coffee') + ], + 'js': [ + resource_string(__name__, 'js/src/time.js') + ] + } js_module_name = "InlineDiscussion" def get_html(self): diff --git a/common/lib/xmodule/xmodule/js/spec/.gitignore b/common/lib/xmodule/xmodule/js/spec/.gitignore index 138742c14c..0f9afc716a 100644 --- a/common/lib/xmodule/xmodule/js/spec/.gitignore +++ b/common/lib/xmodule/xmodule/js/spec/.gitignore @@ -2,3 +2,6 @@ # Tests for video are written in pure JavaScript. !video/*.js + +# Tests for Time are written in pure JavaScript. +!time_spec.js diff --git a/common/lib/xmodule/xmodule/js/spec/time_spec.coffee b/common/lib/xmodule/xmodule/js/spec/time_spec.coffee deleted file mode 100644 index 80891d58b5..0000000000 --- a/common/lib/xmodule/xmodule/js/spec/time_spec.coffee +++ /dev/null @@ -1,18 +0,0 @@ -describe 'Time', -> - describe 'format', -> - describe 'with duration more than or equal to 1 hour', -> - it 'return a correct time format', -> - expect(Time.format(3600)).toEqual '1:00:00' - expect(Time.format(7272)).toEqual '2:01:12' - - describe 'with duration less than 1 hour', -> - it 'return a correct time format', -> - expect(Time.format(1)).toEqual '0:01' - expect(Time.format(61)).toEqual '1:01' - expect(Time.format(3599)).toEqual '59:59' - - describe 'convert', -> - it 'return a correct time based on speed modifier', -> - expect(Time.convert(0, 1, 1.5)).toEqual '0.000' - expect(Time.convert(100, 1, 1.5)).toEqual '66.667' - expect(Time.convert(100, 1.5, 1)).toEqual '150.000' diff --git a/common/lib/xmodule/xmodule/js/spec/time_spec.js b/common/lib/xmodule/xmodule/js/spec/time_spec.js new file mode 100644 index 0000000000..55a98ebb72 --- /dev/null +++ b/common/lib/xmodule/xmodule/js/spec/time_spec.js @@ -0,0 +1,50 @@ +(function (undefined) { + 'use strict'; + + describe('Time', function () { + describe('format', function () { + describe('with duration more than or equal to 1 hour', function () { + it('return a correct time format', function () { + expect(Time.format(3600)).toEqual('1:00:00'); + expect(Time.format(7272)).toEqual('2:01:12'); + }); + }); + + describe('with duration less than 1 hour', function () { + it('return a correct time format', function () { + expect(Time.format(1)).toEqual('0:01'); + expect(Time.format(61)).toEqual('1:01'); + expect(Time.format(3599)).toEqual('59:59'); + }); + }); + }); + + describe('formatFull', function () { + it('gives correct string for times', function () { + var testTimes = [ + [0, '00:00:00'], [60, '00:01:00'], + [488, '00:08:08'], [2452, '00:40:52'], + [3600, '01:00:00'], [28800, '08:00:00'], + [144532, '40:08:52'], [190360, '52:52:40'], + [294008, '81:40:08'] + ]; + + $.each(testTimes, function (index, times) { + var timeInt = times[0], + timeStr = times[1]; + + expect(Time.formatFull(timeInt)).toBe(timeStr); + }); + }); + }); + + describe('convert', function () { + it('return a correct time based on speed modifier', function () { + expect(Time.convert(0, 1, 1.5)).toEqual('0.000'); + expect(Time.convert(100, 1, 1.5)).toEqual('66.667'); + expect(Time.convert(100, 1.5, 1)).toEqual('150.000'); + }); + }); + }); + +}).call(this); diff --git a/common/lib/xmodule/xmodule/js/src/.gitignore b/common/lib/xmodule/xmodule/js/src/.gitignore index 54af6653e9..73a4527726 100644 --- a/common/lib/xmodule/xmodule/js/src/.gitignore +++ b/common/lib/xmodule/xmodule/js/src/.gitignore @@ -6,4 +6,8 @@ # Video are written in pure JavaScript. !video/*.js -!video/transcripts/*.js \ No newline at end of file +!video/transcripts/*.js + + +# Converted to JS from CoffeeScript. +!time.js diff --git a/common/lib/xmodule/xmodule/js/src/time.coffee b/common/lib/xmodule/xmodule/js/src/time.coffee deleted file mode 100644 index e2da2c583d..0000000000 --- a/common/lib/xmodule/xmodule/js/src/time.coffee +++ /dev/null @@ -1,30 +0,0 @@ -class @Time - @format: (time) -> - pad = (number) -> if number < 10 then "0#{number}" else number - - seconds = Math.floor time - minutes = Math.floor seconds / 60 - hours = Math.floor minutes / 60 - seconds = seconds % 60 - minutes = minutes % 60 - - if hours - "#{hours}:#{pad(minutes)}:#{pad(seconds % 60)}" - else - "#{minutes}:#{pad(seconds % 60)}" - - @formatFull: (time) -> - pad = (number) -> if number < 10 then "0#{number}" else number - - seconds = Math.floor time - minutes = Math.floor seconds / 60 - hours = Math.floor minutes / 60 - seconds = seconds % 60 - minutes = minutes % 60 - - # The returned value will not be user-facing. So no need for - # internationalization. - "#{pad(hours)}:#{pad(minutes)}:#{pad(seconds % 60)}" - - @convert: (time, oldSpeed, newSpeed) -> - (time * oldSpeed / newSpeed).toFixed(3) diff --git a/common/lib/xmodule/xmodule/js/src/time.js b/common/lib/xmodule/xmodule/js/src/time.js new file mode 100644 index 0000000000..464d06ed5f --- /dev/null +++ b/common/lib/xmodule/xmodule/js/src/time.js @@ -0,0 +1,47 @@ +(function (undefined) { + 'use strict'; + + this.Time = { + format: format, + formatFull: formatFull, + convert: convert + }; + + return; + + function format(time, formatFull) { + var hours, minutes, seconds; + + seconds = Math.floor(time); + minutes = Math.floor(seconds / 60); + hours = Math.floor(minutes / 60); + seconds = seconds % 60; + minutes = minutes % 60; + + if (formatFull) { + return '' + _pad(hours) + ':' + _pad(minutes) + ':' + _pad(seconds % 60); + } else if (hours) { + return '' + hours + ':' + _pad(minutes) + ':' + _pad(seconds % 60); + } else { + return '' + minutes + ':' + _pad(seconds % 60); + } + } + + function formatFull(time) { + // The returned value will not be user-facing. So no need for + // internationalization. + return format(time, true); + } + + function convert(time, oldSpeed, newSpeed) { + return (time * oldSpeed / newSpeed).toFixed(3); + } + + function _pad(number) { + if (number < 10) { + return '0' + number; + } else { + return '' + number; + } + } +}).call(this);