unserver-unify
Version:
244 lines (243 loc) • 7.43 kB
JavaScript
;
angular.module('bamboo.topic').controller('BBSCtrl', function($scope, deviceDetector, $rootScope, TopicApi, ApiService, loginService, CommonService, $state, smoothScroll, $stateParams) {
this.id = $stateParams.id;
this.mvsubject = {};
this._userdata = {};
var _self = this;
this.attachmentsURL = ApiService.SHOST + "/bbs/attachments/";
this.publicUrl = ApiService.SHOST + "/public/images/";
this.defaultPersonPhotoUrl = this.publicUrl + "user_icon&48.png";
this.avatarpath = ApiService.PSURL + "/avatar/";
this.r_content = "";
this.pageNumber = 5;
$scope.topicexist = false;
this.scrollToEdit = function() {
smoothScroll(document.getElementById('reply_comments'));
}
function getBBSInfo() {
console.log("--- bbs detail ---");
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('/bbs/' + _self.id).then(function(_result) {
// console.log(_result);
if (_result.data.success) {
$scope.topicexist = true;
// console.log(_result.data.data);
$scope.topic = _result.data.data;
if (deviceDetector.isMobile() && !deviceDetector.isTablet()) {
$rootScope.detailName = "BBS";
} else {
$rootScope.detailName = $scope.topic.title;
}
// post-processing of author's avatar
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) {
console.log(reply);
var author = {
id: reply.author._id,
avatar: reply.author.avatar,
}
reply.avatar_url = CommonService.getAvatarSrc(author);
})*/
} else {
$scope.error = _result.data.error;
}
});
}
getBBSInfo();
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('/getbbsreplies', 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(flag) {
console.log("-- top --");
var params = {
id: _self.id,
is_top: flag,
action: "top",
};
console.log(params);
TopicApi.BBSApi(params, function(result) {
$state.reload();
});
};
this.delete = function() {
CommonService.confirm({
message: 'Are you sure you want to delete it?'
}).then(function() {
var params = {
id: _self.id,
action: "delete",
};
TopicApi.BBSApi(params, function(result) {
$state.go("^");
});
});
};
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('/bbsreply/' + reply._id + '/delete', params).then(function(result) {
if (result.data.success) {
$state.reload();
} else {
$scope.error = result.data.error;
}
});
});
};
this.deleteMyReplay = function(reply, idx) {
CommonService.confirm({
message: 'Are you sure you want to delete it?'
}).then(function() {
var params = {
reply_id: reply._id
};
ApiService.post('/bbsreply/' + reply._id + '/delete', params).then(function(result) {
if (result.data.success) {
CommonService.showNoBlockInfo('Delete Successful!');
$state.reload();
} else {
$scope.error = result.data.error;
}
});
});
};
this.good = function(id, flag) {
var params = {
id: id,
is_good: flag,
action: "good",
};
TopicApi.BBSApi(params, function(result) {
$state.reload();
});
};
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('/bbsreplies', params).then(function(result) {
if (result.data.success) {
$state.reload();
} else {
$scope.error = result.data.error;
}
});
};
var wikiImgSetting = {
image: true,
promise: true,
title: "Image",
maxheight: 400,
maxwidth: 400,
randomFlag: true,
resPath: "public/" + ApiService.RES + "/reply/" + _self.id || 'new' + "/",
};
var forumurl = ApiService.PSURL + '/reply/' + _self.id || 'new' + "/";
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);
});
}
}).filter('filterFileExt', function() {
return function(input) {
console.log(input);
if(input){
var ext = input.split('.').pop();
return ext;
} else {
return input;
}
}
});