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

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

60.4% Statements 61/101
55.1% Branches 27/49
31.25% Functions 5/16
60.4% Lines 61/101

    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
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    2061×
    1×
    1×

    1×
    1×
    79×

    79×
    79×
    79×
    79×
    79×

    1×
    77×

    77×

    77×
    77×

    77×

    1×
    2×
    2×

    1×

    1×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×
    5×

    5×

    5×

    5×

    5×

    5×

    5×
    2×
    2×

    3×

    3×

    2×

    2×

    1×

    1×

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

    export default class VideoDialog {
      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;
      }

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

        const body = [
          '<div class="form-group note-form-group row-fluid">',
          `<label class="note-form-label">${this.lang.video.url} <small class="text-muted">${this.lang.video.providers}</small></label>`,
          '<input class="note-video-url form-control note-form-control note-input" type="text" />',
          '</div>'
        ].join('');
        const buttonClass = 'btn btn-primary note-btn note-btn-primary note-video-btn';
        const footer = `<button type="submit" href="#" class="${buttonClass}" disabled>${this.lang.video.insert}</button>`;

        this.$dialog = this.ui.dialog({
          title: this.lang.video.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');
          }
        });
      }

      createVideoNode(url) {
        // video url patterns(youtube, instagram, vimeo, dailymotion, youku, mp4, ogg, webm)
        const ytRegExp = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
        const ytMatch = url.match(ytRegExp);

        const igRegExp = /(?:www\.|\/\/)instagram\.com\/p\/(.[a-zA-Z0-9_-]*)/;
        const igMatch = url.match(igRegExp);

        const vRegExp = /\/\/vine\.co\/v\/([a-zA-Z0-9]+)/;
        const vMatch = url.match(vRegExp);

        const vimRegExp = /\/\/(player\.)?vimeo\.com\/([a-z]*\/)*(\d+)[?]?.*/;
        const vimMatch = url.match(vimRegExp);

        const dmRegExp = /.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/;
        const dmMatch = url.match(dmRegExp);

        const youkuRegExp = /\/\/v\.youku\.com\/v_show\/id_(\w+)=*\.html/;
        const youkuMatch = url.match(youkuRegExp);

        const qqRegExp = /\/\/v\.qq\.com.*?vid=(.+)/;
        const qqMatch = url.match(qqRegExp);

        const qqRegExp2 = /\/\/v\.qq\.com\/x?\/?(page|cover).*?\/([^\/]+)\.html\??.*/;
        const qqMatch2 = url.match(qqRegExp2);

        const mp4RegExp = /^.+.(mp4|m4v)$/;
        const mp4Match = url.match(mp4RegExp);

        const oggRegExp = /^.+.(ogg|ogv)$/;
        const oggMatch = url.match(oggRegExp);

        const webmRegExp = /^.+.(webm)$/;
        const webmMatch = url.match(webmRegExp);

        let $video;
        Iif (ytMatch && ytMatch[1].length === 11) {
          const youtubeId = ytMatch[1];
          $video = $('<iframe>')
            .attr('frameborder', 0)
            .attr('src', '//www.youtube.com/embed/' + youtubeId)
            .attr('width', '640').attr('height', '360');
        } else Iif (igMatch && igMatch[0].length) {
          $video = $('<iframe>')
            .attr('frameborder', 0)
            .attr('src', 'https://instagram.com/p/' + igMatch[1] + '/embed/')
            .attr('width', '612').attr('height', '710')
            .attr('scrolling', 'no')
            .attr('allowtransparency', 'true');
        } else Iif (vMatch && vMatch[0].length) {
          $video = $('<iframe>')
            .attr('frameborder', 0)
            .attr('src', vMatch[0] + '/embed/simple')
            .attr('width', '600').attr('height', '600')
            .attr('class', 'vine-embed');
        } else Iif (vimMatch && vimMatch[3].length) {
          $video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>')
            .attr('frameborder', 0)
            .attr('src', '//player.vimeo.com/video/' + vimMatch[3])
            .attr('width', '640').attr('height', '360');
        } else Iif (dmMatch && dmMatch[2].length) {
          $video = $('<iframe>')
            .attr('frameborder', 0)
            .attr('src', '//www.dailymotion.com/embed/video/' + dmMatch[2])
            .attr('width', '640').attr('height', '360');
        } else Iif (youkuMatch && youkuMatch[1].length) {
          $video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>')
            .attr('frameborder', 0)
            .attr('height', '498')
            .attr('width', '510')
            .attr('src', '//player.youku.com/embed/' + youkuMatch[1]);
        } else if ((qqMatch && qqMatch[1].length) || (qqMatch2 && qqMatch2[2].length)) {
          const vid = ((qqMatch && qqMatch[1].length) ? qqMatch[1] : qqMatch2[2]);
          $video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>')
            .attr('frameborder', 0)
            .attr('height', '310')
            .attr('width', '500')
            .attr('src', 'http://v.qq.com/iframe/player.html?vid=' + vid + '&amp;auto=0');
        } else Iif (mp4Match || oggMatch || webmMatch) {
          $video = $('<video controls>')
            .attr('src', url)
            .attr('width', '640').attr('height', '360');
        } else {
          // this is not a known video link. Now what, Cat? Now what?
          return false;
        }

        $video.addClass('note-video-clip');

        return $video[0];
      }

      show() {
        const text = this.context.invoke('editor.getSelectedText');
        this.context.invoke('editor.saveRange');
        this.showVideoDialog(text).then((url) => {
          // [workaround] hide dialog before restore range for IE range focus
          this.ui.hideDialog(this.$dialog);
          this.context.invoke('editor.restoreRange');

          // build node
          const $node = this.createVideoNode(url);

          if ($node) {
            // insert video node
            this.context.invoke('editor.insertNode', $node);
          }
        }).fail(() => {
          this.context.invoke('editor.restoreRange');
        });
      }

      /**
       * show image dialog
       *
       * @param {jQuery} $dialog
       * @return {Promise}
       */
      showVideoDialog(text) {
        return $.Deferred((deferred) => {
          const $videoUrl = this.$dialog.find('.note-video-url');
          const $videoBtn = this.$dialog.find('.note-video-btn');

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

            $videoUrl.val(text).on('input', () => {
              this.ui.toggleBtn($videoBtn, $videoUrl.val());
            });

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

            $videoBtn.click((event) => {
              event.preventDefault();

              deferred.resolve($videoUrl.val());
            });

            this.bindEnterKey($videoUrl, $videoBtn);
          });

          this.ui.onDialogHidden(this.$dialog, () => {
            $videoUrl.off('input');
            $videoBtn.off('click');

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

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