UNPKG

unserver-unify

Version:

304 lines (293 loc) 8.74 kB
'use strict'; angular.module('bamboo.course').controller('CourseinfosCtrl', function($scope, ApiService, deviceDetector, $stateParams) { var self = this; var page = 1; var groupType = $stateParams.type; var mycourses = []; this.content = {}; this.allkeys = []; this.allkeycolor = []; this.allkeyresult = []; this.ifSearch = false; var colorarray = ['panel-grey', 'panel-red', 'panel-blue', 'panel-sea', 'panel-green', 'panel-orange']; this.items = []; this.setting = { ctrl: self, hasPhoto: false, listName: "courses", emptyTitle: 'No Course!', pageSize: 9, currentPage: (self.page + 1) }; this.togglecat = function() { if ($scope.category) { $scope.category = false; } else { $scope.category = true; } } this.defaultCoursePhoto = ApiService.SHOST + "/public/images/videos.png"; console.log(this.setting); this.show0item = true; //console.log('isOwn:' + isOwn); //console.log($stateParams); self.showMobileSid = false; //init the var to show/hide sidebar for mobile view self.toggleSideBar = function() { self.showMobileSid = !self.showMobileSid; } this.mobileFlag = false; self.totalCoursesCountUnderCat = 0; var option; this.option; var industryAll = "All"; $scope.mycourses = []; // this.Departments = ApiService.school.departments; this.totalCoursesCountUnderDepartment = 0; this.toggledrop = function() { if ($scope.dropdown) { $scope.dropdown = false; } else { $scope.dropdown = true; } } this.subschool = false; this.category = ApiService.category; this.categorykeys = ApiService.school.categorykeys; console.log(this.category); this.sortmethod = "visitercounter"; //visitercounter, position,score,update,created,recommend,top if (deviceDetector.isMobile() && !deviceDetector.isTablet()) { this.setting.pageSize = Math.floor((window.screen.height - $('.row').offset().top) / 70); this.mobileFlag = true; this.sortmethod = "update"; } this.pageChanged = function(newPage) { page = newPage; getInfo(); }; this.nextPage = function() { page++; getInfo(); } function getArray(obj, array, level, parent) { if ((typeof obj) === "object") { for (var key in obj) { var fullkey; if (parent == '') { fullkey = key; } else { fullkey = parent + "," + key; } var _i = { key: fullkey, value: level, title: key, items: [], }; array.push(_i); var info = obj[key]; if (typeof info === "object") { getArray(info, _i.items, level + 1, fullkey); } } } } function generateCategoryList() { var catList = []; getArray(self.category, catList, 0, ''); console.log(catList); if (!self.categorykeys) { $scope.catitems = catList; } else { var keyindex = {}; var results = []; angular.forEach(catList, function(val, index) { var key = val.title; keyindex[key] = val; }) angular.forEach(self.categorykeys, function(key, index) { if (keyindex[key]) { results.push(keyindex[key]); } }) angular.forEach(keyindex, function(val, key) { var found = false; for (var i = 0; i < results.length; i++) { var record = results[i].title; if (record == key) { found = true; break; } } if (!found) { results.push(val); } }) $scope.catitems = results; } } function getInfo() { console.log("getInfo"); generateCategoryList(); getCatNumber(); //getCourseList(); getcourses(); //self.getCourseInfos(self._tableState); } var searchmethod; function getCatNumber() { var info = { action: 'getcategorynumber', } ApiService.post('/market', info).then(function(result) { if (result.data.success) { console.log(result.data.data); var catresult = result.data.data; for (var i = 0; i < $scope.catitems.length; i++) { var catitem = $scope.catitems[i]; var key = catitem.key; if (result.data.data[key]) { catitem.counter = catresult[key]; } } } }) } this.getHotestCourses = function() { self.clrCoursesSearchName(); // clear name search box self.sortmethod = "visitor"; searchmethod = { visitor: { $gte: 100 }, }; getcourses(); } this.getNewCourses = function() { self.clrCoursesSearchName(); // clear name search box // console.log("getNewCourses"); self.sortmethod = "created"; var weekago = new Date(); weekago.setDate(weekago.getDate() - 30); searchmethod = { created: { $gte: weekago }, } getcourses(); } this.getPositionCourses = function() { self.clrCoursesSearchName(); // clear name search box // console.log("getNewCourses"); self.sortmethod = "recommend"; searchmethod = { recommend: { $gte: 0 } } getcourses(); } this.getTopCourses = function() { self.clrCoursesSearchName(); // clear name search box // console.log("getNewCourses"); self.sortmethod = "top"; searchmethod = { top: true, } getCourseList(); } var userfullnames = {}; var weekago = new Date(); weekago.setDate(weekago.getDate() - 7); function compareDate(courseDate) { var cd = new Date(courseDate); if (cd > weekago) return true; else return false; }; this.total = 0; function getcourses() { console.log("getcourses"); console.log(self.department); var names = []; // self.toggleSideBar (); self.showMobileSid = false; var info = { // id: ApiService.gid, action: "getenablecourses", start: (page-1)*(self.setting.pageSize || 9) , sort: self.sortmethod, limit: self.setting.pageSize || 9, search: searchmethod, }; console.log(info); if (option) { if (!info.search) { info.search = {}; } info.search.category = option; } console.log(info); return ApiService.post('/market', info).then(function(result) { console.log(result.data.data); $scope.courses = result.data.data.items; self.courses = result.data.data.items; //$scope.displayCourses =[].concat($scope.courses); $scope.totalCount = result.data.data.counter; self.setting.totalCount = result.data.data.counter; $scope.setting = self.setting; angular.forEach($scope.courses, function(course, index) { // console.log(course); switch (course.type) { case 'bamboo': if (course.photo) { course.photourl = ApiService.SHOST + '/lcourse/' + course.id + '/' + course.photo; } else { course.photourl = self.defaultCoursePhoto; } course.landingUrl = "index.courseinfos.contentlanding({cid: lcourse._id})"; // course.url = '//' + course.schoolkey + "." + ApiService.BASEDOMAIN + '/#/courselist//info/' + course.id; break; case 'udemy': course.photourl = course.photo; course.schoolname = 'Udemy'; course.landingUrl = "index.courseinfos.landing({cid: lcourse._id})"; break; case 'coursera': course.photourl = course.photo; course.schoolname = 'Coursera'; // course.url="https://www.coursera.org/learn/" course.landingUrl = "index.courseinfos.landing({cid: lcourse._id})"; break; } }) }); } getInfo(); this.list = function(key, title) { self.ifSearch = true; self.currentContent = self.content[title]; self.clrCoursesSearchName(); // clear name search box console.log(key); option = key; searchmethod = null; this.option = option; self.setting.items = []; //page = 1; getcourses(); }; this.searchCourseByName = function(q) { self.ifSearch = true; searchmethod = { name: q }; if (self.mobileFlag) { self.setting.items = []; } getcourses(); }; this.clrCoursesSearchName = function() { self.q = ''; } this.goToDetail = function(key) { self.list(key, key); }; });