/*
Copyright 2013, KISSY UI Library v1.31
MIT Licensed
build time: Jul 24 20:24
*/
KISSY.add("mvc/collection",function(c,j,f,i){function b(){b.superclass.constructor.apply(this,arguments)}b.ATTRS={model:{value:f},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,i,{sort:function(){var a=this.get("comparator");a&&this.get("models").sort(function(k,b){return a(k)-
a(b)})},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 g=[].concat(a);c.each(g,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 f||(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 g=b.get("parse").call(b,e);g&&b.set("models",g,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 f=e(a),c=0;c<d.length;c++){var h=e(d[c]);if(f<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 e=b[d];if(e.getId()===a)return e}return null},
getByCid:function(a){for(var b=this.get("models"),d=0;d<b.length;d++){var e=b[d];if(e.get("clientId")===a)return e}return null}});return b},{requires:["event","./model","base"]});
KISSY.add("mvc/model",function(c,j){function f(){f.superclass.constructor.apply(this,arguments);this.collections={}}var i="idAttribute,clientId,urlRoot,url,parse,sync".split(",");c.extend(f,j,{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)},setInternal:function(){this.__isModified=
1;return f.superclass.setInternal.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 g=a.get("parse").call(a,d);g&&a.set(g,b)}for(var f in e)e[f].remove(a,b),e[f]&&a.removeFromCollection(e[f]);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(i,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,c=this.collections;for(b in c)if(c.hasOwnProperty(b)){a=c[b];break}b=a;var d;b=(b&&(d=b.get("url"))?"string"==typeof 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 f},{requires:["base"]});KISSY.add("mvc",function(c,j,f,i,b,a){return{sync:a,Model:j,View:i,Collection:f,Router:b}},{requires:["mvc/model","mvc/collection","mvc/view","mvc/router","mvc/sync"]});
KISSY.add("mvc/router",function(c,j,f){function i(a){var b,d;for(d=0;d<a.length;d++)if(b=a.charAt(d),"\\"==b)d++;else if("("==b)return d;throw Error("impossible to not to get capture group in kissy mvc route");}function b(a){a=a||location.href;if(h.nativeHistory&&o){var a=new c.Uri(a),b=a.getQuery().toString();return a.getPath().substr(h.urlRoot.length)+(b?"?"+b:"")}return(new c.Uri(a)).getFragment().replace(/^!/,"")}function a(a){c.endsWith(a,"/")&&(a=a.substring(0,a.length-1));return a}function k(a){a&&
(c.startsWith(a,"/")&&(a=a.substring(1)),a="/"+a);return a}function d(b,d){b=a(b);d=a(d);return b==d}function e(){var a,d,e=0,f=-1,g="",k=-1,h=0,j="";a=new c.Uri(b());var t=0;d=a.clone();d.query.reset();d=d.toString();m(r,function(a){var b=0;m(a[l],function(c){var s=c.regStr,o=c.paramNames,l=-1,n,p=c.name,q=c.callback;if(n=d.match(c.reg)){n.shift();var r=function(){if(o){var a={};m(n,function(b,d){a[o[d]]=b});return a}return[].concat(n)},c=function(){g=s;k=l;h=q;t=r();e=a;j=p;f=n.length};if(n.length)if(s)l=
i(s),l>k?c():l==k&&f>=n.length?n.length<f?c():s.length>g.length&&c():e||c();else return c(),b=1,!1;else return c(),b=1,!1}});if(b)return!1});t&&(a=a.query.get(),h.apply(e,[t,a,{path:d,url:location.href}]),a={name:j,paths:t,path:d,url:location.href,query:a},e.fire("route:"+j,a),e.fire("route",a))}function g(a,b,d){var e=b,f=[];return c.isFunction(d)?(b=c.escapeRegExp(b),b=b.replace(w,function(a,b,d,c,e){f.push(d||e);if(d)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)&&"string"==typeof d.callback?a[d.callback]:d.callback}}function u(a){this[l]={};this.addRoutes(a.newVal)}function h(){h.superclass.constructor.apply(this,arguments);this.on("afterRoutesChange",u,this);u.call(this,{newVal:this.get("routes")});r.push(this)}var m=c.each,w=/(:([\w\d]+))|(\\\*([\w\d]+))/g,r=[],p=c.Env.host,v=p.document.documentMode||c.UA.ie,q=p.history,o=!(!q||!q.pushState),l="__routerMap";h.ATTRS={routes:{}};c.extend(h,
f,{addRoutes:function(a){var b=this;m(a,function(a,d){b[l][d]=g(b,d,!c.isFunction(a)&&"string"==typeof a?b[a]:a)})}},{hasRoute:function(a){var b=0;m(r,function(d){m(d[l],function(d){if(a.match(d.reg))return b=1,!1});if(b)return!1});return!!b},removeRoot:function(a){return(new c.Uri(a)).getPath().substr(h.urlRoot.length)},navigate:function(d,c){var c=c||{},f=c.replaceHistory,g;b()!==d?h.nativeHistory&&o?(q[f?"replaceState":"pushState"]({},"",location.protocol+"//"+location.host+a(h.urlRoot)+k(d)),
e()):(g="#!"+d,f?location.replace(g+(v&&8>v?j.REPLACE_HISTORY:"")):location.hash=g):c&&c.triggerRoute&&e()},start:function(c){c=c||{};if(h.__started)return c.success&&c.success();c.urlRoot=(c.urlRoot||"").replace(/\/$/,"");var f,g=c.nativeHistory,i=location.pathname,l=b(),m=location.hash.match(/#!.+/);f=h.urlRoot=c.urlRoot;if(h.nativeHistory=g)if(o)m&&d(i,f)&&(q.replaceState({},"",location.protocol+"//"+location.host+a(h.urlRoot)+k(l)),c.triggerRoute=1);else if(!d(i,f)){location.replace(a(f)+"/#!"+
l);return}setTimeout(function(){if(g&&o)j.on(p,"popstate",e);else j.on(p,"hashchange",e),c.triggerRoute=1;c.triggerRoute&&e();c.success&&c.success()},100);h.__started=1}});return h},{requires:["event","base"]});
KISSY.add("mvc/sync",function(c,j,f){var i={create:"POST",update:"POST","delete":"POST",read:"GET"};return function(b,a,k){var k=c.merge({type:i[a],dataType:"json"},k),d,e;d=k.data=k.data||{};d._method=a;k.url||(e=b.get("url"),k.url="string"==typeof e?e:e.call(b));if("create"==a||"update"==a)d.model=f.stringify(b.toJSON());return j(k)}},{requires:["ajax","json"]});
KISSY.add("mvc/view",function(c,j,f){function i(){i.superclass.constructor.apply(this,arguments);var a;(a=this.get("events"))&&this._afterEventsChange({newVal:a})}var b=j.all;i.ATTRS={el:{value:"<div />",getter:function(a){"string"==typeof a&&(a=b(a),this.setInternal("el",a));return a}},events:{}};c.extend(i,f,{_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 c=a[d],f;for(f in c)b.undelegate(f,
d,"string"==typeof c[f]?this[c[f]]:c[f],this)}},_addEvents:function(a){var b=this.get("el"),d;for(d in a){var c=a[d],f;for(f in c)b.delegate(f,d,"string"==typeof c[f]?this[c[f]]:c[f],this)}},render:function(){return this},destroy:function(){this.get("el").remove()}});return i},{requires:["node","base"]});
