mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-05 10:22:23 +01:00
e1617a41e8
git-svn-id: http://core.svn.wordpress.org/trunk@24370 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 line
3.0 KiB
JavaScript
1 line
3.0 KiB
JavaScript
window.wp=window.wp||{};(function(a){wp.Backbone={};wp.Backbone.Subviews=function(c,b){this.view=c;this._views=_.isArray(b)?{"":b}:b||{}};wp.Backbone.Subviews.extend=Backbone.Model.extend;_.extend(wp.Backbone.Subviews.prototype,{all:function(){return _.flatten(this._views)},get:function(b){b=b||"";return this._views[b]},first:function(b){var c=this.get(b);return c&&c.length?c[0]:null},set:function(b,c,d){var f,e;if(!_.isString(b)){d=c;c=b;b=""}d=d||{};c=_.isArray(c)?c:[c];f=this.get(b);e=c;if(f){if(d.add){if(_.isUndefined(d.at)){e=f.concat(c)}else{e=f;e.splice.apply(e,[d.at,0].concat(c))}}else{_.each(e,function(g){g.__detach=true});_.each(f,function(g){if(g.__detach){g.$el.detach()}else{g.remove()}});_.each(e,function(g){delete g.__detach})}}this._views[b]=e;_.each(c,function(i){var g=i.Views||wp.Backbone.Subviews,h=i.views=i.views||new g(i);h.parent=this.view;h.selector=b},this);if(!d.silent){this._attach(b,c,_.extend({ready:this._isReady()},d))}return this},add:function(b,c,d){if(!_.isString(b)){d=c;c=b;b=""}return this.set(b,c,_.extend({add:true},d))},unset:function(b,c,d){var e;if(!_.isString(b)){d=c;c=b;b=""}c=c||[];if(e=this.get(b)){c=_.isArray(c)?c:[c];this._views[b]=c.length?_.difference(e,c):[]}if(!d||!d.silent){_.invoke(c,"remove")}return this},detach:function(){a(_.pluck(this.all(),"el")).detach();return this},render:function(){var b={ready:this._isReady()};_.each(this._views,function(d,c){this._attach(c,d,b)},this);this.rendered=true;return this},remove:function(b){if(!b||!b.silent){if(this.parent&&this.parent.views){this.parent.views.unset(this.selector,this.view,{silent:true})}delete this.parent;delete this.selector}_.invoke(this.all(),"remove");this._views=[];return this},replace:function(b,c){b.html(c);return this},insert:function(c,f,e){var b=e&&e.at,d;if(_.isNumber(b)&&(d=c.children()).length>b){d.eq(b).before(f)}else{c.append(f)}return this},ready:function(){this.view.trigger("ready");_.chain(this.all()).map(function(b){return b.views}).flatten().where({attached:true}).invoke("ready")},_attach:function(b,c,d){var f=b?this.view.$(b):this.view.$el,e;if(!f.length){return this}e=_.chain(c).pluck("views").flatten().value();_.each(e,function(g){if(g.rendered){return}g.view.render();g.rendered=true},this);this[d.add?"insert":"replace"](f,_.pluck(c,"el"),d);_.each(e,function(g){g.attached=true;if(d.ready){g.ready()}},this);return this},_isReady:function(){var b=this.view.el;while(b){if(b===document.body){return true}b=b.parentNode}return false}});wp.Backbone.View=Backbone.View.extend({Subviews:wp.Backbone.Subviews,constructor:function(){this.views=new this.Subviews(this,this.views);this.on("ready",this.ready,this);Backbone.View.apply(this,arguments)},remove:function(){var b=Backbone.View.prototype.remove.apply(this,arguments);if(this.views){this.views.remove()}return b},render:function(){var b;if(this.prepare){b=this.prepare()}this.views.detach();if(this.template){b=b||{};this.trigger("prepare",b);this.$el.html(this.template(b))}this.views.render();return this},prepare:function(){return this.options},ready:function(){}})}(jQuery)); |