socialforge/public/javascripts/wechat/controllers/issue.js

129 lines
4.3 KiB
JavaScript

app.controller('IssueController', ['$scope', '$http', '$routeParams', 'auth', 'common',
function($scope, $http, $routeParams, auth, common){
var vm = $scope;
vm.previewImgUrls = [];
vm.showAtDialog = false;
var parseAtPersons = function (comment) {
var selectedPersons = [];
var ss = comment.match(/@(.+?)\s+/g);
for(var i in ss){
var personName = ss[i].substr(1, ss[i].length-2);
console.log(personName);
for(var j in vm.at_persons){
var person = vm.at_persons[j];
if(person.name == personName){
selectedPersons.push(person);
}
}
}
for(var i in selectedPersons){
var person = selectedPersons[i];
comment = comment.replace('@'+person.name+' ',
'<span class="atwho-inserted"><span class="at" data-user-id="'+person.id
+'"><a href="/users/'+person.id+'">@'+person.name+'('+person.login+')'+' </a></span></span>'
);
}
return comment;
};
vm.onPostChange = function (newValue, oldValue) {
if(newValue.length > oldValue.length && newValue.match(/@$/)=='@'){
console.log('@ fire');
vm.showAtDialog = true;
if(!vm.at_persons){
$http.get('/at/'+$routeParams.id+'.json?type=Issue').then(function (response) {
vm.at_persons = response.data;
});
}
}
console.log(vm.issue.comment);
};
vm.selectAtPerson = function (index) {
var person = vm.at_persons[index];
vm.showAtDialog = false;
vm.issue.comment += person.name + ' ';
};
vm.cancelAt = function(){
vm.showAtDialog = false;
}
common.init({
id: $routeParams.id,
scope: $scope,
type: 'issues',
replyType: 'Issue',
urlName: 'issues',
loadCallback: function(data){
console.log(data.data);
if(data.status == -1){
$scope.showtip = true;
return;
}
//回复级别 0 一级回复 1 二级回复
replytype = data.type;
page = data.page;
var parseImgAttachment = function(attachments){
var urls = [];
if(!attachments){
return urls;
}
for(var i = attachments.length-1; i>=0; i--){
if(attachments[i].filename.match('.jpg$')=='.jpg' || attachments[i].filename.match('.png$')=='.png'){
urls.push(attachments[i].download_url);
attachments.splice(i, 1);
}
}
return urls;
};
if (replytype == 0){
if (page == 0){
$scope.issue = data.data;
$scope.page = 0;
$scope.is_public = data.is_public;
$scope.previewImgUrls = parseImgAttachment($scope.issue.attachments);
}
else{
$scope.issue.all_children = $scope.issue.all_children.concat(data.data.all_children);
}
$scope.has_more = $scope.issue.all_children.length < $scope.issue.comment_count;
console.log($scope.has_more);
}
else{
comment_id = data.data.id;
for (var i in $scope.issue.all_children) {
var comment = $scope.issue.all_children[i];
if(comment.id == comment_id){
// comment.parents_reply_top = comment.parents_reply_top.concat(data.data.parents_reply_top);
comment.parents_reply_top = data.data.parents_reply_top.concat(comment.parents_reply_top);
}
}
}
},
replyCallback: function(){
},
beforeReplay: function(data){
return parseAtPersons(data);
}
});
$scope.previewImg = function(index){
console.log(index);
wx.previewImage({
current: $scope.previewImgUrls[index], // 当前显示图片的http链接
urls: $scope.previewImgUrls // 需要预览的图片http链接列表
});
}
}]);