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

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

function getMaxHeight(element, elementCss) {
  var container = element.parentElement.parentElement.parentElement,
    header = element.parentElement.parentElement.previousElementSibling,

    containerHeight = container.getBoundingClientRect().height,
    headerHeight = header.getBoundingClientRect().height;
  return containerHeight - parseInt(headerHeight);
}

module.directive('lineHeight', 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),
          adjustHeihgt = parseInt(maxHeight / 5);
        
        element[0].style.lineHeight = adjustHeihgt + 'px';
      }, 0);

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