// Backbone.Inline.Template, v1.0.1
// Copyright (c) 2016 Michael Heim, Zeilenwechsel.de
// Distributed under MIT license
// http://github.com/hashchange/backbone.inline.template
;( function ( root, factory ) {
"use strict";
// UMD for a Backbone plugin. Supports AMD, Node.js, CommonJS and globals.
//
// - Code lives in the Backbone namespace.
// - The module does not export a meaningful value.
// - The module does not create a global.
var supportsExports = typeof exports === "object" && exports && !exports.nodeType && typeof module === "object" && module && !module.nodeType;
// AMD:
// - Some AMD build optimizers like r.js check for condition patterns like the AMD check below, so keep it as is.
// - Check for `exports` after `define` in case a build optimizer adds an `exports` object.
// - The AMD spec requires the dependencies to be an array **literal** of module IDs. Don't use a variable there,
// or optimizers may fail.
if ( typeof define === "function" && typeof define.amd === "object" && define.amd ) {
// AMD module
define( [ "exports", "underscore", "backbone", "backbone.declarative.views" ], factory );
} else if ( supportsExports ) {
// Node module, CommonJS module
factory( exports, require( "underscore" ), require( "backbone" ), require( "backbone.declarative.views" ) );
} else {
// Global (browser or Rhino)
factory( {}, _, Backbone );
}
}( this, function ( exports, _, Backbone ) {
"use strict";
var $ = Backbone.$,
$document = $( document ),
pluginNamespace = Backbone.InlineTemplate = {
hasInlineEl: _hasInlineEl,
updateTemplateSource: false,
version: "1.0.1"
},
rxLeadingComments = /^(\s*)+/,
rxTrailingComments = /(\s*)+$/,
rxOutermostHtmlTagWithContent = /(<\s*[a-zA-Z][\s\S]*?>)([\s\S]*)(<\s*\/\s*[a-zA-Z]+\s*>)/,
rxSelfClosingHtmlTag = /<\s*[a-zA-Z][\s\S]*?\/?\s*>/,
bbdvLoadTemplate = Backbone.DeclarativeViews.defaults.loadTemplate;
//
// Initialization
// --------------
Backbone.DeclarativeViews.plugins.registerDataAttribute( "el-definition" );
Backbone.DeclarativeViews.plugins.registerDataAttribute( "bbit-internal-template-status" );
Backbone.DeclarativeViews.plugins.registerCacheAlias( pluginNamespace, "inlineTemplate" );
Backbone.DeclarativeViews.plugins.enforceTemplateLoading();
//
// Template loader
//----------------
Backbone.DeclarativeViews.defaults.loadTemplate = function ( templateProperty ) {
var parsedTemplateData, $resultTemplate,
// Check Backbone.InlineTemplate.custom.hasInlineEl first, even though it is undocumented, to catch
// accidental assignments.
hasInlineEl = pluginNamespace.custom.hasInlineEl || pluginNamespace.hasInlineEl || _hasInlineEl,
updateTemplateContainer = pluginNamespace.updateTemplateSource,
$inputTemplate = bbdvLoadTemplate( templateProperty );
if ( _isMarkedAsUpdated( $inputTemplate ) || !hasInlineEl( $inputTemplate ) ) {
// No inline el definition. Just return the template as is.
$resultTemplate = $inputTemplate;
} else {
// Parse the template data.
//
// NB Errors are not handled here and bubble up further. Try-catch is just used to enhance the error message
// for easier debugging.
try {
parsedTemplateData = _parseTemplateHtml( $inputTemplate.html() );
} catch ( err ) {
err.message += '\nThe template was requested for template property "' + templateProperty + '"';
throw err;
}
if ( updateTemplateContainer ) {
// For updating the template container, it has to be a node in the DOM. Throw an error if it has been
// passed in as a raw HTML string.
if ( !existsInDOM( templateProperty ) ) throw new Backbone.DeclarativeViews.TemplateError( "Backbone.Inline.Template: Can't update the template container because it doesn't exist in the DOM. The template property must be a valid selector (and not, for instance, a raw HTML string). Instead, we got \"" + templateProperty + '"' );
$resultTemplate = $inputTemplate;
// The template is updated and the inline `el` removed. Set a flag on the template to make sure the
// template is never processed again as having an inline `el`.
_markAsUpdated( $resultTemplate );
} else {
// No updating of the input template. Create a new template node which will stay out of the DOM, but is
// passed to the cache.
$resultTemplate = $( "" ).attr( "type", "text/x-template" );
}
_mapElementToDataAttributes( parsedTemplateData.$elSample, $resultTemplate );
$resultTemplate.empty().text( parsedTemplateData.templateContent );
}
return $resultTemplate;
};
/**
* Checks if a template is marked as having an inline `el`. Is also exposed as Backbone.InlineTemplate.hasInlineEl().
*
* By default, a template is recognized as having an inline `el` when the container has the following data attribute:
* `data-el-definition: "inline"`.
*
* The check can be changed by overriding Backbone.InlineTemplate.hasInlineEl with a custom function. In order to
* treat all templates as having an inline `el`, for instance, the custom function just has to return true:
*
* Backbone.InlineTemplate.hasInlineEl = function () { return true; };
*
* @param {jQuery} $templateContainer the template node (usually a