var angular = require('angular');
require('../modules');
'use strict';

/**
 * 信息发布Detail页面 缓存服务
 */
var module = angular.module('info.directives');

function getMaxHeight(element, elementCss) {
  var container = element.parentElement,
    sibling = element.previousElementSibling,
    containerHeight = container.getBoundingClientRect().height,
    containerPaddingTop = document.defaultView.getComputedStyle(container, null).paddingTop || 0,
    siblingHeight = sibling.getBoundingClientRect().height,
    outlineMarginVertical = parseInt(elementCss.marginTop) +
      parseInt(elementCss.marginBottom);

  return containerHeight - parseInt(containerPaddingTop) - siblingHeight - outlineMarginVertical;
}

module.directive('moreOutline', function($timeout) {
  return {
    restrict: 'C',
    link: function(scope, element, attrs) {
      var promise = $timeout(function() {
        var outlineCss = document.defaultView.getComputedStyle(element[0], null),
          maxHeight = getMaxHeight(element[0], outlineCss),
          lineHeight = parseInt(outlineCss.lineHeight),
          adjustHeihgt = parseInt((maxHeight + (lineHeight - parseInt(outlineCss.fontSize)) / 2.0)/ lineHeight) * lineHeight;
        
        element[0].style.maxHeight = adjustHeihgt + 'px';
      }, 0);

      scope.$on('$destroy', function() {
        $timeout.cancel(promise);
      });
    }
  };
});