UNPKG

nestedreact

Version:

Advanced models, state management, and data binding solution for React

3 lines (2 loc) 17.8 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("nestedtypes"),require("prop-types"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","react","nestedtypes","prop-types","react-dom"],e):e(t.ReactMVx={},t.React,t.Nested,t.PropTypes,t.ReactDOM)}(this,function(t,e,n,o,r){"use strict";function i(t,e){function n(){this.constructor=t}_(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function s(t,e,n,o){var r,i=arguments.length,s=i<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,o);else for(var u=t.length-1;u>=0;u--)(r=t[u])&&(s=(i<3?r(s):i>3?r(e,n,s):r(e,n))||s);return i>3&&s&&Object.defineProperty(e,n,s),s}function u(t){var e,r,i,s={},u=n.Record.defaults(t).prototype;return u.forEachAttr(u._attributes,function(t,n){if("id"!==n){var u=t.value,a=t.type,p=t.options;if(s[n]=function(t,e){var n=function(t){switch(t){case Number:case Number.integer:return o.number;case String:return o.string;case Boolean:return o.bool;case Array:return o.array;case Function:return o.func;case Object:return o.object;case O:return o.node;case R:return o.element;case void 0:case null:return o.any;default:return o.instanceOf(t)}}(t);return e?n.isRequired:n}(a,p.isRequired),p._onChange&&(r||(r={}),r[n]=function(t){return"function"==typeof t?t:function(e,n){this[t]&&this[t](e,n)}}(p._onChange)),p.changeHandlers&&p.changeHandlers.length&&(i||(i={}),i[n]=p.changeHandlers),p.changeEvents){i||(i={});var c=i[n]||(i[n]=[]),l="string"==typeof p.changeEvents?p.changeEvents:null;c.push(function(t,e,n){e&&n.stopListening(e),t&&n.listenTo(t,l||t._changeEventName,n.asyncUpdate)})}void 0!==u&&(e||(e={}),e[n]=t.convert(u,void 0,null,{}))}}),{propTypes:s,defaults:e,watchers:r,changeHandlers:i}}function a(t){var e=Object.keys(t),n=new Function("p","s","\n var v;\n this._s = s && s._changeToken;\n "+e.map(function(t){return"\n this."+t+" = ( ( v = p."+t+") && v._changeToken ) || v;\n "}).join("")+"\n ");return n.prototype._hasChanges=new Function("p","s","\n var v;\n return ( ( s && s._changeToken ) !== this._s ) "+e.map(function(t){return" ||\n this."+t+" !== ( ( ( v = p."+t+") && v._changeToken ) || v )\n "}).join("")+";\n "),n}function p(){this._propsChangeTokens=new this.PropsChangeTokens(this.props,this.state)}function c(t,e,n){var o=t._changeHandlers;for(var r in o)if(e[r]!==n[r])for(var i=0,s=o[r];i<s.length;i++){(0,s[i])(n[r],e[r],t)}}function l(t,e){(function(t,e){var o=t.store,r=t.Store;if(o&&o instanceof n.Store)this.prototype.store=o,this.mixins.merge([S,x]);else if(o||t.Store){if("function"==typeof o&&(r=o,o=void 0),o){var u=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return i(e,t),e}(r||this.prototype.Store||n.Store);u.attrbutes=o,u=s([n.define],u),this.prototype.Store=u}else r&&(this.prototype.Store=r);this.mixins.merge([k,b,x])}}).call(this,t,e),function(t,e){var o=this.prototype,r=t.state,u=t.State;if("function"==typeof r&&(u=r,r=void 0),r){var a=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return i(e,t),e}(u||o.State||n.Record);a.attributes=r,a=s([n.define],a),o.State=a}else u&&(o.State=u);(r||u)&&this.mixins.merge([w,b])}.call(this,t,e),function(t,e){var o=t.context,r=t.childContext,i=this.prototype;o&&(i._context=n.tools.defaults(o,e.prototype._context||{}),this.contextTypes=u(o).propTypes),r&&(i._childContext=n.tools.defaults(r,e.prototype._childContext),this.childContextTypes=u(r).propTypes)}.call(this,t,e),function(t,e){var o=t.props,r=t.pureRender,i=this.prototype;if(o){i._props=n.tools.defaults(o,e.prototype._props||{});var s=u(o),p=s.propTypes,c=s.defaults,l=s.watchers,h=s.changeHandlers;this.propTypes=p,c&&(this.defaultProps=c),l&&(i._watchers=l,this.mixins.merge([T])),h&&(i._changeHandlers=h,this.mixins.merge([U])),i.pureRender&&(i.PropsChangeTokens=a(o))}r&&this.mixins.merge([j])}.call(this,t,e),n.Messenger.onDefine.call(this,t,e)}function h(t){if(t&&"object"==typeof t)switch(Object.getPrototypeOf(t)){case D:return W;case M:return V}return A}function f(t){return t._links||(t._links=new t.AttributesCopy({}))}function d(t,e,n){var o=t[n],r=e[n];return o&&o.value===r?o:t[n]=new K(e,n,r)}function v(){return!1}function m(e){var n=e.statics,o=function(t,e){var n={};for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(n[o]=t[o]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols)for(var r=0,o=Object.getOwnPropertySymbols(t);r<o.length;r++)e.indexOf(o[r])<0&&(n[o[r]]=t[o[r]]);return n}(e,["statics"]),r=[],i=t.Component.extend(C({constructor:function(){t.Component.apply(this,arguments);for(var e=0,n=r;e<n.length;e++){var o=n[e];this[o]=this[o].bind(this)}}},o),n),s=i.prototype;for(var u in s)s.hasOwnProperty(u)&&-1===Y.indexOf(u)&&"function"==typeof s[u]&&r.push(u);return i}function y(t){tt=function(t){var o=t.prototype.dispose||function(){},i=t.prototype.setElement,s=t.extend({reactClass:null,props:{},element:null,initialize:function(t){this.options=t||{}},setElement:function(){return this.unmountComponent(!0),i.apply(this,arguments)},component:null,prevState:null,resize:function(){window.Page.forceResize()},render:function(){var t=this.prevState?n.tools.fastAssign({__keepState:this.prevState},this.options):this.options,o=e.createElement(this.reactClass,t),i=r.render(o,this.el);this.component||this.mountComponent(i)},mountComponent:function(t){this.component=t,this.prevState=null,t.trigger&&this.listenTo(t,"all",function(){this.trigger.apply(this,arguments)})},unmountComponent:function(t){var e=this.component;e&&(this.prevState=e.state,e.trigger&&this.stopListening(e),e._preventDispose=Boolean(t),r.unmountComponentAtNode(this.el),this.component=null)},dispose:function(){return this.unmountComponent(),o.apply(this,arguments)}});return Object.defineProperty(s.prototype,"model",{get:function(){return this.component||this.render(),this.component&&this.component.state}}),s}(t)}var g="default"in n?n.default:n,_=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])},C=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++){e=arguments[n];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},w={_initializeState:function(){var t=this.state=this.props.__keepState||new this.State;t._owner=this,t._ownerKey="state"},context:{_nestedStore:n.Store},getStore:function(){var t,e;return(t=this.context)&&t._nestedStore||(e=this.state)&&e._defaultStore},componentWillUnmount:function(){var t=this.state;t._owner=t._ownerKey=void 0,this._preventDispose||t.dispose(),this.state=void 0}},b={_onChildrenChange:function(){},componentDidMount:function(){this._onChildrenChange=this.asyncUpdate}},x={childContext:{_nestedStore:n.Store},getChildContext:function(){return{_nestedStore:this.store}},getStore:function(){return this.store},get:function(t){var e=w.getStore.call(this,t);return e&&e.get(t)}},S={componentDidMount:function(){this.listenTo(this.store,"change",this.asyncUpdate)}},k={componentWillMount:function(){var t=this.store=new this.Store;t._owner=this,t._ownerKey="store"},componentWillUnmount:function(){this.store._ownerKey=this.store._owner=void 0,this.store.dispose(),this.store=void 0}},O=function(){return function(){}}(),R=function(){return function(){}}(),P=a({}),j={shouldComponentUpdate:function(t){return this._propsChangeTokens._hasChanges(t)},componentDidMount:p,componentDidUpdate:p},U={componentDidMount:function(){c(this,{},this.props)},componentDidUpdate:function(t){c(this,t,this.props)},componentWillUnmount:function(){c(this,this.props,{})}},T={componentWillReceiveProps:function(t){var e=this._watchers,n=this.props;for(var o in e)t[o]!==n[o]&&e[o].call(this,t[o],o)},componentWillMount:function(){var t=this._watchers,e=this.props;for(var n in t)t[n].call(this,e[n],n)}},D=Array.prototype,M=Object.prototype,A={clone:function(t){return t},map:function(t,e){return[]},remove:function(t){return t}},V={map:function(t,e){var n=[];for(var o in t.value){var r=e(t.at(o),o);void 0===r||n.push(r)}return n},remove:function(t,e){return delete t[e],t},clone:function(t){var e={};for(var n in t)e[n]=t[n];return e}},W={clone:function(t){return t.slice()},remove:function(t,e){return t.splice(e,1),t},map:function(t,e){for(var n=t.value.length,o=Array(n),r=0,i=0;r<n;r++){var s=e(t.at(r),r);void 0===s||(o[i++]=s)}return o.length===i||(o.length=i),o}},E=function(){function t(t){this.value=t}return t.value=function(t,e){return new L(t,e)},Object.defineProperty(t.prototype,"validationError",{get:function(){return this.error},enumerable:!0,configurable:!0}),t.prototype.onChange=function(t){var e=this;return new N(this,function(n){t(n),e.set(n)})},Object.defineProperty(t.prototype,"props",{get:function(){var t=this;return"boolean"==typeof this.value?{checked:this.value,onChange:function(e){return t.set(Boolean(e.target.checked))}}:{value:this.value,onChange:function(e){return t.set(e.target.value)}}},enumerable:!0,configurable:!0}),t.prototype.requestChange=function(t){this.set(t)},t.prototype.update=function(t,e){var n=t(this.clone(),e);void 0===n||this.set(n)},t.prototype.pipe=function(t){var e=this;return new N(this,function(n){var o=t(n,e.value);void 0===o||e.set(o)})},t.prototype.action=function(t){var e=this;return function(n){return e.update(t,n)}},t.prototype.equals=function(t){return new q(this,t)},t.prototype.enabled=function(t){return new H(this,t||"")},t.prototype.contains=function(t){return new z(this,t)},t.prototype.push=function(){var t=W.clone(this.value);Array.prototype.push.apply(t,arguments),this.set(t)},t.prototype.unshift=function(){var t=W.clone(this.value);Array.prototype.unshift.apply(t,arguments),this.set(t)},t.prototype.splice=function(){var t=W.clone(this.value);Array.prototype.splice.apply(t,arguments),this.set(t)},t.prototype.map=function(t){return h(this.value).map(this,t)},t.prototype.removeAt=function(t){var e=this.value,n=h(e);this.set(n.remove(n.clone(e),t))},t.prototype.at=function(t){return new B(this,t)},t.prototype.clone=function(){var t=this.value;return h(t).clone(t)},t.prototype.pick=function(){for(var t={},e=0;e<arguments.length;e++){var n=arguments[e];t[n]=new B(this,n)}return t},t.prototype.check=function(t,e){return this.error||t(this.value)||(this.error=e||t.error||F),this},t}(),L=function(t){function e(e,n){var o=t.call(this,e)||this;return o.set=n,o}return i(e,t),e.prototype.set=function(t){},e}(E),N=function(t){function e(e,n){var o=t.call(this,e.value)||this;o.set=n;var r=e.error;return r&&(o.error=r),o}return i(e,t),e.prototype.set=function(t){},e}(E),q=function(t){function e(e,n){var o=t.call(this,e.value===n)||this;return o.parent=e,o.truthyValue=n,o}return i(e,t),e.prototype.set=function(t){this.parent.set(t?this.truthyValue:null)},e}(E),H=function(t){function e(e,n){var o=t.call(this,null!=e.value)||this;return o.parent=e,o.defaultValue=n,o}return i(e,t),e.prototype.set=function(t){this.parent.set(t?this.defaultValue:null)},e}(E),z=function(t){function e(e,n){var o=t.call(this,e.value.indexOf(n)>=0)||this;return o.parent=e,o.element=n,o}return i(e,t),e.prototype.set=function(t){var e=this,n=Boolean(t);if(this.value!==n){var o=this.parent.value,r=t?o.concat(this.element):o.filter(function(t){return t!==e.element});this.parent.set(r)}},e}(E),F="Invalid value",B=function(t){function e(e,n){var o=t.call(this,e.value[n])||this;return o.parent=e,o.key=n,o}return i(e,t),e.prototype.remove=function(){this.parent.removeAt(this.key)},e.prototype.set=function(t){var e=this;this.value!==t&&this.parent.update(function(n){return n[e.key]=t,n})},e}(E);n.Mixable.mixins.populate(E),n.MixinsState.get(n.Record).merge([{linkAt:function(t){return d(f(this),this,t)},linkPath:function(t,e){return new $(this,t,e)},linkAll:function(){var t=f(this);if(arguments.length)for(var e=0;e<arguments.length;e++)d(t,this,arguments[e]);else{var n=this.attributes;for(var o in n)void 0===n[o]||d(t,this,o)}return t}}]);var K=function(t){function e(e,n,o){var r=t.call(this,o)||this;return r.record=e,r.attr=n,r}return i(e,t),e.prototype.set=function(t){this.record[this.attr]=t},Object.defineProperty(e.prototype,"error",{get:function(){return void 0===this._error?this.record.getValidationError(this.attr):this._error},set:function(t){this._error=t},enumerable:!0,configurable:!0}),e}(E),$=function(t){function e(e,n,o){var r=t.call(this,e.deepGet(n))||this;return r.record=e,r.path=n,r.options=o,r}return i(e,t),Object.defineProperty(e.prototype,"error",{get:function(){return void 0===this._error&&(this._error=this.record.deepValidationError(this.path)||null),this._error},set:function(t){this._error=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"_changeToken",{get:function(){return this.record._changeToken},enumerable:!0,configurable:!0}),e.prototype.set=function(t){this.record.deepSet(this.path,t,this.options)},e}(E);n.MixinsState.get(n.Record.Collection).merge([{linkContains:function(t){return new I(this,t)},linkAt:function(t){var e=this;return E.value(this[t],function(n){return e[t]=n})}}]);var I=function(t){function e(e,n){var o=t.call(this,Boolean(e._byId[n.cid]))||this;return o.collection=e,o.record=n,o}return i(e,t),e.prototype.set=function(t){this.collection.toggle(this.record,t)},e}(E);t.Component=function(t){function e(e,n){var o=t.call(this,e,n)||this;return o._initializeState(),o}return i(e,t),e.prototype.linkAt=function(t){return this.state.linkAt(t)},e.prototype.linkAll=function(){var t=this.state;return t.linkAll.apply(t,arguments)},e.prototype.linkPath=function(t){return this.state.linkPath(t)},Object.defineProperty(e.prototype,"links",{get:function(){return this.state._links},enumerable:!0,configurable:!0}),e.prototype._initializeState=function(){this.state=null},e.prototype.assignToState=function(t,e){this.state.assignFrom((n={},n[e]=t,n));var n},e.prototype.componentWillUnmount=function(){this.dispose()},e.prototype.transaction=function(t){var e=this.shouldComponentUpdate,n=e!==v;n&&(this.shouldComponentUpdate=v);var o=this.state,r=this.store,i=r?function(e){return r.transaction(function(){return t(e)})}:t;o?o.transaction(i):i(o),n&&(this.shouldComponentUpdate=e,this.asyncUpdate())},e.prototype.asyncUpdate=function(){this.shouldComponentUpdate===v||this._disposed||this.forceUpdate()},e}(e.Component),t.Component.onDefine=l,t.Component=s([n.define({PropsChangeTokens:P}),n.definitions({state:n.mixinRules.merge,State:n.mixinRules.value,store:n.mixinRules.merge,Store:n.mixinRules.value,props:n.mixinRules.merge,context:n.mixinRules.merge,childContext:n.mixinRules.merge,pureRender:n.mixinRules.protoValue}),n.mixinRules({componentWillMount:n.mixinRules.classLast,componentDidMount:n.mixinRules.classLast,componentWillReceiveProps:n.mixinRules.classLast,componentWillUpdate:n.mixinRules.classLast,componentDidUpdate:n.mixinRules.classLast,componentWillUnmount:n.mixinRules.classFirst,shouldComponentUpdate:n.mixinRules.some,getChildContext:n.mixinRules.defaults}),n.mixins(n.Messenger)],t.Component),Object.defineProperty(t.Component.prototype,"isMounted",{value:function(){return!this._disposed}});var G=Object.create(e);G.default=G,G.define=n.define,G.mixins=n.mixins,G.Node=O.value(null),G.Element=R.value(null),G.Link=E,G.Component=t.Component;var J=G.assignToState=function(t){return function(e){this.state.assignFrom((n={},n[t]=e&&e instanceof Link?e.value:e,n));var n}},Q=n.tools.notEqual,X=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.saveRef=function(t){e.root=t},e}return i(e,t),e.prototype.shouldComponentUpdate=function(t){var e=this.props;return t.View!==e.View||Q(t.options,e.options)},e.prototype.hasUnsavedChanges=function(){var t=this.view;return t&&("function"==typeof t.hasUnsavedChanges?t.hasUnsavedChanges():t.hasUnsavedChanges)},e.prototype.render=function(){return G.createElement("div",{ref:this.saveRef,className:this.props.className})},e.prototype.componentDidMount=function(){this._mountView()},e.prototype.componentDidUpdate=function(){this._dispose(),this._mountView()},e.prototype.componentWillUnmount=function(){this._dispose()},e.prototype._mountView=function(){var t=this.root,e=this.props,n=this.view=e.options?new e.View(e.options):new e.View;t.appendChild(n.el),n.render()},e.prototype._dispose=function(){var t=this.view;t&&(t.dispose?t.dispose():(t.stopListening(),t.off()),this.root.innerHTML="",this.view=null)},e}(t.Component);window.Page||(window.Page={forceResize:function(){}});var Y=["State","Store","constructor","componentWillMount","componentDidMount","componentWillReceiveProps","shouldComponentUpdate","componentWillUpdate","componentDidUpdate","componentWillUnmount","render","getDefaultProps","getChildContext"],Z=Object.create(G);Z.subview=X,Object.defineProperty(Z,"createClass",{value:m}),Object.defineProperty(Z,"PropTypes",{value:o});var tt,et=Z.Component.onDefine;Z.Component.onDefine=function(t,e){return this.View=tt.extend({reactClass:this}),et.call(this,t,e)};var nt=n.Record.prototype;nt.getLink=nt.linkAt,nt.deepLink=nt.linkPath;var ot=n.Record.Collection.prototype;ot.hasLink=ot.linkContains,y(n.View);var rt={el:{get:function(){return r.findDOMNode(this)}},$el:{get:function(){return g.$(this.el)}},$:{value:function(t){return this.$el.find(t)}}};Object.defineProperties(Z.Component.prototype,rt),t.default=Z,t.subview=X,t.PropTypes=o,t.createClass=m,t.useView=y,t.define=n.define,t.mixins=n.mixins,t.Node=O,t.Element=R,t.Link=E,t.assignToState=J,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=index.min.js.map