eventman/angular_app/js/controllers.js

437 lines
16 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-05-10 10:30:37 +02:00
eventManControllers.controller('NavigationCtrl', ['$scope', '$location', 'Setting',
function ($scope, $location, Setting) {
$scope.logo = {};
$scope.go = function(url) {
2015-03-20 22:47:21 +01:00
$location.url(url);
};
2015-04-05 10:13:00 +02:00
2015-05-10 10:30:37 +02:00
Setting.query({setting: 'logo'}, function(data) {
if (data && data.length) {
$scope.logo = data[0];
}
});
$scope.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
}]
);
/* Controller for modals. */
eventManControllers.controller('ModalConfirmInstanceCtrl', ['$scope', '$modalInstance', 'message',
function ($scope, $modalInstance, message) {
$scope.message = message;
$scope.ok = function () {
$modalInstance.close($scope);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
}]
);
2015-05-16 13:30:18 +02:00
eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$modal', '$log', '$translate', '$rootScope',
function ($scope, Event, $modal, $log, $translate, $rootScope) {
2015-03-21 11:34:55 +01:00
$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
2015-05-17 10:38:11 +02:00
$scope.confirm_delete = 'Do you really want to delete this event?';
2015-05-16 13:30:18 +02:00
$rootScope.$on('$translateChangeSuccess', function () {
2015-05-17 10:38:11 +02:00
$translate('Do you really want to delete this event?').then(function (translation) {
2015-05-16 13:30:18 +02:00
$scope.confirm_delete = translation;
});
});
2015-03-22 17:08:25 +01:00
$scope.remove = function(_id) {
var modalInstance = $modal.open({
scope: $scope,
templateUrl: 'modal-confirm-action.html',
controller: 'ModalConfirmInstanceCtrl',
resolve: {
2015-05-16 13:30:18 +02:00
message: function() { return $scope.confirm_delete; }
}
});
modalInstance.result.then(function() {
console.debug('here');
Event.remove({'id': _id}, function() {
$scope.events = Event.all();
}
);
2015-03-22 17:08:25 +01:00
});
};
2015-03-15 18:00:08 +01:00
}]
);
2015-05-16 13:30:18 +02:00
eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate',
function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate) {
$scope.personsOrder = ["name", "surname"];
2015-04-08 21:55:23 +02:00
$scope.countAttendees = 0;
$scope.message = {};
2015-05-01 16:30:53 +02:00
$scope.event = {};
2015-05-01 16:42:55 +02:00
$scope.event.persons = [];
$scope.customFields = Setting.query({setting: 'person_custom_field',
in_event_details: true});
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();
}
);
2015-04-08 21:55:23 +02:00
});
$scope.allPersons = Person.all();
if ($state.is('event.info')) {
// Handle WebSocket connection used to update the list of persons.
$scope.EventUpdates = EventUpdates;
$scope.EventUpdates.open();
$scope.$watchCollection(function() {
return $scope.EventUpdates.data;
}, function(prev, old) {
if (!($scope.EventUpdates.data && $scope.EventUpdates.data.persons)) {
return;
}
$scope.event.persons = $scope.EventUpdates.data.persons;
}
);
}
2015-03-21 15:33:17 +01:00
}
$scope.updateOrded = function(key) {
var new_order = [key];
var inv_key;
if (key && key[0] === '-') {
inv_key = key.substring(1);
} else {
inv_key = '-' + key;
}
angular.forEach($scope.personsOrder,
function(value, idx) {
if (value !== key && value !== inv_key) {
new_order.push(value)
}
}
);
$scope.personsOrder = new_order;
};
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-05-05 21:31:37 +02:00
$scope.eventForm.$setPristine(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;
angular.forEach($scope.event.persons, function(value, key) {
if (value.attended) {
attendees += 1;
}
});
$scope.countAttendees = attendees;
};
2015-04-18 19:58:15 +02:00
$scope._addPerson = function(person_data) {
var original_data = angular.copy(person_data);
person_data.person_id = person_data._id;
person_data._id = $stateParams.id;
2015-04-18 19:58:15 +02:00
Event.addPerson(person_data, function() {
// This could be improved adding it only locally.
//$scope.event.persons.push(person_data);
$scope.setPersonAttribute(person_data, 'attended', true, function() {
Event.get($stateParams, function(data) {
$scope.event.persons = angular.fromJson(data).persons;
});
var idx = $scope.allPersons.indexOf(original_data);
if (idx != -1) {
$scope.allPersons.splice(idx, 1);
} else {
$scope.allPersons = Person.all();
}
$scope.newPerson = {};
2015-05-16 13:30:18 +02:00
$translate('{{person_name}} {{person_surname}} successfully added to event {{event_title}}',
{person_name: person_data.name, person_surname: person_data.surname, event_title: $scope.event.title}).then(
function (translation) {
$scope.showMessage({message: translation});
}
);
});
});
$scope.query = '';
};
$scope.fastAddPerson = function(person, isNew) {
$log.debug('EventDetailsCtrl.fastAddPerson.person:');
$log.debug(person);
if (isNew) {
var personObj = new Person(person);
personObj.$save(function(p) {
2015-04-18 19:58:15 +02:00
$scope._addPerson(angular.copy(p));
});
} else {
2015-04-18 19:58:15 +02:00
$scope._addPerson(angular.copy(person));
}
};
$scope.setPersonAttribute = function(person, key, value, callback) {
$log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
$log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
$log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
var data = {_id: $stateParams.id, person_id: person.person_id};
data[key] = value;
2015-04-18 19:56:00 +02:00
Event.updatePerson(data,
function(data) {
$scope.event.persons = data;
if (callback) {
callback(data);
}
if (key === 'attended') {
var msg = {};
if (value) {
msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
} else {
msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
msg.isError = true;
}
$scope.showMessage(msg);
}
});
};
$scope.setPersonAttributeAndRefocus = function(person, key, value) {
$scope.setPersonAttribute(person, key, value);
$scope.query = '';
};
2015-04-05 11:20:57 +02:00
$scope.removeAttendee = function(person) {
2015-04-18 19:58:15 +02:00
Event.deletePerson({
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
};
$scope.showMessage = function(cfg) {
$scope.message.show(cfg);
};
2015-04-26 12:42:47 +02:00
$scope.$on('$destroy', function() {
$scope.EventUpdates && $scope.EventUpdates.close();
});
2015-03-15 23:05:59 +01:00
}]
);
2015-05-16 13:30:18 +02:00
eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting', '$modal', '$translate', '$rootScope',
function ($scope, Person, Setting, $modal, $translate, $rootScope) {
2015-03-21 11:34:55 +01:00
$scope.persons = Person.all();
2015-05-02 09:58:53 +02:00
$scope.personsOrder = ["name", "surname"];
2015-04-18 17:33:42 +02:00
$scope.customFields = Setting.query({setting: 'person_custom_field',
in_persons_list: true});
2015-05-17 10:38:11 +02:00
$scope.confirm_delete = 'Do you really want to delete this person?';
2015-05-16 13:30:18 +02:00
$rootScope.$on('$translateChangeSuccess', function () {
2015-05-17 10:38:11 +02:00
$translate('Do you really want to delete this person?').then(function (translation) {
2015-05-16 13:30:18 +02:00
$scope.confirm_delete = translation;
});
});
2015-05-02 09:58:53 +02:00
$scope.updateOrded = function(key) {
var new_order = [key];
var inv_key;
if (key && key[0] === '-') {
inv_key = key.substring(1);
} else {
inv_key = '-' + key;
}
angular.forEach($scope.personsOrder,
function(value, idx) {
if (value !== key && value !== inv_key) {
new_order.push(value)
}
}
);
$scope.personsOrder = new_order;
};
2015-04-18 17:33:42 +02:00
$scope.setAttribute = function(person, key, value) {
var data = {_id: person._id};
data[key] = value;
Person.update(data, function() {
$scope.persons = Person.all();
});
};
2015-03-22 17:08:25 +01:00
$scope.remove = function(_id) {
var modalInstance = $modal.open({
scope: $scope,
templateUrl: 'modal-confirm-action.html',
controller: 'ModalConfirmInstanceCtrl',
resolve: {
2015-05-16 13:30:18 +02:00
message: function() { return $scope.confirm_delete; }
}
});
modalInstance.result.then(function() {
console.debug('here');
Person.remove({'id': _id}, function() {
$scope.persons = Person.all();
}
);
2015-03-22 17:08:25 +01:00
});
};
2015-03-14 17:32:16 +01:00
}]
);
2015-03-15 23:05:59 +01:00
2015-04-18 17:52:36 +02:00
eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', 'Setting', '$log',
function ($scope, $stateParams, Person, Event, Setting, $log) {
$scope.personsOrderProp = 'name';
$scope.eventsOrderProp = '-begin-date';
$scope.addToEvent = '';
$scope.customFields = Setting.query({setting: 'person_custom_field',
in_persons_list: true});
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-05-04 22:36:27 +02: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;
2015-04-18 19:58:15 +02:00
Event.addPerson(data);
2015-04-06 22:55:16 +02:00
}
});
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;
2015-04-18 19:58:15 +02:00
Event.addPerson(data);
2015-04-06 22:55:16 +02:00
}
});
}
2015-05-05 21:31:37 +02:00
$scope.personForm.$setPristine(false);
2015-03-21 16:48:00 +01:00
};
2015-04-18 19:56:00 +02:00
$scope.setPersonAttributeAtEvent = function(evnt, key, value) {
var attrs = {_id: evnt._id, person_id: $stateParams.id};
attrs[key] = value;
Event.updatePerson(attrs,
2015-04-05 00:55:59 +02:00
function(data) {
$scope.events = 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;
2015-04-18 19:58:15 +02:00
Event.addPerson(data,
function(data) {
$scope.events = Person.getEvents({_id: $stateParams.id, all: true});
}
);
} else {
2015-04-18 19:58:15 +02:00
Event.deletePerson({_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
});
};
}]
);