import React from 'react/addons';
import Reflux from 'reflux';

import wikiHomeStore from './wiki-home-store.jsx';
import util from '../utils/util.jsx';

import $ from 'jquery';
import _ from 'lodash';

export default React.createClass({
    mixins: [Reflux.connect(wikiHomeStore, 'store')],

    _lastUrl: null,
    /**
     * 为什么我们需要 _.debounce 呢呢呢呢?!!! @[  #wiki渲染器 todo  ]{ihk14kbo_pi5qjshq_h9ewg}@
     *
     * 为什么.. iframe的location在快速replace的情况下不管用呢?
     */
    componentDidUpdate: _.debounce(function() {
        var store = this.state.store;

        var targetUrl = util.getWikiPageUrl(store.currentContentUrl);
        if (targetUrl == this._lastUrl) {
            return;
        }

        this._lastUrl = targetUrl;

        var iframe = this.refs.iframe.getDOMNode().contentDocument;
        // 为iframe replace
        // http://stackoverflow.com/questions/15198972/javascript-location-replace-and-iframe
        iframe.location.replace(targetUrl);
    }, 80),

    lastIframe: null,

    render() {
        var store = this.state.store;

        return <div className="content">
            <iframe ref='iframe' type="text/html"></iframe>
        </div>
    }
});