Skip to content

Instantly share code, notes, and snippets.

@dwaligora
Created February 3, 2014 14:53
Show Gist options
  • Select an option

  • Save dwaligora/8785222 to your computer and use it in GitHub Desktop.

Select an option

Save dwaligora/8785222 to your computer and use it in GitHub Desktop.
timetable ctrl
angular.module('CalendarApp')
.controller('CalendarDayCtrl', function ($rootScope, $scope, notification, flash, $timeout, $injector, $route, $routeParams, TimetableFormatterService, CalendarRepository, UserRepository, ReminderRepository) {
$scope.$emit('LOADING');
$scope.showEvents = false;
$scope.startDate = moment($routeParams.start_date).unix();
$injector.invoke(function ($controller) {
$controller('ReminderPermissionCtrl', {$scope: $scope, $rootScope: $rootScope, UserRepository: UserRepository});
});
CalendarRepository.startDate = moment($routeParams.start_date).unix();
CalendarRepository.endDate = moment($routeParams.start_date).add('days', CalendarRepository.daysIncrementation).unix();
CalendarRepository.queryData({}).then(function(timetable) {
$timeout(function() {
$scope.timetable = TimetableFormatterService.getTimetableByDay(timetable.data);
$scope.$emit('UNLOADING');
}, 10);
$scope.$emit('REFRESH_LIST');
});
$scope.today = moment().format('YYYY-MM-DD');
$scope.CalendarRepository = CalendarRepository;
$scope.remove = function(object, index) {
if (!_.isObject(object)) {
throw new Error('KambalaApp.RemoveCtrl.remove expect object as an argument!');
}
ReminderRepository.delete(object.id).then(function(response) {
if (600 === response.status) {
_.remove($scope.timetable[index].reminders, function(reminder) {
if (reminder.id === object.id) {
return reminder;
}
});
$scope.$emit('UNLOADING');
flash.success = 'Reminder was removed successfully!';
} else if(603 === response.status) {
$scope.$emit('UNLOADING');
notification.alert(response.message, null, 'Oops!', 'Ok', 'error');
}
}, function() {
$scope.$emit('UNLOADING');
notification.alert('Something went wrong. Please report this to your school admin!', null, 'Oops!', 'Ok', 'error');
});
};
$scope.toggleEvents = function() {
$scope.showEvents = !$scope.showEvents;
};
$scope.loadData = function() {
if (CalendarRepository.busy) {
return;
}
$scope.$emit('LOADING');
CalendarRepository.busy = true;
CalendarRepository.startDate = moment.unix(CalendarRepository.endDate).add('days', 1).unix();
CalendarRepository.endDate = moment.unix(CalendarRepository.startDate).add('days', CalendarRepository.daysIncrementation).unix();
CalendarRepository.queryData({})
.then(function(response) {
var items = TimetableFormatterService.getTimetableByDay(response.data);
for (var i = 0; i < items.length; i++) {
$scope.timetable.push(items[i]);
}
CalendarRepository.busy = false;
$timeout(function() {
CalendarRepository.busy = false;
$scope.$emit('UNLOADING');
}, 100);
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment