# Code coverage report for lib/template/index.js

## Statements: 92.16% (47 / 51) Branches: 86.84% (33 / 38) Functions: 100% (6 / 6) Lines: 92% (46 / 50) Ignored: none

All files » lib/template/ » index.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    1701

    1

    1

    1

    310
    310
    310
    310
    310
    310
    310
    310
    310

    310
    174

    136

    310

    310

    1

    310

    310

    310

    310
    113
    113

    197

    310
    228

    7

    136

    265

    265

    265

    265
    265
    265
    264

    1

    175

    175

    171

    171
    4
    3

    1

    174

     var protoclass    = require("protoclass"),
    nofactor          = require("nofactor"),
    BlockNode         = require("./vnode/block"),
    ElementNode       = require("./vnode/element"),
    FragmentNode      = require("./vnode/fragment"),
    TextNode          = require("./vnode/text"),
    CommentNode       = require("./vnode/comment"),
    View              = require("./view"),
    FragmentSection   = require("../section/fragment"),
    NodeSection       = require("../section/node"),
    TemplateComponent = require("./component"),
    defaults          = require("../defaults"),
    extend            = require("../utils/extend");

    /**
     * Compiles the template
     */

    var isIE = false;

    // check for all versions of IE - IE doesn't properly support
    // element.cloneNode(true), so we can't use that optimization.
    Iif (process.browser) {
      isIE = !!(~navigator.userAgent.toLowerCase().indexOf("msie") || ~navigator.userAgent.toLowerCase().indexOf("trident"));
    }

    function Template(script, options) {

      this.options         = options;
      this.accessor        = options.accessor;
      this.useCloneNode    = !isIE;
      this.accessorClass   = options.accessorClass || defaults.accessorClass;
      this.components      = options.components    || defaults.components;
      this.modifiers       = options.modifiers     || defaults.modifiers;
      this.attributes      = options.attributes    || defaults.attributes;
      this.runloop         = options.runloop       || defaults.runloop;
      this.nodeFactory     = options.nodeFactory   || nofactor;

      if (typeof script === "function") {
        this.vnode = script(
          FragmentNode.create,
          BlockNode.create,
          ElementNode.create,
          TextNode.create,
          CommentNode.create,
          void 0,
          this.modifiers
        );
      } else {
        this.vnode = script;
      }

      this._viewPool   = [];

      this.initialize();
    }

    /**
     */

    module.exports = protoclass(Template, {

      /**
       */

      initialize: function() {
        this.hydrators = [];

        // first build the cloneable DOM node
        this.section = new FragmentSection(this.nodeFactory);

        var node = this.vnode.initialize(this);

        if (node.nodeType === 11) {
          this.section = new FragmentSection(this.nodeFactory);
          this.section.appendChild(node);
        } else {
          this.section = new NodeSection(this.nodeFactory, node);
        }

        // next we need to initialize the hydrators - many of them
        // keep track of the path to a particular nodes.
        for (var i = this.hydrators.length; i--;) {
          this.hydrators[i].initialize();
        }
      },

      /**
       */

      createComponentClass: function(contextClass) {
        return TemplateComponent.extend({
          template     : this,
          contextClass : contextClass || Object
        });
      },

      /**
       * Creates a child template with the same options, difference source.
       * This method allows child nodes to have a different context, or the same
       * context of a different template. Used in components.
       */

      child: function(vnode, options) {
        return new Template(vnode, extend(options, {}, this.options));
      },

      /**
       * Creates a new or recycled view which binds a cloned node
       * from the template to a context (or view scope).
       */

      view: function(context, options) {

        var clonedSection;

        /*
          TODO (for IE):

          if (internetExplorer) {
            var clone = this.nodeCreator.createNode();
          } else {
            if (!this._templateNode) this._templateNode = this.nodeCreator.createNode();
            var clone = this._templateNode.clone();
          }
         */

        // re-init for now
        Iif (!this.useCloneNode) {
          this.initialize();
          clonedSection = this.section;
        } else {
          clonedSection = this.section.clone();
        }

        var view = this._viewPool.pop() || new View(this, this._viewPool, clonedSection, this.hydrators);
        view.setOptions(options || {});
        if (context) view.bind(context);
        return view;
      }
    });

    /**
     */

    module.exports = function(source, options) {

      var script, tos = typeof source;

      if (tos === "string") {

        Iif (!module.exports.parser) {
          throw new Error("paperclip parser does not exist");
        }

        script = module.exports.parser.compile(source);
      } else if (tos === "function") {
        script = source;
      } else {
        throw new Error("source must either be type 'string' or 'function'");
      }

      /**
       * Note: the template used to be cached on the script, but this isn't
       * possible now since components are registered on the template level.
       */

      return new Template(script, options || defaults);
    };