unserver-unify
Version:
156 lines (155 loc) • 4.75 kB
JavaScript
;
angular.module('bamboo.blog').controller('BlogDetailCtrl', function($scope, ApiService, TopicApi, loginService, CommonService, $state, smoothScroll, $stateParams) {
this.id = $stateParams.id;
this.mvsubject = {};
this._userdata = {};
var _self = this;
this.publicUrl = ApiService.SHOST + "/public/images/";
this.defaultPersonPhotoUrl = this.publicUrl + "user_icon&48.png";
this.avatarpath = ApiService.PSURL + "/avatar/";
this.r_content = "";
$scope.topicexist = false;
getBlogInfo();
this.pageNumber = 5;
this.scrollToEdit = function() {
smoothScroll(document.getElementById('reply_comments'));
}
function getBlogInfo() {
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('/blogs/' + _self.id).then(function(result) {
if (result.data.success) {
$scope.topicexist = true;
$scope.topic = result.data.data;
console.log($scope.topic);
$scope.allowcomment = $scope.topic.allowcomment;
/* if ($scope.topic.replies) {
for (var i = 0; i < $scope.topic.replies.length; i++) {
var reply = $scope.topic.replies[i];
reply.avatar_url = CommonService.getAvatarSrc(reply.author);
}
}*/
} 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('/getblogreplies', 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,
action: "top",
};
TopicApi.BlogApi(params, function(result) {
$state.reload();
});
};
this.delete = function() {
CommonService.showConfirm('Are you sure you want to delete it?', function() {
var params = {
id: _self.id,
action: "delete",
};
TopicApi.BlogApi(params, function(result) {
$state.go("^");
});
});
};
this.deleteReply = function(id) {
CommonService.showConfirm('Are you sure you want to delete it?', function() {
var params = {
reply_id: id
};
ApiService.post('/blogreply/' + 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",
};
TopicApi.BlogApi(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,
};
ApiService.put('/blogreplies', 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() {
CommonService.uploadFile(wikiImgSetting).then(function(data) {
if (data.result) {
var imgstr = '<img src="' + forumurl + data.fileName + '" width="50%"/>';
_self.r_content += imgstr;
} else {
CommonService.showError("Update failed: " + data.message);
}
});
};
this.OnSelectItem = function(_idx) {
$scope.idx = _idx;
}
});