[Pkg-javascript-commits] [backbone] 07/34: beginnings of a different demo.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:58:52 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.2.0
in repository backbone.
commit 0f3c524a3a35637013480d46308da7c7112c2192
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Sat Oct 16 17:28:19 2010 -0400
beginnings of a different demo.
---
demos/documents.html | 348 --------------------------------
demos/gist.html | 42 ++++
demos/gist.js | 63 ++++++
test/vendor/jquery.tmpl.js | 486 +++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 591 insertions(+), 348 deletions(-)
diff --git a/demos/documents.html b/demos/documents.html
deleted file mode 100644
index 3ce7767..0000000
--- a/demos/documents.html
+++ /dev/null
@@ -1,348 +0,0 @@
-<!DOCTYPE HTML>
-<html>
-<head>
- <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="chrome=1">
- <title>Backbone.js Demo</title>
- <style>
-
- /* ============ */
- /* = Document = */
- /* ============ */
-
- #document_list {
- overflow: hidden;
- }
- .document {
- float: left;
- width: 140px;
- height: 140px;
- text-align: center;
- margin: 0 12px 12px 0;
- }
- .document img {
- -webkit-box-shadow: 2px 2px 0 #E0E0E0;
- -moz-box-shadow: 2px 2px 0 #E0E0E0;
- box-shadow: 2px 2px 0 #E0E0E0;
- border: 1px solid #505050;
- }
- .document .document_title {
- font: 11px "Lucida Grande", "Trebuchet MS", Verdana, sans-serif;
- font-weight: bold;
- text-shadow: 1px 1px 0 #F0F0F0;
- }
- .document:hover {
- cursor: pointer;
- }
- .document:hover .document_title {
- color: #101080;
- }
- .document:hover img {
- -webkit-box-shadow: 2px 2px 0 #C0C0E0;
- -moz-box-shadow: 2px 2px 0 #C0C0E0;
- box-shadow: 2px 2px 0 #C0C0E0;
- }
-
- /* =================== */
- /* = Document Detail = */
- /* =================== */
-
- #document_detail {
- float: right;
- width: 300px;
- height: 160px;
- text-align: left;
- margin: 0 12px 12px 0;
- padding-left: 80px;
- position: relative;
- font: 18px "Lucida Grande", "Trebuchet MS", Verdana, sans-serif;
- }
- .document_detail img {
- -webkit-box-shadow: 2px 2px 0 #E0E0E0;
- -moz-box-shadow: 2px 2px 0 #E0E0E0;
- box-shadow: 2px 2px 0 #E0E0E0;
- border: 1px solid #505050;
- position: absolute;
- left: 0;
- }
- .document_detail .document_title {
- font-weight: bold;
- text-shadow: 1px 1px 0 #F0F0F0;
- }
- .document_detail .document_description {
- font-size: 14px;
- margin: 4px 0 0 0;
- }
- .document_detail .document_date {
- font-size: 11px;
- margin: 4px 0 0 0;
- color: #A0A0A0;
- text-transform: uppercase;
- }
- .document_detail .edit_icon {
- width: 0;
- height: 10px;
- padding-right: 10px;
- margin: 2px 0 0 4px;
- display: inline;
- vertical-align: text-bottom;
- background-image: url("data:image/png;charset=utf-8;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAqCAYAAACKnsqLAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAIlJREFUeNrs1DEOwCAIBVDxepzX81E1wQiCOHRw0MUoLz+2IQARpZOV0+H6B5ZSKISMeAfrY+YkN9FCiAj5BInEHRowQh02pC/1WSRy0ULLGz3k/scLmuLBB9dmBYgnBaOxWx0+J7mJFqphclJ4SCTu0IAR6rAhfanPIpGLFuqm1XZJF0yKT4ABAJjKX31veUWhAAAAAElFTkSuQmCC");
- background-position: center 0;
- background-repeat: no-repeat;
- cursor: pointer;
- }
- .document_detail .edit_icon:hover {
- background-position: center -26px;
- }
- </style>
-</head>
-
-<body>
- <div id="document_detail"></div>
- <div id="document_list"></div>
-
- <script src="../test/vendor/underscore-1.1.0.js"></script>
- <script src="../test/vendor/jquery-1.4.2.js"></script>
- <script src="../test/vendor/json2.js"></script>
- <script src="../backbone.js"></script>
-
- <script>
-
- window.TEMPLATES = {
- documentView : '<div class="document">' +
- ' <img src="<%= doc.pageThumbnailURL(1) %>" />' +
- ' <div class="document_title"><%= doc.get("title") %></div>' +
- '</div>',
- documentDetailView : '<div class="document_detail">' +
- ' <img src="<%= doc.pageThumbnailURL(1) %>" />' +
- ' <div class="document_title"><%= doc.get("title") %> <span class="edit_icon"></span></div>' +
- ' <div class="document_description"><%= doc.get("description") %></div>' +
- ' <div class="document_date"><%= doc.get("created_at") %></div>' +
- '</div>'
- }
-
- /* Generated through:
- var d = Documents.models.map(function(m) {
- return {
- 'title': m.get('title'),
- 'created_at': m.get('created_at'),
- 'page_image_url': m.get('page_image_url'),
- 'document_viewer_url': m.get('document_viewer_url'),
- 'description': m.get('description')
- };
- });
- JSON.stringify(d);
- */
- var documentsData = [
- {
- "title":"Brooklyn History",
- "created_at":"Aug 11, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/236/pages/brooklyn-history-p{page}-{size}.gif?1281557493",
- "document_viewer_url":"http://dev.dcloud.org/documents/236-brooklyn-history.html",
- "description":"History of the great borough."
- },
- {
- "title":"Bayesian Filtering Beyond Binary Classification",
- "created_at":"Aug 10, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/217/pages/bayesian-filtering-beyond-binary-classification-p{page}-{size}.gif?1284663782",
- "document_viewer_url":"http://dev.dcloud.org/documents/217-bayesian-filtering-beyond-binary-classification.html",
- "description":"Filter, filter, filter..."
- },
- {
- "title":"BigTable",
- "created_at":"Aug 05, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/157/pages/bigtable-p{page}-{size}.gif?1281048012",
- "document_viewer_url":"http://dev.dcloud.org/documents/157-bigtable.html",
- "description":"Google's proprietary persistent and scalable database."
- },
- {
- "title":"Bayesian Filtering: Binary Classification",
- "created_at":"Jul 30, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/49/pages/bayesian-filtering-binary-classification-p{page}-{size}.gif?1281710719",
- "document_viewer_url":"http://dev.dcloud.org/documents/49-bayesian-filtering-binary-classification.html",
- "description":"Bayesian classifiers can be classified, too!"
- },
- {
- "title":"History of the Brooklyn Brewery",
- "created_at":"Jul 21, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/18/pages/history-of-the-brooklyn-brewery-p{page}-{size}.gif?1279747404",
- "document_viewer_url":"http://dev.dcloud.org/documents/18-history-of-the-brooklyn-brewery.html",
- "description":"Brews..."
- },
- {
- "title":"Brooklyn Guide",
- "created_at":"Jul 19, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/15/pages/brooklyn-guide-p{page}-{size}.gif?1279556444",
- "document_viewer_url":"http://dev.dcloud.org/documents/15-brooklyn-guide.html",
- "description":"It's a guide to Brooklyn."
- },
- {
- "title":"NYC Guide",
- "created_at":"Jul 19, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/14/pages/nyc-guide-p{page}-{size}.gif?1279556171",
- "document_viewer_url":"http://dev.dcloud.org/documents/14-nyc-guide.html",
- "description":"It's a guide to New York City."
- },
- {
- "title":"NYC Crime in Parks",
- "created_at":"Jul 19, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/13/pages/nyc-crime-in-parks-p{page}-{size}.gif?1279746795",
- "document_viewer_url":"http://dev.dcloud.org/documents/13-nyc-crime-in-parks.html",
- "description":"Lotsa crime, lotsa parks."
- },
- {
- "title":"Web Scale k-means Clustering",
- "created_at":"Jul 16, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/9/pages/web-scale-k-means-clusteirng-p{page}-{size}.gif?1280174544",
- "document_viewer_url":"http://dev.dcloud.org/documents/9-web-scale-k-means-clusteirng.html",
- "description":"Learn what k-means clustering on the big scale means."
- },
- {
- "title":"K-Means Clustering",
- "created_at":"Jul 16, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/8/pages/k-means-clustering-p{page}-{size}.gif?1280244348",
- "document_viewer_url":"http://dev.dcloud.org/documents/8-k-means-clustering.html",
- "description":"Aligning neighbors and weighing them against their closer neighbors."
- },
- {
- "title":"History of NLP",
- "created_at":"Jul 16, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/7/pages/history-of-nlp-p{page}-{size}.gif?1279310296",
- "document_viewer_url":"http://dev.dcloud.org/documents/7-history-of-nlp.html",
- "description":"Natural language Processing, an old technology hardly touched."
- },
- {
- "title":"Intro to NLP",
- "created_at":"Jul 16, 2010",
- "page_image_url":"http://dev.dcloud.org/asset_store/documents/6/pages/intro-to-nlp-p{page}-{size}.gif?1279294973",
- "document_viewer_url":"http://dev.dcloud.org/documents/6-intro-to-nlp.html",
- "description":"The very basics to Natural Language Processing."
- }
- ];
- </script>
-
- <script>
-
- window.dc = {
- app : {},
- model : {},
- view : {},
- collection : {}
- };
-
- dc.model.Document = Backbone.Model.extend({
-
- pageThumbnailURL : function(page) {
- return this.get('page_image_url').replace('{size}', 'thumbnail').replace('{page}', page);
- },
-
- });
-
- dc.collection.DocumentSet = Backbone.Collection.extend({
-
- model : dc.model.Document,
-
- constructor : function(options) {
- Backbone.Collection.call(this, options);
- },
-
- comparator : function(doc) {
- return doc.get('index');
- }
-
- });
-
- dc.view.Document = Backbone.View.extend({
-
- events : {
- "click .document" : "renderDetail",
- "dblclick .document" : "openDocument"
- },
-
- initialize : function(options) {
- _.bindAll(this, 'render', 'renderDetail');
- this.model.bind('change', this.render);
- },
-
- render : function() {
- $(this.el).html(_.template(TEMPLATES.documentView, {
- doc : this.model
- }))
- this.handleEvents();
- return this;
- },
-
- renderDetail : function() {
- var $detail = (new dc.view.DocumentDetail({model : this.model})).render().el;
- $('#document_detail').html($detail);
- },
-
- openDocument : function() {
- window.open(this.model.get('document_viewer_url'));
- }
-
- });
-
- dc.view.DocumentDetail = dc.view.Document.extend({
-
- events : {
- "click .edit_icon" : "editTitle"
- },
-
- render : function() {
- $(this.el).html(_.template(TEMPLATES.documentDetailView, {
- doc : this.model
- }))
- this.handleEvents();
- return this;
- },
-
- editTitle : function() {
- var newTitle = window.prompt("Change document title from \""+this.model.get('title')+"\" to:");
- if (newTitle !== null) {
- this.model.set({title : newTitle || "[Untitled]"});
- }
- }
-
- });
-
- dc.view.DocumentList = Backbone.View.extend({
-
- events : {
-
- },
-
- initialize : function(options) {
- _.bindAll(this, 'refresh', '_removeDocument', '_addDocument');
- dc.app.Documents.bind('refresh', this.refresh);
- dc.app.Documents.bind('remove', this._removeDocument);
- dc.app.Documents.bind('add', this._addDocument);
- },
-
- refresh : function() {
- this.views = dc.app.Documents.map(_.bind(function(doc){
- return this._addDocument(doc);
- }, this));
- },
-
- _addDocument : function(doc) {
- var view = new dc.view.Document({model : doc});
- $(this.el).append(view.render().el);
- return view;
- },
-
- _removeDocument : function(doc) {
- console.log(['views', this.views, doc]);
- }
-
- });
-
- dc.app.Documents = new dc.collection.DocumentSet();
- dc.app.DocumentList = new dc.view.DocumentList();
- dc.app.Documents.refresh(documentsData);
-
- $('#document_list').append(dc.app.DocumentList.render().el);
-
- </script>
-</body>
-</html>
\ No newline at end of file
diff --git a/demos/gist.html b/demos/gist.html
new file mode 100644
index 0000000..597a439
--- /dev/null
+++ b/demos/gist.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Backbone Demo: Gist Browser</title>
+ <script src="../test/vendor/underscore-1.1.0.js"></script>
+ <script src="../test/vendor/jquery-1.4.2.js"></script>
+ <script src="../test/vendor/jquery.tmpl.js"></script>
+ <script src="../test/vendor/json2.js"></script>
+ <script src="../backbone.js"></script>
+
+ <style>
+ #gists {
+ margin: 50px;
+ border: 1px solid gray;
+ min-height: 100px;
+ }
+ </style>
+
+ <script id="accountTemplate" type="text/x-jquery-tmpl">
+ <div class="account">
+ <div class="name">${ account.get("name") }</div>
+ <div class="location">${ account.get("location") }</div>
+ <div class="blog">${ account.get("blog") }</div>
+ </div>
+ </script>
+
+ <script src="gist.js"></script>
+ </head>
+ <body>
+
+ <form id="picker">
+ <label for="github_account">Enter a Github Account:</label>
+ <input id="github_account" type="text" value="documentcloud" />
+ <button>Go</button>
+ </form>
+
+ <div id="account_container">
+
+ </div>
+
+ </body>
+</html>
diff --git a/demos/gist.js b/demos/gist.js
new file mode 100644
index 0000000..46b5a3c
--- /dev/null
+++ b/demos/gist.js
@@ -0,0 +1,63 @@
+// Backbone Demo: Gist Browser
+// ---------------------------
+
+var GistApp = Backbone.View.extend({
+
+ events : {
+ 'submit #picker': 'loadAccount'
+ },
+
+ initialize : function() {
+ this.handleEvents();
+ },
+
+ loadAccount : function() {
+ var name = this.$("#github_account").val();
+ this.account = new GithubAccount({username : name});
+ this.accountView = new AccountView({model: this.account});
+ this.$('#account_container').html(this.accountView.el);
+ return false;
+ }
+
+});
+
+var GithubAccount = Backbone.Model.extend({
+
+ initialize : function() {
+ $.getJSON(this.url(), {}, _.bind(function(resp) {
+ this.set(resp.user);
+ }, this));
+ },
+
+ url : function() {
+ return 'http://gist.github.com/api/v1/json/' + this.get('username');
+ }
+
+});
+
+var AccountView = Backbone.View.extend({
+
+ template : $('#accountTemplate'),
+
+ initialize : function() {
+ _.bindAll(this, 'render');
+ this.model.bind("change", this.render);
+ },
+
+ render : function() {
+ console.log(this.model);
+ var ui = $('#accountTemplate').tmpl({account : this.model});
+ $(this.el).html(ui);
+ }
+
+});
+
+var GistList = Backbone.View.extend({
+
+ id : 'gists'
+
+});
+
+$(function() {
+ new GistApp({el: document.body});
+});
\ No newline at end of file
diff --git a/test/vendor/jquery.tmpl.js b/test/vendor/jquery.tmpl.js
new file mode 100644
index 0000000..1c5b2d6
--- /dev/null
+++ b/test/vendor/jquery.tmpl.js
@@ -0,0 +1,486 @@
+/*
+ * jQuery Templating Plugin
+ * Copyright 2010, John Resig
+ * Dual licensed under the MIT or GPL Version 2 licenses.
+ */
+(function( jQuery, undefined ){
+ var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]*(<[\w\W]+>)[^>]*$|\{\{\! /,
+ newTmplItems = {}, wrappedItems = {}, appendToTmplItems, topTmplItem = { key: 0, data: {} }, itemKey = 0, cloneIndex = 0, stack = [];
+
+ function newTmplItem( options, parentItem, fn, data ) {
+ // Returns a template item data structure for a new rendered instance of a template (a 'template item').
+ // The content field is a hierarchical array of strings and nested items (to be
+ // removed and replaced by nodes field of dom elements, once inserted in DOM).
+ var newItem = {
+ data: data || (parentItem ? parentItem.data : {}),
+ _wrap: parentItem ? parentItem._wrap : null,
+ tmpl: null,
+ parent: parentItem || null,
+ nodes: [],
+ calls: tiCalls,
+ nest: tiNest,
+ wrap: tiWrap,
+ html: tiHtml,
+ update: tiUpdate
+ };
+ if ( options ) {
+ jQuery.extend( newItem, options, { nodes: [], parent: parentItem } );
+ }
+ if ( fn ) {
+ // Build the hierarchical content to be used during insertion into DOM
+ newItem.tmpl = fn;
+ newItem._ctnt = newItem._ctnt || newItem.tmpl( jQuery, newItem );
+ newItem.key = ++itemKey;
+ // Keep track of new template item, until it is stored as jQuery Data on DOM element
+ (stack.length ? wrappedItems : newTmplItems)[itemKey] = newItem;
+ }
+ return newItem;
+ }
+
+ // Override appendTo etc., in order to provide support for targeting multiple elements. (This code would disappear if integrated in jquery core).
+ jQuery.each({
+ appendTo: "append",
+ prependTo: "prepend",
+ insertBefore: "before",
+ insertAfter: "after",
+ replaceAll: "replaceWith"
+ }, function( name, original ) {
+ jQuery.fn[ name ] = function( selector ) {
+ var ret = [], insert = jQuery( selector ), elems, i, l, tmplItems,
+ parent = this.length === 1 && this[0].parentNode;
+
+ appendToTmplItems = newTmplItems || {};
+ if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
+ insert[ original ]( this[0] );
+ ret = this;
+ } else {
+ for ( i = 0, l = insert.length; i < l; i++ ) {
+ cloneIndex = i;
+ elems = (i > 0 ? this.clone(true) : this).get();
+ jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
+ ret = ret.concat( elems );
+ }
+ cloneIndex = 0;
+ ret = this.pushStack( ret, name, insert.selector );
+ }
+ tmplItems = appendToTmplItems;
+ appendToTmplItems = null;
+ jQuery.tmpl.complete( tmplItems );
+ return ret;
+ };
+ });
+
+ jQuery.fn.extend({
+ // Use first wrapped element as template markup.
+ // Return wrapped set of template items, obtained by rendering template against data.
+ tmpl: function( data, options, parentItem ) {
+ return jQuery.tmpl( this[0], data, options, parentItem );
+ },
+
+ // Find which rendered template item the first wrapped DOM element belongs to
+ tmplItem: function() {
+ return jQuery.tmplItem( this[0] );
+ },
+
+ // Consider the first wrapped element as a template declaration, and get the compiled template or store it as a named template.
+ template: function( name ) {
+ return jQuery.template( name, this[0] );
+ },
+
+ domManip: function( args, table, callback, options ) {
+ // This appears to be a bug in the appendTo, etc. implementation
+ // it should be doing .call() instead of .apply(). See #6227
+ if ( args[0] && args[0].nodeType ) {
+ var dmArgs = jQuery.makeArray( arguments ), argsLength = args.length, i = 0, tmplItem;
+ while ( i < argsLength && !(tmplItem = jQuery.data( args[i++], "tmplItem" ))) {}
+ if ( argsLength > 1 ) {
+ dmArgs[0] = [jQuery.makeArray( args )];
+ }
+ if ( tmplItem && cloneIndex ) {
+ dmArgs[2] = function( fragClone ) {
+ // Handler called by oldManip when rendered template has been inserted into DOM.
+ jQuery.tmpl.afterManip( this, fragClone, callback );
+ };
+ }
+ oldManip.apply( this, dmArgs );
+ } else {
+ oldManip.apply( this, arguments );
+ }
+ cloneIndex = 0;
+ if ( !appendToTmplItems ) {
+ jQuery.tmpl.complete( newTmplItems );
+ }
+ return this;
+ }
+ });
+
+ jQuery.extend({
+ // Return wrapped set of template items, obtained by rendering template against data.
+ tmpl: function( tmpl, data, options, parentItem ) {
+ var ret, topLevel = !parentItem;
+ if ( topLevel ) {
+ // This is a top-level tmpl call (not from a nested template using {{tmpl}})
+ parentItem = topTmplItem;
+ tmpl = jQuery.template[tmpl] || jQuery.template( null, tmpl );
+ wrappedItems = {}; // Any wrapped items will be rebuilt, since this is top level
+ } else if ( !tmpl ) {
+ // The template item is already associated with DOM - this is a refresh.
+ // Re-evaluate rendered template for the parentItem
+ tmpl = parentItem.tmpl;
+ newTmplItems[parentItem.key] = parentItem;
+ parentItem.nodes = [];
+ if ( parentItem.wrapped ) {
+ updateWrapped( parentItem, parentItem.wrapped );
+ }
+ // Rebuild, without creating a new template item
+ return jQuery( build( parentItem, null, parentItem.tmpl( jQuery, parentItem ) ));
+ }
+ if ( !tmpl ) {
+ return []; // Could throw...
+ }
+ if ( typeof data === "function" ) {
+ data = data.call( parentItem || {} );
+ }
+ if ( options && options.wrapped ) {
+ updateWrapped( options, options.wrapped );
+ }
+ ret = jQuery.isArray( data ) ?
+ jQuery.map( data, function( dataItem ) {
+ return dataItem ? newTmplItem( options, parentItem, tmpl, dataItem ) : null;
+ }) :
+ [ newTmplItem( options, parentItem, tmpl, data ) ];
+ return topLevel ? jQuery( build( parentItem, null, ret ) ) : ret;
+ },
+
+ // Return rendered template item for an element.
+ tmplItem: function( elem ) {
+ var tmplItem;
+ if ( elem instanceof jQuery ) {
+ elem = elem[0];
+ }
+ while ( elem && elem.nodeType === 1 && !(tmplItem = jQuery.data( elem, "tmplItem" )) && (elem = elem.parentNode) ) {}
+ return tmplItem || topTmplItem;
+ },
+
+ // Set:
+ // Use $.template( name, tmpl ) to cache a named template,
+ // where tmpl is a template string, a script element or a jQuery instance wrapping a script element, etc.
+ // Use $( "selector" ).template( name ) to provide access by name to a script block template declaration.
+
+ // Get:
+ // Use $.template( name ) to access a cached template.
+ // Also $( selectorToScriptBlock ).template(), or $.template( null, templateString )
+ // will return the compiled template, without adding a name reference.
+ // If templateString includes at least one HTML tag, $.template( templateString ) is equivalent
+ // to $.template( null, templateString )
+ template: function( name, tmpl ) {
+ if (tmpl) {
+ // Compile template and associate with name
+ if ( typeof tmpl === "string" ) {
+ // This is an HTML string being passed directly in.
+ tmpl = buildTmplFn( tmpl )
+ } else if ( tmpl instanceof jQuery ) {
+ tmpl = tmpl[0] || {};
+ }
+ if ( tmpl.nodeType ) {
+ // If this is a template block, use cached copy, or generate tmpl function and cache.
+ tmpl = jQuery.data( tmpl, "tmpl" ) || jQuery.data( tmpl, "tmpl", buildTmplFn( tmpl.innerHTML ));
+ }
+ return typeof name === "string" ? (jQuery.template[name] = tmpl) : tmpl;
+ }
+ // Return named compiled template
+ return name ? (typeof name !== "string" ? jQuery.template( null, name ):
+ (jQuery.template[name] ||
+ // If not in map, treat as a selector. (If integrated with core, use quickExpr.exec)
+ jQuery.template( null, htmlExpr.test( name ) ? name : jQuery( name )))) : null;
+ },
+
+ encode: function( text ) {
+ // Do HTML encoding replacing < > & and ' and " by corresponding entities.
+ return ("" + text).split("<").join("<").split(">").join(">").split('"').join(""").split("'").join("'");
+ }
+ });
+
+ jQuery.extend( jQuery.tmpl, {
+ tag: {
+ "tmpl": {
+ _default: { $2: "null" },
+ open: "if($notnull_1){_=_.concat($item.nest($1,$2));}"
+ // tmpl target parameter can be of type function, so use $1, not $1a (so not auto detection of functions)
+ // This means that {{tmpl foo}} treats foo as a template (which IS a function).
+ // Explicit parens can be used if foo is a function that returns a template: {{tmpl foo()}}.
+ },
+ "wrap": {
+ _default: { $2: "null" },
+ open: "$item.calls(_,$1,$2);_=[];",
+ close: "call=$item.calls();_=call._.concat($item.wrap(call,_));"
+ },
+ "each": {
+ _default: { $2: "$index, $value" },
+ open: "if($notnull_1){$.each($1a,function($2){with(this){",
+ close: "}});}"
+ },
+ "if": {
+ open: "if(($notnull_1) && $1a){",
+ close: "}"
+ },
+ "else": {
+ _default: { $1: "true" },
+ open: "}else if(($notnull_1) && $1a){"
+ },
+ "html": {
+ // Unecoded expression evaluation.
+ open: "if($notnull_1){_.push($1a);}"
+ },
+ "=": {
+ // Encoded expression evaluation. Abbreviated form is ${}.
+ _default: { $1: "$data" },
+ open: "if($notnull_1){_.push($.encode($1a));}"
+ },
+ "!": {
+ // Comment tag. Skipped by parser
+ open: ""
+ }
+ },
+
+ // This stub can be overridden, e.g. in jquery.tmplPlus for providing rendered events
+ complete: function( items ) {
+ newTmplItems = {};
+ },
+
+ // Call this from code which overrides domManip, or equivalent
+ // Manage cloning/storing template items etc.
+ afterManip: function afterManip( elem, fragClone, callback ) {
+ // Provides cloned fragment ready for fixup prior to and after insertion into DOM
+ var content = fragClone.nodeType === 11 ?
+ jQuery.makeArray(fragClone.childNodes) :
+ fragClone.nodeType === 1 ? [fragClone] : [];
+
+ // Return fragment to original caller (e.g. append) for DOM insertion
+ callback.call( elem, fragClone );
+
+ // Fragment has been inserted:- Add inserted nodes to tmplItem data structure. Replace inserted element annotations by jQuery.data.
+ storeTmplItems( content );
+ cloneIndex++;
+ }
+ });
+
+ //========================== Private helper functions, used by code above ==========================
+
+ function build( tmplItem, nested, content ) {
+ // Convert hierarchical content into flat string array
+ // and finally return array of fragments ready for DOM insertion
+ var frag, ret = content ? jQuery.map( content, function( item ) {
+ return (typeof item === "string") ?
+ // Insert template item annotations, to be converted to jQuery.data( "tmplItem" ) when elems are inserted into DOM.
+ (tmplItem.key ? item.replace( /(<\w+)(?=[\s>])(?![^>]*_tmplitem)([^>]*)/g, "$1 " + tmplItmAtt + "=\"" + tmplItem.key + "\" $2" ) : item) :
+ // This is a child template item. Build nested template.
+ build( item, tmplItem, item._ctnt );
+ }) :
+ // If content is not defined, insert tmplItem directly. Not a template item. May be a string, or a string array, e.g. from {{html $item.html()}}.
+ tmplItem;
+ if ( nested ) {
+ return ret;
+ }
+
+ // top-level template
+ ret = ret.join("");
+
+ // Support templates which have initial or final text nodes, or consist only of text
+ // Also support HTML entities within the HTML markup.
+ ret.replace( /^\s*([^<\s][^<]*)?(<[\w\W]+>)([^>]*[^>\s])?\s*$/, function( all, before, middle, after) {
+ frag = jQuery( middle ).get();
+
+ storeTmplItems( frag );
+ if ( before ) {
+ frag = unencode( before ).concat(frag);
+ }
+ if ( after ) {
+ frag = frag.concat(unencode( after ));
+ }
+ });
+ return frag ? frag : unencode( ret );
+ }
+
+ function unencode( text ) {
+ // Use createElement, since createTextNode will not render HTML entities correctly
+ var el = document.createElement( "div" );
+ el.innerHTML = text;
+ return jQuery.makeArray(el.childNodes);
+ }
+
+ // Generate a reusable function that will serve to render a template against data
+ function buildTmplFn( markup ) {
+ return new Function("jQuery","$item",
+ "var $=jQuery,call,_=[],$data=$item.data;" +
+
+ // Introduce the data as local variables using with(){}
+ "with($data){_.push('" +
+
+ // Convert the template into pure JavaScript
+ jQuery.trim(markup)
+ .replace( /([\\'])/g, "\\$1" )
+ .replace( /[\r\t\n]/g, " " )
+ .replace( /\$\{([^\}]*)\}/g, "{{= $1}}" )
+ .replace( /\{\{(\/?)(\w+|.)(?:\(((?:[^\}]|\}(?!\}))*?)?\))?(?:\s+(.*?)?)?(\(((?:[^\}]|\}(?!\}))*?)\))?\s*\}\}/g,
+ function( all, slash, type, fnargs, target, parens, args ) {
+ var tag = jQuery.tmpl.tag[ type ], def, expr, exprAutoFnDetect;
+ if ( !tag ) {
+ throw "Template command not found: " + type;
+ }
+ def = tag._default || [];
+ if ( parens && !/\w$/.test(target)) {
+ target += parens;
+ parens = "";
+ }
+ if ( target ) {
+ target = unescape( target );
+ args = args ? ("," + unescape( args ) + ")") : (parens ? ")" : "");
+ // Support for target being things like a.toLowerCase();
+ // In that case don't call with template item as 'this' pointer. Just evaluate...
+ expr = parens ? (target.indexOf(".") > -1 ? target + parens : ("(" + target + ").call($item" + args)) : target;
+ exprAutoFnDetect = parens ? expr : "(typeof(" + target + ")==='function'?(" + target + ").call($item):(" + target + "))";
+ } else {
+ exprAutoFnDetect = expr = def.$1 || "null";
+ }
+ fnargs = unescape( fnargs );
+ return "');" +
+ tag[ slash ? "close" : "open" ]
+ .split( "$notnull_1" ).join( target ? "typeof(" + target + ")!=='undefined' && (" + target + ")!=null" : "true" )
+ .split( "$1a" ).join( exprAutoFnDetect )
+ .split( "$1" ).join( expr )
+ .split( "$2" ).join( fnargs ?
+ fnargs.replace( /\s*([^\(]+)\s*(\((.*?)\))?/g, function( all, name, parens, params ) {
+ params = params ? ("," + params + ")") : (parens ? ")" : "");
+ return params ? ("(" + name + ").call($item" + params) : all;
+ })
+ : (def.$2||"")
+ ) +
+ "_.push('";
+ }) +
+ "');}return _;"
+ );
+ }
+ function updateWrapped( options, wrapped ) {
+ // Build the wrapped content.
+ options._wrap = build( options, true,
+ // Suport imperative scenario in which options.wrapped can be set to a selector or an HTML string.
+ jQuery.isArray( wrapped ) ? wrapped : [htmlExpr.test( wrapped ) ? wrapped : jQuery( wrapped ).html()]
+ ).join("");
+ }
+
+ function unescape( args ) {
+ return args ? args.replace( /\\'/g, "'").replace(/\\\\/g, "\\" ) : null;
+ }
+ function outerHtml( elem ) {
+ var div = document.createElement("div");
+ div.appendChild( elem.cloneNode(true) );
+ return div.innerHTML;
+ }
+
+ // Store template items in jQuery.data(), ensuring a unique tmplItem data data structure for each rendered template instance.
+ function storeTmplItems( content ) {
+ var keySuffix = "_" + cloneIndex, elem, elems, newClonedItems = {}, i, l, m;
+ for ( i = 0, l = content.length; i < l; i++ ) {
+ if ( (elem = content[i]).nodeType !== 1 ) {
+ continue;
+ }
+ elems = elem.getElementsByTagName("*");
+ for ( m = elems.length - 1; m >= 0; m-- ) {
+ processItemKey( elems[m] );
+ }
+ processItemKey( elem );
+ }
+ function processItemKey( el ) {
+ var pntKey, pntNode = el, pntItem, tmplItem, key;
+ // Ensure that each rendered template inserted into the DOM has its own template item,
+ if ( (key = el.getAttribute( tmplItmAtt ))) {
+ while ( pntNode.parentNode && (pntNode = pntNode.parentNode).nodeType === 1 && !(pntKey = pntNode.getAttribute( tmplItmAtt ))) { }
+ if ( pntKey !== key ) {
+ // The next ancestor with a _tmplitem expando is on a different key than this one.
+ // So this is a top-level element within this template item
+ // Set pntNode to the key of the parentNode, or to 0 if pntNode.parentNode is null, or pntNode is a fragment.
+ pntNode = pntNode.parentNode ? (pntNode.nodeType === 11 ? 0 : (pntNode.getAttribute( tmplItmAtt ) || 0)) : 0;
+ if ( !(tmplItem = newTmplItems[key]) ) {
+ // The item is for wrapped content, and was copied from the temporary parent wrappedItem.
+ tmplItem = wrappedItems[key];
+ tmplItem = newTmplItem( tmplItem, newTmplItems[pntNode]||wrappedItems[pntNode], null, true );
+ tmplItem.key = ++itemKey;
+ newTmplItems[itemKey] = tmplItem;
+ }
+ if ( cloneIndex ) {
+ cloneTmplItem( key );
+ }
+ }
+ el.removeAttribute( tmplItmAtt );
+ } else if ( cloneIndex && (tmplItem = jQuery.data( el, "tmplItem" )) ) {
+ // This was a rendered element, cloned during append or appendTo etc.
+ // TmplItem stored in jQuery data has already been cloned in cloneCopyEvent. We must replace it with a fresh cloned tmplItem.
+ cloneTmplItem( tmplItem.key );
+ newTmplItems[tmplItem.key] = tmplItem;
+ pntNode = jQuery.data( el.parentNode, "tmplItem" );
+ pntNode = pntNode ? pntNode.key : 0;
+ }
+ if ( tmplItem ) {
+ pntItem = tmplItem;
+ // Find the template item of the parent element.
+ // (Using !=, not !==, since pntItem.key is number, and pntNode may be a string)
+ while ( pntItem && pntItem.key != pntNode ) {
+ // Add this element as a top-level node for this rendered template item, as well as for any
+ // ancestor items between this item and the item of its parent element
+ pntItem.nodes.push( el );
+ pntItem = pntItem.parent;
+ }
+ // Delete content built during rendering - reduce API surface area and memory use, and avoid exposing of stale data after rendering...
+ delete tmplItem._ctnt;
+ delete tmplItem._wrap;
+ // Store template item as jQuery data on the element
+ jQuery.data( el, "tmplItem", tmplItem );
+ }
+ function cloneTmplItem( key ) {
+ key = key + keySuffix;
+ tmplItem = newClonedItems[key] =
+ (newClonedItems[key] || newTmplItem( tmplItem, newTmplItems[tmplItem.parent.key + keySuffix] || tmplItem.parent, null, true ));
+ }
+ }
+ }
+
+ //---- Helper functions for template item ----
+
+ function tiCalls( content, tmpl, data, options ) {
+ if ( !content ) {
+ return stack.pop();
+ }
+ stack.push({ _: content, tmpl: tmpl, item:this, data: data, options: options });
+ }
+
+ function tiNest( tmpl, data, options ) {
+ // nested template, using {{tmpl}} tag
+ return jQuery.tmpl( jQuery.template( tmpl ), data, options, this );
+ }
+
+ function tiWrap( call, wrapped ) {
+ // nested template, using {{wrap}} tag
+ var options = call.options || {};
+ options.wrapped = wrapped;
+ // Apply the template, which may incorporate wrapped content,
+ return jQuery.tmpl( jQuery.template( call.tmpl ), call.data, options, call.item );
+ }
+
+ function tiHtml( filter, textOnly ) {
+ var wrapped = this._wrap;
+ return jQuery.map(
+ jQuery( jQuery.isArray( wrapped ) ? wrapped.join("") : wrapped ).filter( filter || "*" ),
+ function(e) {
+ return textOnly ?
+ e.innerText || e.textContent :
+ e.outerHTML || outerHtml(e);
+ });
+ }
+
+ function tiUpdate() {
+ var coll = this.nodes;
+ jQuery.tmpl( null, null, null, this).insertBefore( coll[0] );
+ jQuery( coll ).remove();
+ }
+})( jQuery );
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list