eventman/angular_app/js/controllers.js

271 lines
9.6 KiB
JavaScript
Raw Normal View History

2015-03-14 17:32:16 +01:00
'use strict';
2015-03-22 09:36:32 +01:00
/* Controllers; their method are available where specified with the ng-controller
2015-04-08 21:55:23 +02:00
* directive or for a given route/state (see app.js). They use some services to
2015-03-22 09:36:32 +01:00
* connect to the backend (see services.js). */
2015-03-15 18:00:08 +01:00
var eventManControllers = angular.module('eventManControllers', []);
2015-03-14 17:32:16 +01:00
2015-03-21 20:31:36 +01:00
2015-03-22 09:19:30 +01:00
/* A controller that can be used to navigate. */
2015-03-28 15:48:59 +01:00
eventManControllers.controller('NavigationCtrl', ['$location',
2015-03-20 22:47:21 +01:00
function ($location) {
this.go = function(url) {
$location.url(url);
};
2015-04-05 10:13:00 +02:00
this.isActive = function (view) {
2015-04-05 10:25:16 +02:00
if (view === $location.path()) {
return true;
}
if (view[view.length-1] !== '/') {
view = view + '/';
}
return $location.path().indexOf(view) == 0;
2015-04-05 10:13:00 +02:00
};
2015-03-20 22:47:21 +01:00
}]
);
2015-03-14 17:32:16 +01:00
2015-03-28 17:42:27 +01:00
/* Controller for a group of date and time pickers. */
eventManControllers.controller('DatetimePickerCtrl', ['$scope',
function ($scope) {
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
2015-03-28 18:20:23 +01:00
};
2015-03-28 17:42:27 +01:00
}]
);
2015-03-21 11:34:55 +01:00
eventManControllers.controller('EventsListCtrl', ['$scope', 'Event',
function ($scope, Event) {
$scope.events = Event.all();
$scope.personsOrderProp = 'name';
2015-04-12 21:59:30 +02:00
$scope.eventsOrderProp = "'-begin-date'";
2015-03-22 17:08:25 +01:00
$scope.remove = function(_id) {
Event.remove({'id': _id}, function() {
$scope.events = Event.all();
});
};
2015-03-15 18:00:08 +01:00
}]
);
eventManControllers.controller('EventDetailsCtrl', ['$scope', 'Event', 'Person', '$stateParams', '$log',
function ($scope, Event, Person, $stateParams, $log) {
$scope.personsOrderProp = 'name';
$scope.eventsOrderProp = '-begin-date';
2015-04-08 21:55:23 +02:00
$scope.countAttendees = 0;
2015-04-05 16:57:21 +02:00
if ($stateParams.id) {
2015-04-08 21:55:23 +02:00
$scope.event = Event.get($stateParams, function() {
$scope.$watchCollection(function() {
return $scope.event.persons;
}, function(prev, old) {
$scope.calcAttendees();
});
});
$scope.allPersons = Person.all();
2015-03-21 15:33:17 +01:00
}
2015-03-22 09:19:30 +01:00
// store a new Event or update an existing one
2015-03-21 15:45:40 +01:00
$scope.save = function() {
2015-04-05 00:55:59 +02:00
// avoid override of event.persons list.
2015-04-04 17:26:00 +02:00
var this_event = angular.copy($scope.event);
if (this_event.persons) {
delete this_event.persons;
}
if (this_event.id === undefined) {
$scope.event = Event.save(this_event);
2015-03-21 15:45:40 +01:00
} else {
2015-04-04 17:26:00 +02:00
$scope.event = Event.update(this_event);
2015-03-21 15:45:40 +01:00
}
2015-03-28 17:42:27 +01:00
$scope.eventForm.$dirty = false;
2015-03-21 15:45:40 +01:00
};
2015-04-04 17:26:00 +02:00
2015-04-08 21:55:23 +02:00
$scope.calcAttendees = function() {
if (!($scope.event && $scope.event.persons)) {
return;
}
var attendees = 0;
$log.info($scope.event.persons.length);
angular.forEach($scope.event.persons, function(value, key) {
if (value.attended) {
attendees += 1;
}
});
$scope.countAttendees = attendees;
};
$scope._addAttendee = function(person_data) {
person_data.person_id = person_data._id;
person_data._id = $stateParams.id;
person_data.attended = true;
Event.addAttendee(person_data, function() {
$scope.event = Event.get($stateParams);
$scope.allPersons = Person.all();
$scope.newPerson = {};
});
};
$scope.fastAddPerson = function(person, isNew) {
$log.debug('EventDetailsCtrl.fastAddPerson.person:');
$log.debug(person);
if (isNew) {
var personObj = new Person(person);
personObj.$save(function(p) {
$scope._addAttendee(angular.copy(p));
});
} else {
$scope._addAttendee(angular.copy(person));
}
};
2015-04-05 00:55:59 +02:00
$scope.updateAttendee = function(person, attended) {
2015-04-05 16:57:21 +02:00
$log.debug('EventDetailsCtrl.event_id: ' + $stateParams.id);
2015-04-05 00:55:59 +02:00
$log.debug('EventDetailsCtrl.person_id: ' + person.person_id);
$log.debug('EventDetailsCtrl.attended: ' + attended);
Event.personAttended({
2015-04-05 16:57:21 +02:00
_id: $stateParams.id,
2015-04-05 00:55:59 +02:00
person_id: person.person_id,
'persons.$.attended': attended
},
function(data) {
$log.debug('EventDetailsCtrl.personAttended.data');
$log.debug(data);
$scope.event.persons = data;
2015-04-05 11:20:57 +02:00
});
};
2015-04-05 11:20:57 +02:00
$scope.removeAttendee = function(person) {
Event.deleteAttendee({
2015-04-05 16:57:21 +02:00
_id: $stateParams.id,
2015-04-05 11:20:57 +02:00
person_id: person.person_id
},
function(data) {
$scope.event.persons = data;
$scope.allPersons = Person.all();
2015-04-05 00:55:59 +02:00
});
2015-04-04 17:26:00 +02:00
};
2015-03-15 23:05:59 +01:00
}]
);
2015-03-21 11:34:55 +01:00
eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person',
function ($scope, Person) {
$scope.persons = Person.all();
$scope.personsOrderProp = 'name';
$scope.eventsOrderProp = '-begin-date';
2015-03-22 17:08:25 +01:00
$scope.remove = function(_id) {
Person.remove({'id': _id}, function() {
$scope.persons = Person.all();
});
};
2015-03-14 17:32:16 +01:00
}]
);
2015-03-15 23:05:59 +01:00
2015-04-05 16:57:21 +02:00
eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', '$log',
function ($scope, $stateParams, Person, Event, $log) {
$scope.personsOrderProp = 'name';
$scope.eventsOrderProp = '-begin-date';
$scope.addToEvent = '';
2015-04-05 16:57:21 +02:00
if ($stateParams.id) {
$scope.person = Person.get($stateParams);
$scope.events = Person.getEvents({_id: $stateParams.id, all: true});
2015-04-06 22:55:16 +02:00
} else {
$scope.events = Event.all();
2015-03-21 16:48:00 +01:00
}
2015-03-22 09:19:30 +01:00
// store a new Person or update an existing one
2015-03-21 16:48:00 +01:00
$scope.save = function() {
2015-04-05 00:55:59 +02:00
if ($scope.person.id === undefined) {
2015-04-06 22:55:16 +02:00
$scope.person = new Person($scope.person);
$scope.person.$save(function(person) {
if ($scope.addToEvent) {
var data = angular.copy(person);
data.person_id = data._id;
data._id = $scope.addToEvent;
data.attended = false;
Event.addAttendee(data);
}
});
2015-04-05 00:55:59 +02:00
} else {
2015-04-06 22:55:16 +02:00
$scope.person = Person.update($scope.person, function(data) {
if ($scope.addToEvent) {
var data = angular.copy($scope.person);
data._id = $scope.addToEvent;
data.person_id = $scope.person._id;
data.attended = false;
Event.addAttendee(data);
}
});
}
2015-04-05 17:36:29 +02:00
$scope.personForm.$dirty = false;
2015-03-21 16:48:00 +01:00
};
2015-04-05 00:55:59 +02:00
$scope.updateAttendee = function(event, attended) {
2015-04-05 16:57:21 +02:00
$log.debug('PersonDetailsCtrl.event_id: ' + $stateParams.id);
2015-04-05 00:55:59 +02:00
$log.debug('PersonDetailsCtrl.event_id: ' + event.event_id);
$log.debug('PersonDetailsCtrl.attended: ' + attended);
Event.personAttended({
_id: event._id,
2015-04-05 16:57:21 +02:00
person_id: $stateParams.id,
2015-04-05 00:55:59 +02:00
'persons.$.attended': attended
},
function(data) {
$scope.events = data = Person.getEvents({_id: $stateParams.id, all: true});
2015-04-05 00:55:59 +02:00
}
);
};
$scope.switchRegistered = function(evnt, person, add) {
$log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
$log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
$log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
if (add) {
var data = angular.copy(person);
data._id = evnt._id;
data.person_id = person._id;
data.attended = false;
Event.addAttendee(data,
function(data) {
$scope.events = Person.getEvents({_id: $stateParams.id, all: true});
}
);
} else {
Event.deleteAttendee({_id: evnt._id, person_id: person._id},
function(data) {
$scope.events = Person.getEvents({_id: $stateParams.id, all: true});
}
);
}
};
2015-03-15 23:05:59 +01:00
}]
);
2015-03-29 00:46:42 +01:00
2015-03-29 15:50:36 +02:00
eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
function ($scope, $log, $upload, Event) {
2015-03-29 15:05:01 +02:00
$scope.file = null;
$scope.reply = {};
2015-03-29 15:50:36 +02:00
$scope.events = Event.all();
2015-03-29 09:58:52 +02:00
$scope.upload = function(file, url) {
2015-04-05 00:55:59 +02:00
$log.debug("FileUploadCtrl.upload");
2015-03-29 09:58:52 +02:00
$upload.upload({
url: url,
2015-03-29 15:50:36 +02:00
file: file,
fields: {targetEvent: $scope.targetEvent}
2015-03-29 15:05:01 +02:00
}).progress(function(evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
$log.debug('progress: ' + progressPercentage + '%');
}).success(function(data, status, headers, config) {
$scope.file = null;
$scope.reply = angular.fromJson(data);
2015-03-29 09:58:52 +02:00
});
};
}]
);