unserver-unify
Version:
238 lines (237 loc) • 7.03 kB
JavaScript
;
angular.module('bamboo.topic').controller('TopicCtrl', function($scope, CourseApi, ApiService, smoothScroll, loginService, CommonService, AppConfig, $state, $stateParams) {
this.id = $stateParams.id;
this.editmode = false;
this._userdata = {};
this.config = AppConfig;
var _self = this;
this.publicUrl = ApiService.SHOST + "/public/images/";
this.defaultPersonPhotoUrl = this.publicUrl + "user_icon&48.png";
this.attachmentsURL = ApiService.SHOST + "/bbs/attachments/";
this.avatarpath = ApiService.PSURL + "/avatar/";
this.r_content = "";
$scope.topicexist = false;
this.pageNumber=5;
getTopicInfo();
this.scrollToEdit = function() {
smoothScroll(document.getElementById('reply_comments'));
}
if (loginService.user) {
if (loginService.user.is_admin || loginService.user.is_schoolAdmin) {
_self.adminflag = true;
}
}
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.edit = function() {
_self.editmode = true;
}
function getTopicInfo() {
var result = loginService.user;
if (result) {
_self._userdata.name = result.name;
_self._userdata.userurl = result.userurl;
_self._userdata.fullname = result.fullname;
_self._userdata.is_admin = result.is_admin;
$scope.login_user = true;
$scope.current_user = _self._userdata;
}
ApiService.get('/topics/' + _self.id).then(function(result) {
if (result.data.success) {
$scope.topicexist = true;
$scope.topic = result.data.data;
console.log($scope.topic);
var user = {
id: $scope.topic.author_id,
avatar: $scope.topic.author.avatar,
}
console.log(user);
$scope.topic.author_url = CommonService.getAvatarSrc(user);
/* angular.forEach($scope.topic.replies, function(reply, index) {
var author = {
id: reply.author._id,
avatar: reply.author.avatar,
}
reply.avatar_url = CommonService.getAvatarSrc(author);
})*/
// console.log($scope.topic);
} else {
$scope.error = result.data.error;
}
});
}
this.pageChanged=function(page){
console.log(page);
var info={
tid:_self.id,
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.pageChanged(1);
this.top = function(id, flag) {
var params = {
id: id,
is_top: flag,
};
ApiService.post('/topic/top', params).then(function(result) {
if (result.data.success) {
$state.reload();
} else {
$scope.error = result.data.error;
}
});
};
this.delete = function() {
CommonService.confirm({
message: 'Are you sure you want to delete it?'
}).then(function() {
var params = {
id: _self.id
};
ApiService.post('/topic/' + _self.id + '/delete', params).then(function(result) {
if (result.data.success) {
$state.go("^");
} else {
$scope.error = result.data.error;
}
});
});
};
this.deleteReplay = function(reply) {
CommonService.confirm({
message: 'Are you sure you want to delete it?'
}).then(function() {
var params = {
reply_id: reply._id
};
ApiService.post('/reply/' + reply._id + '/delete', params).then(function(result) {
if (result.data.success) {
$state.reload();
} else {
$scope.error = result.data.error;
}
});
});
};
this.good = function(id, flag) {
var params = {
id: id,
is_good: flag,
action: 'good',
};
ApiService.post('/mvtopics', params).then(function(result) {
if (result.data.success) {
$state.reload();
} else {
$scope.error = result.data.error;
}
});
};
this.addReply = function(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
};
console.log(params);
ApiService.put('/replies', params).then(function(result) {
if (result.data.success) {
$state.reload();
} else {
$scope.error = result.data.error;
}
});
};
var wikiImgSetting = {
image: true,
promise: true,
s3: true,
title: "Image",
maxheight: 400,
maxwidth: 400,
randomFlag: true,
resPath: "public/" + ApiService.RES + "/reply/" + _self.id + "/",
};
var forumurl = ApiService.PSURL + '/reply/' + _self.id + "/";
this.uploadFile = function() {
// initImageUrl(uploadSetting, index);
CommonService.uploadFile(wikiImgSetting).then(function(data) {
if (data.result) {
console.log(data.fileName);
var imgstr = '<img src="' + forumurl + data.fileName + '" width="50%"/>';
//console.log(imgstr);
_self.r_content += imgstr;
} else {
CommonService.showError("Update failed: " + data.message);
}
});
};
this.OnSelectItem = function(_idx) {
$scope.idx = _idx;
}
this.attachmentFiles = [];
this.replyAttachmentURL = ApiService.SHOST + "/reply/" + _self.id + "/";
var filesetting = {
promise: true,
s3: true,
title: 'Attachment',
maxSize: 50000,
randomFlag: false,
resPath: "reply/" + _self.id + "/"
};
this.videoSetting = {
promise: true,
accept: ".mp4",
title: "Video Attachment",
videorecord: true,
randomFlag: true,
maxSize: 204800,
convert: true,
resPath: "reply/" + _self.id + "/"
};
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);
});
}
});