@beanreact/permission
Version:
Easy to process permission of Component
2 lines (1 loc) • 6.16 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((n=n||self).JSLib={},n.React)}(this,function(n,f){"use strict";var l="default"in f?f.default:f;function a(n){return(a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n})(n)}function u(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function")}function r(n,t){for(var e=0;e<t.length;e++){var r=t[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(n,r.key,r)}}function s(n,t,e){return t&&r(n.prototype,t),e&&r(n,e),n}function c(n,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");n.prototype=Object.create(t&&t.prototype,{constructor:{value:n,writable:!0,configurable:!0}}),t&&e(n,t)}function p(n){return(p=Object.setPrototypeOf?Object.getPrototypeOf:function(n){return n.__proto__||Object.getPrototypeOf(n)})(n)}function e(n,t){return(e=Object.setPrototypeOf||function(n,t){return n.__proto__=t,n})(n,t)}function y(n,t){return!t||"object"!=typeof t&&"function"!=typeof t?function(n){if(void 0===n)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return n}(n):t}function h(n,t,e){return(h="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(n,t,e){var r=function(n,t){for(;!Object.prototype.hasOwnProperty.call(n,t)&&null!==(n=p(n)););return n}(n,t);if(r){var o=Object.getOwnPropertyDescriptor(r,t);return o.get?o.get.call(e):o.value}})(n,t,e||n)}var m={UNSET:"unset",PENDING:"pending",ERROR:"error",DONE:"done"},d={AUTHORIZED:"authorized",DENIED:"denied"};function t(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";this.title="set permission error: ",this.message=n,this.toString=function(){return this.title+this.message}}function o(n){return Object.prototype.toString.call(n).match(/^\[object\s(.*)\]$/)[1]}function v(n){return"array"===o(n).toLowerCase()}function i(n){return"string"===o(n).toLowerCase()}function b(n){return"number"===o(n).toLowerCase()}function g(n){var t=!1;if(null==n)t=!0;else if((v(n)||i(n))&&0===n.length)t=!0;else if(function(n){return"object"===o(n).toLowerCase()}(n)){var e=!1;for(var r in n)if(n.hasOwnProperty(r)){e=!0;break}e||(t=!0)}else b(n)&&isNaN(n)&&(t=!0);return t}function O(n){return i(n)?n.trim():n}function P(n){return n&&"string"==typeof n.type}function E(n){return n&&"function"==typeof n.type}function D(n){var t=[];return v(n)?t=n:b(n)?t.push(n):i(n)&&(n.includes(",")?t=n.split(","):t.push(n)),t}var w,j,N=m.UNSET,R=[],_={onDenied:null,transformData:null,comparePermission:function(){for(var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:[],r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[],n=function(n){var t=e[n];if(!r.some(function(n){return O(t)==O(n)}))return{v:!1}},t=0;t<e.length;t++){var o=n(t);if("object"===a(o))return o.v}return!0}};function S(n,t){if(g(n))return!0;if(g(t))return!1;var e=D(n);return _.comparePermission(e,t)}function G(n){return D(_.transformData?_.transformData(n):n)}function k(n,t,e){var r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:0;if(N===m.DONE){var o=e&&e(n,t,r);return l.isValidElement(o)?o:void 0}}function C(n,r,o,t){if(n){if(P(n)||E(n)||function(n){return E(n)&&"_class"===n.type.name}(n)){var e=n.props["data-permission"]||n.props["data-permissions"]||n.props.permission||n.props.permissions;if(S(e,r)){var i=[],u=n.props.children;return u&&f.Children.forEach(u,function(n,t){var e=C(n,r,o,t);e&&i.push(e)}),0===i.length?i=null:1===i.length&&1===u.length&&(i=i[0]),l.cloneElement(n,{key:n.key||function(n){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:0;return P(n)?n.type:E(n)&&n.type.name,"permission__".concat(t)}(n,t)},i)}return k(e,n,o,t)}if(v(n)||function(n){return n&&n.type===Symbol.for("react.fragment")}(n)||function(n){return n&&"object"===a(n.containerInfo)}(n)){var s,c=(null==n?void 0:null===(s=n.props)||void 0===s?void 0:s.children)||n.children||n;return f.Children.map(c,function(n,t){return C(n,r,o,t)})}return n}}function I(n,t){var o,i;return(i="function"==typeof n&&1===arguments.length?n:(o=n,t))||null===i||(i=_.onDenied),function(t){return function(n){function r(){return u(this,r),y(this,p(r).apply(this,arguments))}return c(r,t),s(r,[{key:"componentDidMount",value:function(){h(p(r.prototype),"componentDidMount",this)&&h(p(r.prototype),"componentDidMount",this).call(this),N!==m.DONE&&R.push(this)}},{key:"componentWillUnmount",value:function(){var t=this,n=R.findIndex(function(n){return n===t});-1!==n&&R.splice(n,1),h(p(r.prototype),"componentWillUnmount",this)&&h(p(r.prototype),"componentWillUnmount",this).call(this)}},{key:"render",value:function(){var n=null,t=d.AUTHORIZED,e=d.DENIED;switch(S(o,j)?t:e){case t:n=C(h(p(r.prototype),"render",this).call(this),j,i);break;case e:n=k(o,this,i)}return n||null}}]),r}()}}I.settings=function(n){Object.assign(_,n)};var U=I.setGlobalPermissions=function(n){j=G(n),N=m.DONE,function(n){return!g(n)}(R)&&function(){for(var n,t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:[];n=t.shift();)n.forceUpdate()}(R)},T=I.setGlobalPermissionsAsync=function(n){!function(n){return"object"===a(n)&&n.then&&n.catch&&n.finally}(n)||(w=n,N=m.PENDING,w.then(function(n){I.setGlobalPermissions(n)},function(n){throw N=m.ERROR,j=null,new t(n)}).finally(function(){w=null}))},A=I.getGlobalPermissions=function(){return j},x=I.getGlobalPermissionsAsync=function(t){setTimeout(function(){w?w.then(function(n){t(G(n))},t):t(j)},0)};n.default=I,n.getGlobalPermissions=A,n.getGlobalPermissionsAsync=x,n.setGlobalPermissions=U,n.setGlobalPermissionsAsync=T,n.withPermission=function(e,n,t){var r;return I(n,t)(r=function(n){function t(){return u(this,t),y(this,p(t).apply(this,arguments))}return c(t,l.PureComponent),s(t,[{key:"render",value:function(){return e(this.props)}}]),t}())||r},Object.defineProperty(n,"__esModule",{value:!0})});