dialog component

This commit is contained in:
cookfront
2017-02-16 15:12:28 +08:00
parent 82bb27896e
commit 4c3cdd433b
15 changed files with 350 additions and 6 deletions

View File

@@ -0,0 +1,8 @@
## 0.0.2 (2017-01-20)
* 改了bug A
* 加了功能B
## 0.0.1 (2017-01-10)
* 第一版

26
packages/dialog/README.md Normal file
View File

@@ -0,0 +1,26 @@
# @youzan/<%= name %>
!!! 请在此处填写你的文档最简单描述 !!!
[![version][version-image]][download-url]
[![download][download-image]][download-url]
[version-image]: http://npm.qima-inc.com/badge/v/@youzan/<%= name %>.svg?style=flat-square
[download-image]: http://npm.qima-inc.com/badge/d/@youzan/<%= name %>.svg?style=flat-square
[download-url]: http://npm.qima-inc.com/package/@youzan/<%= name %>
## Demo
## Usage
## API
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|-----------|-----------|-----------|-------------|-------------|
| className | 自定义额外类名 | string | '' | '' |
## License
[MIT](https://opensource.org/licenses/MIT)

3
packages/dialog/index.js Normal file
View File

@@ -0,0 +1,3 @@
import Dialog from './src/dialog.js';
export default Dialog;

View File

@@ -0,0 +1,10 @@
{
"name": "<%= name %>",
"version": "<%= version %>",
"description": "<%= description %>",
"main": "./lib/index.js",
"author": "<%= author %>",
"license": "<%= license %>",
"devDependencies": {},
"dependencies": {}
}

View File

@@ -0,0 +1,98 @@
import Vue from 'vue';
import Dialog from './dialog.vue';
import merge from 'src/utils/merge';
const DialogConstructor = Vue.extend(Dialog);
let currentDialog;
let instance;
let dialogQueue = [];
const defaultCallback = action => {
if (currentDialog) {
let callback = currentDialog.callback;
if (typeof callback === 'function') {
callback(action);
}
if (currentDialog.resolve && action !== 'cancel') {
currentDialog.resolve(action);
} else {
currentDialog.reject(action);
}
}
};
var initInstance = () => {
instance = new DialogConstructor({
el: document.createElement('div')
});
instance.callback = defaultCallback;
};
var showNextDialog = () => {
if (!instance) {
initInstance();
}
if (!instance.value && dialogQueue.length > 0) {
currentDialog = dialogQueue.shift();
let options = currentDialog.options;
for (let prop in options) {
if (options.hasOwnProperty(prop)) {
instance[prop] = options[prop];
}
}
console.log(instance)
if (options.callback === undefined) {
instance.callback = defaultCallback;
}
document.body.appendChild(instance.$el);
Vue.nextTick(() => {
instance.value = true;
});
}
};
var DialogBox = options => {
console.log(options)
return new Promise((resolve, reject) => { // eslint-disable-line
dialogQueue.push({
options: merge({}, options),
callback: options.callback,
resolve: resolve,
reject: reject
});
showNextDialog();
});
};
DialogBox.alert = function(options) {
return DialogBox(merge({
type: 'alert',
closeOnClickOverlay: false
}, options));
};
DialogBox.confirm = function(options) {
return DialogBox(merge({
type: 'confirm',
showCancelButton: true
}, options));
};
DialogBox.close = function() {
instance.value = false;
dialogQueue = [];
currentDialog = null;
};
export default DialogBox;

View File

@@ -0,0 +1,63 @@
<template>
<transition name="dialog-fade">
<div class="o2-dialog-wrapper">
<div class="o2-dialog" v-show="value">
<div class="o2-dialog-header" v-if="title">
<div class="o2-dialog-title" v-text="title"></div>
</div>
<div class="o2-dialog-content" v-if="message">
<div class="o2-dialog-message" v-html="message"></div>
</div>
<div class="o2-dialog-footer" :class="{ 'is-twobtn': showCancelButton && showConfirmButton }">
<button class="o2-dialog-btn o2-dialog-cancel" v-show="showCancelButton" @click="handleAction('cancel')">{{ cancelButtonText }}</button>
<button class="o2-dialog-btn o2-dialog-confirm" v-show="showConfirmButton" @click="handleAction('confirm')">{{ confirmButtonText }}</button>
</div>
</div>
</div>
</transition>
</template>
<script>
import Popup from 'packages/popup';
const CANCEL_TEXT = '取消';
const CONFIRM_TEXT = '确认';
export default {
name: 'o2-dialog',
mixins: [Popup],
props: {
overlay: {
default: true
},
closeOnClickOverlay: {
default: true
},
lockOnScroll: {
default: true
}
},
data() {
return {
title: '提示',
message: '',
type: '',
showConfirmButton: true,
showCancelButton: false,
confirmButtonText: CONFIRM_TEXT,
cancelButtonText: CANCEL_TEXT,
callback: null
};
},
methods: {
handleAction(action) {
this.value = false;
this.callback && this.callback(action);
}
}
};
</script>