/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:24
*/
KISSY.add("mvc/collection",function(c,k,g,h){function b(){b.superclass.constructor.apply(this,arguments)}b.ATTRS={model:{value:g},models:{setter:function(a){this.remove(this.get("models"),{silent:1});this.add(a,{silent:1});return this.get("models")},value:[]},url:{value:""},comparator:{},sync:{value:function(){c.require("mvc").sync.apply(this,arguments)}},parse:{value:function(a){return a}}};c.extend(b,h,{sort:function(){var a=this.get("comparator");a&&this.get("models").sort(function(b,d){return a(b)-
a(d)})},toJSON:function(){return c.map(this.get("models"),function(a){return a.toJSON()})},add:function(a,b){var d=this,e=!0;if(c.isArray(a)){var f=[].concat(a);c.each(f,function(a){a=d._add(a,b);e=e&&a})}else e=d._add(a,b);return e},remove:function(a,b){var d=this;if(c.isArray(a)){var e=[].concat(a);c.each(e,function(a){d._remove(a,b)})}else a&&d._remove(a,b)},at:function(a){return this.get("models")[a]},_normModel:function(a){var b=!0;a instanceof g||(b=a,a=new (this.get("model")),b=a.set(b,{silent:1}));
return b&&a},load:function(a){var b=this,a=a||{},d=a.success;a.success=function(e){if(e){var f=b.get("parse").call(b,e);f&&b.set("models",f,a)}c.each(b.get("models"),function(a){a.__isModified=0});d&&d.apply(this,arguments)};b.get("sync").call(b,b,"read",a);return b},create:function(a,b){var d=this,b=b||{};if(a=this._normModel(a)){a.addToCollection(d);var e=b.success;b.success=function(){d.add(a,b);e&&e()};a.save(b)}return a},_add:function(a,b){if(a=this._normModel(a)){var b=b||{},d=this.get("models"),
e=this.get("comparator"),c=d.length;if(e)for(var g=e(a),c=0;c<d.length;c++){var h=e(d[c]);if(g<h)break}this.get("models").splice(c,0,a);a.addToCollection(this);b.silent||this.fire("add",{model:a})}return a},_remove:function(a,b){var b=b||{},d=c.indexOf(a,this.get("models"));-1!=d&&(this.get("models").splice(d,1),a.removeFromCollection(this));b.silent||this.fire("remove",{model:a})},getById:function(a){for(var b=this.get("models"),d=0;d<b.length;d++){var c=b[d];if(c.getId()===a)return c}return null},
getByCid:function(a){for(var b=this.get("models"),d=0;d<b.length;d++){var c=b[d];if(c.get("clientId")===a)return c}return null}});return b},{requires:["event","./model","base"]});
KISSY.add("mvc/model",function(c,k){function g(){g.superclass.constructor.apply(this,arguments);this.publish("*Change",{bubbles:1});this.collections={}}var h="idAttribute,clientId,urlRoot,url,parse,sync".split(",");c.extend(g,k,{addToCollection:function(b){this.collections[c.stamp(b)]=b;this.addTarget(b)},removeFromCollection:function(b){delete this.collections[c.stamp(b)];this.removeTarget(b)},getId:function(){return this.get(this.get("idAttribute"))},setId:function(b){return this.set(this.get("idAttribute"),
b)},__set:function(){this.__isModified=1;return g.superclass.__set.apply(this,arguments)},isNew:function(){return!this.getId()},isModified:function(){return!(!this.isNew()&&!this.__isModified)},destroy:function(b){var a=this,b=b||{},c=b.success;b.success=function(d){var e=a.collections;if(d){var f=a.get("parse").call(a,d);f&&a.set(f,b)}for(var g in e)e[g].remove(a,b),a.removeFromCollection(e[g]);a.fire("destroy");c&&c.apply(this,arguments)};!a.isNew()&&b["delete"]?a.get("sync").call(a,a,"delete",
b):(b.success(),b.complete&&b.complete());return a},load:function(b){var a=this,b=b||{},c=b.success;b.success=function(d){if(d){var e=a.get("parse").call(a,d);e&&a.set(e,b)}a.__isModified=0;c&&c.apply(this,arguments)};a.get("sync").call(a,a,"read",b);return a},save:function(b){var a=this,b=b||{},c=b.success;b.success=function(d){if(d){var e=a.get("parse").call(a,d);e&&a.set(e,b)}a.__isModified=0;c&&c.apply(this,arguments)};a.get("sync").call(a,a,a.isNew()?"create":"update",b);return a},toJSON:function(){var b=
this.getAttrVals();c.each(h,function(a){delete b[a]});return b}},{ATTRS:{idAttribute:{value:"id"},clientId:{valueFn:function(){return c.guid("mvc-client")}},url:{value:function(){var b,a,i=this.collections;for(b in i)if(i.hasOwnProperty(b)){a=i[b];break}b=a;var d;b=(b&&(d=b.get("url"))?c.isString(d)?d:d.call(b):d)||this.get("urlRoot");if(this.isNew())return b;b+="/"==b.charAt(b.length-1)?"":"/";return b+encodeURIComponent(this.getId())+"/"}},urlRoot:{value:""},sync:{value:function(){c.require("mvc").sync.apply(this,
arguments)}},parse:{value:function(b){return b}}}});return g},{requires:["base"]});KISSY.add("mvc",function(c,k,g,h,b,a){return{sync:a,Model:k,View:h,Collection:g,Router:b}},{requires:["mvc/model","mvc/collection","mvc/view","mvc/router","mvc/sync"]});
KISSY.add("mvc/router",function(c,k,g){function h(a){var b,c;for(c=0;c<a.length;c++)if(b=a.charAt(c),"\\"==b)c++;else if("("==b)return c;throw Error("impossible to not to get capture group in kissy mvc route");}function b(){return j.nativeHistory&&o?location.pathname.substr(j.urlRoot.length)+location.search:location.href.replace(/^[^#]*#?!?(.*)$/,"$1")}function a(a){c.endsWith(a,"/")&&(a=a.substring(0,a.length-1));return a}function i(a){c.startsWith(a,"/")&&(a=a.substring(1));return a}function d(b,
c){b=a(b);c=a(c);return b==c}function e(a){var b,d={};return(b=a.match(u))?c.unparam(b[1]):d}function f(){var a=b(),c=a,d=0,f=-1,i="",g=-1,j=0,k="",l=0,a=c.replace(u,"");t(v,function(b){var c=0;t(b[p],function(e){var s=e.regStr,o=e.paramNames,m=-1,n,p=e.name,q=e.callback;if(n=a.match(e.reg)){n.shift();var r=function(){if(o){var a={};t(n,function(b,c){a[o[c]]=b});return a}return[].concat(n)},e=function(){i=s;g=m;j=q;l=r();d=b;k=p;f=n.length};if(n.length)if(s)m=h(s),m>g?e():m==g&&f>=n.length?n.length<
f?e():s.length>i.length&&e():d||e();else return e(),c=1,!1;else return e(),c=1,!1}});if(c)return!1});l&&(c=e(c),j.apply(d,[l,c]),c={name:k,paths:l,query:c},d.fire("route:"+k,c),d.fire("route",c))}function l(a,b,d){var e=b,f=[];return c.isFunction(d)?(b=c.escapeRegExp(b),b=b.replace(w,function(a,b,c,d,e){f.push(c||e);if(c)return"([^/]+)";if(e)return"(.*)"}),{name:e,paramNames:f,reg:RegExp("^"+b+"$"),regStr:b,callback:d}):{name:e,reg:d.reg,callback:!c.isFunction(d.callback)&&c.isString(d.callback)?
a[d.callback]:d.callback}}function r(a){this[p]={};this.addRoutes(a.newVal)}function j(){j.superclass.constructor.apply(this,arguments);this.on("afterRoutesChange",r,this);r.call(this,{newVal:this.get("routes")});v.push(this)}var u=/\?(.*)/,t=c.each,w=/(:([\w\d]+))|(\\\*([\w\d]+))/g,v=[],q=c.Env.host,m=q.history,o=!(!m||!m.pushState),p="__routerMap";j.ATTRS={routes:{}};c.extend(j,g,{addRoutes:function(a){var b=this;t(a,function(a,d){b[p][d]=l(b,d,!c.isFunction(a)&&c.isString(a)?b[a]:a)})}},{navigate:function(c,
d){b()!==c?j.nativeHistory&&o?(m.pushState({},"",location.protocol+"//"+location.host+a(j.urlRoot)+("/"+i(c))),f()):location.hash="!"+c:d&&d.triggerRoute&&f()},start:function(c){if(!j.__started){c=c||{};c.urlRoot=c.urlRoot||"";var e,g=c.nativeHistory,h=location.pathname,l=b(),p=location.hash.match(/#!.+/);e=j.urlRoot=c.urlRoot;if(j.nativeHistory=g)if(o)p&&d(h,e)&&(m.replaceState({},"",location.protocol+"//"+location.host+a(j.urlRoot)+("/"+i(l))),c.triggerRoute=1);else if(!d(h,e)){location.replace(a(e)+
"/#!"+l);return}setTimeout(function(){if(g&&o)k.on(q,"popstate",f);else k.on(q,"hashchange",f),c.triggerRoute=1;c.triggerRoute&&f();c.success&&c.success()},100);j.__started=1}}});return j},{requires:["event","base"]});
KISSY.add("mvc/sync",function(c,k,g){var h={create:"POST",update:"POST","delete":"POST",read:"GET"};return function(b,a,i){var i=c.merge({type:h[a],dataType:"json"},i),d=i.data=i.data||{};d._method=a;i.url||(i.url=c.isString(b.get("url"))?b.get("url"):b.get("url").call(b));if("create"==a||"update"==a)d.model=g.stringify(b.toJSON());return k(i)}},{requires:["ajax","json"]});
KISSY.add("mvc/view",function(c,k,g){function h(){h.superclass.constructor.apply(this,arguments);var a;(a=this.get("events"))&&this._afterEventsChange({newVal:a})}var b=k.all;h.ATTRS={el:{value:"<div />",getter:function(a){c.isString(a)&&(a=b(a),this.__set("el",a));return a}},events:{}};c.extend(h,g,{_afterEventsChange:function(a){var b=a.prevVal;b&&this._removeEvents(b);this._addEvents(a.newVal)},_removeEvents:function(a){var b=this.get("el"),d;for(d in a){var e=a[d],f;for(f in e){var g=c.isString(e[f])?
this[e[f]]:e[f];b.undelegate(f,d,g,this)}}},_addEvents:function(a){var b=this.get("el"),d;for(d in a){var e=a[d],f;for(f in e){var g=c.isString(e[f])?this[e[f]]:e[f];b.delegate(f,d,g,this)}}},render:function(){return this},destroy:function(){this.get("el").remove()}});return h},{requires:["node","base"]});
