unserver-unify
Version:
458 lines (452 loc) • 13.8 kB
JavaScript
;
angular.module('bamboo.course').controller('CourseDiscussCtrl', function($scope, smoothScroll, ApiService, loginService, CommonService, CourseApi, $state, $stateParams, $timeout) {
'use strict';
this.id = $stateParams.id;
var tid = $stateParams.tid;
this.cid = $stateParams.cid;
$scope.tabs = 'discussion';
this.mvsubject = {};
this._userdata = {};
var _self = this;
this.publicUrl = ApiService.SHOST + "/public/images/";
this.attachmentsURL = ApiService.SHOST + "/bbs/attachments/";
this.newtitle = "";
this.newcontent = "";
this.subtab = "Question";
this.r_content = "";
this.currentId = -1;
this.editmode = false;
$scope.replySubmitted = false;
$scope.addSubmitted = false;
this.pageSize = 10;
this.pageNumber=5;
var currenttab;
console.log(loginService.user);
if (loginService.user) {
if (loginService.user.is_admin || loginService.user.is_schoolAdmin) {
_self.adminflag = true;
}
}
this.scrollToEdit = function() {
smoothScroll(document.getElementById('reply_comments'));
}
var info = {
action: "getmvsidertopics",
sid: _self.id,
}
console.log(info);
ApiService.post('/mvtopics', info).then(function(result) {
console.log(result.data);
if (result.data.success) {
console.log(result.data.success);
$scope.newtopics = result.data.data.latest;
angular.forEach($scope.newtopics, function(item) {
var user = {
id: item.author_id,
avatar: item.author.avatar,
}
item.photourl = CommonService.getAvatarSrc(user);
})
$scope.hottopics = result.data.data.hotest;
angular.forEach($scope.hottopics, function(item) {
var user = {
id: item.author_id,
avatar: item.author.avatar,
}
item.photourl = CommonService.getAvatarSrc(user);
})
}
});
this.getTopics = function(tableState) {
var limit = tableState.pagination.number;
var start = tableState.pagination.start;
var search = tableState.search.predicateObject;
var params = {
sid: _self.id,
start: start,
limit: limit,
sort: tableState.sort,
search: search,
action: 'getsectiontopics',
};
//console.log("---1----");
console.log(params);
ApiService.post('/mvtopics', params).then(function(result) {
console.log(result.data);
if (result.data.success) {
$scope.topics = result.data.data.items;
console.log($scope.topics);
$scope.counter = result.data.data.counter || 0;
tableState.pagination.numberOfPages = Math.ceil(result.data.data.counter / limit);
}
});
}
var wikiImgSetting = {
image: true,
promise: true,
s3: true,
title: "Image",
maxheight: 400,
maxwidth: 400,
randomFlag: true,
resPath: "discussion/" + _self.id + "/",
};
this.addFile = function() {
if (!_self.newcontent) {
_self.newcontent = " ";
}
// initImageUrl(uploadSetting, index);
CommonService.uploadFile(wikiImgSetting).then(function(data) {
if (data.result) {
console.log(data.fileName);
var imgstr = '<img src="' + ApiService.SHOST + "/" + wikiImgSetting.resPath + data.fileName + '" width="50%"/>';
//console.log(imgstr);
_self.newcontent += imgstr;
} else {
CommonService.showError("Update failed: " + data.message);
}
});
}
this.addFile2Q = function() {
if (!_self.newcontent) {
_self.newcontent = "";
}
// initImageUrl(uploadSetting, index);
CommonService.uploadFile(wikiImgSetting).then(function(data) {
if (data.result) {
console.log(data.fileName);
var imgstr = '<img src="' + ApiService.SHOST + "/" + wikiImgSetting.resPath + data.fileName + '" width="50%"/>';
//console.log(imgstr);
_self.newtitle += imgstr;
} else {
CommonService.showError("Update failed: " + data.message);
}
});
}
var filesTopicSetting = {
image: false,
accept: ".pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.mp3,.mp4,.flv,.avi",
promise: true,
title: "Documents",
maxSize: 102400,
randomFlag: true,
multiple: true,
resPath: "bbs/attachments/",
};
this.videoTopicSetting = {
promise: true,
accept: ".mp4",
title: "Video Attachment",
videorecord: true,
randomFlag: true,
maxSize: 204800,
convert: true,
resPath: "bbs/attachments/"
};
_self.topic = {};
this.uploadTopicAttachment = function(setting) {
setting = setting || filesTopicSetting;
CommonService.directUploadFile(setting).then(function(data) {
if (data.result) {
var _ppts = [];
var _files = data.pass;
if (_self.topic.files && _self.topic.files.length >= 0) {
_self.topic.files = _self.topic.files.concat(_files);
} else {
_self.topic.files = _files;
}
//self.save();
} else {
CommonService.showError("Update failed: " + data.message);
}
});
}
this.deleteAttach = function(file) {
var idx = _self.topic.files.indexOf(file);
if (idx >= 0) {
_self.topic.files.splice(idx, 1);
}
}
this.uploadFile = function() {
if (!$scope.topic.content) {
$scope.topic.content = " ";
}
// initImageUrl(uploadSetting, index);
CommonService.uploadFile(wikiImgSetting).then(function(data) {
if (data.result) {
console.log(data.fileName);
var imgstr = '<img src="' + ApiService.SHOST + "/" + wikiImgSetting.resPath + data.fileName + '" width="50%"/>';
//console.log(imgstr);
$scope.topic.content += imgstr;
} else {
CommonService.showError("Update failed: " + data.message);
}
});
}
this.uploadReplyFile = function() {
if (!_self.r_content) {
_self.r_content = " ";
}
// initImageUrl(uploadSetting, index);
CommonService.uploadFile(wikiImgSetting).then(function(data) {
if (data.result) {
console.log(data.fileName);
var imgstr = '<img src="' + ApiService.SHOST + "/" + wikiImgSetting.resPath + data.fileName + '" width="50%"/>';
//console.log(imgstr);
_self.r_content += imgstr;
} else {
CommonService.showError("Update failed: " + data.message);
}
});
}
/*
function getMvsubjectInfo() {
$scope.id = _self.id;
var url = '/mvsubject/' + _self.id;
// $scope.hosturl = ApiService.getFullUrl(url);
//$scope.topicurl = ApiService.getFullUrl('/mvsubjects');
// $scope.photourl = ApiService.SHOST + '/public/' + ApiService.RES + '/mvsubject/' + _self.id + '/';
// getTopics();
CourseApi.getInfo(_self.id, function(_result) {
_self.mvsubject = _result;
$scope.subjectname = _self.mvsubject.name;
if (tid) {
_self.displaytopic(tid);
}
});
}*/
this.update = function(topic) {
var object = {
title: topic.title,
content: topic.content,
}
var info = {
action: 'update',
id: topic._id,
object: object,
}
CourseApi.post('/mvtopics', info);
}
this.top = function(id, flag) {
//console.log(id);
//console.log(flag);
var params = {
id: id,
is_top: flag,
};
CourseApi.post('/topic/top', params);
};
this.good = function(id, flag) {
//console.log(id);
//console.log(flag);
var params = {
id: id,
is_good: flag,
action: 'good',
};
CourseApi.post('/mvtopics', params);
};
this.reload = function() {
$scope.topic = null;
}
this.edit = function() {
_self.editmode = true;
}
this.displaytopic = function(id) {
console.log(id);
_self.currentId = id;
_self.editmode = false;
/*CourseApi.get('/fulltopic/' + id, function(result) {
console.log(result);
result.author_url = CommonService.getAvatarSrc(result.author);
var replies = result.replies;
var _replys = [];
angular.forEach(result.replies, function(reply, index) {
var author = {
id: reply.author._id,
avatar: reply.author.avatar,
}
reply.avatar_url = CommonService.getAvatarSrc(author);
});
$scope.topic = result;
}); */
ApiService.get('/topics/' + _self.currentId).then(function(result) {
if (result.data.success) {
$scope.topic = result.data.data;
var user = {
id: $scope.topic.author_id,
avatar: $scope.topic.author.avatar,
}
console.log(user);
$scope.topic.author_url = CommonService.getAvatarSrc(user);
console.log($scope.topic);
_self.pageChanged(1);
} else {
$scope.error = result.data.error;
}
});
};
this.pageChanged=function(page){
console.log(page);
var info={
tid:_self.currentId,
start:(page-1)*_self.pageNumber,
limit:_self.pageNumber,
}
console.log(info);
ApiService.post('/getreplies', info).then(function(result) {
if (result.data.success) {
console.log(result.data.data);
_self.totalCount=result.data.data.counter;
var replys=result.data.data.items;
angular.forEach(replys, function(reply, index) {
var author = {
id: reply.user._id,
avatar: reply.user.avatar,
}
reply.avatar_url = CommonService.getAvatarSrc(author);
})
_self.replies=replys;
}
});
}
this.addPost = function(form) {
$scope.addSubmitted = true;
if (form.$invalid) {
$scope.shaking = true;
$timeout(function() {
$scope.shaking = false;
}, 500);
return;
}
if (!_self.newtitle || _self.newtitle.length == 0) {
$scope.error = "title can not be empty.";
return;
}
var params = {
title: _self.newtitle,
subtab: _self.subtab,
t_content: _self.newcontent,
lid: _self.cid,
tab: "mvsubject",
sid: _self.id,
files: _self.topic.files
};
//console.log(_self.newcontent);
console.log(params);
ApiService.put('/topics', params).then(function(result) {
if (result.data.success) {
$state.reload();
} else {
CommonService.showError('Update Fail!');
}
});
};
this.addReply = function(form, id) {
$scope.replySubmitted = true;
if (form.$invalid) {
$scope.shaking = true;
$timeout(function() {
$scope.shaking = false;
}, 500);
return;
}
//console.log(id);
if (_self.r_content.length < 10) {
$scope.rerror = "Reply length can not less than 10.";
return;
}
var params = {
r_content: _self.r_content,
topic_id: id,
files:_self.attachmentFiles
};
CourseApi.put('/replies', params, function(){
CommonService.showNoBlockInfo('Update Successful!');
_self.r_content = '';
_self.pageChanged(1);
});
//$state.go('index.courses.course.section.discuss');
};
this.deleteReplay = function(reply) {
CommonService.confirm({
message: 'Are you sure you want to delete it?'
}).then(function() {
var params = {
reply_id: reply._id
};
CourseApi.post('/reply/' + reply._id + '/delete', params, function() {
CommonService.showNoBlockInfo('Delete Successful!');
_self.pageChanged(1);
});
});
};
this.delete = function(id) {
CommonService.confirm({
message: 'Are you sure you want to delete it?'
}).then(function() {
var params = {
id: id,
cid: _self.cid,
};
CourseApi.post('/topic/' + id + '/delete', params, function() {
CommonService.showNoBlockInfo('Delete Successful!');
// getMvsubjectInfo();
});
});
};
this.newPost = function(form) {
_self.newtitle = '';
_self.newcontent = '';
_self.subtab = "Question";
_self.currentId = null;
$scope.topic = null;
$scope.addSubmitted = false;
form.$setPristine();
form.$setValidity();
};
this.attachmentFiles = [];
this.replyAttachmentURL = ApiService.SHOST + "/reply/";
var filesetting = {
promise: true,
s3: true,
title: 'Attachment',
maxSize: 50000,
randomFlag: false,
resPath: "reply/" + _self.currentId + "/"
};
this.videoSetting = {
promise: true,
accept: ".mp4",
title: "Video Attachment",
videorecord: true,
randomFlag: true,
maxSize: 204800,
convert: true,
resPath: "reply/" + _self.currentId + "/"
};
_self.topic = {};
this.uploadAttachment = function(setting) {
setting = setting || filesetting;
CommonService.uploadFile(setting).then(function(data) {
console.log(data);
if (data.result) {
_self.attachmentFiles.push(data.fileName);
}
});
}
this.deleteAttachment = function(index) {
CommonService.showDeleteConfirm(function(){
_self.attachmentFiles.splice(index, 1);
});
}
// getMvsubjectInfo();
}).filter('toArray', function() {
return function(obj) {
var result = [];
angular.forEach(obj, function(val, key) {
result.push(val);
});
return result;
};
});