<template>
  <div class="panel panel-flat">
    <div class="panel-heading">
      <h6 class="panel-title">
        <slot name="source"></slot>
      </h6>
    </div>
    <div class="panel-body">
      <div style="" class="code-content">
        <div class="left"><pre class="language-javascript"><code><slot name="highlight"></slot></code></pre></div>
        <!-- <div class="right">
          <slot></slot>
        </div> -->
      </div>
    </div>
    <div class="panel-footer" @click="showCode">
      {{text}}
    </div>
  </div>
</template>
<style>
.panel {
  background-color: #fff;
  border: 1px solid transparent;
  border-radius: 3px;
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
  border-color: #ddd;
  color: #333333;
  max-width: 1000px;
}

.panel-flat > .panel-heading {
  padding-top: 20px;
  padding-bottom: 20px;
  background-color: #fff;
}

.panel-heading {
  position: relative;
}

.panel-heading {
  padding: 15px 20px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  /*-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);*/
  border-top-right-radius: 2px;
  border-top-left-radius: 2px;
}

h6.panel-title,
.h6.panel-title {
  font-size: 15px;
}

.panel-title {
  position: relative;
  font-size: 13px;
}

.panel-title {
  margin-top: 0;
  margin-bottom: 0;
  font-size: 15px;
  color: inherit;
}

.panel-footer {
  text-align: center;
  padding: 10px 0px;
  cursor: pointer;
}

.heading-elements {
  background-color: inherit;
  position: absolute;
  top: 50%;
  right: 20px;
  height: 36px;
  margin-top: -18px;
}

.heading-elements .icons-list {
  float: left;
  margin-left: 20px;
  margin-top: 9px;
}

.icons-list {
  margin: 0;
  padding: 0;
  list-style: none;
  line-height: 1;
  font-size: 0;
}

.panel-body {
  position: relative;
  padding: 0px 20px;
  overflow: hidden;
}

.code-content {
  display: table;
  width: 100%;
}

.code-content > .left {
  display: table-cell;
  width: 100%;
  padding-right: 10px;
}

.code-content > .rigth {
  display: table-cell;
  width: 40%;
  padding-left: 10px;
}

pre {
  padding: 20px;
  margin: 0;
  -moz-tab-size: 4;
  -o-tab-size: 4;
  tab-size: 4;
  -webkit-hyphens: none;
  -moz-hyphens: none;
  -ms-hyphens: none;
  hyphens: none;
}

pre {
  display: block;
  padding: 9.5px;
  margin: 0 0 10px;
  font-size: 12px;
  line-height: 1.5384616;
  word-break: break-all;
  word-wrap: break-word;
  color: #333333;
  background-color: #fcfcfc;
  border: 1px solid #ddd;
  border-radius: 3px;
}

pre code {
  padding: 0;
  font-size: inherit;
  color: inherit;
  white-space: pre-wrap;
  background-color: transparent;
  border-radius: 0;
}

.pre-scrollable {
  max-height: 340px;
  overflow-y: scroll;
}


/* ------------------------------------------------------------------------------
 *
 *  # Prism
 *
 *  Lightweight, extensible syntax highlighter
 *
 *  Version: 1.0
 *  Latest update: May 10, 2015
 *
 * ---------------------------------------------------------------------------- */

code[class*="language-"],
pre[class*="language-"] {
  text-shadow: 0 1px #fff;
  word-break: normal;
  line-height: 1.5;
  direction: ltr;
}

@media print {
  code[class*="language-"],
  pre[class*="language-"] {
    text-shadow: none;
  }
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata,
.token.punctuation {
  color: #999;
}

.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol {
  color: #905;
}

.token.selector,
.token.attr-name,
.token.string,
.token.builtin {
  color: #690;
}

.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string,
.token.variable {
  color: #a67f59;
  background: rgba(255, 255, 255, 0.5);
}

.token.atrule,
.token.attr-value,
.token.keyword {
  color: #07a;
}

.token.function {
  color: #DD4A68;
}

.token.regex,
.token.important {
  color: #e90;
}

.token.important {
  font-weight: bold;
}

.token.entity {
  cursor: help;
}

.namespace {
  opacity: 0.7;
  filter: alpha(opacity=70);
}

.line-highlight {
  position: absolute;
  left: 0;
  right: 0;
  margin-top: 20px;
  background: rgba(133, 238, 149, 0.12);
  pointer-events: none;
  white-space: pre;
  line-height: inherit;
}

.line-highlight:before,
.line-highlight:after {
  content: attr(data-start);
  position: absolute;
  top: 0px;
  left: 10px;
  padding: 2px;
  text-align: center;
  font-size: 10px;
  color: #999999;
}

.line-highlight[data-end]:after {
  content: attr(data-end);
  top: auto;
  bottom: 0;
}

pre.line-numbers {
  position: relative;
  padding-left: 50px;
  counter-reset: linenumber;
}

pre.line-numbers > code {
  position: relative;
}

.line-numbers .line-numbers-rows {
  position: absolute;
  pointer-events: none;
  top: 0;
  font-size: 100%;
  left: -50px;
  width: 36px;
  letter-spacing: -1px;
  border-right: 1px solid #999;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.line-numbers .line-numbers-rows > span {
  pointer-events: none;
  display: block;
  counter-increment: linenumber;
}

.line-numbers .line-numbers-rows > span:before {
  content: counter(linenumber);
  color: #999999;
  display: block;
  padding-right: 10px;
  text-align: right;
}

pre[class*="language-"] {
  overflow: auto;
  position: relative;
  border-left-width: 3px;
  border-left-color: #2196F3;
  background-color: #fdfdfd;
  background-image: -webkit-linear-gradient(transparent 50%, rgba(25, 118, 210, 0.025) 50%);
  background-image: -moz-linear-gradient(transparent 50%, rgba(25, 118, 210, 0.025) 50%);
  background-image: -ms-linear-gradient(transparent 50%, rgba(25, 118, 210, 0.025) 50%);
  background-image: -o-linear-gradient(transparent 50%, rgba(25, 118, 210, 0.025) 50%);
  background-image: linear-gradient(transparent 50%, rgba(25, 118, 210, 0.025) 50%);
  background-size: 3em 3em;
  background-origin: content-box;
  border-bottom-left-radius: 0;
  border-top-left-radius: 0;
}

pre[class*="language-"] > code[data-language] {
  display: block;
}

pre[class*="language-"] > code[data-language]:before {
  /*content: attr(data-language);*/
  content: 'Code';
  background-color: #f8f8f8 !important;
  display: inline-block;
  position: absolute;
  top: 0px;
  right: 0px;
  font-size: 11px;
  border-radius: 0 0 0 2px;
  padding: 4px 8px;
  text-shadow: none;
  color: #333333;
  border-left: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
}

pre[class*="language-"].line-numbers > code[data-language]:before {
  top: -15px;
  right: -15px;
}

pre[data-line] {
  position: relative;
  padding-left: 36px;
}

code {
  color: #5e6d82;
  background-color: #e6effb;
  margin: 0 4px;
  -ms-transform: translateY(-2px);
  transform: translateY(-2px);
  display: inline-block;
  padding: 1px 5px;
  font-size: 12px;
  border-radius: 3px;
}
</style>
<script>
import {
  version
} from 'main/index.js';

import '../plugins/prism.min.js'

export default {
  data() {
      return {
        isCollapsed: true,
        panelBodyHeight: 0,
        text: '显示代码'
      };
    },
    props: {
      jsfiddle: Object
    },
    mounted() {
      Prism.highlightAll();
      const panelBody = this.$el.getElementsByClassName('panel-body')[0];

      panelBody.setAttribute('data-transition-height', panelBody.offsetHeight);

      panelBody.style = 'height: 0px;transition: height .3s;'
    },
    methods: {
      showCode(event) {
        this.isCollapsed = !this.isCollapsed

        let currentPanelBody = event.currentTarget.parentElement.getElementsByClassName('panel-body')[0]
        
        if (this.isCollapsed) {
          currentPanelBody.style = 'height: 0px;transition: height .3s;'
          this.text = '显示代码'
        } else {
          currentPanelBody.style = 'height: ' + currentPanelBody.getAttribute('data-transition-height') + 'px;transition: height .3s;'

          this.text = '隐藏代码'
        }

      }
    }
};
</script>
