;
(function() {
    const
        EventEmitter = require('myevents'),
        apiService = require('app-service'),
        appCache = require('app-cache');

    angular
        .module('app.widgets')
        .directive('middleMenu', middleMenu);

    function middleMenu() {
        var directive = {
            controller: link,
            templateUrl: "./app/widgets/middleMenu/middle_widgets.html",
            restrict: 'EA',
            replace: true,
            scope: {
                breadcrumbs: '=',
                crumbChange: " ="
            }
        };
        link.$inject = ['$scope','$element','$attrs','$timeout'] ;
        return directive;

        function link($scope, $element, $attrs, $timeout) {
            var vm = $scope;
            nw.Window.get().on('resize', function() {
               EventEmitter.emit('more');
            }); 
            $scope.$on('$destroy', () => {
                EventEmitter.removeAllListeners('more');
            });
            vm.crumbClk = function(idx) {
                
                if(idx === (vm.breadcrumbs.length-1)){
                    return false;
                }
                if (vm.crumbChange) {
                    vm.crumbChange(idx);
                }
            };

            vm.isSelected = false;//默认隐藏侧边栏按钮为false
            vm.changeList = function(){
                if(vm.$parent.$parent.changeLeftMenu && vm.$parent.$parent.changeLeftMenu == true){
                    vm.isSelected = true;
                    return vm.$parent.$parent.changeLeftMenu = false;
                }
                vm.isSelected = false;
                vm.$parent.$parent.changeLeftMenu = true;
            };

            function more() {
                 $timeout(() => {              
                     angular.element('.storage').find('.more').remove();
                     var cb = Array.prototype.slice.call(angular.element('.storage').find('.m'));  
                     var mdHeight = $element.find('.md-middle')[0].clientWidth - 32;                                        
                     var crumbHeight = 0,
                         crumbAll = 0;
                     for (var i = cb.length - 1; i > -1; i--) {
                         var num;
                         if (crumbHeight < (mdHeight - 201)   ) {
                             crumbHeight += (cb[i].clientWidth + 6);
                             num = i;
                         } 
                        angular.element('.storage').find('.m').eq(i).css({'float':'left'});
                        crumbAll += (cb[i].clientWidth + 6);
                     }
                     if (crumbAll > mdHeight) {
                        angular.element('.storage').find('.m:lt(' + num + '):gt(0)').css({ 'float': 'right' });
                        angular.element('.storage').find('.m').eq(0).after('<span class="more" style="float:left;padding:0 10px;">.....</span>');
                     }
                 });
             }

            EventEmitter.on('more',more);
             more();
        }
    }
})();
