mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-19 00:55:13 +01:00
07adb0640f
Also update Underscore to 1.6. Includes the development versions of both, which are not included in the build. Here is Backbone's changelog: * Made the return values of Collection’s `set`, `add`, `remove`, and `reset` more useful. Instead of returning `this`, they now return the changed (added, removed or updated) model or list of models. (This means they can no longer be chained.) * Backbone Views no longer automatically attach options passed to the constructor as `this.options` and Backbone Models no longer attach `url` and `urlRoot` options, but you can do it yourself if you prefer. (But if you extend `wp.Backbone.View`, options will be attached for you.) * All `"invalid"` events now pass consistent arguments. First the model in question, then the error object, then options. * You are no longer permitted to change the id of your model during `parse`. Use `idAttribute` instead. * On the other hand, `parse` is now an excellent place to extract and vivify incoming nested JSON into associated submodels. See [27171] (next commit) for changes made to WordPress to be compatible with Backbone 1.1. props gcorne, georgestephanis. see #26799. Built from https://develop.svn.wordpress.org/trunk@27170 git-svn-id: http://core.svn.wordpress.org/trunk@27035 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 line
18 KiB
JavaScript
1 line
18 KiB
JavaScript
(function(){{var a,b=this,c=b.Backbone,d=[],e=(d.push,d.slice);d.splice}a="undefined"!=typeof exports?exports:b.Backbone={},a.VERSION="1.1.0";var f=b._;f||"undefined"==typeof require||(f=require("underscore")),a.$=b.jQuery||b.Zepto||b.ender||b.$,a.noConflict=function(){return b.Backbone=c,this},a.emulateHTTP=!1,a.emulateJSON=!1;var g=a.Events={on:function(a,b,c){if(!i(this,"on",a,[b,c])||!b)return this;this._events||(this._events={});var d=this._events[a]||(this._events[a]=[]);return d.push({callback:b,context:c,ctx:c||this}),this},once:function(a,b,c){if(!i(this,"once",a,[b,c])||!b)return this;var d=this,e=f.once(function(){d.off(a,e),b.apply(this,arguments)});return e._callback=b,this.on(a,e,c)},off:function(a,b,c){var d,e,g,h,j,k,l,m;if(!this._events||!i(this,"off",a,[b,c]))return this;if(!a&&!b&&!c)return this._events={},this;for(h=a?[a]:f.keys(this._events),j=0,k=h.length;k>j;j++)if(a=h[j],g=this._events[a]){if(this._events[a]=d=[],b||c)for(l=0,m=g.length;m>l;l++)e=g[l],(b&&b!==e.callback&&b!==e.callback._callback||c&&c!==e.context)&&d.push(e);d.length||delete this._events[a]}return this},trigger:function(a){if(!this._events)return this;var b=e.call(arguments,1);if(!i(this,"trigger",a,b))return this;var c=this._events[a],d=this._events.all;return c&&j(c,b),d&&j(d,arguments),this},stopListening:function(a,b,c){var d=this._listeningTo;if(!d)return this;var e=!b&&!c;c||"object"!=typeof b||(c=this),a&&((d={})[a._listenId]=a);for(var g in d)a=d[g],a.off(b,c,this),(e||f.isEmpty(a._events))&&delete this._listeningTo[g];return this}},h=/\s+/,i=function(a,b,c,d){if(!c)return!0;if("object"==typeof c){for(var e in c)a[b].apply(a,[e,c[e]].concat(d));return!1}if(h.test(c)){for(var f=c.split(h),g=0,i=f.length;i>g;g++)a[b].apply(a,[f[g]].concat(d));return!1}return!0},j=function(a,b){var c,d=-1,e=a.length,f=b[0],g=b[1],h=b[2];switch(b.length){case 0:for(;++d<e;)(c=a[d]).callback.call(c.ctx);return;case 1:for(;++d<e;)(c=a[d]).callback.call(c.ctx,f);return;case 2:for(;++d<e;)(c=a[d]).callback.call(c.ctx,f,g);return;case 3:for(;++d<e;)(c=a[d]).callback.call(c.ctx,f,g,h);return;default:for(;++d<e;)(c=a[d]).callback.apply(c.ctx,b)}},k={listenTo:"on",listenToOnce:"once"};f.each(k,function(a,b){g[b]=function(b,c,d){var e=this._listeningTo||(this._listeningTo={}),g=b._listenId||(b._listenId=f.uniqueId("l"));return e[g]=b,d||"object"!=typeof c||(d=this),b[a](c,d,this),this}}),g.bind=g.on,g.unbind=g.off,f.extend(a,g);var l=a.Model=function(a,b){var c=a||{};b||(b={}),this.cid=f.uniqueId("c"),this.attributes={},b.collection&&(this.collection=b.collection),b.parse&&(c=this.parse(c,b)||{}),c=f.defaults({},c,f.result(this,"defaults")),this.set(c,b),this.changed={},this.initialize.apply(this,arguments)};f.extend(l.prototype,g,{changed:null,validationError:null,idAttribute:"id",initialize:function(){},toJSON:function(){return f.clone(this.attributes)},sync:function(){return a.sync.apply(this,arguments)},get:function(a){return this.attributes[a]},escape:function(a){return f.escape(this.get(a))},has:function(a){return null!=this.get(a)},set:function(a,b,c){var d,e,g,h,i,j,k,l;if(null==a)return this;if("object"==typeof a?(e=a,c=b):(e={})[a]=b,c||(c={}),!this._validate(e,c))return!1;g=c.unset,i=c.silent,h=[],j=this._changing,this._changing=!0,j||(this._previousAttributes=f.clone(this.attributes),this.changed={}),l=this.attributes,k=this._previousAttributes,this.idAttribute in e&&(this.id=e[this.idAttribute]);for(d in e)b=e[d],f.isEqual(l[d],b)||h.push(d),f.isEqual(k[d],b)?delete this.changed[d]:this.changed[d]=b,g?delete l[d]:l[d]=b;if(!i){h.length&&(this._pending=!0);for(var m=0,n=h.length;n>m;m++)this.trigger("change:"+h[m],this,l[h[m]],c)}if(j)return this;if(!i)for(;this._pending;)this._pending=!1,this.trigger("change",this,c);return this._pending=!1,this._changing=!1,this},unset:function(a,b){return this.set(a,void 0,f.extend({},b,{unset:!0}))},clear:function(a){var b={};for(var c in this.attributes)b[c]=void 0;return this.set(b,f.extend({},a,{unset:!0}))},hasChanged:function(a){return null==a?!f.isEmpty(this.changed):f.has(this.changed,a)},changedAttributes:function(a){if(!a)return this.hasChanged()?f.clone(this.changed):!1;var b,c=!1,d=this._changing?this._previousAttributes:this.attributes;for(var e in a)f.isEqual(d[e],b=a[e])||((c||(c={}))[e]=b);return c},previous:function(a){return null!=a&&this._previousAttributes?this._previousAttributes[a]:null},previousAttributes:function(){return f.clone(this._previousAttributes)},fetch:function(a){a=a?f.clone(a):{},void 0===a.parse&&(a.parse=!0);var b=this,c=a.success;return a.success=function(d){return b.set(b.parse(d,a),a)?(c&&c(b,d,a),void b.trigger("sync",b,d,a)):!1},K(this,a),this.sync("read",this,a)},save:function(a,b,c){var d,e,g,h=this.attributes;if(null==a||"object"==typeof a?(d=a,c=b):(d={})[a]=b,c=f.extend({validate:!0},c),d&&!c.wait){if(!this.set(d,c))return!1}else if(!this._validate(d,c))return!1;d&&c.wait&&(this.attributes=f.extend({},h,d)),void 0===c.parse&&(c.parse=!0);var i=this,j=c.success;return c.success=function(a){i.attributes=h;var b=i.parse(a,c);return c.wait&&(b=f.extend(d||{},b)),f.isObject(b)&&!i.set(b,c)?!1:(j&&j(i,a,c),void i.trigger("sync",i,a,c))},K(this,c),e=this.isNew()?"create":c.patch?"patch":"update","patch"===e&&(c.attrs=d),g=this.sync(e,this,c),d&&c.wait&&(this.attributes=h),g},destroy:function(a){a=a?f.clone(a):{};var b=this,c=a.success,d=function(){b.trigger("destroy",b,b.collection,a)};if(a.success=function(e){(a.wait||b.isNew())&&d(),c&&c(b,e,a),b.isNew()||b.trigger("sync",b,e,a)},this.isNew())return a.success(),!1;K(this,a);var e=this.sync("delete",this,a);return a.wait||d(),e},url:function(){var a=f.result(this,"urlRoot")||f.result(this.collection,"url")||J();return this.isNew()?a:a+("/"===a.charAt(a.length-1)?"":"/")+encodeURIComponent(this.id)},parse:function(a){return a},clone:function(){return new this.constructor(this.attributes)},isNew:function(){return null==this.id},isValid:function(a){return this._validate({},f.extend(a||{},{validate:!0}))},_validate:function(a,b){if(!b.validate||!this.validate)return!0;a=f.extend({},this.attributes,a);var c=this.validationError=this.validate(a,b)||null;return c?(this.trigger("invalid",this,c,f.extend(b,{validationError:c})),!1):!0}});var m=["keys","values","pairs","invert","pick","omit"];f.each(m,function(a){l.prototype[a]=function(){var b=e.call(arguments);return b.unshift(this.attributes),f[a].apply(f,b)}});var n=a.Collection=function(a,b){b||(b={}),b.model&&(this.model=b.model),void 0!==b.comparator&&(this.comparator=b.comparator),this._reset(),this.initialize.apply(this,arguments),a&&this.reset(a,f.extend({silent:!0},b))},o={add:!0,remove:!0,merge:!0},p={add:!0,remove:!1};f.extend(n.prototype,g,{model:l,initialize:function(){},toJSON:function(a){return this.map(function(b){return b.toJSON(a)})},sync:function(){return a.sync.apply(this,arguments)},add:function(a,b){return this.set(a,f.extend({merge:!1},b,p))},remove:function(a,b){var c=!f.isArray(a);a=c?[a]:f.clone(a),b||(b={});var d,e,g,h;for(d=0,e=a.length;e>d;d++)h=a[d]=this.get(a[d]),h&&(delete this._byId[h.id],delete this._byId[h.cid],g=this.indexOf(h),this.models.splice(g,1),this.length--,b.silent||(b.index=g,h.trigger("remove",h,this,b)),this._removeReference(h));return c?a[0]:a},set:function(a,b){b=f.defaults({},b,o),b.parse&&(a=this.parse(a,b));var c=!f.isArray(a);a=c?a?[a]:[]:f.clone(a);var d,e,g,h,i,j,k,m=b.at,n=this.model,p=this.comparator&&null==m&&b.sort!==!1,q=f.isString(this.comparator)?this.comparator:null,r=[],s=[],t={},u=b.add,v=b.merge,w=b.remove,x=!p&&u&&w?[]:!1;for(d=0,e=a.length;e>d;d++){if(i=a[d],g=i instanceof l?h=i:i[n.prototype.idAttribute],j=this.get(g))w&&(t[j.cid]=!0),v&&(i=i===h?h.attributes:i,b.parse&&(i=j.parse(i,b)),j.set(i,b),p&&!k&&j.hasChanged(q)&&(k=!0)),a[d]=j;else if(u){if(h=a[d]=this._prepareModel(i,b),!h)continue;r.push(h),h.on("all",this._onModelEvent,this),this._byId[h.cid]=h,null!=h.id&&(this._byId[h.id]=h)}x&&x.push(j||h)}if(w){for(d=0,e=this.length;e>d;++d)t[(h=this.models[d]).cid]||s.push(h);s.length&&this.remove(s,b)}if(r.length||x&&x.length)if(p&&(k=!0),this.length+=r.length,null!=m)for(d=0,e=r.length;e>d;d++)this.models.splice(m+d,0,r[d]);else{x&&(this.models.length=0);var y=x||r;for(d=0,e=y.length;e>d;d++)this.models.push(y[d])}if(k&&this.sort({silent:!0}),!b.silent){for(d=0,e=r.length;e>d;d++)(h=r[d]).trigger("add",h,this,b);(k||x&&x.length)&&this.trigger("sort",this,b)}return c?a[0]:a},reset:function(a,b){b||(b={});for(var c=0,d=this.models.length;d>c;c++)this._removeReference(this.models[c]);return b.previousModels=this.models,this._reset(),a=this.add(a,f.extend({silent:!0},b)),b.silent||this.trigger("reset",this,b),a},push:function(a,b){return this.add(a,f.extend({at:this.length},b))},pop:function(a){var b=this.at(this.length-1);return this.remove(b,a),b},unshift:function(a,b){return this.add(a,f.extend({at:0},b))},shift:function(a){var b=this.at(0);return this.remove(b,a),b},slice:function(){return e.apply(this.models,arguments)},get:function(a){return null==a?void 0:this._byId[a.id]||this._byId[a.cid]||this._byId[a]},at:function(a){return this.models[a]},where:function(a,b){return f.isEmpty(a)?b?void 0:[]:this[b?"find":"filter"](function(b){for(var c in a)if(a[c]!==b.get(c))return!1;return!0})},findWhere:function(a){return this.where(a,!0)},sort:function(a){if(!this.comparator)throw new Error("Cannot sort a set without a comparator");return a||(a={}),f.isString(this.comparator)||1===this.comparator.length?this.models=this.sortBy(this.comparator,this):this.models.sort(f.bind(this.comparator,this)),a.silent||this.trigger("sort",this,a),this},pluck:function(a){return f.invoke(this.models,"get",a)},fetch:function(a){a=a?f.clone(a):{},void 0===a.parse&&(a.parse=!0);var b=a.success,c=this;return a.success=function(d){var e=a.reset?"reset":"set";c[e](d,a),b&&b(c,d,a),c.trigger("sync",c,d,a)},K(this,a),this.sync("read",this,a)},create:function(a,b){if(b=b?f.clone(b):{},!(a=this._prepareModel(a,b)))return!1;b.wait||this.add(a,b);var c=this,d=b.success;return b.success=function(a,b,e){e.wait&&c.add(a,e),d&&d(a,b,e)},a.save(null,b),a},parse:function(a){return a},clone:function(){return new this.constructor(this.models)},_reset:function(){this.length=0,this.models=[],this._byId={}},_prepareModel:function(a,b){if(a instanceof l)return a.collection||(a.collection=this),a;b=b?f.clone(b):{},b.collection=this;var c=new this.model(a,b);return c.validationError?(this.trigger("invalid",this,c.validationError,b),!1):c},_removeReference:function(a){this===a.collection&&delete a.collection,a.off("all",this._onModelEvent,this)},_onModelEvent:function(a,b,c,d){("add"!==a&&"remove"!==a||c===this)&&("destroy"===a&&this.remove(b,d),b&&a==="change:"+b.idAttribute&&(delete this._byId[b.previous(b.idAttribute)],null!=b.id&&(this._byId[b.id]=b)),this.trigger.apply(this,arguments))}});var q=["forEach","each","map","collect","reduce","foldl","inject","reduceRight","foldr","find","detect","filter","select","reject","every","all","some","any","include","contains","invoke","max","min","toArray","size","first","head","take","initial","rest","tail","drop","last","without","difference","indexOf","shuffle","lastIndexOf","isEmpty","chain"];f.each(q,function(a){n.prototype[a]=function(){var b=e.call(arguments);return b.unshift(this.models),f[a].apply(f,b)}});var r=["groupBy","countBy","sortBy"];f.each(r,function(a){n.prototype[a]=function(b,c){var d=f.isFunction(b)?b:function(a){return a.get(b)};return f[a](this.models,d,c)}});var s=a.View=function(a){this.cid=f.uniqueId("view"),a||(a={}),f.extend(this,f.pick(a,u)),this._ensureElement(),this.initialize.apply(this,arguments),this.delegateEvents()},t=/^(\S+)\s*(.*)$/,u=["model","collection","el","id","attributes","className","tagName","events"];f.extend(s.prototype,g,{tagName:"div",$:function(a){return this.$el.find(a)},initialize:function(){},render:function(){return this},remove:function(){return this.$el.remove(),this.stopListening(),this},setElement:function(b,c){return this.$el&&this.undelegateEvents(),this.$el=b instanceof a.$?b:a.$(b),this.el=this.$el[0],c!==!1&&this.delegateEvents(),this},delegateEvents:function(a){if(!a&&!(a=f.result(this,"events")))return this;this.undelegateEvents();for(var b in a){var c=a[b];if(f.isFunction(c)||(c=this[a[b]]),c){var d=b.match(t),e=d[1],g=d[2];c=f.bind(c,this),e+=".delegateEvents"+this.cid,""===g?this.$el.on(e,c):this.$el.on(e,g,c)}}return this},undelegateEvents:function(){return this.$el.off(".delegateEvents"+this.cid),this},_ensureElement:function(){if(this.el)this.setElement(f.result(this,"el"),!1);else{var b=f.extend({},f.result(this,"attributes"));this.id&&(b.id=f.result(this,"id")),this.className&&(b["class"]=f.result(this,"className"));var c=a.$("<"+f.result(this,"tagName")+">").attr(b);this.setElement(c,!1)}}}),a.sync=function(b,c,d){var e=w[b];f.defaults(d||(d={}),{emulateHTTP:a.emulateHTTP,emulateJSON:a.emulateJSON});var g={type:e,dataType:"json"};if(d.url||(g.url=f.result(c,"url")||J()),null!=d.data||!c||"create"!==b&&"update"!==b&&"patch"!==b||(g.contentType="application/json",g.data=JSON.stringify(d.attrs||c.toJSON(d))),d.emulateJSON&&(g.contentType="application/x-www-form-urlencoded",g.data=g.data?{model:g.data}:{}),d.emulateHTTP&&("PUT"===e||"DELETE"===e||"PATCH"===e)){g.type="POST",d.emulateJSON&&(g.data._method=e);var h=d.beforeSend;d.beforeSend=function(a){return a.setRequestHeader("X-HTTP-Method-Override",e),h?h.apply(this,arguments):void 0}}"GET"===g.type||d.emulateJSON||(g.processData=!1),"PATCH"===g.type&&v&&(g.xhr=function(){return new ActiveXObject("Microsoft.XMLHTTP")});var i=d.xhr=a.ajax(f.extend(g,d));return c.trigger("request",c,i,d),i};var v=!("undefined"==typeof window||!window.ActiveXObject||window.XMLHttpRequest&&(new XMLHttpRequest).dispatchEvent),w={create:"POST",update:"PUT",patch:"PATCH","delete":"DELETE",read:"GET"};a.ajax=function(){return a.$.ajax.apply(a.$,arguments)};var x=a.Router=function(a){a||(a={}),a.routes&&(this.routes=a.routes),this._bindRoutes(),this.initialize.apply(this,arguments)},y=/\((.*?)\)/g,z=/(\(\?)?:\w+/g,A=/\*\w+/g,B=/[\-{}\[\]+?.,\\\^$|#\s]/g;f.extend(x.prototype,g,{initialize:function(){},route:function(b,c,d){f.isRegExp(b)||(b=this._routeToRegExp(b)),f.isFunction(c)&&(d=c,c=""),d||(d=this[c]);var e=this;return a.history.route(b,function(f){var g=e._extractParameters(b,f);d&&d.apply(e,g),e.trigger.apply(e,["route:"+c].concat(g)),e.trigger("route",c,g),a.history.trigger("route",e,c,g)}),this},navigate:function(b,c){return a.history.navigate(b,c),this},_bindRoutes:function(){if(this.routes){this.routes=f.result(this,"routes");for(var a,b=f.keys(this.routes);null!=(a=b.pop());)this.route(a,this.routes[a])}},_routeToRegExp:function(a){return a=a.replace(B,"\\$&").replace(y,"(?:$1)?").replace(z,function(a,b){return b?a:"([^/]+)"}).replace(A,"(.*?)"),new RegExp("^"+a+"$")},_extractParameters:function(a,b){var c=a.exec(b).slice(1);return f.map(c,function(a){return a?decodeURIComponent(a):null})}});var C=a.History=function(){this.handlers=[],f.bindAll(this,"checkUrl"),"undefined"!=typeof window&&(this.location=window.location,this.history=window.history)},D=/^[#\/]|\s+$/g,E=/^\/+|\/+$/g,F=/msie [\w.]+/,G=/\/$/,H=/[?#].*$/;C.started=!1,f.extend(C.prototype,g,{interval:50,getHash:function(a){var b=(a||this).location.href.match(/#(.*)$/);return b?b[1]:""},getFragment:function(a,b){if(null==a)if(this._hasPushState||!this._wantsHashChange||b){a=this.location.pathname;var c=this.root.replace(G,"");a.indexOf(c)||(a=a.slice(c.length))}else a=this.getHash();return a.replace(D,"")},start:function(b){if(C.started)throw new Error("Backbone.history has already been started");C.started=!0,this.options=f.extend({root:"/"},this.options,b),this.root=this.options.root,this._wantsHashChange=this.options.hashChange!==!1,this._wantsPushState=!!this.options.pushState,this._hasPushState=!!(this.options.pushState&&this.history&&this.history.pushState);var c=this.getFragment(),d=document.documentMode,e=F.exec(navigator.userAgent.toLowerCase())&&(!d||7>=d);this.root=("/"+this.root+"/").replace(E,"/"),e&&this._wantsHashChange&&(this.iframe=a.$('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo("body")[0].contentWindow,this.navigate(c)),this._hasPushState?a.$(window).on("popstate",this.checkUrl):this._wantsHashChange&&"onhashchange"in window&&!e?a.$(window).on("hashchange",this.checkUrl):this._wantsHashChange&&(this._checkUrlInterval=setInterval(this.checkUrl,this.interval)),this.fragment=c;var g=this.location,h=g.pathname.replace(/[^\/]$/,"$&/")===this.root;if(this._wantsHashChange&&this._wantsPushState){if(!this._hasPushState&&!h)return this.fragment=this.getFragment(null,!0),this.location.replace(this.root+this.location.search+"#"+this.fragment),!0;this._hasPushState&&h&&g.hash&&(this.fragment=this.getHash().replace(D,""),this.history.replaceState({},document.title,this.root+this.fragment+g.search))}return this.options.silent?void 0:this.loadUrl()},stop:function(){a.$(window).off("popstate",this.checkUrl).off("hashchange",this.checkUrl),clearInterval(this._checkUrlInterval),C.started=!1},route:function(a,b){this.handlers.unshift({route:a,callback:b})},checkUrl:function(){var a=this.getFragment();return a===this.fragment&&this.iframe&&(a=this.getFragment(this.getHash(this.iframe))),a===this.fragment?!1:(this.iframe&&this.navigate(a),void this.loadUrl())},loadUrl:function(a){return a=this.fragment=this.getFragment(a),f.any(this.handlers,function(b){return b.route.test(a)?(b.callback(a),!0):void 0})},navigate:function(a,b){if(!C.started)return!1;b&&b!==!0||(b={trigger:!!b});var c=this.root+(a=this.getFragment(a||""));if(a=a.replace(H,""),this.fragment!==a){if(this.fragment=a,""===a&&"/"!==c&&(c=c.slice(0,-1)),this._hasPushState)this.history[b.replace?"replaceState":"pushState"]({},document.title,c);else{if(!this._wantsHashChange)return this.location.assign(c);this._updateHash(this.location,a,b.replace),this.iframe&&a!==this.getFragment(this.getHash(this.iframe))&&(b.replace||this.iframe.document.open().close(),this._updateHash(this.iframe.location,a,b.replace))}return b.trigger?this.loadUrl(a):void 0}},_updateHash:function(a,b,c){if(c){var d=a.href.replace(/(javascript:|#).*$/,"");a.replace(d+"#"+b)}else a.hash="#"+b}}),a.history=new C;var I=function(a,b){var c,d=this;c=a&&f.has(a,"constructor")?a.constructor:function(){return d.apply(this,arguments)},f.extend(c,d,b);var e=function(){this.constructor=c};return e.prototype=d.prototype,c.prototype=new e,a&&f.extend(c.prototype,a),c.__super__=d.prototype,c};l.extend=n.extend=x.extend=s.extend=C.extend=I;var J=function(){throw new Error('A "url" property or function must be specified')},K=function(a,b){var c=b.error;b.error=function(d){c&&c(a,d,b),a.trigger("error",a,d,b)}}}).call(this); |