all files / src/js/base/module/ Toolbar.js

93.24% Statements 69/74
67.86% Branches 19/28
100% Functions 12/12
93.24% Lines 69/74
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 77×   77× 77×   77× 77× 77× 77× 77×   77×     87×     78× 77×   77×     77×     77×       77×   77× 52×     77× 77× 77×               544×     543× 543× 543×   543× 543×         543× 543×       543× 543× 543× 543× 543×   543×             543×               79×   78×                                    
import $ from 'jquery';
export default class Toolbar {
  constructor(context) {
    this.context = context;
 
    this.$window = $(window);
    this.$document = $(document);
 
    this.ui = $.summernote.ui;
    this.$note = context.layoutInfo.note;
    this.$editor = context.layoutInfo.editor;
    this.$toolbar = context.layoutInfo.toolbar;
    this.options = context.options;
 
    this.followScroll = this.followScroll.bind(this);
  }
 
  shouldInitialize() {
    return !this.options.airMode;
  }
 
  initialize() {
    this.options.toolbar = this.options.toolbar || [];
 
    Iif (!this.options.toolbar.length) {
      this.$toolbar.hide();
    } else {
      this.context.invoke('buttons.build', this.$toolbar, this.options.toolbar);
    }
 
    Iif (this.options.toolbarContainer) {
      this.$toolbar.appendTo(this.options.toolbarContainer);
    }
 
    this.changeContainer(false);
 
    this.$note.on('summernote.keyup summernote.mouseup summernote.change', () => {
      this.context.invoke('buttons.updateCurrentStyle');
    });
 
    this.context.invoke('buttons.updateCurrentStyle');
    Eif (this.options.followingToolbar) {
      this.$window.on('scroll resize', this.followScroll);
    }
  }
 
  destroy() {
    this.$toolbar.children().remove();
 
    Eif (this.options.followingToolbar) {
      this.$window.off('scroll resize', this.followScroll);
    }
  }
 
  followScroll() {
    if (this.$editor.hasClass('fullscreen')) {
      return false;
    }
 
    const $toolbarWrapper = this.$toolbar.parent('.note-toolbar-wrapper');
    const editorHeight = this.$editor.outerHeight();
    const editorWidth = this.$editor.width();
 
    const toolbarHeight = this.$toolbar.height();
    $toolbarWrapper.css({
      height: toolbarHeight
    });
 
    // check if the web app is currently using another static bar
    let otherBarHeight = 0;
    Iif (this.options.otherStaticBar) {
      otherBarHeight = $(this.options.otherStaticBar).outerHeight();
    }
 
    const currentOffset = this.$document.scrollTop();
    const editorOffsetTop = this.$editor.offset().top;
    const editorOffsetBottom = editorOffsetTop + editorHeight;
    const activateOffset = editorOffsetTop - otherBarHeight;
    const deactivateOffsetBottom = editorOffsetBottom - otherBarHeight - toolbarHeight;
 
    Iif ((currentOffset > activateOffset) && (currentOffset < deactivateOffsetBottom)) {
      this.$toolbar.css({
        position: 'fixed',
        top: otherBarHeight,
        width: editorWidth
      });
    } else {
      this.$toolbar.css({
        position: 'relative',
        top: 0,
        width: '100%'
      });
    }
  }
 
  changeContainer(isFullscreen) {
    if (isFullscreen) {
      this.$toolbar.prependTo(this.$editor);
    } else {
      Iif (this.options.toolbarContainer) {
        this.$toolbar.appendTo(this.options.toolbarContainer);
      }
    }
  }
 
  updateFullscreen(isFullscreen) {
    this.ui.toggleBtnActive(this.$toolbar.find('.btn-fullscreen'), isFullscreen);
 
    this.changeContainer(isFullscreen);
  }
 
  updateCodeview(isCodeview) {
    this.ui.toggleBtnActive(this.$toolbar.find('.btn-codeview'), isCodeview);
    if (isCodeview) {
      this.deactivate();
    } else {
      this.activate();
    }
  }
 
  activate(isIncludeCodeview) {
    let $btn = this.$toolbar.find('button');
    if (!isIncludeCodeview) {
      $btn = $btn.not('.btn-codeview');
    }
    this.ui.toggleBtn($btn, true);
  }
 
  deactivate(isIncludeCodeview) {
    let $btn = this.$toolbar.find('button');
    if (!isIncludeCodeview) {
      $btn = $btn.not('.btn-codeview');
    }
    this.ui.toggleBtn($btn, false);
  }
}