`
bellstar
  • 浏览: 148487 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

qooxdoo--Variant类(配置用)

阅读更多
概述:qx.core.Variant类为qooxdoo框架提供全局的配置,qooxdoo的已有的配置项有client:设置代码运行浏览器,debug:设置代码的运行模式,aspects:设置是否开启方面,dynlocale:动态区域? 。Variant为qooxdoo通过工具根据具体环境设置从一份代码中提取出不同的分支代码提供了基础。所谓具体环境设置就是Variant中的设置值。

Variant
--------------
define(key, allowedValues, defaultValue) 定义一个设置
get(key)
isSet(key, variants)
select(key, variantFunctionMap)
     第二个参数是一个形为{值A:对象B}的Map,如果值A与Variant中key(参数1)的值一致,返回对象B,否则返回variantFunctionMap[default];

/**
 * Manage variants of source code. May it be for different debug options,
 * browsers or other environment flags.
 * [源代码配置]管理。用于设定不同的调试选项、浏览器和其他环境标记。
 * 
 *
 * Variants enable the selection and removal of code from the build version.
 * A variant consists of a collection of states from which exactly one is active
 * at load time of the framework. The global map <code>qxvariants</code> can be
 * used to select a variant before the Framework is loades.
 * [源代码配置]使得从构建版本选定或者移除代码成为可能
 * 一个[源代码配置]由“确切对象:在框架载入时激活”的状态集合组成
 * 全局的map qxvariants用于在框架载入之前选择一个[源代码配置]
 *
 *
 * Depending on the selected variant a specific code
 * path can be choosen using the <code>select</code> method. The generator is
 * able to set a variant and remove all code paths which are
 * not selected by the variant.
 * 一份具体的代码依赖于选择的[源代码配置]
 * 路径可以用select方法选择,generator可以设置一个[源代码配置]和移除所有没有被[源代码配置]选择的代码
 *
 * Variants are used to implement browser optimized builds and to remove
 * debugging code from the build version. It is very similar to conditional
 * compilation in C/C++.
 * [源代码配置]用于实现浏览器最小化构建和从构建版本移除调试代码。它非常像C/C++的条件编绎。
 *
 * Here is a list of pre-defined variant names, the possible values they take,
 * and their system default:
 * 这里是一个清单,包含预定义的[源代码配置]名称、可供设置的值和系统默认值
 * <table>
 *  <tr>
 *  <th>Variant name</th><th>Possible values</th><th>System default</th>
 *  </tr><tr>
 *  <td>qx.client          <td>[ "gecko", "mshtml", "opera", "webkit" ]   <td>&lt;auto-detected&gt;
 *  </tr><tr>
 *  <td>qx.debug                   <td>[ "on", "off" ]                    <td>"on"
 *  </tr><tr>
 *  <td>qx.aspects                 <td>[ "on", "off" ]                    <td>"off"
 *  </tr><tr>
 *  <td>qx.dynlocale     <td>[ "on", "off" ]                    <td>"on"
 *  </tr>
 * </table>
 */
qx.Bootstrap.define("qx.core.Variant",
{
  statics :
  {
    /** {Map} stored variants */
    __variants : {},


    /** {Map} cached results */
    __cache : {},


    /**
     * Pseudo function as replacement for isSet() which will only be handled by the optimizer
     * 替代isSet的假冒方法,仅用于优化程序
     *
     * @return {Boolean}
     */
    compilerIsSet : function() {
      return true;
    },


    /**
     * Define a variant 定义一个[源代码配置]
     *
     * @param key {String} An Unique key for the variant. The key must be prefixed with a
     *   namespace identifier (e.g. <code>"qx.debug"</code>)
     * @param allowedValues {String[]} An array of all allowed values for this variant.
     * @param defaultValue {String} Default value for the variant. Must be one of the values
     *   defined in <code>defaultValues</code>.
     */
    define : function(key, allowedValues, defaultValue)
    {
      if (qx.core.Variant.compilerIsSet("qx.debug", "on"))
      {
        if (!this.__isValidArray(allowedValues)) {
          throw new Error('Allowed values of variant "' + key + '" must be defined!');
        }

        if (defaultValue === undefined) {
          throw new Error('Default value of variant "' + key + '" must be defined!');
        }
      }

      if (!this.__variants[key])
      {
        this.__variants[key] = {};
      }
      else if (qx.core.Variant.compilerIsSet("qx.debug", "on"))
      {
        if (this.__variants[key].defaultValue !== undefined) {
          throw new Error('Variant "' + key + '" is already defined!');
        }
      }

      this.__variants[key].allowedValues = allowedValues;
      this.__variants[key].defaultValue = defaultValue;
    },


    /**
     * Get the current value of a variant.
     * 获取[源代码配置]的当前值
     *
     * @param key {String} name of the variant
     * @return {String} current value of the variant

     */
    get : function(key)
    {
      var data = this.__variants[key];

      if (qx.core.Variant.compilerIsSet("qx.debug", "on"))
      {
        if (data === undefined) {
          throw new Error('Variant "' + key + '" is not defined.');
        }
      }

      if (data.value !== undefined) {
        return data.value;
      }

      return data.defaultValue;
    },


    /**
     * Import settings from global qxvariants into current environment
     * 从全局的qxvariants导入设置到当前环境
     *
     * @return {void}
     */
    __init : function()
    {
      if (window.qxvariants)
      {
        for (var key in qxvariants)
        {
          if (qx.core.Variant.compilerIsSet("qx.debug", "on"))
          {
            if ((key.split(".")).length < 2) {
              throw new Error('Malformed settings key "' + key + '". Must be following the schema "namespace.key".');
            }
          }

          if (!this.__variants[key]) {
            this.__variants[key] = {};
          }

          this.__variants[key].value = qxvariants[key];
        }

        window.qxvariants = undefined;

        try {
          delete window.qxvariants;
        } catch(ex) {};

        this.__loadUrlVariants(this.__variants);
      }
    },


    /**
     * Load variants from URL parameters if the setting <code>"qx.allowUrlSettings"</code>
     * is set to true.
     * 如果qx.allowUrlSettings设定为true,从URL参数载入[源代码配置]
     *
     *
     * The url scheme for variants is: <code>qxvariant:VARIANT_NAME:VARIANT_VALUE</code>.
     */
    __loadUrlVariants : function()
    {
      if (qx.core.Setting.get("qx.allowUrlVariants") != true) {
        return;
      }

      var urlVariants = document.location.search.slice(1).split("&");

      for (var i=0; i<urlVariants.length; i++)
      {
        var variant = urlVariants[i].split(":");
        if (variant.length != 3 || variant[0] != "qxvariant") {
          continue;
        }

        var key = variant[1];
        if (!this.__variants[key]) {
          this.__variants[key] = {};
        }

        this.__variants[key].value = decodeURIComponent(variant[2]);
      }
    },


    /**
     * Select a function depending on the value of the variant.
     * 依据[源代码设置]的值获取一个function
     *
     * Example:
     * 示例
     *
     * <pre class='javascript'>
     * var f = qx.Variant.select("qx.client", {
     *   "gecko": fucntion() { ... },
     *   "mshtml|opera": function() { ... },
     *   "default": function() { ... }
     * });
     * </pre>
     *
     * Depending on the value of the <code>"qx.client"</code> variant whit will select the
     * corresponding function. The first case is selected if the variant is "gecko", the second
     * is selected if the variant is "mshtml" or "opera" and the third function is selected if
     * none of the other keys match the variant. "default" is the default case.
     *
     * @param key {String} name of the variant. To enable the generator to optimize
     *   this selection, the key must be a string literal.
     * @param variantFunctionMap {Map} map with variant names as keys and functions as values.
     * @return {Function} The selected function from the map.
     */
    select : function(key, variantFunctionMap)
    {
      if (qx.core.Variant.compilerIsSet("qx.debug", "on"))
      {
        // WARINING: all changes to this function must be duplicated in the generator!!
        // modules/variantoptimizer.py (processVariantSelect)
        if (!this.__isValidObject(this.__variants[key])) {
          throw new Error("Variant \"" + key + "\" is not defined");
        }

        if (!this.__isValidObject(variantFunctionMap)) {
          throw new Error("the second parameter must be a map!");
        }
      }

      for (var variant in variantFunctionMap)
      {
        if (this.isSet(key, variant)) {
          return variantFunctionMap[variant];
        }
      }

      if (variantFunctionMap["default"] !== undefined) {
        return variantFunctionMap["default"];
      }

      if (qx.core.Variant.compilerIsSet("qx.debug", "on"))
      {
        throw new Error('No match for variant "' + key +
          '" in variants [' + qx.lang.Object.getKeysAsString(variantFunctionMap) +
          '] found, and no default ("default") given');
      }
    },


    /**
     * Check whether a variant is set to a given value. To enable the generator to optimize
     * this selection, both parameters must be string literals.
     * 检查是否[源代码配置]设定为给定的值,使得generator可以优化这个选择,两个参数都必须为字面量
     *
     * This method is meant to be used in if statements to select code paths. If the condition of
     * an if statement is only this method, the generator is able to optimize the if
     * statement.
     * 此方法表达了它被用于if语句选择一个代码路径,如果if语句的条件仅仅是此方法,generator就可优化这个if语句
     *
     * Example:
     *
     * <pre class='javascript'>
     * if (qx.core.Variant.isSet("qx.client", "mshtml")) {
     *   // some Internet Explorer specific code
     * } else if(qx.core.Variant.isSet("qx.client", "opera")){
     *   // Opera specific code
     * } else {
     *   // common code for all other browsers
     * }
     * </pre>
     *
     * @param key {String} name of the variant
     * @param variants {String} value to check for. Several values can be "or"-combined by separating
     *   them with a "|" character. A value of "mshtml|opera" would for example check if the variant is
     *   set to "mshtml" or "opera"
     * @return {Boolean} whether the variant is set to the given value
     */
    isSet : function(key, variants)
    {
      var access = key + "$" + variants;
      if (this.__cache[access] !== undefined) {
        return this.__cache[access];
      }

      var retval = false;

      // fast path
      if (variants.indexOf("|") < 0)
      {
        retval = this.get(key) === variants;
      }
      else
      {
        var keyParts = variants.split("|");

        for (var i=0, l=keyParts.length; i<l; i++)
        {
          if (this.get(key) === keyParts[i])
          {
            retval = true;
            break;
          }
        }
      }

      this.__cache[access] = retval;
      return retval;
    },


    /**
     * Whether a value is a valid array. Valid arrays are:
     *
     * * type is object
     * * instance is Array
     *
     * @name __isValidArray
     * @param v {var} the value to validate.
     * @return {Boolean} whether the variable is valid
     */
    __isValidArray : function(v) {
      return typeof v === "object" && v !== null && v instanceof Array;
    },


    /**
     * Whether a value is a valid object. Valid object are:
     *
     * * type is object
     * * instance != Array
     *
     * @name __isValidObject
     * @param v {var} the value to validate.
     * @return {Boolean} whether the variable is valid
     */
    __isValidObject : function(v) {
      return typeof v === "object" && v !== null && !(v instanceof Array);
    },


    /**
     * Whether the array contains the given element
     *
     * @name __arrayContains
     * @param arr {Array} the array
     * @param obj {var} object to look for
     * @return {Boolean} whether the array contains the element
     */
    __arrayContains : function(arr, obj)
    {
      for (var i=0, l=arr.length; i<l; i++)
      {
        if (arr[i] == obj) {
          return true;
        }
      }

      return false;
    }
  },




  /*
  *****************************************************************************
     DEFER
  *****************************************************************************
  */

  defer : function(statics)
  {
    statics.define("qx.client", [ "gecko", "mshtml", "opera", "webkit" ], qx.bom.client.Engine.NAME);
    statics.define("qx.debug", [ "on", "off" ], "on");
    statics.define("qx.aspects", [ "on", "off" ], "off");
    statics.define("qx.dynlocale", [ "on", "off" ], "on");

    statics.__init();
  }
});
分享到:
评论

相关推荐

    前端开源库-qooxdoo-sdk

    前端开源库-qooxdoo-sdkQooxdoo SDK,面向编码人员的JS框架

    qooxdoo-qooxdoo-release_2_0_1

    qooxdoo 2.0.1 开发包,就算不会HTML也可以轻松开发完美流畅的动态页面应用,就如同开发PC软件,完全面向对象,会JavaScript 就OK

    qooxdoo-cli:(已弃用,移至qooxdoo-compiler中)qx命令行

    将不建议使用npm模块qx-cli ,从现在开始,您要做的所有事情是: $ npm install -g qooxdoo-compiler$ qx create myapp$ cd myapp$ qx compile不要忘记,如果您以前安装过qx-cli ,则必须发出以下命令: $ npm ...

    qooxdoo-compiler:Qooxdoo的编译器,100%javascript

    Qooxdoo-Compiler是Qooxdoo( )应用程序的新编译器和命令行界面,使用100%Node.JS Javascript编写,它在标准python生成器上进行了以下关键改进: 包括Babel,用于将ES6添加到所有Qooxdoo应用程序中 快速(最快24...

    qooxdoo-contrib-开源

    qooxdoo-contrib是qooxdoo项目(http://qooxdoo.org)的组成部分。 用户可以在简洁的结构中灵活地开发,维护和促进贡献。 贡献可以轻松地集成到自定义qooxdoo应用程序中。 注意:SourceForge上的此存储库是旧版! 它...

    qooxdoo:qooxdoo-通用JavaScript框架

    Qooxdoo JavaScript框架qooxdoo是一个通用JavaScript框架,使您可以为各种平台创建应用程序。 通过其面向对象的编程模型,您可以构建丰富的交互式应用程序(RIA),用于移动设备的类似于本机的应用程序,轻量级的...

    GUI RAD for qooxdoo-开源

    qooxdoo框架的快速应用程序开发。 您可以拖放组件,定义属性并生成源代码。

    前端项目-qooxdoo.zip

    前端项目-qooxdoo,Universal JavaScript Framework

    qooxdoo.pdf

    qooxdoo的官方文档,详细介绍了如何入门qx.英文版的.建议多读英文原版,因为有些不错的技术不一定会及时的出汉化的。

    qooxdoo sdk 1.0

    qooxdoo sdk 1.0开源AJAX框架之qooxdoo之SDK

    Qooxdoo Tools-crx插件

    语言:English (United States) 扩展开发人员工具,添加显示与所选DOM元素关联的QooxDoo数据的侧栏。 扩展开发人员工具,添加一个侧栏,显示与所选相关联的Qooxdoo数据。... 在控制台中使用$ q使用所选窗口小部件。

    qooxdoo4.1

    一个面像对象的富客户端的js开发框架。这里做一个备份,供大家加快下载速度

    qooxdoo:一个全面和创新的AJAX应用程序框架_part1

    qooxdoo是一个全面和创新的AJAX应用程序框架。利用面向对象的JavaScript允许开发令人印象深刻的跨浏览器的应用。没有HTML,CSS知识,也不是必要的。它包括一个独立于平台的开发工具链,一个最先进的图形用户界面工具...

    Qooxdoo工具「Qooxdoo Tools」-crx插件

    扩展开发工具,添加一个侧栏,显示与所选DOM元素相关联的Qooxdoo数据。 扩展开发人员工具,添加侧边栏,以显示与所选DOM元素关联的...在控制台中使用$ q与选定的小部件一起使用。 支持语言:English (United States)

    qooxdoo:一个通用JavaScript框架-开源

    qooxdoo是一个通用JavaScript框架,用于为各种平台创建应用程序。 它具有全面的功能集,包括面向对象的编程模型,集成的工具链和最新的GUI工具包。

    新将delphi的form窗口转换为qooxdoo窗体

    为了加快qooxdoo窗口的设计,我使用delphi做一个简单的转换程序,使用它可以将delphi窗体转换为qooxdoo窗口布局文件。

    springmvc-qooxdoo:Spring MVC + Qooxdoo

    Spring MVC和Qooxdoo

    ville.Embed:Qooxdoo JavaScript框架的新控件

    Qooxdoo JavaScript框架的控件 关于该项目 ville.Embed是一个控件,旨在使图像,图标和图形更加动态。 好处 使用令人惊叹的图标集在项目中使用纯CSS图标 使用代码或结合代码和HTML5的模板标签将SVG图标嵌入项目中 ...

    Qooxdoo GUI Designer

    qooxdoo开发框架的界面可视化设计器

    qxl.apiviewer:qooxdoo apiviewer应用

    Qooxdoo API查看器这是Qooxdoo的API Viewer程序包,您可以使用该程序包为自己的应用程序生成API Viewer。在线版本(qx名称空间) 为您自己的代码添加API查看器 $ qx pkg update$ qx pkg install qooxdoo/qxl.api...

Global site tag (gtag.js) - Google Analytics