[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/widget-htmlparser/ -> widget-htmlparser.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('widget-htmlparser', function (Y, NAME) {
   9  
  10  /**
  11   * Adds HTML Parser support to the base Widget class
  12   *
  13   * @module widget
  14   * @submodule widget-htmlparser
  15   * @for Widget
  16   */
  17  
  18  var Widget = Y.Widget,
  19      Node = Y.Node,
  20      Lang = Y.Lang,
  21  
  22      SRC_NODE = "srcNode",
  23      CONTENT_BOX = "contentBox";
  24  
  25  /**
  26   * Object hash, defining how attribute values are to be parsed from
  27   * markup contained in the widget's content box. e.g.:
  28   * <pre>
  29   *   {
  30   *       // Set single Node references using selector syntax
  31   *       // (selector is run through node.one)
  32   *       titleNode: "span.yui-title",
  33   *       // Set NodeList references using selector syntax
  34   *       // (array indicates selector is to be run through node.all)
  35   *       listNodes: ["li.yui-item"],
  36   *       // Set other attribute types, using a parse function.
  37   *       // Context is set to the widget instance.
  38   *       label: function(contentBox) {
  39   *           return contentBox.one("span.title").get("innerHTML");
  40   *       }
  41   *   }
  42   * </pre>
  43   *
  44   * @property HTML_PARSER
  45   * @type Object
  46   * @static
  47   */
  48  Widget.HTML_PARSER = {};
  49  
  50  /**
  51   * The build configuration for the Widget class.
  52   * <p>
  53   * Defines the static fields which need to be aggregated,
  54   * when this class is used as the main class passed to
  55   * the <a href="Base.html#method_build">Base.build</a> method.
  56   * </p>
  57   * @property _buildCfg
  58   * @type Object
  59   * @static
  60   * @final
  61   * @private
  62   */
  63  Widget._buildCfg = {
  64      aggregates : ["HTML_PARSER"]
  65  };
  66  
  67  /**
  68   * The DOM node to parse for configuration values, passed to the Widget's HTML_PARSER definition
  69   *
  70   * @attribute srcNode
  71   * @type String | Node
  72   * @writeOnce
  73   */
  74  Widget.ATTRS[SRC_NODE] = {
  75      value: null,
  76      setter: Node.one,
  77      getter: "_getSrcNode",
  78      writeOnce: true
  79  };
  80  
  81  Y.mix(Widget.prototype, {
  82  
  83      /**
  84       * @method _getSrcNode
  85       * @protected
  86       * @return {Node} The Node to apply HTML_PARSER to
  87       */
  88      _getSrcNode : function(val) {
  89          return val || this.get(CONTENT_BOX);
  90      },
  91  
  92      /**
  93       * Implement the BaseCore _preAddAttrs method hook, to add
  94       * the srcNode and related attributes, so that HTML_PARSER
  95       * (which relies on `this.get("srcNode")`) can merge in it's
  96       * results before the rest of the attributes are added.
  97       *
  98       * @method _preAddAttrs
  99       * @protected
 100       *
 101       * @param attrs {Object} The full hash of statically defined ATTRS
 102       * attributes being added for this instance
 103       *
 104       * @param userVals {Object} The hash of user values passed to
 105       * the constructor
 106       *
 107       * @param lazy {boolean} Whether or not to add the attributes lazily
 108       */
 109      _preAddAttrs : function(attrs, userVals, lazy) {
 110  
 111          var preAttrs = {
 112              id : attrs.id,
 113              boundingBox : attrs.boundingBox,
 114              contentBox : attrs.contentBox,
 115              srcNode : attrs.srcNode
 116          };
 117  
 118          this.addAttrs(preAttrs, userVals, lazy);
 119  
 120          delete attrs.boundingBox;
 121          delete attrs.contentBox;
 122          delete attrs.srcNode;
 123          delete attrs.id;
 124  
 125          if (this._applyParser) {
 126              this._applyParser(userVals);
 127          }
 128      },
 129  
 130      /**
 131       * @method _applyParsedConfig
 132       * @protected
 133       * @return {Object} The merged configuration literal
 134       */
 135      _applyParsedConfig : function(node, cfg, parsedCfg) {
 136          return (parsedCfg) ? Y.mix(cfg, parsedCfg, false) : cfg;
 137      },
 138  
 139      /**
 140       * Utility method used to apply the <code>HTML_PARSER</code> configuration for the
 141       * instance, to retrieve config data values.
 142       *
 143       * @method _applyParser
 144       * @protected
 145       * @param config {Object} User configuration object (will be populated with values from Node)
 146       */
 147      _applyParser : function(config) {
 148  
 149          var widget = this,
 150              srcNode = this._getNodeToParse(),
 151              schema = widget._getHtmlParser(),
 152              parsedConfig,
 153              val;
 154  
 155          if (schema && srcNode) {
 156              Y.Object.each(schema, function(v, k, o) {
 157                  val = null;
 158  
 159                  if (Lang.isFunction(v)) {
 160                      val = v.call(widget, srcNode);
 161                  } else {
 162                      if (Lang.isArray(v)) {
 163                          val = srcNode.all(v[0]);
 164                          if (val.isEmpty()) {
 165                              val = null;
 166                          }
 167                      } else {
 168                          val = srcNode.one(v);
 169                      }
 170                  }
 171  
 172                  if (val !== null && val !== undefined) {
 173                      parsedConfig = parsedConfig || {};
 174                      parsedConfig[k] = val;
 175                  }
 176              });
 177          }
 178          config = widget._applyParsedConfig(srcNode, config, parsedConfig);
 179      },
 180  
 181      /**
 182       * Determines whether we have a node reference which we should try and parse.
 183       *
 184       * The current implementation does not parse nodes generated from CONTENT_TEMPLATE,
 185       * only explicitly set srcNode, or contentBox attributes.
 186       *
 187       * @method _getNodeToParse
 188       * @return {Node} The node reference to apply HTML_PARSER to.
 189       * @private
 190       */
 191      _getNodeToParse : function() {
 192          var srcNode = this.get("srcNode");
 193          return (!this._cbFromTemplate) ? srcNode : null;
 194      },
 195  
 196      /**
 197       * Gets the HTML_PARSER definition for this instance, by merging HTML_PARSER
 198       * definitions across the class hierarchy.
 199       *
 200       * @private
 201       * @method _getHtmlParser
 202       * @return {Object} HTML_PARSER definition for this instance
 203       */
 204      _getHtmlParser : function() {
 205          // Removed caching for kweight. This is a private method
 206          // and only called once so don't need to cache HTML_PARSER
 207          var classes = this._getClasses(),
 208              parser = {},
 209              i, p;
 210  
 211          for (i = classes.length - 1; i >= 0; i--) {
 212              p = classes[i].HTML_PARSER;
 213              if (p) {
 214                  Y.mix(parser, p, true);
 215              }
 216          }
 217          return parser;
 218      }
 219  });
 220  
 221  
 222  }, '3.17.2', {"requires": ["widget-base"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1