/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */

/*************************************************************
 *
 *  MathJax/extensions/CHTML-preview.js
 *  
 *  Implements a fast preview using the Common-HTML output jax
 *  and then a slower update to the more accurate HTML-CSS output
 *  (or whatever the user has selected).
 *  
 *  ---------------------------------------------------------------------
 *  
 *  Copyright (c) 2014 The MathJax Consortium
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

(function (HUB,HTML) {
  
  var SETTINGS = HUB.config.menuSettings;

  var CHTMLpreview = MathJax.Extension["CHTML-preview"] = {
    version: "1.0",

    //
    //  Configuration for the chunking of the main output
    //  after the previews have been created, and other configuration.
    //
    config: HUB.CombineConfig("CHTML-preview",{
      Chunks: {EqnChunk: 10000, EqnChunkFactor: 1, EqnChunkDelay: 0},
      color: "inherit!important",
      updateTime: 10, updateDelay: 10,
      messageStyle: "none",
      disabled: false
    }),

    //
    //  Ajust the chunking of the output jax
    //
    Config: function () {
      HUB.Config({
        "HTML-CSS": this.config.Chunks,
        SVG: this.config.Chunks,
      });
      MathJax.Ajax.Styles({".MathJax_Preview":{color:this.config.color}});
      var update, delay, style, done, saved;
      var config = this.config;

      HUB.Register.MessageHook("Begin Math Output",function () {
        if (!done && !config.disabled && SETTINGS.CHTMLpreview &&
            SETTINGS.renderer !== "CommonHTML") {
          update = HUB.processUpdateTime; delay = HUB.processUpdateDelay;
          style = HUB.config.messageStyle;
          HUB.processUpdateTime = config.updateTime;
          HUB.processUpdateDelay = config.updateDelay;
          HUB.Config({messageStyle: config.messageStyle});
          MathJax.Message.Clear(0,0);
          saved = true;
        }
      });
      HUB.Register.MessageHook("End Math Output",function () {
        if (!done && saved) {
          HUB.processUpdateTime = update;
          HUB.processUpdateDelay = delay;
          HUB.Config({messageStyle: style});
          done = true;
        }
      });
    },

    //
    //  Insert a preview span, if there isn't one already,
    //  and call the CommonHTML output jax to create the preview
    //
    Preview: function (data) {
      if (this.config.disabled || !SETTINGS.CHTMLpreview ||
          SETTINGS.renderer === "CommonHTML") return;
      var preview = data.script.MathJax.preview || data.script.previousSibling;
      if (!preview || preview.className !== MathJax.Hub.config.preRemoveClass) {
        preview = HTML.Element("span",{className:MathJax.Hub.config.preRemoveClass});
        data.script.parentNode.insertBefore(preview,data.script);
        data.script.MathJax.preview = preview;
      }
      preview.innerHTML = "";
      return this.postFilter(preview,data);
    },
    postFilter: function (preview,data) {
      try {
        data.math.root.toCommonHTML(preview);
      } catch (err) {
        //
        //  Load the CommonHTML jax if it is not already loaded
        //
        if (!data.math.root.toCommonHTML) {
          var queue = MathJax.Callback.Queue();
          HUB.RestartAfter(queue.Push(
            ["Require",MathJax.Ajax,"[MathJax]/jax/output/CommonHTML/config.js"],
            ["Require",MathJax.Ajax,"[MathJax]/jax/output/CommonHTML/jax.js"]
          ));
        }
        if (!err.restart) {throw err} // an actual error
        return MathJax.Callback.After(["postFilter",this,preview,data],err.restart);
      }
    },

    //
    //  Hook into the input jax postFilter to create the previews as
    //  the input jax are processed.
    //
    Register: function (name) {
      HUB.Register.StartupHook(name+" Jax Require",function () {
        var jax = MathJax.InputJax[name];
        jax.postfilterHooks.Add(["Preview",MathJax.Extension["CHTML-preview"]],50);
      });
    }
  }

  //
  //  Hook into each input jax
  //
  CHTMLpreview.Register("TeX");
  CHTMLpreview.Register("MathML");
  CHTMLpreview.Register("AsciiMath");
  
  HUB.Register.StartupHook("End Config",["Config",CHTMLpreview]);

})(MathJax.Hub,MathJax.HTML);

MathJax.Ajax.loadComplete("[MathJax]/extensions/CHTML-preview.js");


@@ -1862,16 +1862,27 @@
 _Preview
+ .MJXc-math
 %22:%7Bcolor
@@ -1982,16 +1982,133 @@
 onfig;%0A%0A
+      if (!config.disabled && SETTINGS.CHTMLpreview == null)%0A        HUB.Config(%7BmenuSettings:%7BCHTMLpreview:true%7D%7D);%0A
       HU
@@ -2188,28 +2188,8 @@
 e &&
- !config.disabled &&
  SET
@@ -2209,28 +2209,16 @@
 eview &&
-%0A           
  SETTING
@@ -3070,32 +3070,8 @@
 if (
-this.config.disabled %7C%7C 
 !SET
@@ -3091,26 +3091,16 @@
 eview %7C%7C
-%0A         
  SETTING
@@ -3533,16 +3533,49 @@
 ML = %22%22;
+ preview.style.color = %22inherit%22;
 %0A      r
@@ -3932,25 +3932,8 @@
     
-HUB.RestartAfter(
 queu
@@ -4110,16 +4110,59 @@
         
+);%0A          HUB.RestartAfter(queue.Push(%7B%7D
 ));%0A    

Allow configuration to disable CHTML-preivew, but still allow menu to override. Make loading of this page enable preview by default. Fix handling of preview color. Fix restart when CommonHTML config is loaded but jax.js isn't.