Merge pull request #2616 from edx/valera/convert_time_to_js

Converting Time module to JS.
This commit is contained in:
Valera Rozuvan
2014-02-22 20:41:39 +02:00
7 changed files with 113 additions and 53 deletions

View File

@@ -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):

View File

@@ -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

View File

@@ -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'

View File

@@ -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);

View File

@@ -6,4 +6,8 @@
# Video are written in pure JavaScript.
!video/*.js
!video/transcripts/*.js
!video/transcripts/*.js
# Converted to JS from CoffeeScript.
!time.js

View File

@@ -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)

View File

@@ -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);