element/examples/components/demo-block.vue

316 lines
8.0 KiB
Vue
Raw Normal View History

2016-08-23 16:57:58 +08:00
<template>
<div
class="demo-block"
:class="[blockClass, { 'hover': hovering }]"
@mouseenter="hovering = true"
@mouseleave="hovering = false">
2016-11-08 15:59:02 +08:00
<slot name="source"></slot>
2017-09-29 15:37:50 +08:00
<div class="meta" ref="meta">
<div class="description" v-if="$slots.default">
2016-11-08 15:59:02 +08:00
<slot></slot>
</div>
<slot name="highlight"></slot>
</div>
2017-09-29 15:37:50 +08:00
<div
class="demo-block-control"
ref="control"
:class="{ 'is-fixed': fixedControl }"
@click="isExpanded = !isExpanded">
2016-09-18 22:01:26 +08:00
<transition name="arrow-slide">
<i :class="[iconClass, { 'hovering': hovering }]"></i>
</transition>
<transition name="text-slide">
<span v-show="hovering">{{ controlText }}</span>
</transition>
2017-09-29 15:37:50 +08:00
<el-tooltip effect="dark" :content="langConfig['tooltip-text']" placement="right">
<transition name="text-slide">
<el-button
v-show="hovering || isExpanded"
size="small"
type="text"
class="control-button"
@click.stop="goJsfiddle">
{{ langConfig['button-text'] }}
</el-button>
</transition>
</el-tooltip>
2016-08-23 16:57:58 +08:00
</div>
</div>
</template>
<style>
.demo-block {
2017-09-29 15:37:50 +08:00
border: solid 1px #ebebeb;
border-radius: 3px;
2016-08-23 16:57:58 +08:00
transition: .2s;
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
&.hover {
2016-09-13 20:02:33 +08:00
box-shadow: 0 0 8px 0 rgba(232, 237, 250, .6), 0 2px 4px 0 rgba(232, 237, 250, .5);
}
2016-11-02 18:47:19 +08:00
2016-09-13 20:02:33 +08:00
code {
font-family: Menlo, Monaco, Consolas, Courier, monospace;
2016-08-23 16:57:58 +08:00
}
2016-11-02 18:47:19 +08:00
.demo-button {
float: right;
}
2016-08-23 16:57:58 +08:00
.source {
padding: 24px;
}
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
.meta {
2017-09-29 15:37:50 +08:00
background-color: #fafafa;
2016-08-23 16:57:58 +08:00
border-top: solid 1px #eaeefb;
overflow: hidden;
height: 0;
transition: height .2s;
}
.description {
2017-09-29 15:37:50 +08:00
padding: 20px;
2016-08-23 16:57:58 +08:00
box-sizing: border-box;
2017-09-29 15:37:50 +08:00
border: solid 1px #ebebeb;
border-radius: 3px;
2016-08-23 16:57:58 +08:00
font-size: 14px;
2017-09-29 15:37:50 +08:00
line-height: 22px;
color: #666;
word-break: break-word;
2017-09-29 15:37:50 +08:00
margin: 10px;
background-color: #fff;
2016-08-23 16:57:58 +08:00
p {
2017-09-29 15:37:50 +08:00
margin: 0;
line-height: 26px;
2016-08-23 16:57:58 +08:00
}
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
code {
2016-09-13 20:02:33 +08:00
color: #5e6d82;
background-color: #e6effb;
2016-08-23 16:57:58 +08:00
margin: 0 4px;
2016-09-18 22:01:26 +08:00
display: inline-block;
padding: 1px 5px;
2016-08-23 16:57:58 +08:00
font-size: 12px;
2016-09-18 22:01:26 +08:00
border-radius: 3px;
2017-06-15 21:32:14 +08:00
height: 18px;
line-height: 18px;
2016-08-23 16:57:58 +08:00
}
}
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
.highlight {
pre {
margin: 0;
}
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
code.hljs {
margin: 0;
border: none;
max-height: none;
border-radius: 0;
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
&::before {
content: none;
}
}
}
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
.demo-block-control {
border-top: solid 1px #eaeefb;
2017-09-29 15:37:50 +08:00
height: 44px;
2016-08-23 16:57:58 +08:00
box-sizing: border-box;
background-color: #fff;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
text-align: center;
margin-top: -1px;
color: #d3dce6;
cursor: pointer;
2016-09-18 22:01:26 +08:00
position: relative;
2017-09-29 15:37:50 +08:00
&.is-fixed {
position: fixed;
bottom: 0;
width: 868px;
}
2016-08-23 16:57:58 +08:00
i {
2017-10-19 12:11:47 +08:00
font-size: 16px;
2017-09-29 15:37:50 +08:00
line-height: 44px;
2016-09-18 22:01:26 +08:00
transition: .3s;
&.hovering {
transform: translateX(-40px);
}
2016-08-23 16:57:58 +08:00
}
2017-09-29 15:37:50 +08:00
> span {
2016-09-18 22:01:26 +08:00
position: absolute;
transform: translateX(-30px);
2016-08-23 16:57:58 +08:00
font-size: 14px;
2017-09-29 15:37:50 +08:00
line-height: 44px;
2016-09-18 22:01:26 +08:00
transition: .3s;
display: inline-block;
2016-08-23 16:57:58 +08:00
}
2016-11-02 18:47:19 +08:00
2016-08-23 16:57:58 +08:00
&:hover {
color: #409EFF;
2016-09-13 20:02:33 +08:00
background-color: #f9fafc;
2016-08-23 16:57:58 +08:00
}
2016-11-02 18:47:19 +08:00
2016-09-18 22:01:26 +08:00
& .text-slide-enter,
& .text-slide-leave-active {
opacity: 0;
transform: translateX(10px);
}
2017-09-29 15:37:50 +08:00
.control-button {
line-height: 26px;
position: absolute;
top: 0;
right: 0;
2017-09-29 15:37:50 +08:00
font-size: 14px;
padding-left: 5px;
padding-right: 25px;
2017-09-29 15:37:50 +08:00
}
2016-08-23 16:57:58 +08:00
}
}
</style>
<script type="text/babel">
import compoLang from '../i18n/component.json';
2017-10-26 12:50:28 +08:00
import { version } from 'main/index.js';
2016-08-23 16:57:58 +08:00
export default {
data() {
return {
hovering: false,
2017-09-29 15:37:50 +08:00
isExpanded: false,
fixedControl: false,
scrollParent: null
2016-08-23 16:57:58 +08:00
};
},
2016-11-02 18:47:19 +08:00
props: {
jsfiddle: Object,
default() {
return {};
}
},
methods: {
goJsfiddle() {
const { script, html, style } = this.jsfiddle;
2016-11-02 18:47:19 +08:00
const resourcesTpl = '<scr' + 'ipt src="//unpkg.com/vue/dist/vue.js"></scr' + 'ipt>' +
2017-10-26 12:50:28 +08:00
'\n<scr' + `ipt src="//unpkg.com/element-ui@${ version }/lib/index.js"></scr` + 'ipt>';
2016-11-02 18:47:19 +08:00
let jsTpl = (script || '').replace(/export default/, 'var Main =').trim();
let htmlTpl = `${resourcesTpl}\n<div id="app">\n${html.trim()}\n</div>`;
2017-10-26 12:50:28 +08:00
let cssTpl = `@import url("//unpkg.com/element-ui@${ version }/lib/theme-chalk/index.css");\n${(style || '').trim()}\n`;
2016-11-02 18:47:19 +08:00
jsTpl = jsTpl
? jsTpl + '\nvar Ctor = Vue.extend(Main)\nnew Ctor().$mount(\'#app\')'
: 'new Vue().$mount(\'#app\')';
const data = {
js: jsTpl,
css: cssTpl,
html: htmlTpl,
panel_js: 3,
panel_css: 1
2016-11-02 18:47:19 +08:00
};
const form = document.getElementById('fiddle-form') || document.createElement('form');
form.innerHTML = '';
2016-11-02 18:47:19 +08:00
const node = document.createElement('textarea');
form.method = 'post';
form.action = 'https://jsfiddle.net/api/post/library/pure/';
2016-11-02 18:47:19 +08:00
form.target = '_blank';
for (let name in data) {
node.name = name;
node.value = data[name].toString();
form.appendChild(node.cloneNode());
}
form.setAttribute('id', 'fiddle-form');
form.style.display = 'none';
document.body.appendChild(form);
2016-11-02 18:47:19 +08:00
form.submit();
2017-09-29 15:37:50 +08:00
},
scrollHandler() {
const { top, bottom, left } = this.$refs.meta.getBoundingClientRect();
this.fixedControl = bottom > document.documentElement.clientHeight &&
top + 44 <= document.documentElement.clientHeight;
this.$refs.control.style.left = this.fixedControl ? `${ left }px` : '0';
},
removeScrollHandler() {
this.scrollParent && this.scrollParent.removeEventListener('scroll', this.scrollHandler);
2016-11-02 18:47:19 +08:00
}
},
2016-08-23 16:57:58 +08:00
computed: {
lang() {
return this.$route.path.split('/')[1];
},
langConfig() {
return compoLang.filter(config => config.lang === this.lang)[0]['demo-block'];
},
2016-08-23 16:57:58 +08:00
blockClass() {
2016-11-10 23:50:55 +08:00
return `demo-${ this.lang } demo-${ this.$router.currentRoute.path.split('/').pop() }`;
2016-08-23 16:57:58 +08:00
},
iconClass() {
return this.isExpanded ? 'el-icon-caret-top' : 'el-icon-caret-bottom';
},
controlText() {
return this.isExpanded ? this.langConfig['hide-text'] : this.langConfig['show-text'];
2016-08-23 16:57:58 +08:00
},
codeArea() {
return this.$el.getElementsByClassName('meta')[0];
},
codeAreaHeight() {
if (this.$el.getElementsByClassName('description').length > 0) {
2017-09-29 15:37:50 +08:00
return this.$el.getElementsByClassName('description')[0].clientHeight +
this.$el.getElementsByClassName('highlight')[0].clientHeight + 20;
2016-08-23 16:57:58 +08:00
}
return this.$el.getElementsByClassName('highlight')[0].clientHeight;
}
},
watch: {
isExpanded(val) {
this.codeArea.style.height = val ? `${ this.codeAreaHeight + 1 }px` : '0';
2017-09-29 15:37:50 +08:00
if (!val) {
this.fixedControl = false;
this.$refs.control.style.left = '0';
this.removeScrollHandler();
return;
}
setTimeout(() => {
this.scrollParent = document.querySelector('.page-component__scroll > .el-scrollbar__wrap');
this.scrollParent && this.scrollParent.addEventListener('scroll', this.scrollHandler);
2017-09-29 15:37:50 +08:00
this.scrollHandler();
}, 200);
2016-08-23 16:57:58 +08:00
}
},
mounted() {
this.$nextTick(() => {
let highlight = this.$el.getElementsByClassName('highlight')[0];
if (this.$el.getElementsByClassName('description').length === 0) {
highlight.style.width = '100%';
highlight.borderRight = 'none';
}
});
2017-09-29 15:37:50 +08:00
},
beforeDestroy() {
this.removeScrollHandler();
2016-08-23 16:57:58 +08:00
}
};
</script>