UNPKG

unserver-unify

Version:

254 lines (242 loc) 6.57 kB
'use strict'; angular.module('bamboo.course') .controller('CourseNoteCtrl', function($scope, CourseApi, ApiService,loginService, CommonService, $stateParams, Lightbox) { var self = this; var _type = "notes"; $scope.tabs = 'notes'; $scope.id = this.id = $stateParams.id; this.lid=$stateParams.cid; this.imgdes = ''; this.wikikey = ''; this.titleinfo = ""; this.contentModel = {}; this.wiki = { otype: _type }; this.imgs = []; var _pages = []; this.publicUrl = ApiService.SHOST + "/public/images/"; this.contentModelPicUrl = ApiService.SHOST + '/public/' + ApiService.RES + '/mvsubject/' + self.id + '/'; this.wikiurl = ApiService.PSURL + '/wiki/' + self.id + "/"; $scope.submitted = false; this.wikiImgSetting = { image: true, promise: true, s3: true, title: "Wiki Image", maxheight: 400, maxwidth: 400, ctrl: self, randomFlag: true, id: self.id, resPath: "public/" + ApiService.RES + "/wiki/" + self.id + "/", key: "wikiimg" }; function getWikis() { CourseApi.getInfo(self.id, function(result) { self.mvsubject = result; if(loginService.user){ if(loginService.user.is_admin){ $scope.allowedit = true; }else if(loginService.user.grole&&loginService.user.grole!="user"){ var cid = $stateParams.cid; ApiService.get('/course/' + cid).then(function(result) { if (result.data.success) { var course = result.data.data; console.log(course); var username=loginService.user.name; if(course.editors.indexOf(username)>-1||course.creater==username){ $scope.allowedit = true; } } }); } } _pages = result[_type]; $scope.name = self.mvsubject.name; self.titleinfo = result.pageinfo; var _wk = []; var mindex = 0; var sindex = 0; for (var i = 0; i < _pages.length; i++) { var page = _pages[i]; if (!page.level || page.level == 0) { mindex++; sindex = 0; } else { sindex++; } var idx = '' + mindex; if (sindex > 0) { idx += '-' + sindex } var info = { title: page.title, content: page.content, user: page.user, level: page.level, idx: idx, } _wk.push(info); } $scope.wikis = _wk; if (_wk.length > 0) { self.displaywiki(0); } }); }; this.delete = function(index) { var info = { otype: _type, index: index, id: self.id, lid:self.lid, action: 'deletepage', }; CourseApi.mvApi(info,null,1); }; this.moveup = function(id) { var info = { otype: _type, id: self.id, index: id, lid:self.lid, action: 'uppage', }; CourseApi.mvApi(info,null,1); }; this.updateTitle = function() { var info = { id: self.id, lid:self.lid, action: 'updatemvsubject', pageinfo: self.titleinfo, }; CourseApi.mvApi(info,null,1); } this.moveleft = function(id) { var info = { id: self.id, index: id, lid:self.lid, level: 0, otype: _type, action: 'updatepage', }; //console.log(info); CourseApi.mvApi(info,null,1); }; this.moveright = function(id) { //console.log(" --- move up page id "); var info = { id: self.id, index: id, lid:self.lid, level: 1, otype: _type, action: 'updatepage', }; CourseApi.mvApi(info,null,1); }; this.movedown = function(id) { //console.log(" --- move down page id "); var info = { id: self.id, index: id, lid:self.lid, otype: _type, action: 'downpage', }; CourseApi.mvApi(info,null,1); }; this.edit = function() { $scope.submitted = false; $scope.mode = 'edit'; }; this.create = function(form) { $scope.submitted = true; if (form.$invalid) { $scope.shaking = true; $timeout(function() { $scope.shaking = false; }, 500); return; } self.wiki.action = 'addpage'; self.wiki.lid=self.lid; //console.log(self.wiki); //console.log(self.wiki); CourseApi.mvApi(self.wiki,null,1); }; this.update = function(form) { $scope.submitted = true; if (form.$invalid) { $scope.shaking = true; $timeout(function() { $scope.shaking = false; }, 500); return; } self.wiki.otype = _type; self.wiki.lid=self.lid; self.wiki.action = 'updatepage'; //console.log(self.wiki); CourseApi.mvApi(self.wiki,null,1); }; this.displaywiki = function(id) { //console.log(" display page : "+id); self.wiki = $scope.wikis[id]; self.wiki.index = id; self.wiki.id = self.id; self.wiki.otype = _type; self._index = id; $scope.mode = 'view'; //console.log(self.wiki); }; this.newWiki = function() { //console.log("-- new wiki --"); self.wiki = { level: 0, id: self.id, lid:self.lid, otype: _type }; $scope.mode = 'new'; //console.log($scope); }; function addImg(name) { var imgstr = ' <img src="' + self.wikiurl + name + '" width="30%"/> '; //console.log(imgstr); self.wiki.content += imgstr; } this.linkwiki = function() { var imgstr = '<a onclick="showWiki(\'' + self.wikikey + '\')">' + self.wikikey + '</a>'; //console.log(imgstr); self.wiki.content += imgstr; }; function _update(fileName, key, index) { if (key == 'wikiimg') { addImg(fileName); } } this.uploadFile = function(uploadSetting, index) { // initImageUrl(uploadSetting, index); CommonService.uploadFile(uploadSetting).then(function(data) { if (data.result) { _update(data.fileName, uploadSetting.key, index); } else { CommonService.showError("Update failed: " + data.message); } }); }; $scope.showImage = function(src) { var images = []; console.log(src); images.push({'url' : src}); Lightbox.openModal(images, 0); }; $scope.showWiki = function(key) { CommonService.showWiki(key); }; getWikis(); });