ui-nav.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. angular.module('app')
  2. .directive('uiNav', ['$timeout', function($timeout) {
  3. return {
  4. restrict: 'AC',
  5. link: function(scope, el, attr) {
  6. var _window = $(window),
  7. _mb = 768,
  8. wrap = $('.app-aside'),
  9. next,
  10. backdrop = '.dropdown-backdrop';
  11. // unfolded
  12. el.on('click', 'a', function(e) {
  13. next && next.trigger('mouseleave.nav');
  14. var _this = $(this);
  15. _this.parent().siblings( ".active" ).toggleClass('active');
  16. _this.next().is('ul') && _this.parent().toggleClass('active') && e.preventDefault();
  17. // mobile
  18. _this.next().is('ul') || ( ( _window.width() < _mb ) && $('.app-aside').removeClass('show off-screen') );
  19. });
  20. // folded & fixed
  21. el.on('mouseenter', 'a', function(e){
  22. next && next.trigger('mouseleave.nav');
  23. $('> .nav', wrap).remove();
  24. if ( !$('.app-aside-fixed.app-aside-folded').length || ( _window.width() < _mb ) || $('.app-aside-dock').length) return;
  25. var _this = $(e.target)
  26. , top
  27. , w_h = $(window).height()
  28. , offset = 50
  29. , min = 150;
  30. !_this.is('a') && (_this = _this.closest('a'));
  31. if( _this.next().is('ul') ){
  32. next = _this.next();
  33. }else{
  34. return;
  35. }
  36. _this.parent().addClass('active');
  37. top = _this.parent().position().top + offset;
  38. next.css('top', top);
  39. if( top + next.height() > w_h ){
  40. next.css('bottom', 0);
  41. }
  42. if(top + min > w_h){
  43. next.css('bottom', w_h - top - offset).css('top', 'auto');
  44. }
  45. next.appendTo(wrap);
  46. next.on('mouseleave.nav', function(e){
  47. $(backdrop).remove();
  48. next.appendTo(_this.parent());
  49. next.off('mouseleave.nav').css('top', 'auto').css('bottom', 'auto');
  50. _this.parent().removeClass('active');
  51. });
  52. $('.smart').length && $('<div class="dropdown-backdrop"/>').insertAfter('.app-aside').on('click', function(next){
  53. next && next.trigger('mouseleave.nav');
  54. });
  55. });
  56. wrap.on('mouseleave', function(e){
  57. next && next.trigger('mouseleave.nav');
  58. $('> .nav', wrap).remove();
  59. });
  60. }
  61. };
  62. }]);