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

77.78% Statements 28/36
16.67% Branches 1/6
100% Functions 10/10
77.78% Lines 28/36
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   78× 77×   77× 77× 77×   53×             79×     77×     77× 77×     77×   77×           53× 53× 53×                                       57×    
import $ from 'jquery';
import lists from '../core/lists';
import dom from '../core/dom';
 
export default class LinkPopover {
  constructor(context) {
    this.context = context;
 
    this.ui = $.summernote.ui;
    this.options = context.options;
    this.events = {
      'summernote.keyup summernote.mouseup summernote.change summernote.scroll': () => {
        this.update();
      },
      'summernote.disable summernote.dialog.shown': () => {
        this.hide();
      }
    };
  }
 
  shouldInitialize() {
    return !lists.isEmpty(this.options.popover.link);
  }
 
  initialize() {
    this.$popover = this.ui.popover({
      className: 'note-link-popover',
      callback: ($node) => {
        const $content = $node.find('.popover-content,.note-popover-content');
        $content.prepend('<span><a target="_blank"></a>&nbsp;</span>');
      }
    }).render().appendTo(this.options.container);
    const $content = this.$popover.find('.popover-content,.note-popover-content');
 
    this.context.invoke('buttons.build', $content, this.options.popover.link);
  }
 
  destroy() {
    this.$popover.remove();
  }
 
  update() {
    // Prevent focusing on editable when invoke('code') is executed
    Eif (!this.context.invoke('editor.hasFocus')) {
      this.hide();
      return;
    }
 
    const rng = this.context.invoke('editor.createRange');
    if (rng.isCollapsed() && rng.isOnAnchor()) {
      const anchor = dom.ancestor(rng.sc, dom.isAnchor);
      const href = $(anchor).attr('href');
      this.$popover.find('a').attr('href', href).html(href);
 
      const pos = dom.posFromPlaceholder(anchor);
      this.$popover.css({
        display: 'block',
        left: pos.left,
        top: pos.top
      });
    } else {
      this.hide();
    }
  }
 
  hide() {
    this.$popover.hide();
  }
}