le-player
Version:
The best HTML5 video player made for Lectoriy.
92 lines (78 loc) • 1.89 kB
JavaScript
;
/**
* @file DownloadControl.js
*
* Download control
*/
import $ from 'jquery';
import Control from './Control';
import Component from './Component';
import Icon from './Icon';
/**
* @param {Player} player Main player
* @param {Object} [options]
* @class DownloadControl
* @exnteds Control
*/
class DownloadControl extends Control {
constructor (player, options={}) {
options = $.extend({
title : 'Скачать видео',
className : 'download',
name : 'download',
}, options);
super(player, options);
this.player.on('loadstart', this.onLoadStart.bind(this));
}
/**
* @override
*/
createElement() {
// TODO: Доопределить этот метод, а не переопредлеить
this.element = $('<a />')
.attr({
href : '',
target : '_blank',
download : true,
title : this.options.title,
})
.addClass(this.buildCSSClass())
.append(new Icon(this.player, { iconName : 'download' }).element);
}
/**
* @override
*/
buildCSSClass() {
return `${super.buildCSSClass()}`;
}
/**
* Setter of link field
* @public
* @param {String} value Path for video
*/
set link (value) {
const title = this.player.options.title;
let fileName;
if(title != null) {
fileName = decodeURIComponent(title) + '.' + getFileExtension(value);
} else {
const parser = document.createElement('a');
parser.href = value;
fileName = parser.pathname.split('/');
fileName = fileName[fileName.length - 1];
}
this.element.attr({
href : value,
download : fileName
});
}
onLoadStart(e, data) {
this.link = this.player.video.src.url;
}
}
function getFileExtension(fileName) {
return fileName.split('.').pop();
}
Component.registerComponent('DownloadControl', DownloadControl);
Control.registerControl('download', DownloadControl);
export default DownloadControl;