<commit_before>/*!
 * backbone.layoutmanager.js v0.2.1
 * Copyright 2012, <NAME> (@tbranyen)
 * backbone.layoutmanager.js may be freely distributed under the MIT license.
 */
(function(window) {

"use strict";

// Alias the libraries from the global object
var Backbone = window.Backbone;
var _ = window._;
var $ = window.$;

// Accept either a single view or an array of views to clean of all DOM events
// internal model and collection references and all Backbone.Events.
function cleanViews(views) {
  // Clear out all existing views
  _.each(_.isArray(views) ? views : [views], function(view) {
    // Ensure the Element is scrubbed of all jQuery events and data
    view.remove();
    // Remove all custom events attached to this View
    view.unbind();

    // Ensure all nested views are cleaned as well
    if (view.views) {
      _.each(view.views, function(view) {
        cleanViews(view);
      });
    }

    // If a custom cleanup method was provided on the view, call it after
    // the initial cleanup is done
    if (_.isFunction(view.cleanup)) {
      view.cleanup.call(view);
    }
  });
}

// This gets passed to all _render methods.
function viewRender(root) {
  var url, contents, handler;
  var options = root._options();

  // Once the template is successfully fetched, use its contents to
  // proceed.  Context argument is first, since it is bound for
  // partial application reasons.
  function done(context, contents) {
    // Ensure the cache is up-to-date.
    LayoutManager.cache(url, contents);

    // Render the View into the el property.
    if (contents) {
      options.html(root.el, options.render(contents, context));
    }

    // Resolve partials with the View element.
    handler.resolveWith(root, [root.el]);
  }

  return {
    // Shorthand to root.view function with append flag
    insert: function(partial, view) {
      if (view) {
        return root.view(partial, view, true);
      }

      return root.view("", partial, true);
    },

    render: function(context) {
      var template = root.template || options.template;

      if (root.serialize) {
        options.serialize = root.serialize;
      }

      // Seek out serialize method and use that object.
      if (!context && _.isFunction(options.serialize)) {
        context = options.serialize.call(root);
      // If serialize is an object, just use that
      } else if (!context && _.isObject(options.serialize)) {
        context = options.serialize;
      }

      // Create an asynchronous handler
      handler = LayoutManager.makeAsync(options, _.bind(done, root, context));

      // Set the url to the prefix + the view's template property.
      if (_.isString(template)) {
        url = root._prefix + template;
      }

      // Check if contents are already cached
      if (contents = LayoutManager.cache(url)) {
        done(context, contents, url);

        return handler;
      }

      // Fetch layout and template contents
      if (_.isString(template)) {
        contents = options.fetch.call(handler, root._prefix + template);
      // If its not a string just pass the object/function/whatever
      } else if (template != null) {
        contents = options.fetch.call(handler, template);
      }

      // If the function was synchronous, continue execution.
      if (!handler._isAsync) {
        done(context, contents);
      }

      return handler;
    }
  };
}

var LayoutManager = Backbone.View.extend({
  // Internal state object used to store whether or not a View has been
  // taken over by layout manager and if it has been rendered into the DOM.
  __manager__: {},

  // This is a named function to improve logging and debugging within browser
  // dev tools.  Typically you do not use "anonymous" named functions since IE
  // has a well known bug, BUT I think we all know the reason why I'm ignoring
  // that here.
  constructor: function LayoutManager(options) {
    var proto = Backbone.LayoutManager.prototype;
    // Extend the options with the prototype and passed options
    options = _.extend({}, proto.options, options);

    // Apply the default render scheme.
    this._render = function(manage) {
      return manage(this).render();
    };

    // Set up top level views object
    this.views = {};

    // If the user provided their own render override, use that instead of the
    // default.
    if (this.render !== proto.render) {
      this._render = this.render;
      this.render = proto.render;
    }
    
    // Set the prefix for a layout
    if (options.paths) {
      this._prefix = options.paths.layout || "";
    }

    // Set the internal views
    if (options.views) {
      this.setViews(options.views);
    }

    // Ensure the template is mapped over
    if (this.template) {
      options.template = this.template;
    }

    Backbone.View.call(this, options);
  },

  // Allows the setting of multiple views instead of a single view.
  setViews: function(views) {
    // Iterate over all the views and use the View's view method to assign.
    _.each(views, function(view, name) {
      // If the view is an array put all views into insert mode
      if (_.isArray(view)) {
        _.each(view, function(view) {
          this.view(name, view, true);
        }, this);

      // Assign each view using the view function
      } else {
        this.view(name, view);
      }
    }, this);
  },

  // This takes in a partial name and view instance and assigns them to
  // the internal collection of views.  If a view is not a LayoutManager
  // instance, then mix in the LayoutManager prototype.  This ensures
  // all Views can be used successfully.
  //
  // Must definitely wrap any render method passed in or defaults to a
  // typical render function `return layout(this).render()`.
  view: function(name, view, append) {
    var partials, options;
    var root = this;
<commit_msg>allow setViews to chain<commit_after>/*!
 * backbone.layoutmanager.js v0.2.1
 * Copyright 2012, <NAME> (@tbranyen)
 * backbone.layoutmanager.js may be freely distributed under the MIT license.
 */
(function(window) {

"use strict";

// Alias the libraries from the global object
var Backbone = window.Backbone;
var _ = window._;
var $ = window.$;

// Accept either a single view or an array of views to clean of all DOM events
// internal model and collection references and all Backbone.Events.
function cleanViews(views) {
  // Clear out all existing views
  _.each(_.isArray(views) ? views : [views], function(view) {
    // Ensure the Element is scrubbed of all jQuery events and data
    view.remove();
    // Remove all custom events attached to this View
    view.unbind();

    // Ensure all nested views are cleaned as well
    if (view.views) {
      _.each(view.views, function(view) {
        cleanViews(view);
      });
    }

    // If a custom cleanup method was provided on the view, call it after
    // the initial cleanup is done
    if (_.isFunction(view.cleanup)) {
      view.cleanup.call(view);
    }
  });
}

// This gets passed to all _render methods.
function viewRender(root) {
  var url, contents, handler;
  var options = root._options();

  // Once the template is successfully fetched, use its contents to
  // proceed.  Context argument is first, since it is bound for
  // partial application reasons.
  function done(context, contents) {
    // Ensure the cache is up-to-date.
    LayoutManager.cache(url, contents);

    // Render the View into the el property.
    if (contents) {
      options.html(root.el, options.render(contents, context));
    }

    // Resolve partials with the View element.
    handler.resolveWith(root, [root.el]);
  }

  return {
    // Shorthand to root.view function with append flag
    insert: function(partial, view) {
      if (view) {
        return root.view(partial, view, true);
      }

      return root.view("", partial, true);
    },

    render: function(context) {
      var template = root.template || options.template;

      if (root.serialize) {
        options.serialize = root.serialize;
      }

      // Seek out serialize method and use that object.
      if (!context && _.isFunction(options.serialize)) {
        context = options.serialize.call(root);
      // If serialize is an object, just use that
      } else if (!context && _.isObject(options.serialize)) {
        context = options.serialize;
      }

      // Create an asynchronous handler
      handler = LayoutManager.makeAsync(options, _.bind(done, root, context));

      // Set the url to the prefix + the view's template property.
      if (_.isString(template)) {
        url = root._prefix + template;
      }

      // Check if contents are already cached
      if (contents = LayoutManager.cache(url)) {
        done(context, contents, url);

        return handler;
      }

      // Fetch layout and template contents
      if (_.isString(template)) {
        contents = options.fetch.call(handler, root._prefix + template);
      // If its not a string just pass the object/function/whatever
      } else if (template != null) {
        contents = options.fetch.call(handler, template);
      }

      // If the function was synchronous, continue execution.
      if (!handler._isAsync) {
        done(context, contents);
      }

      return handler;
    }
  };
}

var LayoutManager = Backbone.View.extend({
  // Internal state object used to store whether or not a View has been
  // taken over by layout manager and if it has been rendered into the DOM.
  __manager__: {},

  // This is a named function to improve logging and debugging within browser
  // dev tools.  Typically you do not use "anonymous" named functions since IE
  // has a well known bug, BUT I think we all know the reason why I'm ignoring
  // that here.
  constructor: function LayoutManager(options) {
    var proto = Backbone.LayoutManager.prototype;
    // Extend the options with the prototype and passed options
    options = _.extend({}, proto.options, options);

    // Apply the default render scheme.
    this._render = function(manage) {
      return manage(this).render();
    };

    // Set up top level views object
    this.views = {};

    // If the user provided their own render override, use that instead of the
    // default.
    if (this.render !== proto.render) {
      this._render = this.render;
      this.render = proto.render;
    }
    
    // Set the prefix for a layout
    if (options.paths) {
      this._prefix = options.paths.layout || "";
    }

    // Set the internal views
    if (options.views) {
      this.setViews(options.views);
    }

    // Ensure the template is mapped over
    if (this.template) {
      options.template = this.template;
    }

    Backbone.View.call(this, options);
  },

  // Allows the setting of multiple views instead of a single view.
  setViews: function(views) {
    // Iterate over all the views and use the View's view method to assign.
    _.each(views, function(view, name) {
      // If the view is an array put all views into insert mode
      if (_.isArray(view)) {
        _.each(view, function(view) {
          this.view(name, view, true);
        }, this);

      // Assign each view using the view function
      } else {
        this.view(name, view);
      }
    }, this);

    // Allow for chaining
    return this;
  },

  // This takes in a partial name and view instance and assigns them to
  // the internal collection of views.  If a view is not a LayoutManager
  // instance, then mix in the LayoutManager prototype.  This ensures
  // all Views can be used successfully.
  //
  // Must definitely wrap any render method passed in or defaults to a
  // typical render function `return layout(this).render()`.
  view: function(name, view, append) {
    var partials, options;
    var root = this;

{'fraction_non_alphanumeric': 0.10325258343215314, 'fraction_numerical': 0.0011858377096391666, 'mean_word_length': 2.502521506971225, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7524939', 'n_tokens_mistral': 3272, 'n_tokens_neox': 3196, 'n_words': 1234}