Skip to content

Commit 325fb2b

Browse files
committed
chore: Initial setup of JS files and webpack config
1 parent ee8eeae commit 325fb2b

39 files changed

Lines changed: 8918 additions & 2 deletions

webpack.common.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ module.exports = Merge.smart({
139139
ReactRenderer: './common/static/js/src/ReactRenderer.jsx',
140140
XModuleShim: './xmodule/js/src/xmodule.js',
141141
VerticalStudentView: './xmodule/assets/vertical/public/js/vertical_student_view.js',
142+
VideoBlockMain: './xmodule/assets/video/public/js/video_block_main.js',
142143
commons: 'babel-polyfill'
143144
},
144145

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Provides a convenient way to process a large amount of data without UI blocking.
3+
*
4+
* @param {Array} list - The array to process.
5+
* @param {Function} process - The function to execute on each item.
6+
* @returns {Promise<Array>} - Resolves with the processed array.
7+
*/
8+
export const AsyncProcess = {
9+
array(list, process) {
10+
// Validate input
11+
if (!Array.isArray(list)) {
12+
return Promise.reject(new Error('Input is not an array'));
13+
}
14+
15+
if (typeof process !== 'function' || list.length === 0) {
16+
return Promise.resolve(list);
17+
}
18+
19+
const MAX_DELAY = 50;
20+
const result = [];
21+
let index = 0;
22+
const len = list.length;
23+
24+
const handler = (resolve) => {
25+
const start = Date.now();
26+
27+
while (index < len && Date.now() - start < MAX_DELAY) {
28+
result[index] = process(list[index], index);
29+
index++;
30+
}
31+
32+
if (index < len) {
33+
setTimeout(() => handler(resolve), 25);
34+
} else {
35+
resolve(result);
36+
}
37+
};
38+
39+
return new Promise((resolve) => {
40+
setTimeout(() => handler(resolve), 25);
41+
});
42+
}
43+
};
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import $ from 'jquery';
2+
import _ from 'underscore';
3+
4+
'use strict';
5+
6+
/**
7+
* Video commands module.
8+
*
9+
* @constructor
10+
* @param {Object} state - The object containing the state of the video
11+
* @param {Object} i18n - The object containing strings with translations
12+
* @return {jQuery.Promise} - A resolved jQuery promise
13+
*/
14+
function VideoCommands(state, i18n) {
15+
if (!(this instanceof VideoCommands)) {
16+
return new VideoCommands(state, i18n);
17+
}
18+
19+
_.bindAll(this, 'destroy');
20+
this.state = state;
21+
this.state.videoCommands = this;
22+
this.i18n = i18n;
23+
this.commands = [];
24+
this.initialize();
25+
26+
return $.Deferred().resolve().promise();
27+
}
28+
29+
VideoCommands.prototype = {
30+
/**
31+
* Initializes the module by loading commands and binding events.
32+
*/
33+
initialize: function () {
34+
this.commands = this.getCommands();
35+
this.state.el.on('destroy', this.destroy);
36+
},
37+
38+
/**
39+
* Cleans up the module by removing event handlers and deleting the instance.
40+
*/
41+
destroy: function () {
42+
this.state.el.off('destroy', this.destroy);
43+
delete this.state.videoCommands;
44+
},
45+
46+
/**
47+
* Executes a given command with optional arguments.
48+
*
49+
* @param {String} command - The name of the command to execute
50+
* @param {...*} args - Additional arguments to pass to the command
51+
*/
52+
execute: function (command, ...args) {
53+
if (_.has(this.commands, command)) {
54+
this.commands[command].execute(this.state, ...args);
55+
} else {
56+
console.log(`Command "${command}" is not available.`);
57+
}
58+
},
59+
60+
/**
61+
* Returns the available commands as an object.
62+
*
63+
* @return {Object} - A dictionary of available commands
64+
*/
65+
getCommands: function () {
66+
const commands = {};
67+
const commandsList = [
68+
playCommand,
69+
pauseCommand,
70+
togglePlaybackCommand,
71+
toggleMuteCommand,
72+
toggleFullScreenCommand,
73+
setSpeedCommand,
74+
skipCommand,
75+
];
76+
77+
_.each(commandsList, (command) => {
78+
commands[command.name] = command;
79+
});
80+
81+
return commands;
82+
},
83+
};
84+
85+
/**
86+
* Command constructor.
87+
*
88+
* @constructor
89+
* @param {String} name - The name of the command
90+
* @param {Function} execute - The function to execute the command
91+
*/
92+
function Command(name, execute) {
93+
this.name = name;
94+
this.execute = execute;
95+
}
96+
97+
// Individual command definitions
98+
const playCommand = new Command('play', (state) => {
99+
state.videoPlayer.play();
100+
});
101+
102+
const pauseCommand = new Command('pause', (state) => {
103+
state.videoPlayer.pause();
104+
});
105+
106+
const togglePlaybackCommand = new Command('togglePlayback', (state) => {
107+
if (state.videoPlayer.isPlaying()) {
108+
pauseCommand.execute(state);
109+
} else {
110+
playCommand.execute(state);
111+
}
112+
});
113+
114+
const toggleMuteCommand = new Command('toggleMute', (state) => {
115+
state.videoVolumeControl.toggleMute();
116+
});
117+
118+
const toggleFullScreenCommand = new Command('toggleFullScreen', (state) => {
119+
state.videoFullScreen.toggle();
120+
});
121+
122+
const setSpeedCommand = new Command(
123+
'speed',
124+
(state, speed) => {
125+
state.videoSpeedControl.setSpeed(state.speedToString(speed));
126+
}
127+
);
128+
129+
const skipCommand = new Command('skip', (state, doNotShowAgain) => {
130+
if (doNotShowAgain) {
131+
state.videoBumper.skipAndDoNotShowAgain();
132+
} else {
133+
state.videoBumper.skip();
134+
}
135+
});
136+
137+
export {VideoCommands};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
/**
4+
* Creates a new object with the specified prototype object and properties.
5+
* @param {Object} o The object which should be the prototype of the newly-created object.
6+
* @private
7+
* @throws {TypeError, Error}
8+
* @return {Object}
9+
*/
10+
const inherit = Object.create || (function () {
11+
const F = function () { };
12+
13+
return function (o) {
14+
if (arguments.length > 1) {
15+
throw Error('Second argument not supported');
16+
}
17+
if (_.isNull(o) || _.isUndefined(o)) {
18+
throw Error('Cannot set a null [[Prototype]]');
19+
}
20+
if (!_.isObject(o)) {
21+
throw TypeError('Argument must be an object');
22+
}
23+
24+
F.prototype = o;
25+
return new F();
26+
};
27+
}());
28+
29+
/**
30+
* Component constructor function.
31+
* Calls `initialize()` if defined.
32+
* @returns {any}
33+
*/
34+
function Component() {
35+
if ($.isFunction(this.initialize)) {
36+
return this.initialize.apply(this, arguments);
37+
}
38+
}
39+
40+
/**
41+
* Adds an `extend` method to the Component constructor.
42+
* Creates a subclass that inherits from Component.
43+
* @param {Object} protoProps - Prototype methods and properties.
44+
* @param {Object} staticProps - Static methods and properties.
45+
* @returns {Function} Child constructor.
46+
*/
47+
Component.extend = function (protoProps, staticProps) {
48+
const Parent = this;
49+
50+
const Child = function () {
51+
if ($.isFunction(this.initialize)) {
52+
return this.initialize.apply(this, arguments);
53+
}
54+
};
55+
56+
Child.prototype = inherit(Parent.prototype);
57+
Child.constructor = Parent;
58+
Child.__super__ = Parent.prototype;
59+
60+
if (protoProps) {
61+
$.extend(Child.prototype, protoProps);
62+
}
63+
64+
$.extend(Child, Parent, staticProps);
65+
66+
return Child;
67+
};
68+
69+
export { Component };

0 commit comments

Comments
 (0)