unserver-unify
Version:
173 lines (167 loc) • 6.57 kB
JavaScript
;
angular.module('bamboo.mystudy').controller('MyFavouriteCtrl', function($scope, ApiService,$rootScope, loginService) {
var _self = this;
this.eventpath = ApiService.SHOST + "/public/" + ApiService.RES + "/schoolevent/";
this.newspath = ApiService.SHOST + '/public/' + ApiService.RES + '/schoolnews/';
this.newses = [];
this.events = [];
this.subdomain = loginService.subDomain;
//this.defaultEventPhoto = this.defaultNewsPhoto = ApiService.SHOST + "/public/images/details.png";
this.favCourseList = [];
if (loginService.school.eventphoto) {
_self.defaultEventPhoto = ApiService.SHOST + "/public/" + ApiService.RES + "/school/" + ApiService.gid + "/" + ApiService.school.eventphoto;
}
if (loginService.school.newsphoto) {
_self.defaultNewsPhoto = ApiService.SHOST + "/public/" + ApiService.RES + "/school/" + ApiService.gid + "/" + ApiService.school.newsphoto;
}
switch (_self.subdomain) {
case 'bamboo':
_self.TYPE = 'course';
break;
case 'market':
_self.TYPE = 'courseinfo';
break;
default:
_self.TYPE = 'course';
}
this.defaultNewsPhoto = 'assets/images/news_no-image1.jpg';
this.defaultEventImage = 'assets/images/event_no-img1.jpg';
this.defaultpic = ApiService.SHOST + '/public/images/videos.png';
this.defaultCourseImage = ApiService.SHOST + '/public/images/videos.png';
$scope.setting = this.setting = {
ctrl: _self,
hasPhoto: true,
listName: "news",
emptyTitle: 'No News!',
imageFolderPrefix: ApiService.SHOST + '/public/' + ApiService.RES + '/' + 'schoolnews' + '/',
defaultImage: this.defaultNewsPhoto,
pageSize: _self.itemNumber
};
this.addToFav = function() {
$scope.fav = true;
var object = {
type: _self.TYPE,
id: _self.cid
};
var info = {
action: "addfavourite",
object: object
};
ApiService.post("/user", info).then(function(result) {
console.log(result);
if (result.data.success) {
loginService.getMyProfile(true).then(function(result) {
// console.log("-- need to reload ---");
console.log(loginService.user);
console.log(result);
});
}
});
};
this.deleteFromFav = function(course) {
$scope.fav = false;
console.log(course);
if (_self.subdomain == 'bamboo') {
_self.delete(course._id, _self.TYPE);
} else {
_self.delete(course._id, _self.TYPE);
}
var index = _self.favCourseList.indexOf(course);
if (index > -1) _self.favCourseList.splice(index, 1);
};
this.delete = function(id, type) {
console.log(id, type);
loginService.getMyProfile().then(function(_result) {
// console.log("--- return ---");
var favourites = _result.favourites;
var index = -1;
for (var i = 0; i < favourites.length; i++) {
var item = favourites[i];
if (item.type == type && item.id == id) {
index = i;
break;
}
}
if (index > -1) {
var info = {
action: 'delfavourite',
index: index,
}
console.log(info);
ApiService.post("/user", info).then(function(result) {
console.log(result);
if (result.data.success) {
loginService.getMyProfile(true).then(function() {
getInfo();
})
}
});
}
});
}
function getInfo() {
var info = {
action: "getmyfavourite"
}
ApiService.post("/user", info).then(function(result) {
console.log(result);
if (result.data.success) {
console.log(result.data.data);
_self.newses = result.data.data.newses;
_self.events = result.data.data.events;
_self.courses = result.data.data.courses;
//markets
_self.courseinfos = result.data.data.courseinfos;
angular.forEach(_self.newses, function(news) {
news.picurl = news.photo ? _self.newspath + news._id + '/' + news.photo : _self.defaultNewsPhoto;
});
angular.forEach(_self.events, function(event) {
event.picurl = event.photo ? _self.eventpath + event._id + '/' + event.photo : _self.defaultEventImage;
});
//bamboo
if(_self.courses){
$rootScope.totalbookedCourses=_self.courses.length;
angular.forEach(_self.courses, function(course) {
course.coursePhotoUrl = course.photo ? ApiService.SHOST + '/lcourse/' + course._id + "/" + course.photo : _self.defaultCourseImage;
course.url = 'index.courses.course({cid:"' + course._id + ',cs:'+ApiService.getCheckSum(course._id)+'"})';
//course.coursePhotoUrl = course.photo ? _self.eventpath + news._id + '/' + news.photo : _self.defaultEventPhoto;
});
}
//market
angular.forEach(_self.courseinfos, function(course) {
if (!course.created) course.created = new Date();
//market
if (course.type) {
if (course.type == 'bamboo') {
course.coursePhotoUrl = ApiService.SHOST + '/lcourse/' + course.id + "/" + course.photo;
} else {
course.coursePhotoUrl = course.photo;
}
}
if (course.type == 'bamboo') {
if (course.infoOnlyFlag) {
//self.item.url = "//" + self.item.schoolkey + "." + ApiService.BASEDOMAIN + "/#/courselist//infoonly/" + self.item.id;
course.url = 'index.courseinfos.landing({cid:"' + course._id + '"})';
} else {
//self.item.url = "//" + self.item.schoolkey + "." + ApiService.BASEDOMAIN + "/#/courselist//info/" + self.item.id;
course.url = 'index.courseinfos.contentlanding({cid:"' + course._id + '"})';
}
} else {
//self.item.url = self.item.url;
course.url = 'index.courseinfos.landing({cid:"' + course._id + '"})';
}
//course.coursePhotoUrl = course.photo ? _self.eventpath + news._id + '/' + news.photo : _self.defaultEventPhoto;
});
console.log(_self.courses);
console.log(_self.courseinfos);
console.log(_self.subdomain);
if (_self.subdomain == 'market') {
_self.favCourseList = _self.courseinfos;
} else {
_self.favCourseList = _self.courses;
}
}
});
}
getInfo();
})