<template>
    <view :class="'_root ' + (selectable ? '_select' : '')" :style="containerStyle">
        <slot v-if="!nodes[0]" />
        <node id="_root" :childs="nodes" :opts="[lazyLoad, loadingImg, errorImg, showImgMenu, selectable]" @add.stop.prevent="addFun($event, { tagId: '_root' })" />
    </view>
</template>

<script>
'use strict';
import node from './node/node';
function e(e, t, n) {
    if (t in e) {
        Object.defineProperty(e, t, {
            value: n,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        e[t] = n;
    }
    return e;
}
/*!
 * mp-html v2.4.1
 * https://github.com/jin-yufeng/mp-html
 *
 * Released under the MIT license
 * Author: Jin Yufeng
 */
var t = require('./parser');
var n = [];
export default {
    components: {
        node
    },
    data() {
        return {
            nodes: []
        };
    },
    props: {
        containerStyle: String,
        content: {
            type: String,
            default: ''
        },
        copyLink: {
            type: Boolean,
            default: true
        },
        domain: String,
        errorImg: String,
        lazyLoad: Boolean,
        loadingImg: String,
        pauseVideo: {
            type: Boolean,
            default: true
        },
        previewImg: {
            type: Boolean,
            default: true
        },
        scrollTable: Boolean,
        selectable: null,
        setTitle: {
            type: Boolean,
            default: true
        },
        showImgMenu: {
            type: Boolean,
            default: true
        },
        tagStyle: Object,
        useAnchor: null
    },
    created: function () {
        this.plugins = [];
        for (var e = n.length; e--; ) {
            this.plugins.push(new n[e](this));
        }
    },
    destroyed: function () {
        this.hookFun('onDetached');
    },
    methods: {
        inFun: function (e, t, n) {
            if (e && t && n) {
                this._in = {
                    page: e,
                    selector: t,
                    scrollTop: n
                };
            }
        },
        navigateTo: function (t, n) {
            var that = this;
            return new Promise(function (o, r) {
                if (!that.useAnchor) {
                    return void r(Error('Anchor is disabled'));
                }
                var a = uni
                    .createSelectorQuery()
                    .in(that._in ? that._in.page : that)
                    .select((that._in ? that._in.selector : '._root') + (t ? ''.concat('>>>', '#').concat(t) : ''))
                    .boundingClientRect();
                that._in ? a.select(that._in.selector).scrollOffset().select(that._in.selector).boundingClientRect() : a.selectViewport().scrollOffset();
                a.exec(function (t) {
                    if (!t[0]) {
                        return void r(Error('Label not found'));
                    }
                    var a = t[1].scrollTop + t[0].top - (t[2] ? t[2].top : 0) + (n || parseInt(that.useAnchor) || 0);
                    that._in
                        ? that._in.page.setData(e({}, that._in.scrollTop, a))
                        : uni.pageScrollTo({
                              scrollTop: a,
                              duration: 300
                          });
                    o();
                });
            });
        },
        getText: function (e) {
            var t = '';
            (function e(n) {
                for (var i = 0; i < n.length; i++) {
                    var o = n[i];
                    if ('text' === o.type) t += o.text.replace(/&amp;/g, '&');
                    else if ('br' === o.name) {
                        t += '\n';
                    } else {
                        var r = 'p' === o.name || 'div' === o.name || 'tr' === o.name || 'li' === o.name || ('h' === o.name[0] && o.name[1] > '0' && o.name[1] < '7');
                        if (r && t && '\n' !== t[t.length - 1]) {
                            t += '\n';
                        }
                        if (o.children) {
                            e(o.children);
                        }
                        r && '\n' !== t[t.length - 1] ? (t += '\n') : ('td' !== o.name && 'th' !== o.name) || (t += '\t');
                    }
                }
            })(e || this.nodes);
            return t;
        },
        getRect: function () {
            var that = this;
            return new Promise(function (t, n) {
                uni.createSelectorQuery()
                    .in(that)
                    .select('._root')
                    .boundingClientRect()
                    .exec(function (e) {
                        return e[0] ? t(e[0]) : n(Error('Root label not found'));
                    });
            });
        },
        pauseMedia: function () {
            for (var e = (this._videos || []).length; e--; ) {
                this._videos[e].pause();
            }
        },
        setPlaybackRate: function (e) {
            this.playbackRate = e;
            for (var t = (this._videos || []).length; t--; ) {
                this._videos[t].playbackRate(e);
            }
        },
        setContent: function (e, n) {
            var that = this;
            (this.imgList && n) || (this.imgList = []);
            this._videos = [];
            var o = {};
            var r = new t(this).parse(e);
            if (n) {
                for (a = this.nodes.length, s = r.length, void 0; s--; ) {
                    var a;
                    var s;
                    o['nodes['.concat(a + s, ']')] = r[s];
                }
            } else {
                o.nodes = r;
            }
            this.setData(o, function () {
                that.hookFun('onLoad');
                that.$emit('load');
            });
            if (this.lazyLoad || this.imgList._unloadimgs < this.imgList.length / 2) {
                var l = 0;
                var c = function e(t) {
                    (t && t.height) || (t = {});
                    t.height === l
                        ? that.$emit('ready', {
                              detail: t
                          })
                        : ((l = t.height),
                          setTimeout(function () {
                              that.getRect().then(e).catch(e);
                          }, 350));
                };
                this.getRect().then(c).catch(c);
            } else {
                this.imgList._unloadimgs ||
                    this.getRect()
                        .then(function (e) {
                            that.$emit('ready', {
                                detail: e
                            });
                        })
                        .catch(function () {
                            that.$emit('ready', {
                                detail: {}
                            });
                        });
            }
        },
        hookFun: function (e) {
            for (var t = n.length; t--; ) {
                if (this.plugins[t][e]) {
                    this.plugins[t][e]();
                }
            }
        },
        addFun: function (e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            e.detail.root = this;
        }
    },
    watch: {
        content: {
            handler: function (e) {
                this.setContent(e);
            },

            immediate: true
        }
    }
};
</script>
<style>
._root {
    padding: 1px 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}
._select {
    -webkit-user-select: text;
    user-select: text;
}
</style>
