UNPKG

pip-webui

Version:

HTML5 UI for LOB applications

121 lines (99 loc) 5.01 kB
/** * @file Card layout * @copyright Digital Living Software Corp. 2014-2015 */ /* global angular */ (function () { 'use strict'; var thisModule = angular.module('pipLayout.Card', []); thisModule.directive('pipCard', function() { return { restrict: 'EA', //controller: 'pipCardController' link: function($scope, $element, $attrs) { var $window = $(window); // Add class to the element $element.addClass('pip-card'); // Resize every time window is resized $scope.$on('pipWindowResized', resize); // Resize right away to avoid flicking resize(); // Resize the element right away setTimeout(resize, 100); return; //--------------- function resize() { var $mainBody = $('.pip-main-body'), cardContainer = $('.pip-card-container'), windowWidth = $window.width(), maxWidth = $mainBody.width(), maxHeight = $mainBody.height(), minWidth = $attrs.minWidth ? Math.floor($attrs.minWidth) : null, minHeight = $attrs.minHeight ? Math.floor($attrs.minHeight) : null, width = $attrs.width ? Math.floor($attrs.width) : null, height = $attrs.height ? Math.floor($attrs.height) : null, left, top; // Full-screen on phone if (windowWidth <= 768) { minWidth = null; minHeight = null; width = null; height = null; maxWidth = null; maxHeight = null; } // Card view with adjustable margins on tablet and desktop else { // Set margin and maximum dimensions var space = windowWidth > 1200 ? 24 : 16; maxWidth -= space * 2; maxHeight -= space * 2; // Set minimum dimensions minWidth = minWidth ? Math.min(minWidth, maxWidth) : null; minHeight = minHeight ? Math.min(minHeight, maxHeight) : null; // Set regular dimensions width = width ? Math.min(width, maxWidth) : null; height = height ? Math.min(height, maxHeight) : null; } // Set dimensions $element.css('max-width', maxWidth ? maxWidth + 'px' : ''); $element.css('min-width', minWidth ? minWidth + 'px' : ''); $element.css('width', width ? width + 'px' : ''); $element.css('height', height ? height + 'px' : ''); if (!cardContainer.hasClass('pip-outer-scroll')) { $element.css('max-height', maxHeight ? maxHeight + 'px' : ''); $element.css('min-height', minHeight ? minHeight + 'px' : ''); var $header = $element.find('.pip-header:visible'), $footer = $element.find('.pip-footer:visible'), $body = $element.find('.pip-body'), maxBodyHeight = maxHeight || $mainBody.height(); if ($header.length > 0) maxBodyHeight -= $header.outerHeight(true); if ($footer.length > 0) maxBodyHeight -= $footer.outerHeight(true); $body.css('max-height', maxBodyHeight + 'px'); } else { cardContainer.addClass('pip-scroll'); if (windowWidth <= 768) { left = 0; top = 0; } else { left = cardContainer.width() / 2 - $element.width() / 2 - 16; top = Math.max(cardContainer.height() / 2 - $element.height() / 2 - 16, 0); } $element.css('left', left); $element.css('top', top); setTimeout(function () { $element.css('display', 'flex'); }, 100); } // Notify child controls that layout was resized $scope.$broadcast('pipLayoutResized'); }; } } }); })();