Commit cdc25db4 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Pass callback as function to eg-iframe.

Keeps the iframe code much simpler as it now only needs to call
the passed in callback method with true or false.  Previously it
needed to call a static method directly on InlinePreview class,
which was quite ugly.
parent 46dc39ff
Loading
Loading
Loading
Loading
+23 −42
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ Ext.define('Docs.view.examples.InlinePreview', {
         * @private
         * @static
         */
        iframeId: 0,
        iframeCounter: 0,

        /**
         * Returns the next available iframeId.
@@ -24,43 +24,8 @@ Ext.define('Docs.view.examples.InlinePreview', {
         * @static
         */
        getNextIframeId: function() {
            this.iframeId++;
            return this.iframeId.toString();
        },

        /**
         * Returns the preview component with the matching iframeId.
         *
         * @param {String} iframeId
         * @return {Ext.Component}
         * @private
         * @static
         */
        getPreviewByIframeId: function(iframeId) {
            return Ext.ComponentManager.get('inline-preview-' + iframeId.toString());
        },

        /**
         * Called when an preview has been successfully executed.
         *
         * @param {String} iframeId
         * @static
         */
        previewSuccess: function(iframeId) {
            var preview = this.getPreviewByIframeId(iframeId);
            preview.fireEvent('previewsuccess', preview);
        },

        /**
         * Called when an error occured during preview execution.
         *
         * @param {String} iframeId
         * @param {Error} e
         * @static
         */
        previewFailure: function(iframeId, e) {
            var preview = this.getPreviewByIframeId(iframeId);
            preview.fireEvent('previewfailure', preview, e);
            this.iframeCounter++;
            return this.iframeCounter.toString();
        }
    },

@@ -124,9 +89,9 @@ Ext.define('Docs.view.examples.InlinePreview', {
     * @param {String} code  The code to run inside iframe.
     */
    update: function(code) {
        var iframeId = this.iframeId,
            options = Ext.apply({iframeId: iframeId}, this.options),
            iframe = document.getElementById(iframeId);
        var options = this.options;
        var iframe = document.getElementById(this.iframeId);
        var callback = Ext.Function.bind(this.iframeCallback, this);

        if (iframe) {
            // Something is not quite ready when onload fires.
@@ -134,13 +99,29 @@ Ext.define('Docs.view.examples.InlinePreview', {
            // 1 ms works in Chrome, Firefox wants something bigger. Works in IE too.
            iframe.onload = function() {
                Ext.Function.defer(function() {
                    iframe.contentWindow.loadInlineExample(code, options);
                    iframe.contentWindow.loadInlineExample(code, options, callback);
                }, 100);
            };
            iframe.src = "eg-iframe.html";
        }
    },

    /**
     * Called from within iframe.
     *
     * @param {Boolean} success True when iframe code ran fine.
     * @param {Error} [e] The exception object in case of failure.
     * @private
     */
    iframeCallback: function(success, e) {
        if (success) {
            this.fireEvent("previewsuccess", this);
        }
        else {
            this.fireEvent("previewfailure", this, e);
        }
    },

    /**
     * Returns the current height of the preview.
     * @return {Number}
+3 −3
Original line number Diff line number Diff line
@@ -9,14 +9,14 @@
    <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css">

    <script type="text/javascript">
        function loadInlineExample(code, options) {
        function loadInlineExample(code, options, callback) {
            try {
                document.body.innerHTML = '';
                eval(code);
                window.top.Docs.view.examples.InlinePreview.previewSuccess(options.iframeId);
                callback && callback(true);
            } catch (e) {
                document.body.innerHTML = e;
                window.top.Docs.view.examples.InlinePreview.previewFailure(options.iframeId, e);
                callback && callback(false, e);
            }
        }
    </script>