<commit_before>/**
 * @license
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * 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.
 */
var xxxx = 0;
var prefix = '';

/**
 * The Prototype for all Generated Prototypes.
 * The common ancestor of all FOAM Objects.
 **/
var FObject = {
  __proto__: PropertyChangeSupport,

  name_: 'FObject',

  create_: function() {
    return Object.create(this);
  },

  create: function(args, opt_X) {
    var o = this.create_(this);
    o.instance_ = {};
    o.X = opt_X || X;

    // X.__this__ is a sub-context which contains exports and is used by
    // children.
    // TODO(kgr): does __this__ need to be local?
    // if ( this.model_.exports && this.model_.exports.length )
    //  o.X = o.X.sub({__this__: {}));

    if ( this.model_.imports && this.model_.imports.length ) {
      if ( ! this.imports_ ) {
        this.imports_ = this.model_.imports.map(function(e) {
          var s = e.split(' as ');
          return [s[0], s[1] || s[0]];
        });
      }
      for ( var i = 0 ; i < this.imports_.length ; i++ ) {
        var im = this.imports_[i];
        o[im[1]] = o.X[im[0]];
      }
    }

    if ( typeof args === 'object' ) o.copyFrom(args);
    o.init(args);

    if ( o.exportKey ) o.X[o.exportKey] = o;

    return o;
  },

  // TODO: document
  xbind: function(map) {
    return {
      __proto__: this,
      create: function(args, X) {
        args = args || {};
        for ( var key in map ) {
          if ( ! args.hasOwnProperty(key) ) args[key] = map[key];
        }
        return this.__proto__.create(args, X);
      },
      xbind: function(m2) {
        for ( var key in map ) {
          if ( ! m2.hasOwnProperty(key) ) m2[key] = map[key];
        }
        return this.__proto__.xbind(m2);
      }
    }
  },

  /** Context defaults to the global namespace by default. **/
  X: X,

  selectProperties_: function(name, p) {
    if ( Object.hasOwnProperty.call(this.model_, name) ) return this.model_[name];

    var a = [];
    var ps = this.model_.properties;
    for ( var i = 0 ; i < ps.length ; i++ ) if ( ps[i][p] ) a.push(ps[i]);
    this.model_[name] = a;

    return a;
  },

  init: function(_) {
    if ( ! this.model_ ) return;

    var ps;

    ps = this.selectProperties_('factoryProperties_', 'factory');
    for ( var i = 0 ; i < ps.length ; i++ ) {
      var prop = ps[i];

      // If a value was explicitly provided in the create args
      // then don't call the factory if it exists.
      // if ( ! this.instance_[prop.name] ) this[prop.name] = prop.factory.call(this);
      if ( ! this.hasOwnProperty(prop.name) ) this[prop.name] = prop.factory.call(this);

      if ( prop.exportKey )      this.X[this.exportKey]      = this[prop.name];
      if ( prop.exportValueKey ) this.X[this.exportValueKey] = this[prop.name + '$'];
    }


    ps = this.selectProperties_('dynamicValueProperties_', 'dynamicValue');
    ps.forEach(function(prop) {
      var name = prop.name;
      var dynamicValue = prop.dynamicValue;

      Events.dynamic(
        dynamicValue.bind(this),
        function(value) { this[name] = value; }.bind(this));
    }.bind(this));


    // TODO(kgr): exclude values which were handled in the above lists already
    ps = this.selectProperties_('exportKeyProperties_', 'exportKey');
    for ( var i = 0 ; i < ps.length ; i++ ) {
      var prop = ps[i];

      this.X[prop.exportKey] = this[prop.name];
    }


    ps = this.selectProperties_('exportValueKeyProperties_', 'exportValueKey');
    for ( var i = 0 ; i < ps.length ; i++ ) {
      var prop = ps[i];

      this.X[prop.exportValueKey] = this[prop.name + '$'];
    }

    // Add non-property exports to Context
    if ( this.model_.exports && this.model_.exports.length ) {
      if ( ! this.model_.exports_ ) {
        this.model_.exports_ = this.model_.exports.map(function(e) {
          var s = e.split(' as ');
          s[0] = s[0].trim();
          return [s[0], s[1] || s[0]];
        }).filter(function (s) {
          return typeof this[s[0]] === 'function';
        }.bind(this));
      }
      for ( var i = 0 ; i < this.model_.exports_.length ; i++ ) {
        var e = this.model_.exports_[i];
        var v = this[e[0]].bind(this);
        this.X[e[1]] = v;
      }
    }

    // Add shortcut create() method to Models which allows them to be
    // used as constructors.  Don't do this for the Model though
    // because we need the regular behavior there.
    if ( this.model_ == Model && this.name != 'Model' )
      this.create = BootstrapModel.create;
  },

  installInDocument: function(X, document) {
    if ( Object.hasOwnProperty.call(this, 'CSS') ) X.addStyle(this.CSS());
  },

  defineFOAMGetter: function(name, getter) {
    var stack = Events.onGet.stack;
    this.__defineGetter__(name, function() {
      var value = getter.call(this, name);<commit_msg>Added async support for installing CSS templates which haven't been loaded yet.<commit_after>/**
 * @license
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * 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.
 */
var xxxx = 0;
var prefix = '';

/**
 * The Prototype for all Generated Prototypes.
 * The common ancestor of all FOAM Objects.
 **/
var FObject = {
  __proto__: PropertyChangeSupport,

  name_: 'FObject',

  create_: function() {
    return Object.create(this);
  },

  create: function(args, opt_X) {
    var o = this.create_(this);
    o.instance_ = {};
    o.X = opt_X || X;

    // X.__this__ is a sub-context which contains exports and is used by
    // children.
    // TODO(kgr): does __this__ need to be local?
    // if ( this.model_.exports && this.model_.exports.length )
    //  o.X = o.X.sub({__this__: {}));

    if ( this.model_.imports && this.model_.imports.length ) {
      if ( ! this.imports_ ) {
        this.imports_ = this.model_.imports.map(function(e) {
          var s = e.split(' as ');
          return [s[0], s[1] || s[0]];
        });
      }
      for ( var i = 0 ; i < this.imports_.length ; i++ ) {
        var im = this.imports_[i];
        o[im[1]] = o.X[im[0]];
      }
    }

    if ( typeof args === 'object' ) o.copyFrom(args);
    o.init(args);

    if ( o.exportKey ) o.X[o.exportKey] = o;

    return o;
  },

  // TODO: document
  xbind: function(map) {
    return {
      __proto__: this,
      create: function(args, X) {
        args = args || {};
        for ( var key in map ) {
          if ( ! args.hasOwnProperty(key) ) args[key] = map[key];
        }
        return this.__proto__.create(args, X);
      },
      xbind: function(m2) {
        for ( var key in map ) {
          if ( ! m2.hasOwnProperty(key) ) m2[key] = map[key];
        }
        return this.__proto__.xbind(m2);
      }
    }
  },

  /** Context defaults to the global namespace by default. **/
  X: X,

  selectProperties_: function(name, p) {
    if ( Object.hasOwnProperty.call(this.model_, name) ) return this.model_[name];

    var a = [];
    var ps = this.model_.properties;
    for ( var i = 0 ; i < ps.length ; i++ ) if ( ps[i][p] ) a.push(ps[i]);
    this.model_[name] = a;

    return a;
  },

  init: function(_) {
    if ( ! this.model_ ) return;

    var ps;

    ps = this.selectProperties_('factoryProperties_', 'factory');
    for ( var i = 0 ; i < ps.length ; i++ ) {
      var prop = ps[i];

      // If a value was explicitly provided in the create args
      // then don't call the factory if it exists.
      // if ( ! this.instance_[prop.name] ) this[prop.name] = prop.factory.call(this);
      if ( ! this.hasOwnProperty(prop.name) ) this[prop.name] = prop.factory.call(this);

      if ( prop.exportKey )      this.X[this.exportKey]      = this[prop.name];
      if ( prop.exportValueKey ) this.X[this.exportValueKey] = this[prop.name + '$'];
    }


    ps = this.selectProperties_('dynamicValueProperties_', 'dynamicValue');
    ps.forEach(function(prop) {
      var name = prop.name;
      var dynamicValue = prop.dynamicValue;

      Events.dynamic(
        dynamicValue.bind(this),
        function(value) { this[name] = value; }.bind(this));
    }.bind(this));


    // TODO(kgr): exclude values which were handled in the above lists already
    ps = this.selectProperties_('exportKeyProperties_', 'exportKey');
    for ( var i = 0 ; i < ps.length ; i++ ) {
      var prop = ps[i];

      this.X[prop.exportKey] = this[prop.name];
    }


    ps = this.selectProperties_('exportValueKeyProperties_', 'exportValueKey');
    for ( var i = 0 ; i < ps.length ; i++ ) {
      var prop = ps[i];

      this.X[prop.exportValueKey] = this[prop.name + '$'];
    }

    // Add non-property exports to Context
    if ( this.model_.exports && this.model_.exports.length ) {
      if ( ! this.model_.exports_ ) {
        this.model_.exports_ = this.model_.exports.map(function(e) {
          var s = e.split(' as ');
          s[0] = s[0].trim();
          return [s[0], s[1] || s[0]];
        }).filter(function (s) {
          return typeof this[s[0]] === 'function';
        }.bind(this));
      }
      for ( var i = 0 ; i < this.model_.exports_.length ; i++ ) {
        var e = this.model_.exports_[i];
        var v = this[e[0]].bind(this);
        this.X[e[1]] = v;
      }
    }

    // Add shortcut create() method to Models which allows them to be
    // used as constructors.  Don't do this for the Model though
    // because we need the regular behavior there.
    if ( this.model_ == Model && this.name != 'Model' )
      this.create = BootstrapModel.create;
  },

  installInDocument: function(X, document) {
    for ( var i = 0 ; i < this.model_.templates.length ; i++ ) {
      var t = this.model_.templates[i];
      if ( t.name == 'CSS' ) {
        // TODO(kgr): the futureTemplate should be changed to always be there
        // so that we don't need two cases.
        if ( t.futureTemplate ) {
          t.futureTemplate(function() {
            X.addStyle(this.CSS());
          }.bind(this));
        } else {
          X.addStyle(this.CSS());
        }
        return;
      }
    }
  },

  defineFOAMGetter: function(name, getter) {
    var stack = Events.onGet.stack;
    this.__defineGetter__(name, function() {
      var value = getter.call(this, name);
{'fraction_non_alphanumeric': 0.14717687985014719, 'fraction_numerical': 0.005797877085005798, 'mean_word_length': 2.336904761904762, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6047070', 'n_tokens_mistral': 3676, 'n_tokens_neox': 3565, 'n_words': 988}