Blame view

apps/plugins/grass-prompt.js 2.97 KB
yyq authored
1
import {isArray} from 'lodash';
yyq authored
2 3 4 5 6 7 8

export default {
  install(Vue) {
    let promptVM = null;
    let promptEle = null;
    let timer = null;
yyq authored
9
    function createPromptVM(parent) {
yyq authored
10
      let tpl = Vue.extend({
yyq authored
11
        store: parent.$store,
TaoHuang authored
12 13 14 15 16 17 18 19 20 21 22 23
        data: function() {
          return {
            type: '',
            img: '',
            title: '',
            desc: '',
            onClick: null,
            classNames: []
          };
        },
        render: function(h) {
          let classNames = ['yoho-grass-prompt-wrap', ...this.classNames];
yyq authored
24
25 26
          return h('div', {class: ['yoho-grass-prompt', `yoho-grass-prompt-${this.type}`]}, [
            h(this.url ? 'a' : 'div', {
TaoHuang authored
27
              class: classNames,
28 29 30
              attrs: {
                href: this.url || '',
              },
TaoHuang authored
31
              on: {
TaoHuang authored
32
                click: () => {
TaoHuang authored
33
                  this.onClick && this.onClick();
yyq authored
34
                }
TaoHuang authored
35 36 37 38 39 40 41
              }
            }, [
              h('ImageFormat', {
                class: ['prompt-thumb'],
                props: {
                  src: this.img,
                  lazy: false,
yyq authored
42
                  mode: 1,
TaoHuang authored
43 44 45 46 47 48 49 50 51
                  width: 100,
                  height: 100
                }
              }),
              h('div', { class: ['prompt-info'] }, [
                h('p', {
                  class: ['prompt-info-title'],
                  domProps: {
                    innerHTML: this.title
yyq authored
52 53
                  }
                }),
TaoHuang authored
54 55 56 57 58 59 60 61
                h('p', {
                  class: ['prompt-info-desc'],
                  domProps: {
                    innerHTML: this.desc
                  }
                })
              ]),
              h('span', { class: ['iconfont', 'icon-right', 'prompt-icon'] })
yyq authored
62
            ])
TaoHuang authored
63 64
          ]);
        }
yyq authored
65 66 67 68 69 70 71 72
      });

      promptVM = new tpl();
      promptEle = promptVM.$mount().$el;
      document.body.appendChild(promptEle);
    }

    Vue.prototype.$grassPrompt = function(data = {}, time) {
yyq authored
73
      let {img, title, desc, url, type, className, onClick} = data;
yyq authored
74 75

      if (!promptVM) {
TaoHuang authored
76
        createPromptVM(this);
yyq authored
77 78
      }
TaoHuang authored
79
      promptVM.img = img || '';
yyq authored
80 81 82
      promptVM.title = title || '';
      promptVM.desc = desc || '';
      promptVM.url = url;
yyq authored
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
      promptVM.type = type || 'bottom';

      if (className && !isArray(className)) {
        className = [className];
      }

      promptVM.classNames = className || [];

      if (typeof onClick === 'function') {
        promptVM.onClick = onClick;
      }

      let res = {
        show() {
          timer && clearTimeout(timer);
          timer = setTimeout(function() {
            promptVM.classNames.push('yoho-grass-prompt-enter-active');

            timer = setTimeout(function() {
              promptVM.classNames.push('yoho-grass-prompt-leave-active');
            }, time || 8000);
          }, 100);
        },
        hide() {
          timer && clearTimeout(timer);
          promptVM.classNames = [];
        }
      }

      res.show();

      return res;
    }
  }
}