unserver-unify
Version:
134 lines (131 loc) • 4.09 kB
JavaScript
'use strict';
angular.module('bamboo.blog')
.controller('BlogEditCtrl', function($scope, TopicApi, 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 = [];
this.topic = {};
$scope.validStrings = {
title: "Title cannot be empty.",
tab: "Please select a category.",
content: "Content cannot be empty OR the length can not less than 10."
};
this.mode;
$rootScope.editMode = _self.id ? 'Edit' : 'New';
getBlogInfo();
function getBlogInfo() {
if (_self.id) {
//console.log("edit mode :" + _self.id);
$scope.action = "Edit Discussion";
$scope.saveButtonTitle = $filter('trans')({
eng: "Update",
chn: '更新'
});
ApiService.get('/blogs/' + _self.id).then(function(result) {
//console.log(result);
if (result.data.success) {
console.log(result.data.data);
_self.topic = result.data.data;
} 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");
}
$scope.options = [];
$scope.options = $scope.options.concat(ApiService.blogcategory);
console.log($scope.options );
var pos = $scope.options.indexOf('all');
if (pos > -1) {
console.log(pos);
$scope.options.splice(pos, 1);
}
console.log($scope.options );
}
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,
};
params.tab = _self.topic.tab;
//console.log(params);
if (_self.id) {
params["tid"] = _self.id;
params.action = "update";
console.log(params);
TopicApi.BlogApi(params, function(result) {
$state.go('index.blogs.detail', {
id: _self.id
});
});
} else {
console.log(params);
params.action = "create";
TopicApi.BlogApi(params, function(result) {
console.log(result);
$state.go('^');
});
}
};
var forumurl = ApiService.PSURL + '/blog/' + _self.id + "/";
var wikiImgSetting = {
image: true,
promise: true,
s3: true,
title: "Image",
maxheight: 400,
maxwidth: 400,
randomFlag: true,
resPath: "public/" + ApiService.RES + "/blog/" + _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);
}
});
};
});