unserver-unify
Version:
187 lines (186 loc) • 5.86 kB
JavaScript
'use strict';
angular.module('bamboo.topic')
.controller('TopicEditCtrl', function($scope, CommonService, $rootScope, ApiService, AppConfig, $state, $stateParams, $timeout, $filter) {
var _self = this;
this.id = $stateParams.id;
this.mvtopic = false;
this.config = AppConfig;
$scope.submitted = false;
$scope.options = [{
value: 'share',
text: 'Share'
}, {
value: 'ask',
text: 'Q&A'
}, {
value: 'job',
text: 'Job'
}, {
value: 'support',
text: 'Support'
}, {
value: 'suggestion',
text: 'Suggestion '
}, {
value: 'blog',
text: 'Blog '
}, ];
this.topic = {};
$scope.validStrings = {
title: "Title cannot be empty.",
tab: "Please select a catogory.",
content: "Content cannot be empty OR the length can not less than 10."
};
this.mode;
$rootScope.editMode = _self.id ? 'Edit' : 'New';
console.log("----------- step 0" + $state.current.name);
if ($state.current.name == "index.bbs.tab.new") {
console.log("--- new topic");
_self.mode = "newtopic";
$scope.action = "New Discussion";
_self.topic = {
allowcomment : undefined //don't have this feature for bbs
};
$scope.saveButtonTitle = $filter('trans')({eng : "Add", chn : '创建'});
$scope.options = [];
// console.log(_self.bbstab);
} else {
getMvsubjectInfo();
}
function getMvsubjectInfo() {
if (_self.id) {
//console.log("edit mode :" + _self.id);
$scope.action = "Edit Discussion";
$scope.saveButtonTitle = $filter('trans')({eng : "Update", chn : '更新'});
ApiService.get('/topics/' + _self.id).then(function(result) {
//console.log(result);
if (result.data.success) {
console.log(result.data.data);
_self.topic.content = result.data.data.content;
_self.topic.title = result.data.data.title;
_self.topic.allowcomment = result.data.data.allowcomment;
if ($state.current.name == "index.bbs.tab.edit") {
$scope.options = [];
return;
}
var value = result.data.data.tab;
if (value == 'mvsubject') {
_self.mvtopic = true;
$scope.options = [{
value: "Question",
text: "Question"
}, {
value: "Discuss",
text: "Discuss"
}];
}
value = result.data.data.subtab;
var index = 0;
for (var i = 0; i < $scope.options.length; i++) {
if (value == $scope.options[i].value) {
index = i;
break;
}
}
_self.topic.tab = $scope.options[index];
} else {
CommonService.showError(result.data.error);
}
});
} else {
_self.mode = "newtopic";
$scope.action = "New Discussion";
_self.topic = {
allowcomment: true
};
$scope.saveButtonTitle = $filter('trans')({eng : "Add", chn : '创建'});
//console.log("publish mode");
}
}
this.submit = function() {
$scope.submitted = true;
if ($scope.form.$invalid) {
$scope.shaking = true;
$timeout(function() {
$scope.shaking = false;
}, 500);
return;
}
//console.log(_self.topic);
console.log($state.current.name);
if (!_self.topic.tab && $state.current.name != "index.bbs.tab.new" && $state.current.name != "index.bbs.tab.edit") {
CommonService.showError($scope.validStrings.tab);
return;
}
if (!_self.topic.title) {
CommonService.showError($scope.validStrings.title);
return;
}
if (!_self.topic.content || _self.topic.content.length < 10) {
CommonService.showError($scope.validStrings.content);
return;
}
var params = {
title: _self.topic.title,
// tab: _self.topic.tab.value,
t_content: _self.topic.content,
allowcomment: _self.topic.allowcomment,
};
if (_self.topic.tab && $state.current.name != "index.bbs.tab.edit") {
params.tab = _self.topic.tab.value;
}
//console.log(params);
if (_self.id) {
params["tid"] = _self.id;
if (_self.mvtopic) {
params['subtab'] = params.tab;
params.tab = "mvsubject";
}
console.log(params);
ApiService.post('/topics', params).then(function(result) {
//console.log(result);
if (result.data.success) {
$state.go('index.blogs.detail', {
id: _self.id
});
} else {
CommonService.showError(result.data.error);
}
});
} else {
console.log(params);
ApiService.put('/topics', params).then(function(result) {
console.log(result);
if (result.data.success) {
$state.go('^');
} else {
CommonService.showError(result.data.error);
}
});
}
};
var forumurl = ApiService.PSURL + '/forum/' + _self.id + "/";
var wikiImgSetting = {
image: true,
promise: true,
s3: true,
title: "Image",
maxhieght: 400,
maxwidth: 400,
randomFlag: true,
resPath: "public/" + ApiService.RES + "/forum/" + _self.id + "/",
};
this.uploadFile = function(uploadSetting, index) {
// 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.topic.content += imgstr;
} else {
CommonService.showError("Update failed: " + data.message);
}
});
};
});