Code coverage report for src/js/base/module/Toolbar.js

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

85.37% Statements 70/82
67.65% Branches 23/34
100% Functions 12/12
85.37% Lines 70/82

    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
    138
    139
    140
    141
    142
    143
    144
    145
    146
    1471×
    1×
    1×
    96×

    96×
    96×

    96×
    96×
    96×
    96×
    96×
    96×
    96×

    96×
    96×

    1×
    106×

    97×
    96×

    96×

    96×

    96×

    96×

    96×
    63×

    96×
    96×

    1×
    2×

    2×

    1×
    98×
    1×

    97×
    97×
    97×
    97×

    97×
    97×

    97×
    97×
    97×
    97×
    97×

    97×

    97×

    1×
    98×
    1×

    97×

    98×

    1×
    2×

    2×

    1×
    2×
    2×
    1×

    1×

    1×
    2×
    2×
    1×

    2×

    1×
    4×
    4×
    1×

    4×

    1×
     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.$editable = context.layoutInfo.editable;
        this.$statusbar = context.layoutInfo.statusbar;
        this.options = context.options;

        this.isFollowing = false;
        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');
        Iif (this.options.followingToolbar) {
          this.$window.on('scroll resize', this.followScroll);
        }
      }

      destroy() {
        this.$toolbar.children().remove();

        Iif (this.options.followingToolbar) {
          this.$window.off('scroll resize', this.followScroll);
        }
      }

      followScroll() {
        if (this.$editor.hasClass('fullscreen')) {
          return false;
        }

        const editorHeight = this.$editor.outerHeight();
        const editorWidth = this.$editor.width();
        const toolbarHeight = this.$toolbar.height();
        const statusbarHeight = this.$statusbar.height();

        // 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 - statusbarHeight;

        Iif (!this.isFollowing &&
          (currentOffset > activateOffset) && (currentOffset < deactivateOffsetBottom - toolbarHeight)) {
          this.isFollowing = true;
          this.$toolbar.css({
            position: 'fixed',
            top: otherBarHeight,
            width: editorWidth,
          });
          this.$editable.css({
            marginTop: this.$toolbar.height() + 5,
          });
        } else Iif (this.isFollowing &&
          ((currentOffset < activateOffset) || (currentOffset > deactivateOffsetBottom))) {
          this.isFollowing = false;
          this.$toolbar.css({
            position: 'relative',
            top: 0,
            width: '100%',
          });
          this.$editable.css({
            marginTop: '',
          });
        }
      }

      changeContainer(isFullscreen) {
        if (isFullscreen) {
          this.$toolbar.prependTo(this.$editor);
        } else {
          Iif (this.options.toolbarContainer) {
            this.$toolbar.appendTo(this.options.toolbarContainer);
          }
        }
        this.followScroll();
      }

      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);
      }
    }