unserver-unify
Version:
171 lines (164 loc) • 4.71 kB
JavaScript
;
angular.module('bamboo.course').controller('CourseDocCtrl', function($scope, ApiService, loginService, CourseApi, $stateParams, $uibModal, $timeout, $window, CommonService) {
this.id = $stateParams.id;
this.cid = $stateParams.cid;
this.mvsubject = {};
var downloadNumbers = [];
$scope.tabs = 'doc';
var _self = this;
this.contentModelPicUrl = ApiService.SHOST + '/lcourse/' + _self.cid + '/download/';
function getInfo() {
$scope.id = _self.id;
CourseApi.getInfo(_self.id, function(result) {
_self.mvsubject = result;
$scope.docs = _self.mvsubject.docs;
console.log($scope.docs);
processDocs($scope.docs);
});
}
function processDocs(docs) {
angular.forEach(docs, function(val, idx) {
if(val.timecontrol) {
var start = new Date(val.starttime).getTime();
var end = new Date(val.endtime).getTime();
var now = Date.now();
if (now >= start && now <= end && val.availableForDL) {
val.availableForDL = true;
} else {
val.availableForDL = false;
}
}
});
}
this.className = {
pdf: "service-block-red",
doc: "service-block-dark",
ppt: "service-block-u",
xls: "service-block-blue",
mp3: "service-block-sea",
mp4: "service-block-yellow",
};
this.colorName = {
pdf: "icon-color-red",
doc: "icon-color-purple",
ppt: "icon-color-orange",
xls: "icon-color-brown",
mp3: "icon-color-darkblue",
mp4: "icon-color-sea",
zip: "icon-color-blue"
};
this.titleName = {
pdf: "PDF",
doc: "Word",
ppt: "PPT",
xls: "Excel",
mp3: "Audio",
mp4: "Video",
zip: "Zip"
};
this.iconName = {
pdf: "fa-file-pdf-o",
doc: "fa-file-word-o",
ppt: "fa-file-powerpoint-o",
xls: "fa-file-excel-o",
mp3: "fa-music",
mp4: "fa-video-camera",
zip: "fa fa-file-archive-o"
};
getInfo();
$scope.onDownload = function(index) {
var exist = false;
if (downloadNumbers.indexOf(index) < 0) {
downloadNumbers.push(index);
}
};
$scope.$on("$destroy", function() {
if (!downloadNumbers || downloadNumbers.length < 1) {
return;
}
if (loginService.user) {
var info = {
action: "recorddownloads",
records: downloadNumbers,
id: _self.id,
};
CourseApi.mvApi(info, function(data) {
console.log(data);
});
} else {
console.log("aleady logout!");
}
})
// controller function to open a modal to play MP4
this.viewMp4 = function(mp4file) {
$uibModal.open({
templateUrl: 'app/course/download/mp4-dlg.html',
size: 'lg',
controller: 'Mp4DlgCtrl',
controllerAs: 'ctrl',
backdrop: 'static',
resolve: {
// send mp4 file url to dlg controller
mp4url: function() {
return (_self.contentModelPicUrl + mp4file);
}
}
}).result.then(function() {})
}
this.onCheckBoxSelected = function() {
if (_self.selectALL) {
$scope.docs.forEach(function(item) {
item.checked = true;
});
} else {
$scope.docs.forEach(function(item) {
item.checked = false;
});
}
}
function urlToPromise(url)
{
return new Promise(function(resolve, reject)
{
JSZipUtils.getBinaryContent(url, function (err, data)
{
if(err)
{
reject(err);
} else {
resolve(data);
}
});
});
}
this.bulkDownload = function(){
// $scope.docs.forEach(function(item) {
// if (item.checked&&item.download) {
// $timeout(function () {
// console.log(item);
// $window.open(item.download, '_blank');
// },500);
// }
// });
if(ApiService.MEDIAPATH) {
_self.contentModelPicUrl = ApiService.MEDIAPATH + '/lcourse/' + _self.cid + '/download/';
}
var zip = new JSZip();
var zipFilename = "meterials.zip";
var download = false;
$scope.docs.forEach(function(item) {
if (item.checked&&item.des&&item.availableForDL) {
var url = _self.contentModelPicUrl+item.file;
zip.file(item.des, urlToPromise(url), {binary:true});
download = true;
}
});
if(download) {
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, zipFilename);
});
} else {
CommonService.showError('Please select to download');
}
}
});