UNPKG

unserver-unify

Version:

190 lines (189 loc) 5.82 kB
'use strict'; angular.module('bamboo.topic').controller('BBSEditCtrl', function($scope, TopicApi, CommonService, ApiService, $state, $stateParams, $timeout, $filter) { var _self = this; this.id = $stateParams.id; this.mvtopic = false; $scope.submitted = false; 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; $scope.editMode = _self.id ? 'Edit' : 'New'; this.subtab = "Discuss"; 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 { getInfo(); } function getInfo() { if (_self.id) { //console.log("edit mode :" + _self.id); $scope.action = "Edit Discussion"; $scope.saveButtonTitle = $filter('trans')({ eng: "Update", chn: '更新' }); ApiService.get('/bbs/' + _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; _self.topic.files = result.data.data.files || []; } 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.title) { CommonService.showError($scope.validStrings.title); return; } if (!_self.topic.content || _self.topic.content.length < 5) { CommonService.showError($scope.validStrings.content); return; } var params = { title: _self.topic.title, files:_self.topic.files, // tab: _self.topic.tab.value, t_content: _self.topic.content, allowcomment: _self.topic.allowcomment, }; if (_self.id) { var object = { title: _self.topic.title, files:_self.topic.files, t_content: _self.topic.content, allowcomment: _self.topic.allowcomment }; params = { id : _self.id, action : "updatebbs", object : object }; console.log(params); TopicApi.BBSApi(params, function(result) { console.log(result); $state.go('index.bbs.tab.detail', { id: _self.id }); }); } else if ($state.current.name == "index.bbs.tab.new") { params.subtab = $stateParams.subkey; // console.log(params); params.action = "create"; TopicApi.BBSApi(params, function(result) { // console.log(result); $state.go('^'); }); } }; var forumurl = ApiService.PSURL + '/bbs/' + _self.id + "/"; var wikiImgSetting = { image: true, promise: true, title: "Image", maxheight: 480, maxwidth: 640, randomFlag: true, resPath: "public/" + ApiService.RES + "/bbs/" + _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); } }); }; var filesSetting = { image: false, accept: ".pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.mp3,.mp4,.flv,.avi", promise: true, title: "Documents", maxSize: 102400, randomFlag: true, multiple: true, resPath: "bbs/attachments/", }; this.videoSetting = { promise: true, accept: ".mp4", title: "Video Attachment", videorecord: true, randomFlag: true, maxSize: 204800, convert: true, resPath: "bbs/attachments/" }; this.uploadAttachment = function(setting) { setting = setting || filesSetting; CommonService.directUploadFile(setting).then(function(data) { if (data.result) { var _ppts = []; var _files = data.pass; if (_self.topic.files && _self.topic.files.length >= 0) { _self.topic.files = _self.topic.files.concat(_files); } else { _self.topic.files = _files; } //self.save(); } else { CommonService.showError("Update failed: " + data.message); } }); } this.deleteAttach = function(file) { CommonService.showDeleteConfirm(function(res){ var idx = _self.topic.files.indexOf(file); if (idx >= 0) { _self.topic.files.splice(idx, 1); } }); } });