eventman/angular_app/js/controllers.js

317 lines
12 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', 'Setting', '$log',
function ($scope, Event, Person, $stateParams, Setting, $log) {
$scope.personsOrderProp = 'name';
$scope.eventsOrderProp = '-begin-date';
2015-04-08 21:55:23 +02:00
$scope.countAttendees = 0;
$scope.message = {};
$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();
});
});
$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;
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 = {};
// XXX: must be converted in a i18n-able form.
var msg = '' + person_data.name + ' ' + person_data.surname + ' successfully added to event ' + $scope.event.title;
$scope.showMessage({message: msg});
});
});
$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) {
$log.debug('EventDetailsCtrl.setPersonAttribute.data');
$log.debug(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-03-15 23:05:59 +01:00
}]
);
2015-04-18 17:33:42 +02:00
eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting',
function ($scope, Person, Setting) {
2015-03-21 11:34:55 +01:00
$scope.persons = Person.all();
$scope.personsOrderProp = 'name';
$scope.eventsOrderProp = '-begin-date';
2015-04-18 17:33:42 +02:00
$scope.customFields = Setting.query({setting: 'person_custom_field',
in_persons_list: true});
$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) {
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-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-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-04-05 17:36:29 +02:00
$scope.personForm.$dirty = 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
});
};
}]
);