/**
 * ueditor plus 完整配置项
 * 可以在这里配置整个编辑器的特性
 */
/**************************提示********************************
 * 所有被注释的配置项均为UEditor默认值。
 * 修改默认配置请首先确保已经完全明确该参数的真实用途。
 * 主要有两种修改方案,一种是取消此处注释,然后修改成对应参数;另一种是在实例化编辑器时传入对应参数。
 * 当升级编辑器时,可直接使用旧版配置文件替换新版配置文件,不用担心旧版配置文件中因缺少新功能所需的参数而导致脚本报错。
 **************************提示********************************/

(function () {
  /**
   * 编辑器资源文件根路径。它所表示的含义是:以编辑器实例化页面为当前路径,指向编辑器资源文件(即dialog等文件夹)的路径。
   * 鉴于很多同学在使用编辑器的时候出现的种种路径问题,此处强烈建议大家使用"相对于网站根目录的相对路径"进行配置。
   * "相对于网站根目录的相对路径"也就是以斜杠开头的形如"/myProject/ueditor/"这样的路径。
   * 如果站点中有多个不在同一层级的页面需要实例化编辑器,且引用了同一UEditor的时候,此处的URL可能不适用于每个页面的编辑器。
   * 因此,UEditor提供了针对不同页面的编辑器可单独配置的根路径,具体来说,在需要实例化编辑器的页面最顶部写上如下代码即可。当然,需要令此处的URL等于对应的配置。
   * window.UEDITOR_HOME_URL = "/xxxx/xxxx/";
   */
  var URL, CORS_URL;
  if (window.UEDITOR_HOME_URL) {
    URL = window.UEDITOR_HOME_URL;
  } else if (window.__msCDN) {
    URL = window.__msCDN + 'asset/vendor/ueditor/';
  } else if (window.__msRoot) {
    URL = window.__msRoot + 'asset/vendor/ueditor/';
  } else {
    URL = getUEBasePath();
  }
  if (window.UEDITOR_CORS_URL) {
    CORS_URL = window.UEDITOR_CORS_URL;
  } else if (window.__msRoot) {
    CORS_URL = window.__msRoot + 'asset/vendor/ueditor/';
  } else if (window.UEDITOR_HOME_URL) {
    CORS_URL = window.UEDITOR_HOME_URL;
  } else {
    CORS_URL = getUEBasePath();
  }

  /**
   * 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
   */
  window.UEDITOR_CONFIG = {

    // 为编辑器实例添加一个路径,这个不能被注释
    UEDITOR_HOME_URL: URL,
    // 需要能跨域的静态资源请求,主要用户弹窗页面等静态资源
    UEDITOR_CORS_URL: CORS_URL,

    loadConfigFromServer: false,
    // 服务器统一请求接口路径
    serverUrl: "http://192.168.31.51:9000/exam/exam/upload/configAndUpload",
    imageActionName: "imgUpload",
    imageAllowFiles: [
      ".png",
      ".jpg",
      ".jpeg",
      ".gif",
      ".bmp"
    ],

    //工具栏上的所有的功能按钮和下拉框,可以在new编辑器的实例时选择自己需要的重新定义
    toolbars: [
      ["fullscreen", "source", "|", "bold", "italic", "underline", "forecolor", "fontfamily", "fontsize", "|", "justifyleft", "justifycenter", "justifyright", "justifyjustify", "|", "insertimage", "inserttable", 'formula', 'combox']
    ]
    // 公式配置
    , formulaConfig: {
      // 公式渲染链接模板
      imageUrlTemplate: 'https://r.latexeasy.com/image.svg?{}',
      // 编辑器模式 plain live
      editorMode: 'live',
      // 编辑器地址
      editorLiveServer: 'https://latexeasy.com',
    }
    // serverResponsePrepare: function (res) {
    //   return res
    // }

    // 自定义工具栏按钮点击,返回 true 表示已经处理点击,会阻止默认事件
    , toolbarCallback: function (cmd, editor) {
      // console.log('toolbarCallback',cmd, editor);
      // switch(cmd){
      //   case 'insertimage':
      //     editor.execCommand('insertHtml', '<p><img src="xxxxx" /></p>');
      //     console.log('toolbarCallback',cmd, editor)
      //     return true;
      //   case 'insertvideo':
      //     editor.execCommand('insertHtml', '<p><iframe src="xxxxx" /></p>');
      //     console.log('toolbarCallback',cmd, editor)
      //     return true;
      //   case 'attachment':
      //     console.log('toolbarCallback',cmd, editor)
      //     editor.execCommand('insertHtml', '<p><a href="xxx.zip">下载文件</a></p>');
      //     return true;
      // }
    }
    // 插入图片自定义配置
    , imageConfig: {
      // 禁止本地上传
      disableUpload: false,
    }
    , zIndex: 2000
    , fullscreen: false

    , rgb2Hex: true,

    tipError: function (msg, param) {
      if (window && window.MS && window.MS.dialog) {
        window.MS.dialog.tipError(msg);
      } else {
        alert(msg);
      }
    }
  };

  function getUEBasePath (docUrl, confUrl) {
    return getBasePath(
      docUrl || self.document.URL || self.location.href,
      confUrl || getConfigFilePath()
    );
  }

  function getConfigFilePath () {
    var configPath = document.getElementsByTagName("script");

    return configPath[configPath.length - 1].src;
  }

  function getBasePath (docUrl, confUrl) {
    var basePath = confUrl;

    if (/^(\/|\\\\)/.test(confUrl)) {
      basePath =
        /^.+?\w(\/|\\\\)/.exec(docUrl)[0] + confUrl.replace(/^(\/|\\\\)/, "");
    } else if (!/^[a-z]+:/i.test(confUrl)) {
      docUrl = docUrl.split("#")[0].split("?")[0].replace(/[^\\\/]+$/, "");

      basePath = docUrl + "" + confUrl;
    }

    return optimizationPath(basePath);
  }

  function optimizationPath (path) {
    var protocol = /^[a-z]+:\/\//.exec(path)[0],
      tmp = null,
      res = [];

    path = path.replace(protocol, "").split("?")[0].split("#")[0];

    path = path.replace(/\\/g, "/").split(/\//);

    path[path.length - 1] = "";

    while (path.length) {
      if ((tmp = path.shift()) === "..") {
        res.pop();
      } else if (tmp !== ".") {
        res.push(tmp);
      }
    }

    return protocol + res.join("/");
  }

  window.UE = {
    getUEBasePath: getUEBasePath
  };
})();