UNPKG

unserver-unify

Version:

248 lines (242 loc) 7.96 kB
'use strict'; angular.module('bamboo.event').controller('EventCtrl', function($scope, $location, calendarEventService, $compile, loginService, $rootScope, ApiService, smoothScroll, CommonService, $state, $stateParams, Lightbox, $window, $translate) { var _self = this; this.id = $stateParams.id; this.event = {}; this.publicUrl = ApiService.SHOST + "/public/images/"; this.eventPicUrl = ApiService.SHOST + "/public/" + ApiService.RES + "/schoolevent/"; this.eventAlbumPicUrl = ApiService.SHOST + "/public/" + ApiService.RES + "/schoolevent/" + _self.id + "/"; this.defaultEventPhoto = 'assets/images/event_no-img1.jpg'; this.registeredFlag = false; this.registerString = "Register"; _self.shareFlag = $stateParams.share; getInfo(); /* this.embeded = $location.search().embeded; if (this.embeded) { $rootScope.hideHeader = true; } */ // console.log(this.embeded); if (loginService.subDomain == 'gsx') { $rootScope.fullwidthflag = true; $scope.$on("$destroy", function() { console.log('-- destroy --'); console.log($state.current.name); }); } this.scroll = function() { smoothScroll(document.getElementById('calendar')); } this.searchostcode = function() { if (!self.postcode || self.postcode.length < 6) { CommonService.showError('Invalid postcode'); } var url = "https://maps.google.com/maps/api/geocode/json?address=Sg+" + self.postcode; var params = { url: url, method: 'GET', headers: { 'Content-Type': 'application/json' }, timeout: 1200000, // async: false, }; $http.defaults.useXDomain = true; $http(params).then(function(result) { console.log(result); if (result.status == '200' && result.data.status == 'OK') { var location = result.data.results[0].geometry.location; self.mysrclat = location.lat; self.mysrclong = location.lng; resultReady = true; calculateDistance(); } }) } function getInfo() { ApiService.get("/myevents/" + _self.id).then(function(result) { if (result.data.success) { console.log(result.data.data); var item = _self.event = result.data.data.event; var timecounts = result.data.data.timecounts; _self.countMap = _.keyBy(timecounts, function(o) { return o._id; }); console.log(_self.countMap); if (_self.event.albums) { _self.albums = _self.event.albums.map(function(o) { var _result = { url: _self.eventAlbumPicUrl + o.photo, thumbUrl: _self.eventAlbumPicUrl + o.photo, } if (loginService.subDomain != 'stec') { _result.caption = o.name; } return _result }); console.log(_self.albums); } if (_self.event.registrationFlag) { if (_self.event.registrationEnd) { var end = new Date(_self.event.registrationEnd); var now = new Date(); if (end > now) { _self.event.enroll = true; } } else { _self.event.enroll = true; } } var appInfo = {}; angular.forEach(result.data.data.records, function(item, index) { // console.log(app); var key = item.time; appInfo[key] = true; }) console.log(appInfo); var eventdata = []; for (var i = 0; i < item.repeatCounter; i++) { var time = new Date(item.start); switch (item.repeatType) { case 'week': time = moment(item.start).add(i, 'weeks').toDate(); break; case 'month': time = moment(item.start).add(i, 'months').toDate(); break; case 'day': time = moment(item.start).add(i, 'days').toDate(); break; } var info = { title: item.name, start: time, isoTimeStr: new Date(time).toISOString(), stick: true, id: item._id, address: item.address, } var key = time.toISOString(); // console.log(info); // console.log(key); if (appInfo[key]) { info.selected = true; } eventdata.push(info); } _self.dateList = eventdata; _self.eventSources = [eventdata]; console.log(eventdata); _self.eventsready = true; _self.imageUrl = _self.event.photo ? _self.eventPicUrl + _self.event._id + '/' + _self.event.photo : _self.defaultEventPhoto; } }); } $scope.openLightboxModal = function(url) { Lightbox.openModal([{ url: url }], 0); }; function enroll(time) { if (!_self.event.enroll) { return; } if (_self.event.price) { var info = { action: "paypalbuyeventsession", eid: _self.id, time:time, url: $location.absUrl(), } if(loginService.school.paymentGateWay == 'wdpay'){ info.action="weidongBuyEventSession"; } ApiService.post('/payment', info).then(function(result) { console.log(result); if (result.data.success) { console.log(result.data.data); if (result.data.data.redirectUrl) { CommonService.showInfo('Redirect to Payment Page , Please wait ...'); $window.location.href = result.data.data.redirectUrl; }else{ console.log('-- no redirect --'); } } }); } else { var info = { id: _self.id, action: 'addrecord', time: time, }; ApiService.post("/event", info).then(function(result) { if (result.data.success) { CommonService.showInfo('Enroll Application submitted!'); $state.reload(); } else { CommonService.showError(result.data.error); } }) } } this.Enroll = function(item) { /* var string = "Do you really want to book the session ?"; CommonService.showConfirm(string, function() { enroll(item.start); }) */ if (loginService.user) { var string = $translate.instant("Do you really want to book the session ?"); CommonService.showConfirm(string, function() { enroll(item.start); }) } else { $state.go("index.events.enroll", { id: _self.id, time: new Date(item.start).getTime(), }); } }; function eventClick(data, jsEvent, view) { console.log('eventClick'); if (loginService.user) { if (data.selected) { CommonService.showInfo('Already Booked!'); return; } var string = $translate.instant("Do you really want to book the session ?"); CommonService.showConfirm(string, function() { enroll(data.start.toDate()); }) } else { $state.go("index.events.enroll", { id: _self.id, time: data.start.toDate(), }); } } function eventRender(event, element, view) { if (!event.selected) { element.append(calendarEventService.checkEvent(event)); $compile(element)($scope); } } this.calendar = { height: 600, header: { left: 'title', center: '', right: 'today prev,next' }, timeFormat: 'HH:mm', eventClick: eventClick, eventRender: eventRender, } // $scope.openLightboxModal = function(url) { // Lightbox.openModal([{ // url: url // }], 0); // }; $scope.openLightboxModal = function(index) { console.log("---"); Lightbox.openModal(_self.albums, index); }; });