(js) Improve sg-calendar-scroll-view directive

This commit is contained in:
Francis Lachapelle 2016-08-01 12:12:47 -04:00
parent 8389ec5f40
commit 431db5495e

View file

@ -25,34 +25,24 @@
}, },
controller: sgCalendarScrollViewController, controller: sgCalendarScrollViewController,
link: function(scope, element, attrs, controller) { link: function(scope, element, attrs, controller) {
var view, type, deregisterDragStart, deregisterDragStop; var view, type;
view = null; view = null;
type = scope.type; // multiday, multiday-allday, monthly, unknown? type = scope.type; // multiday, multiday-allday, monthly, unknown?
// Listen to dragstart and dragend events
deregisterDragStart = $rootScope.$on('calendar:dragstart', onDragStart);
deregisterDragStop = $rootScope.$on('calendar:dragend', onDragEnd);
// Update the "view" object literal once the Angular template has been transformed // Update the "view" object literal once the Angular template has been transformed
$timeout(initView); $timeout(initView);
// Deregister listeners when destroying the view // Deregister listeners when destroying the view
scope.$on('$destroy', function() { scope.$on('$destroy', function() {
deregisterDragStart();
deregisterDragStop();
if (view) { if (view) {
element.off('mouseover', view.updateFromPointerHandler); view.$destroy();
angular.element($window).off('resize', view.updateCoordinates);
} }
}); });
function initView() { function initView() {
view = new sgScrollView(element, type); view = new sgScrollView(element, type);
// Compute coordinates of view element; recompute it on window resize
angular.element($window).on('resize', view.updateCoordinates);
if (type != 'monthly') if (type != 'monthly')
// Scroll to the day start hour defined in the user's defaults // Scroll to the day start hour defined in the user's defaults
Preferences.ready().then(function() { Preferences.ready().then(function() {
@ -66,19 +56,6 @@
}); });
} }
function onDragStart() {
if (view) {
element.on('mouseover', view.updateFromPointerHandler);
view.updateCoordinates();
view.updateFromPointerHandler();
}
}
function onDragEnd() {
element.off('mouseover', view.updateFromPointerHandler);
Calendar.$view = null;
}
/** /**
* sgScrollView * sgScrollView
*/ */
@ -91,11 +68,37 @@
this.dayNumbers = this.getDayNumbers(); this.dayNumbers = this.getDayNumbers();
this.maxX = this.getMaxColumns(); this.maxX = this.getMaxColumns();
// Listen to dragstart and dragend events
this.deregisterDragStart = $rootScope.$on('calendar:dragstart', angular.bind(this, this.onDragStart));
this.deregisterDragStop = $rootScope.$on('calendar:dragend', angular.bind(this, this.onDragEnd));
this.bindedUpdateCoordinates = angular.bind(this, this.updateCoordinates);
this.bindedUpdateFromPointerHandler = angular.bind(this, this.updateFromPointerHandler);
// Compute coordinates of view element; recompute it on window resize
this.updateCoordinates(); this.updateCoordinates();
angular.element($window).on('resize', this.bindedUpdateCoordinates);
} }
sgScrollView.prototype = { sgScrollView.prototype = {
$destroy: function() {
this.deregisterDragStart();
this.deregisterDragStop();
this.$element.off('mousemove', this.bindedUpdateFromPointerHandler);
angular.element($window).off('resize', this.bindedUpdateCoordinates);
},
onDragStart: function() {
this.$element.on('mousemove', this.bindedUpdateFromPointerHandler);
this.updateCoordinates();
this.updateFromPointerHandler();
},
onDragEnd: function() {
this.$element.off('mousemove', this.bindedUpdateFromPointerHandler);
Calendar.$view = null;
},
getQuarterHeight: function() { getQuarterHeight: function() {
var hour0, hour23, height = null; var hour0, hour23, height = null;
@ -183,11 +186,10 @@
// From SOGoScrollController.updateFromPointerHandler // From SOGoScrollController.updateFromPointerHandler
updateFromPointerHandler: function() { updateFromPointerHandler: function() {
var scrollStep, pointerHandler, pointerCoordinates, now, scrollY, minY, delta; var pointerHandler, pointerCoordinates, now, scrollY, minY, delta;
pointerHandler = Component.$ghost.pointerHandler; pointerHandler = Component.$ghost.pointerHandler;
if (this.coordinates && pointerHandler) { if (this.coordinates && pointerHandler) {
scrollStep = this.scrollStep;
pointerCoordinates = pointerHandler.getContainerBasedCoordinates(this); pointerCoordinates = pointerHandler.getContainerBasedCoordinates(this);
if (pointerCoordinates) { if (pointerCoordinates) {
@ -196,7 +198,7 @@
now = new Date().getTime(); now = new Date().getTime();
if (!this.lastScroll || now > this.lastScroll + 100) { if (!this.lastScroll || now > this.lastScroll + 100) {
this.lastScroll = now; this.lastScroll = now;
scrollY = pointerCoordinates.y - scrollStep; scrollY = pointerCoordinates.y - this.scrollStep;
if (scrollY < 0) { if (scrollY < 0) {
minY = -this.element.scrollTop; minY = -this.element.scrollTop;
if (scrollY < minY) if (scrollY < minY)
@ -204,7 +206,7 @@
this.element.scrollTop += scrollY; this.element.scrollTop += scrollY;
} }
else { else {
scrollY = pointerCoordinates.y + scrollStep; scrollY = pointerCoordinates.y + this.scrollStep;
delta = scrollY - this.element.clientHeight; delta = scrollY - this.element.clientHeight;
if (delta > 0) { if (delta > 0) {
this.element.scrollTop += delta; this.element.scrollTop += delta;