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

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

34.21% Statements 26/76
11.11% Branches 2/18
21.05% Functions 4/19
34.21% Lines 26/76

    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
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    1751×
    1×
    1×

    1×
    1×
    77×

    77×
    77×
    77×
    77×
    77×

    77×

    1×
    77×

    77×

    77×
    77×

    77×

    1×
    2×
    2×

    1×

    1×

    1×

    1×

    1×
     import $ from 'jquery';
    import env from '../core/env';
    import key from '../core/key';

    export default class LinkDialog {
      constructor(context) {
        this.context = context;

        this.ui = $.summernote.ui;
        this.$body = $(document.body);
        this.$editor = context.layoutInfo.editor;
        this.options = context.options;
        this.lang = this.options.langInfo;

        context.memo('help.linkDialog.show', this.options.langInfo.help['linkDialog.show']);
      }

      initialize() {
        const $container = this.options.dialogsInBody ? this.$body : this.$editor;

        const body = [
          '<div class="form-group note-form-group">',
          `<label class="note-form-label">${this.lang.link.textToDisplay}</label>`,
          '<input class="note-link-text form-control note-form-control  note-input" type="text" />',
          '</div>',
          '<div class="form-group note-form-group">',
          `<label class="note-form-label">${this.lang.link.url}</label>`,
          '<input class="note-link-url form-control note-form-control note-input" type="text" value="http://" />',
          '</div>',
          !this.options.disableLinkTarget
            ? $('<div/>').append(this.ui.checkbox({
              id: 'sn-checkbox-open-in-new-window',
              text: this.lang.link.openInNewWindow,
              checked: true
            }).render()).html()
            : ''
        ].join('');

        const buttonClass = 'btn btn-primary note-btn note-btn-primary note-link-btn';
        const footer = `<button type="submit" href="#" class="${buttonClass}" disabled>${this.lang.link.insert}</button>`;

        this.$dialog = this.ui.dialog({
          className: 'link-dialog',
          title: this.lang.link.insert,
          fade: this.options.dialogsFade,
          body: body,
          footer: footer
        }).render().appendTo($container);
      }

      destroy() {
        this.ui.hideDialog(this.$dialog);
        this.$dialog.remove();
      }

      bindEnterKey($input, $btn) {
        $input.on('keypress', (event) => {
          if (event.keyCode === key.code.ENTER) {
            event.preventDefault();
            $btn.trigger('click');
          }
        });
      }

      /**
       * toggle update button
       */
      toggleLinkBtn($linkBtn, $linkText, $linkUrl) {
        this.ui.toggleBtn($linkBtn, $linkText.val() && $linkUrl.val());
      }

      /**
       * Show link dialog and set event handlers on dialog controls.
       *
       * @param {Object} linkInfo
       * @return {Promise}
       */
      showLinkDialog(linkInfo) {
        return $.Deferred((deferred) => {
          const $linkText = this.$dialog.find('.note-link-text');
          const $linkUrl = this.$dialog.find('.note-link-url');
          const $linkBtn = this.$dialog.find('.note-link-btn');
          const $openInNewWindow = this.$dialog.find('input[type=checkbox]');

          this.ui.onDialogShown(this.$dialog, () => {
            this.context.triggerEvent('dialog.shown');

            // if no url was given, copy text to url
            if (!linkInfo.url) {
              linkInfo.url = linkInfo.text;
            }

            $linkText.val(linkInfo.text);

            const handleLinkTextUpdate = () => {
              this.toggleLinkBtn($linkBtn, $linkText, $linkUrl);
              // if linktext was modified by keyup,
              // stop cloning text from linkUrl
              linkInfo.text = $linkText.val();
            };

            $linkText.on('input', handleLinkTextUpdate).on('paste', () => {
              setTimeout(handleLinkTextUpdate, 0);
            });

            const handleLinkUrlUpdate = () => {
              this.toggleLinkBtn($linkBtn, $linkText, $linkUrl);
              // display same link on `Text to display` input
              // when create a new link
              if (!linkInfo.text) {
                $linkText.val($linkUrl.val());
              }
            };

            $linkUrl.on('input', handleLinkUrlUpdate).on('paste', () => {
              setTimeout(handleLinkUrlUpdate, 0);
            }).val(linkInfo.url);

            if (!env.isSupportTouch) {
              $linkUrl.trigger('focus');
            }

            this.toggleLinkBtn($linkBtn, $linkText, $linkUrl);
            this.bindEnterKey($linkUrl, $linkBtn);
            this.bindEnterKey($linkText, $linkBtn);

            const isChecked = linkInfo.isNewWindow !== undefined
              ? linkInfo.isNewWindow : this.context.options.linkTargetBlank;

            $openInNewWindow.prop('checked', isChecked);

            $linkBtn.one('click', (event) => {
              event.preventDefault();

              deferred.resolve({
                range: linkInfo.range,
                url: $linkUrl.val(),
                text: $linkText.val(),
                isNewWindow: $openInNewWindow.is(':checked')
              });
              this.ui.hideDialog(this.$dialog);
            });
          });

          this.ui.onDialogHidden(this.$dialog, () => {
            // detach events
            $linkText.off('input paste keypress');
            $linkUrl.off('input paste keypress');
            $linkBtn.off('click');

            if (deferred.state() === 'pending') {
              deferred.reject();
            }
          });

          this.ui.showDialog(this.$dialog);
        }).promise();
      }

      /**
       * @param {Object} layoutInfo
       */
      show() {
        const linkInfo = this.context.invoke('editor.getLinkInfo');

        this.context.invoke('editor.saveRange');
        this.showLinkDialog(linkInfo).then((linkInfo) => {
          this.context.invoke('editor.restoreRange');
          this.context.invoke('editor.createLink', linkInfo);
        }).fail(() => {
          this.context.invoke('editor.restoreRange');
        });
      }
    }