(function ($) { var scrollWidthHeight = $.getScrollBarWidthHeight(); var settings = { mode: 'calendar', // available modes: team, view, calendar headerWidth: 70, tableHeightMin: 450, noVScroll: true, // use main page scroll, show all calendar resizeTimeout: 15, scrollWidth: scrollWidthHeight[0], scrollHeight: scrollWidthHeight[1], browserNonIE: ["ie", "ie1"].indexOf(checkBrowser()) < 0, //IE places scrollbars above the content and does not need additional space for scrollbar }; var registerWatchers = function ($container) { var data = { $parent: $container.parent(), $container: $container, $firstCellContainer: $container.find("div.ac-left-col"), $resizableTable: $container.find("table.main-table"), $heightContainer: $container.find("div.height-container"), $widthContainer: $container.find("div.width-container") }; //console.log("register resize watchers"); setupResizeEvent($container, data); setupResizeEvent($container.find('.watch-resizing'), data); $container.find('.watch-scroll').on('scroll', data, scrollHandler); }; var setupResizeEvent = function (obj, data) { $(obj).off('resize.ac-grid').on('resize.ac-grid', function () { if (settings.resizeTimeout > 0) { if ($(obj).data('resize.ac-grid.timeout')) { clearTimeout($(obj).data('resize.ac-grid.timeout')); } $(obj).data('resize.ac-grid.timeout', setTimeout(resizeHandler, settings.resizeTimeout, obj, data)); } else resizeHandler(obj, data); }); }; var resizeHandler = function (obj, data) { var startDate = new Date().getTime(); var width = data.$resizableTable.width(), height = data.$resizableTable.height(), heightContainerHeight = data.$heightContainer.height(); var newwidth = width; //console.log('resizing started: ' + startDate + ' ms. Width=' + width + ', Height=' + height + ', heightContainerHeight=' + heightContainerHeight); if (settings.noVScroll) { heightContainerHeight = height; data.$heightContainer.css("overflow-y", "hidden"); //data.$heightContainer.height(heightContainerHeight); } else if ((heightContainerHeight <= settings.tableHeightMin && height > 0)) { heightContainerHeight = height + settings.scrollHeight; //data.$heightContainer.height(heightContainerHeight); } //resize container width var vertScrollShown = height > heightContainerHeight; var visibleCellCount = data.$resizableTable.attr('data-visible-cells'); if (settings.mode === 'calendar') { newwidth = Math.min((settings.headerWidth * visibleCellCount) + (settings.browserNonIE && vertScrollShown ? settings.scrollWidth : 0) + 1, data.$parent.outerWidth() - data.$firstCellContainer.width() - 1) //data.$widthContainer.width(newwidth); } else { newwidth = Math.min(settings.headerWidth * visibleCellCount + (settings.browserNonIE && vertScrollShown ? settings.scrollWidth : 0), data.$parent.outerWidth() - data.$firstCellContainer.width() - 1); } //resize container height again when horizont scroll bar appears if (settings.noVScroll && settings.browserNonIE) { var heightContainerWidth = data.$heightContainer.width(); var horizontScrollShown = width > heightContainerWidth; if (horizontScrollShown) { heightContainerHeight += settings.scrollHeight + 1; //data.$heightContainer.height(heightContainerHeight + 1); // +1 just for better look and feel to have a gap between scroll and content } } data.$heightContainer.height(heightContainerHeight ); data.$widthContainer.width(newwidth); var endDate = new Date().getTime(); //console.log('resizing: ' + (endDate - startDate) + ' ms'); }; var scrollHandler = function (event) { var leftPosition = (-event.target.scrollLeft + 'px'); var topPosition = (-event.target.scrollTop + 'px'); event.data.$container.find('.horizontal-scroll-target').css('left', leftPosition); event.data.$container.find('.vertical-scroll-target').css('margin-top', topPosition); }; var methods = { init: function (options) { settings = $.extend(true, settings, options); this.each(function () { registerWatchers($(this)); }); }, }; $.fn.activityCalendarGrid = function (options) { if (typeof options === 'string' && typeof methods[options] === 'function') { return methods[options].apply(this, Array.prototype.slice.call(arguments, 1)); } else if (typeof options === 'object' || !options) { return methods.init.apply(this, arguments); } }; }(jQuery));