UNPKG

angular-gantt

Version:

Gantt chart component for AngularJS

90 lines (76 loc) 3.47 kB
(function(){ 'use strict'; angular.module('gantt').factory('GanttTimespan', [function() { var Timespan = function(gantt, model) { this.gantt = gantt; this.model = model; }; // Updates the pos and size of the timespan according to the from - to date Timespan.prototype.updatePosAndSize = function() { this.modelLeft = this.gantt.getPositionByDate(this.model.from); this.modelWidth = this.gantt.getPositionByDate(this.model.to) - this.modelLeft; var lastColumn = this.gantt.columnsManager.getLastColumn(); var maxModelLeft = lastColumn ? lastColumn.left + lastColumn.width : 0; if (this.modelLeft + this.modelWidth < 0 || this.modelLeft > maxModelLeft) { this.left = undefined; this.width = undefined; } else { this.left = Math.min(Math.max(this.modelLeft, 0), this.gantt.width); if (this.modelLeft < 0) { this.truncatedLeft = true; if (this.modelWidth + this.modelLeft > this.gantt.width) { this.truncatedRight = true; this.width = this.gantt.width; } else { this.truncatedRight = false; this.width = this.modelWidth + this.modelLeft; } } else if (this.modelWidth + this.modelLeft > this.gantt.width) { this.truncatedRight = true; this.truncatedLeft = false; this.width = this.gantt.width - this.modelLeft; } else { this.truncatedLeft = false; this.truncatedRight = false; this.width = this.modelWidth; } if (this.width < 0) { this.left = this.left + this.width; this.width = -this.width; } } this.updateView(); }; Timespan.prototype.updateView = function() { if (this.$element) { if (this.left === undefined || this.width === undefined) { this.$element.css('display', 'none'); } else { this.$element.css('display', ''); this.$element.css('left', this.left + 'px'); this.$element.css('width', this.width + 'px'); } } }; // Expands the start of the timespan to the specified position (in em) Timespan.prototype.setFrom = function(x) { this.from = this.gantt.getDateByPosition(x); this.updatePosAndSize(); }; // Expands the end of the timespan to the specified position (in em) Timespan.prototype.setTo = function(x) { this.to = this.gantt.getDateByPosition(x); this.updatePosAndSize(); }; // Moves the timespan to the specified position (in em) Timespan.prototype.moveTo = function(x) { this.from = this.gantt.getDateByPosition(x); this.to = this.gantt.getDateByPosition(x + this.width); this.updatePosAndSize(); }; Timespan.prototype.clone = function() { return new Timespan(this.gantt, angular.copy(this.model)); }; return Timespan; }]); }());