index.js 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582
  1. /*!
  2. * @form-create/core v2.5.27
  3. * (c) 2018-2022 xaboy
  4. * Github https://github.com/xaboy/form-create
  5. * Released under the MIT License.
  6. */
  7. import Vue from 'vue';
  8. function _typeof(obj) {
  9. "@babel/helpers - typeof";
  10. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  11. _typeof = function (obj) {
  12. return typeof obj;
  13. };
  14. } else {
  15. _typeof = function (obj) {
  16. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  17. };
  18. }
  19. return _typeof(obj);
  20. }
  21. function _classCallCheck(instance, Constructor) {
  22. if (!(instance instanceof Constructor)) {
  23. throw new TypeError("Cannot call a class as a function");
  24. }
  25. }
  26. function _defineProperty(obj, key, value) {
  27. if (key in obj) {
  28. Object.defineProperty(obj, key, {
  29. value: value,
  30. enumerable: true,
  31. configurable: true,
  32. writable: true
  33. });
  34. } else {
  35. obj[key] = value;
  36. }
  37. return obj;
  38. }
  39. function ownKeys(object, enumerableOnly) {
  40. var keys = Object.keys(object);
  41. if (Object.getOwnPropertySymbols) {
  42. var symbols = Object.getOwnPropertySymbols(object);
  43. if (enumerableOnly) symbols = symbols.filter(function (sym) {
  44. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  45. });
  46. keys.push.apply(keys, symbols);
  47. }
  48. return keys;
  49. }
  50. function _objectSpread2(target) {
  51. for (var i = 1; i < arguments.length; i++) {
  52. var source = arguments[i] != null ? arguments[i] : {};
  53. if (i % 2) {
  54. ownKeys(Object(source), true).forEach(function (key) {
  55. _defineProperty(target, key, source[key]);
  56. });
  57. } else if (Object.getOwnPropertyDescriptors) {
  58. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  59. } else {
  60. ownKeys(Object(source)).forEach(function (key) {
  61. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  62. });
  63. }
  64. }
  65. return target;
  66. }
  67. function _inherits(subClass, superClass) {
  68. if (typeof superClass !== "function" && superClass !== null) {
  69. throw new TypeError("Super expression must either be null or a function");
  70. }
  71. subClass.prototype = Object.create(superClass && superClass.prototype, {
  72. constructor: {
  73. value: subClass,
  74. writable: true,
  75. configurable: true
  76. }
  77. });
  78. if (superClass) _setPrototypeOf(subClass, superClass);
  79. }
  80. function _getPrototypeOf(o) {
  81. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
  82. return o.__proto__ || Object.getPrototypeOf(o);
  83. };
  84. return _getPrototypeOf(o);
  85. }
  86. function _setPrototypeOf(o, p) {
  87. _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
  88. o.__proto__ = p;
  89. return o;
  90. };
  91. return _setPrototypeOf(o, p);
  92. }
  93. function _isNativeReflectConstruct() {
  94. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  95. if (Reflect.construct.sham) return false;
  96. if (typeof Proxy === "function") return true;
  97. try {
  98. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  99. return true;
  100. } catch (e) {
  101. return false;
  102. }
  103. }
  104. function _assertThisInitialized(self) {
  105. if (self === void 0) {
  106. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  107. }
  108. return self;
  109. }
  110. function _possibleConstructorReturn(self, call) {
  111. if (call && (typeof call === "object" || typeof call === "function")) {
  112. return call;
  113. }
  114. return _assertThisInitialized(self);
  115. }
  116. function _createSuper(Derived) {
  117. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  118. return function _createSuperInternal() {
  119. var Super = _getPrototypeOf(Derived),
  120. result;
  121. if (hasNativeReflectConstruct) {
  122. var NewTarget = _getPrototypeOf(this).constructor;
  123. result = Reflect.construct(Super, arguments, NewTarget);
  124. } else {
  125. result = Super.apply(this, arguments);
  126. }
  127. return _possibleConstructorReturn(this, result);
  128. };
  129. }
  130. function _toConsumableArray(arr) {
  131. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
  132. }
  133. function _arrayWithoutHoles(arr) {
  134. if (Array.isArray(arr)) return _arrayLikeToArray(arr);
  135. }
  136. function _iterableToArray(iter) {
  137. if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
  138. }
  139. function _unsupportedIterableToArray(o, minLen) {
  140. if (!o) return;
  141. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  142. var n = Object.prototype.toString.call(o).slice(8, -1);
  143. if (n === "Object" && o.constructor) n = o.constructor.name;
  144. if (n === "Map" || n === "Set") return Array.from(o);
  145. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  146. }
  147. function _arrayLikeToArray(arr, len) {
  148. if (len == null || len > arr.length) len = arr.length;
  149. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  150. return arr2;
  151. }
  152. function _nonIterableSpread() {
  153. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  154. }
  155. function $set(target, field, value) {
  156. Vue.set(target, field, value);
  157. }
  158. function $del(target, field) {
  159. Vue["delete"](target, field);
  160. }
  161. var _extends = Object.assign || function (a) {
  162. for (var b, c = 1; c < arguments.length; c++) {
  163. for (var d in b = arguments[c], b) {
  164. Object.prototype.hasOwnProperty.call(b, d) && $set(a, d, b[d]);
  165. }
  166. }
  167. return a;
  168. };
  169. function extend() {
  170. return _extends.apply(this, arguments);
  171. }
  172. function toArray(value) {
  173. return Array.isArray(value) ? value : [null, undefined, ''].indexOf(value) > -1 ? [] : [value];
  174. }
  175. var NAME = 'FormCreate';
  176. var getRuleInject = function getRuleInject(vm, parent) {
  177. if (!vm || vm === parent) {
  178. return;
  179. }
  180. if (vm.formCreateInject) {
  181. return vm.formCreateInject;
  182. }
  183. if (vm.$parent) {
  184. return getRuleInject(vm.$parent, parent);
  185. }
  186. };
  187. function $FormCreate(FormCreate) {
  188. return {
  189. name: NAME,
  190. componentName: NAME,
  191. model: {
  192. prop: 'api'
  193. },
  194. provide: function provide() {
  195. return {
  196. $pfc: this
  197. };
  198. },
  199. inject: {
  200. $pfc: {
  201. "default": null
  202. }
  203. },
  204. props: {
  205. rule: {
  206. type: Array,
  207. required: true
  208. },
  209. option: {
  210. type: Object,
  211. "default": function _default() {
  212. return {};
  213. }
  214. },
  215. extendOption: Boolean,
  216. value: Object,
  217. api: Object,
  218. name: String,
  219. subForm: {
  220. type: Boolean,
  221. "default": true
  222. },
  223. inFor: Boolean
  224. },
  225. data: function data() {
  226. return {
  227. formData: undefined,
  228. destroyed: false,
  229. validate: {},
  230. $f: undefined,
  231. isShow: true,
  232. unique: 1,
  233. renderRule: _toConsumableArray(this.rule || []),
  234. ctxInject: {},
  235. updateValue: JSON.stringify(this.value || {}),
  236. isMore: !!this.inFor
  237. };
  238. },
  239. render: function render() {
  240. return this.formCreate.render();
  241. },
  242. methods: {
  243. _refresh: function _refresh() {
  244. ++this.unique;
  245. },
  246. _renderRule: function _renderRule() {
  247. this.renderRule = _toConsumableArray(this.rule || []);
  248. },
  249. _updateValue: function _updateValue(value) {
  250. if (this.destroyed) return;
  251. this.updateValue = JSON.stringify(value);
  252. this.$emit('update:value', value);
  253. }
  254. },
  255. watch: {
  256. value: {
  257. handler: function handler(n) {
  258. if (JSON.stringify(n || {}) === this.updateValue) return;
  259. this.$f.config.forceCoverValue ? this.$f.coverValue(n || {}) : this.$f.setValue(n || {});
  260. },
  261. deep: true
  262. },
  263. option: {
  264. handler: function handler(n) {
  265. this.formCreate.initOptions(n);
  266. this.$f.refresh();
  267. },
  268. deep: true
  269. },
  270. rule: function rule(n) {
  271. var _this = this;
  272. if (n.length === this.renderRule.length && n.every(function (v) {
  273. return _this.renderRule.indexOf(v) > -1;
  274. })) return;
  275. this.formCreate.$handle.reloadRule(n);
  276. this._renderRule();
  277. }
  278. },
  279. beforeCreate: function beforeCreate() {
  280. var _this2 = this;
  281. var _this$$options$propsD = this.$options.propsData,
  282. rule = _this$$options$propsD.rule,
  283. option = _this$$options$propsD.option;
  284. this.formCreate = new FormCreate(this, rule, option);
  285. Object.keys(this.formCreate.prop).forEach(function (k) {
  286. extend(_this2.$options[k], _this2.formCreate.prop[k]);
  287. });
  288. this.$emit('beforeCreate', this.formCreate.api());
  289. },
  290. created: function created() {
  291. var _this3 = this;
  292. var vm = this,
  293. fapi = this.formCreate.api();
  294. var addSubForm = function addSubForm() {
  295. if (vm.$pfc) {
  296. var inject = getRuleInject(vm, vm.$pfc);
  297. if (inject) {
  298. var sub;
  299. if (vm.isMore) {
  300. sub = toArray(inject.getSubForm());
  301. sub.push(fapi);
  302. } else {
  303. sub = fapi;
  304. }
  305. inject.subForm(sub);
  306. }
  307. }
  308. };
  309. var rmSubForm = function rmSubForm() {
  310. var inject = getRuleInject(vm, vm.$pfc);
  311. if (inject) {
  312. if (vm.isMore) {
  313. var sub = toArray(inject.getSubForm());
  314. var idx = sub.indexOf(fapi);
  315. if (idx > -1) {
  316. sub.splice(idx, 1);
  317. }
  318. } else {
  319. inject.subForm();
  320. }
  321. }
  322. };
  323. vm.$on('hook:beforeDestroy', function () {
  324. rmSubForm();
  325. });
  326. this.$watch(function () {
  327. return _this3.subForm;
  328. }, function (n) {
  329. n ? addSubForm() : rmSubForm();
  330. }, {
  331. immediate: true
  332. });
  333. }
  334. };
  335. }
  336. var is = {
  337. type: function type(arg, _type) {
  338. return Object.prototype.toString.call(arg) === '[object ' + _type + ']';
  339. },
  340. Undef: function Undef(v) {
  341. return v === undefined || v === null;
  342. },
  343. Element: function Element(arg) {
  344. return _typeof(arg) === 'object' && arg !== null && arg.nodeType === 1 && !is.Object(arg);
  345. },
  346. trueArray: function trueArray(data) {
  347. return Array.isArray(data) && data.length > 0;
  348. },
  349. Function: function Function(v) {
  350. var type = this.getType(v);
  351. return type === 'Function' || type === 'AsyncFunction';
  352. },
  353. getType: function getType(v) {
  354. var str = Object.prototype.toString.call(v);
  355. return /^\[object (.*)\]$/.exec(str)[1];
  356. },
  357. empty: function empty(value) {
  358. if (value === undefined || value === null) {
  359. return true;
  360. }
  361. if (Array.isArray(value) && Array.isArray(value) && !value.length) {
  362. return true;
  363. }
  364. return typeof value === 'string' && !value;
  365. }
  366. };
  367. ['Date', 'Object', 'String', 'Boolean', 'Array', 'Number'].forEach(function (t) {
  368. is[t] = function (arg) {
  369. return is.type(arg, t);
  370. };
  371. });
  372. function hasProperty(rule, k) {
  373. return {}.hasOwnProperty.call(rule, k);
  374. }
  375. var normalMerge = ['attrs', 'props', 'domProps', 'scopedSlots'];
  376. var toArrayMerge = ['class', 'style', 'directives'];
  377. var functionalMerge = ['on', 'nativeOn'];
  378. var mergeProps = function mergeProps(objects) {
  379. var initial = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  380. var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  381. var _normalMerge = [].concat(normalMerge, _toConsumableArray(opt['normal'] || []));
  382. var _toArrayMerge = [].concat(toArrayMerge, _toConsumableArray(opt['array'] || []));
  383. var _functionalMerge = [].concat(functionalMerge, _toConsumableArray(opt['functional'] || []));
  384. var propsMerge = opt['props'] || [];
  385. return objects.reduce(function (a, b) {
  386. for (var key in b) {
  387. if (a[key]) {
  388. if (propsMerge.indexOf(key) > -1) {
  389. a[key] = mergeProps([b[key]], a[key]);
  390. } else if (_normalMerge.indexOf(key) > -1) {
  391. a[key] = _objectSpread2(_objectSpread2({}, a[key]), b[key]);
  392. } else if (_toArrayMerge.indexOf(key) > -1) {
  393. var arrA = a[key] instanceof Array ? a[key] : [a[key]];
  394. var arrB = b[key] instanceof Array ? b[key] : [b[key]];
  395. a[key] = [].concat(_toConsumableArray(arrA), _toConsumableArray(arrB));
  396. } else if (_functionalMerge.indexOf(key) > -1) {
  397. for (var event in b[key]) {
  398. if (a[key][event]) {
  399. var _arrA = a[key][event] instanceof Array ? a[key][event] : [a[key][event]];
  400. var _arrB = b[key][event] instanceof Array ? b[key][event] : [b[key][event]];
  401. a[key][event] = [].concat(_toConsumableArray(_arrA), _toConsumableArray(_arrB));
  402. } else {
  403. a[key][event] = b[key][event];
  404. }
  405. }
  406. } else if (key === 'hook') {
  407. for (var hook in b[key]) {
  408. if (a[key][hook]) {
  409. a[key][hook] = mergeFn(a[key][hook], b[key][hook]);
  410. } else {
  411. a[key][hook] = b[key][hook];
  412. }
  413. }
  414. } else {
  415. a[key] = b[key];
  416. }
  417. } else {
  418. if (_normalMerge.indexOf(key) > -1 || _functionalMerge.indexOf(key) > -1 || propsMerge.indexOf(key) > -1) {
  419. a[key] = _objectSpread2({}, b[key]);
  420. } else if (_toArrayMerge.indexOf(key) > -1) {
  421. a[key] = b[key] instanceof Array ? _toConsumableArray(b[key]) : _typeof(b[key]) === 'object' ? _objectSpread2({}, b[key]) : b[key];
  422. } else a[key] = b[key];
  423. }
  424. }
  425. return a;
  426. }, initial);
  427. };
  428. var mergeFn = function mergeFn(fn1, fn2) {
  429. return function () {
  430. fn1 && fn1.apply(this, arguments);
  431. fn2 && fn2.apply(this, arguments);
  432. };
  433. };
  434. var keyAttrs = ['type', 'slot', 'emitPrefix', 'value', 'name', 'native', 'hidden', 'display', 'inject', 'options', 'emit', 'nativeEmit', 'link', 'prefix', 'suffix', 'update', 'sync', 'optionsTo', 'key', 'preview', 'component', 'cache'];
  435. var arrayAttrs = ['validate', 'children', 'control'];
  436. var normalAttrs = ['effect', 'deep'];
  437. function attrs() {
  438. return [].concat(keyAttrs, _toConsumableArray(normalMerge), _toConsumableArray(toArrayMerge), _toConsumableArray(functionalMerge), arrayAttrs, normalAttrs);
  439. }
  440. function format(type, msg, rule) {
  441. return "[form-create ".concat(type, "]: ").concat(msg) + (rule ? '\n\nrule: ' + JSON.stringify(rule.getRule ? rule.getRule() : rule) : '');
  442. }
  443. function tip(msg, rule) {
  444. console.warn(format('tip', msg, rule));
  445. }
  446. function err(msg, rule) {
  447. console.error(format('err', msg, rule));
  448. }
  449. function logError(e) {
  450. err(e.toString());
  451. console.error(e);
  452. }
  453. function deepExtend(origin) {
  454. var target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  455. var mode = arguments.length > 2 ? arguments[2] : undefined;
  456. var isArr = false;
  457. for (var key in target) {
  458. if (Object.prototype.hasOwnProperty.call(target, key)) {
  459. var clone = target[key];
  460. if ((isArr = Array.isArray(clone)) || is.Object(clone)) {
  461. var nst = origin[key] === undefined;
  462. if (isArr) {
  463. isArr = false;
  464. nst && $set(origin, key, []);
  465. } else if (clone._clone && mode !== undefined) {
  466. if (mode) {
  467. clone = clone.getRule();
  468. nst && $set(origin, key, {});
  469. } else {
  470. $set(origin, key, clone._clone());
  471. continue;
  472. }
  473. } else {
  474. nst && $set(origin, key, {});
  475. }
  476. origin[key] = deepExtend(origin[key], clone, mode);
  477. } else {
  478. $set(origin, key, clone);
  479. if (!is.Undef(clone)) {
  480. if (!is.Undef(clone.__json)) {
  481. origin[key].__json = clone.__json;
  482. }
  483. if (!is.Undef(clone.__origin)) {
  484. origin[key].__origin = clone.__origin;
  485. }
  486. }
  487. }
  488. }
  489. }
  490. return mode !== undefined && Array.isArray(origin) ? origin.filter(function (v) {
  491. return !v || !v.__ctrl;
  492. }) : origin;
  493. }
  494. function deepCopy(value) {
  495. return deepExtend({}, {
  496. value: value
  497. }).value;
  498. }
  499. var PREFIX = '[[FORM-CREATE-PREFIX-';
  500. var SUFFIX = '-FORM-CREATE-SUFFIX]]';
  501. var $T = '$FN:';
  502. var $TX = '$FNX:';
  503. var FUNCTION = 'function';
  504. function toJson(obj, space) {
  505. return JSON.stringify(deepExtend([], obj, true), function (key, val) {
  506. if (val && val._isVue === true) return undefined;
  507. if (_typeof(val) !== FUNCTION) {
  508. return val;
  509. }
  510. if (val.__json) {
  511. return val.__json;
  512. }
  513. if (val.__origin) val = val.__origin;
  514. if (val.__emit) return undefined;
  515. return PREFIX + val + SUFFIX;
  516. }, space);
  517. }
  518. function makeFn(fn) {
  519. return eval('(' + FUNCTION + '(){return ' + fn + ' })()');
  520. }
  521. function parseFn(fn, mode) {
  522. if (fn && is.String(fn) && fn.length > 4) {
  523. var v = fn.trim();
  524. var flag = false;
  525. try {
  526. if (v.indexOf(SUFFIX) > 0 && v.indexOf(PREFIX) === 0) {
  527. v = v.replace(SUFFIX, '').replace(PREFIX, '');
  528. flag = true;
  529. } else if (v.indexOf($T) === 0) {
  530. v = v.replace($T, '');
  531. flag = true;
  532. } else if (v.indexOf($TX) === 0) {
  533. v = makeFn('function($inject){' + v.replace($TX, '') + '}');
  534. v.__json = fn;
  535. v.__inject = true;
  536. return v;
  537. } else if (!mode && v.indexOf(FUNCTION) === 0 && v !== FUNCTION) {
  538. flag = true;
  539. }
  540. if (!flag) return fn;
  541. var val = makeFn(v.indexOf(FUNCTION) === -1 && v.indexOf('(') !== 0 ? FUNCTION + ' ' + v : v);
  542. val.__json = fn;
  543. return val;
  544. } catch (e) {
  545. err("\u89E3\u6790\u5931\u8D25:".concat(v, "\n\nerr: ").concat(e));
  546. return undefined;
  547. }
  548. }
  549. return fn;
  550. }
  551. function parseJson(json, mode) {
  552. return JSON.parse(json, function (k, v) {
  553. if (is.Undef(v) || !v.indexOf) return v;
  554. return parseFn(v, mode);
  555. });
  556. }
  557. function enumerable(value, writable) {
  558. return {
  559. value: value,
  560. enumerable: false,
  561. configurable: false,
  562. writable: !!writable
  563. };
  564. }
  565. function copyRule(rule) {
  566. return copyRules([rule])[0];
  567. }
  568. function copyRules(rules, mode) {
  569. return deepExtend([], _toConsumableArray(rules), mode || false);
  570. }
  571. function mergeRule(rule, merge) {
  572. mergeProps(Array.isArray(merge) ? merge : [merge], rule, {
  573. array: arrayAttrs,
  574. normal: normalAttrs
  575. });
  576. return rule;
  577. }
  578. function getRule(rule) {
  579. var r = is.Function(rule.getRule) ? rule.getRule() : rule;
  580. if (!r.type) {
  581. r.type = 'input';
  582. }
  583. return r;
  584. }
  585. function mergeGlobal(target, merge) {
  586. if (!target) return merge;
  587. Object.keys(merge || {}).forEach(function (k) {
  588. if (merge[k]) {
  589. target[k] = mergeRule(target[k] || {}, merge[k]);
  590. }
  591. });
  592. return target;
  593. }
  594. function funcProxy(that, proxy) {
  595. Object.defineProperties(that, Object.keys(proxy).reduce(function (initial, k) {
  596. initial[k] = {
  597. get: function get() {
  598. return proxy[k]();
  599. }
  600. };
  601. return initial;
  602. }, {}));
  603. }
  604. function byCtx(rule) {
  605. return rule.__fc__ || (rule.__origin__ ? rule.__origin__.__fc__ : null);
  606. }
  607. function invoke(fn, def) {
  608. try {
  609. def = fn();
  610. } catch (e) {
  611. logError(e);
  612. }
  613. return def;
  614. }
  615. function baseRule() {
  616. return {
  617. props: {},
  618. on: {},
  619. options: [],
  620. children: [],
  621. hidden: false,
  622. display: true,
  623. value: undefined
  624. };
  625. }
  626. function creatorFactory(name, init) {
  627. return function (title, field, value) {
  628. var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  629. var maker = new Creator(name, title, field, value, props);
  630. if (init) {
  631. if (is.Function(init)) init(maker);else maker.props(init);
  632. }
  633. return maker;
  634. };
  635. }
  636. function Creator(type, title, field, value, props) {
  637. this._data = extend(baseRule(), {
  638. type: type,
  639. title: title,
  640. field: field,
  641. value: value,
  642. props: props || {}
  643. });
  644. this.event = this.on;
  645. }
  646. extend(Creator.prototype, {
  647. getRule: function getRule() {
  648. return this._data;
  649. },
  650. setProp: function setProp(key, value) {
  651. $set(this._data, key, value);
  652. return this;
  653. },
  654. _clone: function _clone() {
  655. var clone = new this.constructor();
  656. clone._data = copyRule(this._data);
  657. return clone;
  658. }
  659. });
  660. function appendProto(attrs) {
  661. attrs.forEach(function (name) {
  662. Creator.prototype[name] = function (key) {
  663. mergeRule(this._data, _defineProperty({}, name, arguments.length < 2 ? key : _defineProperty({}, key, arguments[1])));
  664. return this;
  665. };
  666. });
  667. }
  668. appendProto(attrs());
  669. var commonMaker = creatorFactory('');
  670. function create(type, field, title) {
  671. var make = commonMaker('', field);
  672. make._data.type = type;
  673. make._data.title = title;
  674. return make;
  675. }
  676. function createTmp(template, vm, field, title) {
  677. var make = commonMaker('', field);
  678. make._data.type = 'template';
  679. make._data.template = template;
  680. make._data.title = title;
  681. make._data.vm = vm;
  682. return make;
  683. }
  684. function makerFactory() {
  685. return {
  686. create: create,
  687. createTmp: createTmp,
  688. template: createTmp,
  689. factory: creatorFactory
  690. };
  691. }
  692. function copy(value) {
  693. return deepCopy(value);
  694. }
  695. function Api(h) {
  696. function tidyFields(fields) {
  697. if (is.Undef(fields)) fields = h.fields();else if (!Array.isArray(fields)) fields = [fields];
  698. return fields;
  699. }
  700. function props(fields, key, val) {
  701. tidyFields(fields).forEach(function (field) {
  702. h.getCtxs(field).forEach(function (ctx) {
  703. $set(ctx.rule, key, val);
  704. h.$render.clearCache(ctx);
  705. });
  706. });
  707. }
  708. function allSubForm() {
  709. var subs = h.subForm;
  710. return Object.keys(subs).reduce(function (initial, k) {
  711. var sub = subs[k];
  712. if (!sub) return initial;
  713. if (Array.isArray(sub)) initial.push.apply(initial, _toConsumableArray(sub));else initial.push(sub);
  714. return initial;
  715. }, []);
  716. }
  717. var api = {
  718. get config() {
  719. return h.options;
  720. },
  721. get options() {
  722. return h.options;
  723. },
  724. get form() {
  725. return h.form;
  726. },
  727. get rule() {
  728. return h.rules;
  729. },
  730. get parent() {
  731. return h.vm.$pfc && h.vm.$pfc.$f;
  732. },
  733. get children() {
  734. return allSubForm();
  735. },
  736. formData: function formData(fields) {
  737. return tidyFields(fields).reduce(function (initial, id) {
  738. var ctx = h.getFieldCtx(id);
  739. if (!ctx) return initial;
  740. initial[ctx.field] = copy(ctx.rule.value);
  741. return initial;
  742. }, h.options.appendValue ? copy(h.appendData) : {});
  743. },
  744. getValue: function getValue(field) {
  745. var ctx = h.getFieldCtx(field);
  746. if (!ctx) return;
  747. return copy(ctx.rule.value);
  748. },
  749. coverValue: function coverValue(formData) {
  750. var data = _objectSpread2({}, formData || {});
  751. h.deferSyncValue(function () {
  752. api.fields().forEach(function (key) {
  753. var ctxs = h.fieldCtx[key];
  754. if (ctxs) {
  755. var flag = hasProperty(data, key);
  756. ctxs.forEach(function (ctx) {
  757. ctx.rule.value = flag ? data[key] : undefined;
  758. });
  759. delete data[key];
  760. }
  761. });
  762. extend(h.appendData, data);
  763. });
  764. },
  765. setValue: function setValue(field) {
  766. var formData = field;
  767. if (arguments.length >= 2) formData = _defineProperty({}, field, arguments[1]);
  768. h.deferSyncValue(function () {
  769. Object.keys(formData).forEach(function (key) {
  770. var ctxs = h.fieldCtx[key];
  771. if (!ctxs) return h.appendData[key] = formData[key];
  772. ctxs.forEach(function (ctx) {
  773. ctx.rule.value = formData[key];
  774. });
  775. });
  776. });
  777. },
  778. removeField: function removeField(field) {
  779. var ctx = h.getCtx(field);
  780. h.deferSyncValue(function () {
  781. h.getCtxs(field).forEach(function (ctx) {
  782. ctx.rm();
  783. });
  784. }, true);
  785. return ctx ? ctx.origin : undefined;
  786. },
  787. removeRule: function removeRule(rule) {
  788. var ctx = rule && byCtx(rule);
  789. if (!ctx) return;
  790. ctx.rm();
  791. return ctx.origin;
  792. },
  793. destroy: function destroy() {
  794. h.vm.$el.parentNode && h.vm.$el.parentNode.removeChild(h.vm.$el);
  795. h.vm.$destroy();
  796. },
  797. fields: function fields() {
  798. return h.fields();
  799. },
  800. append: function append(rule, after, child) {
  801. var index = h.sort.length - 1,
  802. rules;
  803. var ctx = h.getCtx(after);
  804. if (ctx) {
  805. if (child) {
  806. rules = ctx.rule.children;
  807. index = ctx.rule.children.length - 1;
  808. } else {
  809. index = ctx.root.indexOf(ctx.origin);
  810. rules = ctx.root;
  811. }
  812. } else rules = h.rules;
  813. rules.splice(index + 1, 0, rule);
  814. },
  815. prepend: function prepend(rule, after, child) {
  816. var index = 0,
  817. rules;
  818. var ctx = h.getCtx(after);
  819. if (ctx) {
  820. if (child) {
  821. rules = ctx.rule.children;
  822. } else {
  823. index = ctx.root.indexOf(ctx.origin);
  824. rules = ctx.root;
  825. }
  826. } else rules = h.rules;
  827. rules.splice(index, 0, rule);
  828. },
  829. hidden: function hidden(state, fields) {
  830. props(fields, 'hidden', !!state);
  831. h.refresh();
  832. },
  833. hiddenStatus: function hiddenStatus(id) {
  834. var ctx = h.getCtx(id);
  835. if (!ctx) return;
  836. return !!ctx.rule.hidden;
  837. },
  838. display: function display(state, fields) {
  839. props(fields, 'display', !!state);
  840. h.refresh();
  841. },
  842. displayStatus: function displayStatus(id) {
  843. var ctx = h.getCtx(id);
  844. if (!ctx) return;
  845. return !!ctx.rule.display;
  846. },
  847. disabled: function disabled(_disabled, fields) {
  848. tidyFields(fields).forEach(function (field) {
  849. h.getCtxs(field).forEach(function (ctx) {
  850. ctx.rule.props && $set(ctx.rule.props, 'disabled', !!_disabled);
  851. });
  852. });
  853. h.refresh();
  854. },
  855. all: function all(origin) {
  856. return Object.keys(h.ctxs).map(function (k) {
  857. var ctx = h.ctxs[k];
  858. return origin ? ctx.origin : ctx.rule;
  859. });
  860. },
  861. model: function model(origin) {
  862. return h.fields().reduce(function (initial, key) {
  863. var ctx = h.fieldCtx[key][0];
  864. initial[key] = origin ? ctx.origin : ctx.rule;
  865. return initial;
  866. }, {});
  867. },
  868. component: function component(origin) {
  869. return Object.keys(h.nameCtx).reduce(function (initial, key) {
  870. var ctx = h.nameCtx[key].map(function (ctx) {
  871. return origin ? ctx.origin : ctx.rule;
  872. });
  873. initial[key] = ctx.length === 1 ? ctx[0] : ctx;
  874. return initial;
  875. }, {});
  876. },
  877. bind: function bind() {
  878. return api.form;
  879. },
  880. reload: function reload(rules) {
  881. h.reloadRule(rules);
  882. },
  883. updateOptions: function updateOptions(options) {
  884. h.fc.updateOptions(options);
  885. api.refresh();
  886. },
  887. onSubmit: function onSubmit(fn) {
  888. api.updateOptions({
  889. onSubmit: fn
  890. });
  891. },
  892. sync: function sync(field) {
  893. if (Array.isArray(field)) {
  894. field.forEach(function (v) {
  895. return api.sync(v);
  896. });
  897. return;
  898. }
  899. var ctxs = is.Object(field) ? byCtx(field) : h.getCtxs(field);
  900. if (!ctxs) {
  901. return;
  902. }
  903. ctxs = Array.isArray(ctxs) ? ctxs : [ctxs];
  904. ctxs.forEach(function (ctx) {
  905. if (!ctx.deleted) {
  906. var subForm = h.subForm[ctx.id];
  907. if (subForm) {
  908. if (Array.isArray(subForm)) {
  909. subForm.forEach(function (form) {
  910. form.refresh();
  911. });
  912. } else if (subForm) {
  913. subForm.refresh();
  914. }
  915. }
  916. h.$render.clearCache(ctx);
  917. }
  918. });
  919. h.refresh();
  920. },
  921. refresh: function refresh() {
  922. allSubForm().forEach(function (sub) {
  923. sub.refresh();
  924. });
  925. h.$render.clearCacheAll();
  926. h.refresh();
  927. },
  928. refreshOptions: function refreshOptions() {
  929. h.$manager.updateOptions(h.options);
  930. api.refresh();
  931. },
  932. hideForm: function hideForm(hide) {
  933. $set(h.vm, 'isShow', !hide);
  934. },
  935. changeStatus: function changeStatus() {
  936. return h.changeStatus;
  937. },
  938. clearChangeStatus: function clearChangeStatus() {
  939. h.changeStatus = false;
  940. },
  941. updateRule: function updateRule(id, rule) {
  942. h.getCtxs(id).forEach(function (ctx) {
  943. extend(ctx.rule, rule);
  944. });
  945. },
  946. updateRules: function updateRules(rules) {
  947. Object.keys(rules).forEach(function (id) {
  948. api.updateRule(id, rules[id]);
  949. });
  950. },
  951. mergeRule: function mergeRule$1(id, rule) {
  952. h.getCtxs(id).forEach(function (ctx) {
  953. mergeRule(ctx.rule, rule);
  954. });
  955. },
  956. mergeRules: function mergeRules(rules) {
  957. Object.keys(rules).forEach(function (id) {
  958. api.mergeRule(id, rules[id]);
  959. });
  960. },
  961. getRule: function getRule(id, origin) {
  962. var ctx = h.getCtx(id);
  963. if (ctx) {
  964. return origin ? ctx.origin : ctx.rule;
  965. }
  966. },
  967. setEffect: function setEffect(id, attr, value) {
  968. var ctx = h.getCtx(id);
  969. if (ctx && attr) {
  970. if (attr[0] === '$') {
  971. attr = attr.substr(1);
  972. }
  973. if (hasProperty(ctx.rule, '$' + attr)) {
  974. $set(ctx.rule, '$' + attr, value);
  975. }
  976. if (!hasProperty(ctx.rule, 'effect')) {
  977. ctx.rule.effect = {};
  978. }
  979. $set(ctx.rule.effect, attr, value);
  980. }
  981. },
  982. clearEffectData: function clearEffectData(id, attr) {
  983. var ctx = h.getCtx(id);
  984. if (ctx) {
  985. if (attr && attr[0] === '$') {
  986. attr = attr.substr(1);
  987. }
  988. ctx.clearEffectData(attr);
  989. api.sync(id);
  990. }
  991. },
  992. updateValidate: function updateValidate(id, validate, merge) {
  993. if (merge) {
  994. api.mergeRule(id, {
  995. validate: validate
  996. });
  997. } else {
  998. props(id, 'validate', validate);
  999. }
  1000. },
  1001. updateValidates: function updateValidates(validates, merge) {
  1002. Object.keys(validates).forEach(function (id) {
  1003. api.updateValidate(id, validates[id], merge);
  1004. });
  1005. },
  1006. refreshValidate: function refreshValidate() {
  1007. h.vm.validate = {};
  1008. api.refresh();
  1009. },
  1010. resetFields: function resetFields(fields) {
  1011. tidyFields(fields).forEach(function (field) {
  1012. h.getCtxs(field).forEach(function (ctx) {
  1013. h.$render.clearCache(ctx);
  1014. ctx.rule.value = copy(ctx.defaultValue);
  1015. });
  1016. });
  1017. },
  1018. method: function method(id, name) {
  1019. var el = api.el(id);
  1020. if (!el || !el[name]) throw new Error(format('err', "".concat(name, "\u65B9\u6CD5\u4E0D\u5B58\u5728")));
  1021. return function () {
  1022. return el[name].apply(el, arguments);
  1023. };
  1024. },
  1025. exec: function exec(id, name) {
  1026. for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  1027. args[_key - 2] = arguments[_key];
  1028. }
  1029. return invoke(function () {
  1030. return api.method(id, name).apply(void 0, args);
  1031. });
  1032. },
  1033. toJson: function toJson$1(space) {
  1034. return toJson(api.rule, space);
  1035. },
  1036. trigger: function trigger(id, event) {
  1037. var el = api.el(id);
  1038. for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
  1039. args[_key2 - 2] = arguments[_key2];
  1040. }
  1041. el && el.$emit.apply(el, [event].concat(args));
  1042. },
  1043. el: function el(id) {
  1044. var ctx = h.getCtx(id);
  1045. if (ctx) return ctx.el || h.vm.$refs[ctx.ref];
  1046. },
  1047. closeModal: function closeModal(id) {
  1048. var el = api.el(id);
  1049. el && el.$emit && el.$emit('close-modal');
  1050. },
  1051. getSubForm: function getSubForm(field) {
  1052. var ctx = h.getCtx(field);
  1053. return ctx ? h.subForm[ctx.id] : undefined;
  1054. },
  1055. nextTick: function nextTick(fn) {
  1056. h.bus.$once('next-tick', fn);
  1057. h.refresh();
  1058. },
  1059. nextRefresh: function nextRefresh(fn) {
  1060. h.nextRefresh();
  1061. fn && invoke(fn);
  1062. },
  1063. emit: function emit(name) {
  1064. var _h$vm;
  1065. for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
  1066. args[_key3 - 1] = arguments[_key3];
  1067. }
  1068. (_h$vm = h.vm).$emit.apply(_h$vm, [name].concat(args));
  1069. },
  1070. deferSyncValue: function deferSyncValue(fn, sync) {
  1071. h.deferSyncValue(fn, sync);
  1072. },
  1073. helper: {
  1074. tidyFields: tidyFields,
  1075. props: props
  1076. }
  1077. };
  1078. ['on', 'once', 'off', 'set'].forEach(function (n) {
  1079. api[n] = function () {
  1080. var _h$vm2;
  1081. (_h$vm2 = h.vm)["$".concat(n)].apply(_h$vm2, arguments);
  1082. };
  1083. });
  1084. api.changeValue = api.changeField = api.setValue;
  1085. return api;
  1086. }
  1087. function useCache(Render) {
  1088. extend(Render.prototype, {
  1089. initCache: function initCache() {
  1090. this.clearCacheAll();
  1091. },
  1092. clearCache: function clearCache(ctx) {
  1093. if (!this.cache[ctx.id]) {
  1094. ctx.parent && this.clearCache(ctx.parent);
  1095. return;
  1096. }
  1097. if (this.cache[ctx.id].use === true || this.cache[ctx.id].parent) {
  1098. this.$handle.refresh();
  1099. }
  1100. var parent = this.cache[ctx.id].parent;
  1101. this.cache[ctx.id] = null;
  1102. parent && this.clearCache(parent);
  1103. },
  1104. clearCacheAll: function clearCacheAll() {
  1105. this.cache = {};
  1106. },
  1107. setCache: function setCache(ctx, vnode, parent) {
  1108. this.cache[ctx.id] = {
  1109. vnode: vnode,
  1110. use: false,
  1111. parent: parent,
  1112. slot: ctx.rule.slot
  1113. };
  1114. },
  1115. getCache: function getCache(ctx) {
  1116. var cache = this.cache[ctx.id];
  1117. if (cache) {
  1118. cache.use = true;
  1119. return cache.vnode;
  1120. }
  1121. return undefined;
  1122. }
  1123. });
  1124. }
  1125. function toCase(str) {
  1126. var to = str.replace(/(-[a-z])/g, function (v) {
  1127. return v.replace('-', '').toLocaleUpperCase();
  1128. });
  1129. return lower(to);
  1130. }
  1131. function lower(str) {
  1132. return str.replace(str[0], str[0].toLowerCase());
  1133. }
  1134. function toString(val) {
  1135. return val == null ? '' : _typeof(val) === 'object' ? JSON.stringify(val, null, 2) : String(val);
  1136. }
  1137. function toLine(name) {
  1138. var line = name.replace(/([A-Z])/g, '-$1').toLocaleLowerCase();
  1139. if (line.indexOf('-') === 0) line = line.substr(1);
  1140. return line;
  1141. }
  1142. var id = 0;
  1143. function uniqueId() {
  1144. return 'F' + Math.random().toString(36).substr(3, 3) + Number("".concat(Date.now()).concat(++id)).toString(36);
  1145. }
  1146. function deepSet(data, idx, val) {
  1147. var _data = data,
  1148. to;
  1149. (idx || '').split('.').forEach(function (v) {
  1150. if (to) {
  1151. if (!_data[to] || _typeof(_data[to]) != 'object') {
  1152. _data[to] = {};
  1153. }
  1154. _data = _data[to];
  1155. }
  1156. to = v;
  1157. });
  1158. _data[to] = val;
  1159. return _data;
  1160. }
  1161. function useRender(Render) {
  1162. extend(Render.prototype, {
  1163. initRender: function initRender() {
  1164. this.tempList = {};
  1165. this.clearOrgChildren();
  1166. },
  1167. initOrgChildren: function initOrgChildren() {
  1168. var ctxs = this.$handle.ctxs;
  1169. this.orgChildren = Object.keys(ctxs).reduce(function (initial, id) {
  1170. if (ctxs[id].parser.loadChildren !== false) {
  1171. var children = ctxs[id].rule.children;
  1172. initial[id] = is.trueArray(children) ? _toConsumableArray(children) : [];
  1173. }
  1174. return initial;
  1175. }, {});
  1176. },
  1177. clearOrgChildren: function clearOrgChildren() {
  1178. this.orgChildren = {};
  1179. },
  1180. getTypeSlot: function getTypeSlot(type) {
  1181. var name = 'type-' + toLine(type);
  1182. var _fn = function _fn(vm) {
  1183. if (vm) {
  1184. var slot = vm.$scopedSlots[name] || vm.$scopedSlots['type-' + type];
  1185. if (slot) {
  1186. return slot;
  1187. }
  1188. return _fn(vm.$pfc);
  1189. }
  1190. };
  1191. return _fn(this.vm);
  1192. },
  1193. render: function render() {
  1194. var _this = this;
  1195. if (!this.vm.isShow) {
  1196. return;
  1197. }
  1198. this.$h = this.vm.$createElement;
  1199. this.$manager.beforeRender();
  1200. var vn;
  1201. var make = function make() {
  1202. return _this.renderList();
  1203. };
  1204. make.renderSlot = function (slot) {
  1205. return _this.renderList(slot);
  1206. };
  1207. make.renderName = function (name) {
  1208. return _this.renderId(name);
  1209. };
  1210. make.renderField = function (field) {
  1211. return _this.renderId(field, 'field');
  1212. };
  1213. if (this.vm.$scopedSlots.container) {
  1214. vn = [this.vm.$scopedSlots.container(make)];
  1215. } else {
  1216. vn = make();
  1217. }
  1218. return this.$manager.render(vn);
  1219. },
  1220. renderList: function renderList(slot) {
  1221. var _this2 = this;
  1222. return this.sort.map(function (id) {
  1223. return slot ? _this2.renderSlot(_this2.$handle.ctxs[id], slot) : _this2.renderCtx(_this2.$handle.ctxs[id]);
  1224. }).filter(function (val) {
  1225. return val !== undefined;
  1226. });
  1227. },
  1228. makeVm: function makeVm(rule) {
  1229. var _this3 = this;
  1230. var vm = rule.vm;
  1231. if (!vm) return new _vue();else if (is.Function(vm)) return invoke(function () {
  1232. return rule.vm(_this3.$handle.getInjectData(rule));
  1233. });else if (!vm._isVue) return new _vue(vm);
  1234. return vm;
  1235. },
  1236. mergeGlobal: function mergeGlobal(ctx) {
  1237. var g = this.$handle.options.global;
  1238. if (!g) return;
  1239. if (!ctx.cacheConfig) ctx.cacheConfig = g[ctx.originType] || g[ctx.type] || g[ctx.trueType] || {};
  1240. ctx.prop = mergeRule({}, [g['*'], ctx.cacheConfig, ctx.prop]);
  1241. },
  1242. setOptions: function setOptions(ctx) {
  1243. if (ctx.prop.optionsTo && ctx.prop.options) {
  1244. deepSet(ctx.prop, ctx.prop.optionsTo, ctx.prop.options);
  1245. }
  1246. },
  1247. deepSet: function deepSet$1(ctx) {
  1248. var deep = ctx.rule.deep;
  1249. deep && Object.keys(deep).sort(function (a, b) {
  1250. return a.length < b.length ? -1 : 1;
  1251. }).forEach(function (str) {
  1252. deepSet(ctx.prop, str, deep[str]);
  1253. });
  1254. },
  1255. setTempProps: function setTempProps(vm, ctx) {
  1256. if (!vm.$props) return;
  1257. var prop = ctx.prop;
  1258. var keys = Object.keys(vm.$props);
  1259. var inject = this.injectProp(ctx);
  1260. var injectKeys = Object.keys(inject);
  1261. keys.forEach(function (key) {
  1262. if (hasProperty(prop.props, key)) vm.$props[key] = prop.props[key];else if (injectKeys.indexOf(key) > -1) vm.$props[key] = inject[key];
  1263. });
  1264. var key = vm.$options.model && vm.$options.model.prop || 'value';
  1265. if (keys.indexOf(key) > -1) {
  1266. vm.$props[key] = prop.value;
  1267. }
  1268. },
  1269. renderTemp: function renderTemp(ctx) {
  1270. var _this4 = this;
  1271. if (!_vue.compile) {
  1272. tip('当前使用的Vue构建版本不支持compile,无法使用template功能');
  1273. return [];
  1274. }
  1275. var rule = ctx.prop;
  1276. var id = ctx.id,
  1277. key = ctx.key;
  1278. if (!this.tempList[id]) {
  1279. if (!ctx.el) {
  1280. ctx.el = this.makeVm(rule);
  1281. this.vm.$nextTick(function () {
  1282. return ctx.parser.mounted(ctx);
  1283. });
  1284. }
  1285. var _vm = ctx.el;
  1286. if (ctx.input) _vm.$on(_vm.$options.model && _vm.$options.model.event || 'input', function (value) {
  1287. _this4.onInput(ctx, value);
  1288. });
  1289. this.tempList[id] = {
  1290. vm: _vm,
  1291. template: _vue.compile(rule.template)
  1292. };
  1293. }
  1294. var _this$tempList$id = this.tempList[id],
  1295. vm = _this$tempList$id.vm,
  1296. template = _this$tempList$id.template;
  1297. this.setTempProps(vm, ctx);
  1298. var vn = template.render.call(vm);
  1299. if (is.Undef(vn.data)) vn.data = {};
  1300. vn.key = key;
  1301. vn.data.ref = ctx.ref;
  1302. vn.data.key = key;
  1303. return vn;
  1304. },
  1305. parseSide: function parseSide(side) {
  1306. return is.Object(side) ? mergeRule({}, side) : side;
  1307. },
  1308. renderSides: function renderSides(vn, ctx, temp) {
  1309. var prop = ctx[temp ? 'rule' : 'prop'];
  1310. return [this.renderRule(this.parseSide(prop.prefix)), vn, this.renderRule(this.parseSide(prop.suffix))];
  1311. },
  1312. renderSlot: function renderSlot(ctx, slot) {
  1313. return ctx.rule.slot === slot ? this.renderCtx(ctx) : undefined;
  1314. },
  1315. renderId: function renderId(name, type) {
  1316. var _this5 = this;
  1317. var ctxs = this.$handle[type === 'field' ? 'fieldCtx' : 'nameCtx'][name];
  1318. return ctxs ? ctxs.map(function (ctx) {
  1319. return _this5.renderCtx(ctx, ctx.parent);
  1320. }) : undefined;
  1321. },
  1322. renderCtx: function renderCtx(ctx, parent) {
  1323. if (ctx.type === 'hidden') return;
  1324. var rule = ctx.rule;
  1325. if (!this.cache[ctx.id] || this.cache[ctx.id].slot !== rule.slot) {
  1326. var vn;
  1327. var cacheFlag = rule.cache !== false;
  1328. var _type = ctx.trueType;
  1329. var none = !(is.Undef(rule.display) || !!rule.display);
  1330. if (_type === 'template' && !rule.template) {
  1331. vn = this.renderSides(this.renderChildren(ctx), ctx, true);
  1332. if (none) {
  1333. this.display(vn);
  1334. }
  1335. vn = this.item(ctx, vn);
  1336. } else if (_type === 'fcFragment') {
  1337. vn = this.renderChildren(ctx);
  1338. } else {
  1339. ctx.initProp();
  1340. this.mergeGlobal(ctx);
  1341. this.$manager.tidyRule(ctx);
  1342. this.deepSet(ctx);
  1343. this.setOptions(ctx);
  1344. this.ctxProp(ctx);
  1345. var prop = ctx.prop;
  1346. prop.preview = !!(hasProperty(prop, 'preview') ? prop.preview : this.options.preview || false);
  1347. prop.props.formCreateInject = this.injectProp(ctx);
  1348. var preview = prop.preview;
  1349. if (prop.hidden) {
  1350. this.setCache(ctx, undefined, parent);
  1351. return;
  1352. }
  1353. if (_type === 'template' && prop.template) {
  1354. vn = this.renderTemp(ctx);
  1355. cacheFlag = false;
  1356. } else {
  1357. var children = [];
  1358. if (ctx.parser.renderChildren) {
  1359. children = ctx.parser.renderChildren(ctx);
  1360. } else if (ctx.parser.loadChildren !== false) {
  1361. children = this.renderChildren(ctx);
  1362. }
  1363. var slot = this.getTypeSlot(ctx.type);
  1364. if (slot) {
  1365. vn = slot({
  1366. rule: rule,
  1367. prop: prop,
  1368. preview: preview,
  1369. children: children,
  1370. api: this.$handle.api,
  1371. model: prop.model || {}
  1372. });
  1373. } else {
  1374. vn = preview ? ctx.parser.preview(children, ctx) : ctx.parser.render(children, ctx);
  1375. }
  1376. }
  1377. vn = this.renderSides(vn, ctx);
  1378. if (!(!ctx.input && is.Undef(prop["native"])) && prop["native"] !== true) {
  1379. vn = this.$manager.makeWrap(ctx, vn);
  1380. }
  1381. if (none) {
  1382. vn = this.display(vn);
  1383. }
  1384. vn = this.item(ctx, vn);
  1385. }
  1386. if (cacheFlag) {
  1387. this.setCache(ctx, vn, parent);
  1388. }
  1389. return vn;
  1390. }
  1391. return this.getCache(ctx);
  1392. },
  1393. display: function display(vn) {
  1394. var _this6 = this;
  1395. if (Array.isArray(vn)) {
  1396. var data = [];
  1397. vn.forEach(function (v) {
  1398. if (Array.isArray(v)) return _this6.display(v);
  1399. if (_this6.none(v)) data.push(v);
  1400. });
  1401. return data;
  1402. } else {
  1403. return this.none(vn);
  1404. }
  1405. },
  1406. none: function none(vn) {
  1407. if (vn && vn.data) {
  1408. if (Array.isArray(vn.data.style)) {
  1409. vn.data.style.push({
  1410. display: 'none'
  1411. });
  1412. } else if (is.String(vn.data.style)) {
  1413. vn.data.style += ';display:none;';
  1414. } else {
  1415. vn.data.style = [vn.data.style, {
  1416. display: 'none'
  1417. }];
  1418. }
  1419. return vn;
  1420. }
  1421. },
  1422. item: function item(ctx, vn) {
  1423. return this.$h('fcFragment', {
  1424. slot: ctx.rule.slot,
  1425. key: ctx.key
  1426. }, [vn]);
  1427. },
  1428. injectProp: function injectProp(ctx) {
  1429. var _this7 = this;
  1430. if (!this.vm.ctxInject[ctx.id]) {
  1431. $set(this.vm.ctxInject, ctx.id, {
  1432. api: this.$handle.api,
  1433. form: this.fc.create,
  1434. subForm: function subForm(_subForm) {
  1435. _this7.$handle.addSubForm(ctx, _subForm);
  1436. },
  1437. getSubForm: function getSubForm() {
  1438. return _this7.$handle.subForm[ctx.id];
  1439. },
  1440. options: [],
  1441. children: [],
  1442. prop: {},
  1443. preview: false,
  1444. field: ctx.field,
  1445. rule: ctx.rule,
  1446. input: ctx.input
  1447. });
  1448. }
  1449. var inject = this.vm.ctxInject[ctx.id];
  1450. extend(inject, {
  1451. preview: ctx.prop.preview,
  1452. options: ctx.prop.options,
  1453. children: ctx.rule.children,
  1454. prop: function () {
  1455. var temp = _objectSpread2({}, ctx.prop);
  1456. temp.on = temp.on ? _objectSpread2({}, temp.on) : {};
  1457. delete temp.model;
  1458. return temp;
  1459. }()
  1460. });
  1461. return inject;
  1462. },
  1463. ctxProp: function ctxProp(ctx, custom) {
  1464. var _this8 = this;
  1465. var ref = ctx.ref,
  1466. key = ctx.key,
  1467. rule = ctx.rule;
  1468. this.$manager.mergeProp(ctx, custom);
  1469. ctx.parser.mergeProp(ctx, custom);
  1470. var props = [{
  1471. ref: ref,
  1472. key: rule.key || "".concat(key, "fc"),
  1473. slot: undefined,
  1474. on: {
  1475. 'hook:mounted': function hookMounted() {
  1476. _this8.onMounted(ctx);
  1477. },
  1478. 'fc.sub-form': function fcSubForm(subForm) {
  1479. _this8.$handle.addSubForm(ctx, subForm);
  1480. }
  1481. }
  1482. }];
  1483. if (!custom && ctx.input) {
  1484. ctx.prop.model = {
  1485. value: this.$handle.getFormData(ctx),
  1486. callback: function callback(value) {
  1487. _this8.onInput(ctx, value);
  1488. },
  1489. expression: "formData.".concat(ctx.id)
  1490. };
  1491. }
  1492. mergeProps(props, ctx.prop);
  1493. return ctx.prop;
  1494. },
  1495. onMounted: function onMounted(ctx) {
  1496. ctx.el = this.vm.$refs[ctx.ref];
  1497. if (ctx.el) {
  1498. (ctx.el.$el || ctx.el).__rule__ = ctx.rule;
  1499. }
  1500. ctx.parser.mounted(ctx);
  1501. this.$handle.effect(ctx, 'mounted');
  1502. },
  1503. onInput: function onInput(ctx, value) {
  1504. this.$handle.onInput(ctx, value);
  1505. },
  1506. renderChildren: function renderChildren(ctx) {
  1507. var _this9 = this;
  1508. var children = ctx.rule.children,
  1509. orgChildren = this.orgChildren[ctx.id];
  1510. var isRm = function isRm(child) {
  1511. return !is.String(child) && child.__fc__ && !_this9.$handle.ctxs[child.__fc__.id];
  1512. };
  1513. if (!is.trueArray(children) && orgChildren) {
  1514. this.$handle.deferSyncValue(function () {
  1515. orgChildren.forEach(function (child) {
  1516. if (!child) return;
  1517. if (isRm(child)) {
  1518. _this9.$handle.rmCtx(child.__fc__);
  1519. }
  1520. });
  1521. });
  1522. this.orgChildren[ctx.id] = [];
  1523. return [];
  1524. }
  1525. orgChildren && this.$handle.deferSyncValue(function () {
  1526. orgChildren.forEach(function (child) {
  1527. if (!child) return;
  1528. if (children.indexOf(child) === -1 && isRm(child)) {
  1529. _this9.$handle.rmCtx(child.__fc__);
  1530. }
  1531. });
  1532. });
  1533. return children.map(function (child) {
  1534. if (!child) return;
  1535. if (is.String(child)) return child;
  1536. if (child.__fc__) {
  1537. return _this9.renderCtx(child.__fc__, ctx);
  1538. }
  1539. if (child.type) {
  1540. _this9.vm.$nextTick(function () {
  1541. _this9.$handle.loadChildren(children, ctx);
  1542. _this9.$handle.refresh();
  1543. });
  1544. }
  1545. });
  1546. },
  1547. defaultRender: function defaultRender(ctx, children) {
  1548. var prop = ctx.prop;
  1549. if (prop.component) return this.vNode.makeComponent(prop.component, prop, children);
  1550. if (this.vNode[ctx.type]) return this.vNode[ctx.type](prop, children);
  1551. if (this.vNode[ctx.originType]) return this.vNode[ctx.originType](prop, children);
  1552. return this.vNode.make(lower(ctx.originType), prop, children);
  1553. },
  1554. renderRule: function renderRule(rule, children, origin) {
  1555. var _this10 = this;
  1556. if (!rule) return undefined;
  1557. if (is.String(rule)) return rule;
  1558. var type;
  1559. if (origin) {
  1560. type = rule.type;
  1561. } else {
  1562. type = rule.is;
  1563. if (rule.type) {
  1564. type = toCase(rule.type);
  1565. var alias = this.vNode.aliasMap[type];
  1566. if (alias) type = toCase(alias);
  1567. }
  1568. }
  1569. if (!type) return undefined;
  1570. var data = [[children]];
  1571. if (is.trueArray(rule.children)) {
  1572. data.push(rule.children.map(function (v) {
  1573. return _this10.renderRule(v);
  1574. }));
  1575. }
  1576. return this.$h(type, _objectSpread2({}, rule), data);
  1577. }
  1578. });
  1579. }
  1580. function Render(handle) {
  1581. extend(this, {
  1582. $handle: handle,
  1583. fc: handle.fc,
  1584. vm: handle.vm,
  1585. $manager: handle.$manager,
  1586. vNode: new handle.fc.CreateNode(handle.vm)
  1587. });
  1588. funcProxy(this, {
  1589. options: function options() {
  1590. return handle.options;
  1591. },
  1592. sort: function sort() {
  1593. return handle.sort;
  1594. }
  1595. });
  1596. this.initCache();
  1597. this.initRender();
  1598. }
  1599. useCache(Render);
  1600. useRender(Render);
  1601. function useInject(Handler) {
  1602. extend(Handler.prototype, {
  1603. parseInjectEvent: function parseInjectEvent(rule, on) {
  1604. var inject = rule.inject || this.options.injectEvent;
  1605. return this.parseEventLst(rule, on, inject);
  1606. },
  1607. parseEventLst: function parseEventLst(rule, data, inject, deep) {
  1608. var _this = this;
  1609. Object.keys(data).forEach(function (k) {
  1610. var fn = _this.parseEvent(rule, data[k], inject, deep);
  1611. if (fn) {
  1612. data[k] = fn;
  1613. }
  1614. });
  1615. return data;
  1616. },
  1617. parseEvent: function parseEvent(rule, fn, inject, deep) {
  1618. if (is.Function(fn) && (inject !== false && !is.Undef(inject) || fn.__inject)) {
  1619. return this.inject(rule, fn, inject);
  1620. } else if (!deep && Array.isArray(fn) && fn[0] && (is.String(fn[0]) || is.Function(fn[0]))) {
  1621. return this.parseEventLst(rule, fn, inject, true);
  1622. } else if (is.String(fn)) {
  1623. var val = parseFn(fn);
  1624. if (val && fn !== val) {
  1625. return val.__inject ? this.parseEvent(rule, val, inject, true) : val;
  1626. }
  1627. }
  1628. },
  1629. parseEmit: function parseEmit(ctx, on) {
  1630. var _this2 = this;
  1631. var event = {},
  1632. rule = ctx.rule,
  1633. emitPrefix = rule.emitPrefix,
  1634. field = rule.field,
  1635. name = rule.name,
  1636. inject = rule.inject;
  1637. var emit = rule[on ? 'emit' : 'nativeEmit'] || [];
  1638. if (is.trueArray(emit)) {
  1639. var emitKey = emitPrefix || field || name;
  1640. if (emitKey) {
  1641. if (!on) emitKey = "native-".concat(emitKey);
  1642. emit.forEach(function (eventName) {
  1643. if (!eventName) return;
  1644. var eventInject;
  1645. if (is.Object(eventName)) {
  1646. eventInject = eventName.inject;
  1647. eventName = eventName.name;
  1648. }
  1649. var fieldKey = toLine("".concat(emitKey, "-").concat(eventName));
  1650. var fn = function fn() {
  1651. var _this2$vm, _this2$vm2;
  1652. for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
  1653. arg[_key] = arguments[_key];
  1654. }
  1655. (_this2$vm = _this2.vm).$emit.apply(_this2$vm, [fieldKey].concat(arg));
  1656. (_this2$vm2 = _this2.vm).$emit.apply(_this2$vm2, ['emit-event', fieldKey].concat(arg));
  1657. };
  1658. fn.__emit = true;
  1659. if (!eventInject && inject === false) {
  1660. event[eventName] = fn;
  1661. } else {
  1662. var _inject = eventInject || inject || _this2.options.injectEvent;
  1663. event[eventName] = is.Undef(_inject) ? fn : _this2.inject(rule, fn, _inject);
  1664. }
  1665. });
  1666. }
  1667. }
  1668. ctx.computed[on ? 'on' : 'nativeOn'] = event;
  1669. return event;
  1670. },
  1671. getInjectData: function getInjectData(self, inject) {
  1672. var _this$vm$$options$pro = this.vm.$options.propsData,
  1673. option = _this$vm$$options$pro.option,
  1674. rule = _this$vm$$options$pro.rule;
  1675. return {
  1676. api: this.api,
  1677. $f: this.api,
  1678. rule: rule,
  1679. self: self.__origin__,
  1680. option: option,
  1681. inject: inject
  1682. };
  1683. },
  1684. inject: function inject(self, _fn, _inject2) {
  1685. if (_fn.__origin) {
  1686. if (this.watching && !this.loading) return _fn;
  1687. _fn = _fn.__origin;
  1688. }
  1689. var h = this;
  1690. var fn = function fn() {
  1691. var data = h.getInjectData(self, _inject2);
  1692. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  1693. args[_key2] = arguments[_key2];
  1694. }
  1695. data.args = [].concat(args);
  1696. args.unshift(data);
  1697. return _fn.apply(this, args);
  1698. };
  1699. fn.__origin = _fn;
  1700. fn.__json = _fn.__json;
  1701. return fn;
  1702. }
  1703. });
  1704. }
  1705. var EVENT = ['hook:updated', 'hook:mounted'];
  1706. function usePage(Handler) {
  1707. extend(Handler.prototype, {
  1708. usePage: function usePage() {
  1709. var _this = this;
  1710. var page = this.options.page;
  1711. if (!page) return;
  1712. var first = 25;
  1713. var limit = getLimit(this.rules);
  1714. if (is.Object(page)) {
  1715. if (page.first) first = parseInt(page.first, 10) || first;
  1716. if (page.limit) limit = parseInt(page.limit, 10) || limit;
  1717. }
  1718. extend(this, {
  1719. first: first,
  1720. limit: limit,
  1721. pageEnd: this.rules.length <= first
  1722. });
  1723. this.bus.$on('page-end', function () {
  1724. return _this.vm.$emit('page-end', _this.api);
  1725. });
  1726. this.pageLoad();
  1727. },
  1728. pageLoad: function pageLoad() {
  1729. var _this2 = this;
  1730. var pageFn = function pageFn() {
  1731. if (_this2.pageEnd) {
  1732. _this2.vm.$off(EVENT, pageFn);
  1733. _this2.bus.$emit('page-end');
  1734. } else {
  1735. _this2.first += _this2.limit;
  1736. _this2.pageEnd = _this2.rules.length <= _this2.first;
  1737. _this2.loadRule();
  1738. _this2.refresh();
  1739. }
  1740. };
  1741. this.vm.$on(EVENT, pageFn);
  1742. }
  1743. });
  1744. }
  1745. function getLimit(rules) {
  1746. return rules.length < 31 ? 31 : Math.ceil(rules.length / 3);
  1747. }
  1748. function useRender$1(Handler) {
  1749. extend(Handler.prototype, {
  1750. clearNextTick: function clearNextTick() {
  1751. this.nextTick && clearTimeout(this.nextTick);
  1752. this.nextTick = null;
  1753. },
  1754. bindNextTick: function bindNextTick(fn) {
  1755. var _this = this;
  1756. this.clearNextTick();
  1757. this.nextTick = setTimeout(function () {
  1758. fn();
  1759. _this.nextTick = null;
  1760. }, 10);
  1761. },
  1762. render: function render() {
  1763. ++this.loadedId;
  1764. if (this.vm.unique > 0) return this.$render.render();else {
  1765. this.vm.unique = 1;
  1766. return [];
  1767. }
  1768. }
  1769. });
  1770. }
  1771. function bind(ctx) {
  1772. Object.defineProperties(ctx.origin, {
  1773. __fc__: enumerable(ctx, true)
  1774. });
  1775. }
  1776. function RuleContext(handle, rule, defaultValue) {
  1777. var id = uniqueId();
  1778. extend(this, {
  1779. id: id,
  1780. ref: id,
  1781. wrapRef: id + 'fi',
  1782. rule: rule,
  1783. origin: rule.__origin__ || rule,
  1784. name: rule.name,
  1785. watch: [],
  1786. linkOn: [],
  1787. root: [],
  1788. ctrlRule: [],
  1789. parent: null,
  1790. cacheConfig: null,
  1791. prop: _objectSpread2({}, rule),
  1792. computed: {},
  1793. payload: {},
  1794. input: !!rule.field,
  1795. el: undefined,
  1796. defaultValue: rule.field ? deepCopy(defaultValue) : undefined,
  1797. field: rule.field || undefined
  1798. });
  1799. this.updateType();
  1800. this.updateKey();
  1801. bind(this);
  1802. this.update(handle, true);
  1803. }
  1804. extend(RuleContext.prototype, {
  1805. effectData: function effectData(name) {
  1806. if (!this.payload[name]) {
  1807. this.payload[name] = {};
  1808. }
  1809. return this.payload[name];
  1810. },
  1811. clearEffectData: function clearEffectData(name) {
  1812. if (name === undefined) {
  1813. this.payload = {};
  1814. } else {
  1815. delete this.payload[name];
  1816. }
  1817. },
  1818. updateKey: function updateKey(flag) {
  1819. this.key = uniqueId();
  1820. flag && this.parent && this.parent.updateKey(flag);
  1821. },
  1822. updateType: function updateType() {
  1823. this.originType = this.rule.type;
  1824. this.type = toCase(this.rule.type);
  1825. },
  1826. setParser: function setParser(parser) {
  1827. this.parser = parser;
  1828. parser.init(this);
  1829. },
  1830. initProp: function initProp() {
  1831. var _this = this;
  1832. var rule = _objectSpread2({}, this.rule);
  1833. delete rule.children;
  1834. this.prop = mergeProps([rule].concat(_toConsumableArray(Object.keys(this.payload).map(function (k) {
  1835. return _this.payload[k];
  1836. })), [this.computed]));
  1837. },
  1838. check: function check(handle) {
  1839. return this.vm === handle.vm;
  1840. },
  1841. unwatch: function unwatch() {
  1842. this.watch.forEach(function (un) {
  1843. return un();
  1844. });
  1845. this.watch = [];
  1846. },
  1847. unlink: function unlink() {
  1848. this.linkOn.forEach(function (un) {
  1849. return un();
  1850. });
  1851. this.linkOn = [];
  1852. },
  1853. link: function link() {
  1854. this.unlink();
  1855. this.$handle.appendLink(this);
  1856. },
  1857. watchTo: function watchTo() {
  1858. this.$handle.watchCtx(this);
  1859. },
  1860. "delete": function _delete() {
  1861. var undef = void 0;
  1862. this.unwatch();
  1863. this.unlink();
  1864. this.rmCtrl();
  1865. extend(this, {
  1866. deleted: true,
  1867. prop: _objectSpread2({}, this.rule),
  1868. computed: {},
  1869. el: undef,
  1870. $handle: undef,
  1871. $render: undef,
  1872. $api: undef,
  1873. vm: undef,
  1874. vNode: undef,
  1875. parent: null,
  1876. cacheConfig: null
  1877. });
  1878. },
  1879. rmCtrl: function rmCtrl() {
  1880. this.ctrlRule.forEach(function (ctrl) {
  1881. return ctrl.__fc__ && ctrl.__fc__.rm();
  1882. });
  1883. this.ctrlRule = [];
  1884. },
  1885. rm: function rm() {
  1886. var _this2 = this;
  1887. var _rm = function _rm() {
  1888. var index = _this2.root.indexOf(_this2.origin);
  1889. if (index > -1) {
  1890. _this2.root.splice(index, 1);
  1891. _this2.$handle && _this2.$handle.refresh();
  1892. }
  1893. };
  1894. if (this.deleted) {
  1895. _rm();
  1896. return;
  1897. }
  1898. this.$handle.noWatch(function () {
  1899. _this2.$handle.deferSyncValue(function () {
  1900. _this2.rmCtrl();
  1901. _rm();
  1902. _this2.$handle.rmCtx(_this2);
  1903. extend(_this2, {
  1904. root: []
  1905. });
  1906. }, _this2.input);
  1907. });
  1908. },
  1909. update: function update(handle, init) {
  1910. extend(this, {
  1911. deleted: false,
  1912. $handle: handle,
  1913. $render: handle.$render,
  1914. $api: handle.api,
  1915. vm: handle.vm,
  1916. trueType: handle.getType(this.originType),
  1917. vNode: handle.$render.vNode,
  1918. updated: false
  1919. });
  1920. !init && this.unwatch();
  1921. this.watchTo();
  1922. this.link();
  1923. }
  1924. });
  1925. function useLoader(Handler) {
  1926. extend(Handler.prototype, {
  1927. nextRefresh: function nextRefresh(fn) {
  1928. var _this = this;
  1929. var id = this.loadedId;
  1930. this.vm.$nextTick(function () {
  1931. id === _this.loadedId && (fn ? fn() : _this.refresh());
  1932. });
  1933. },
  1934. parseRule: function parseRule(_rule) {
  1935. var _this2 = this;
  1936. var rule = getRule(_rule);
  1937. Object.defineProperties(rule, {
  1938. __origin__: enumerable(_rule, true)
  1939. });
  1940. fullRule(rule);
  1941. this.appendValue(rule);
  1942. rule.options = Array.isArray(rule.options) ? rule.options : [];
  1943. [rule, rule['prefix'], rule['suffix']].forEach(function (item) {
  1944. if (!item) {
  1945. return;
  1946. }
  1947. _this2.loadFn(item, rule);
  1948. });
  1949. this.loadCtrl(rule);
  1950. if (rule.update) {
  1951. rule.update = parseFn(rule.update);
  1952. }
  1953. return rule;
  1954. },
  1955. loadFn: function loadFn(item, rule) {
  1956. var _this3 = this;
  1957. ['on', 'props', 'nativeOn', 'deep'].forEach(function (k) {
  1958. item[k] && _this3.parseInjectEvent(rule, item[k]);
  1959. });
  1960. },
  1961. loadCtrl: function loadCtrl(rule) {
  1962. rule.control && rule.control.forEach(function (ctrl) {
  1963. if (ctrl.handle) {
  1964. ctrl.handle = parseFn(ctrl.handle);
  1965. }
  1966. });
  1967. },
  1968. syncProp: function syncProp(ctx) {
  1969. var _this4 = this;
  1970. var rule = ctx.rule;
  1971. is.trueArray(rule.sync) && mergeProps([{
  1972. on: rule.sync.reduce(function (pre, prop) {
  1973. pre["update:".concat(prop)] = function (val) {
  1974. rule.props[prop] = val;
  1975. _this4.vm.$emit('sync', prop, val, rule, _this4.fapi);
  1976. };
  1977. return pre;
  1978. }, {})
  1979. }], ctx.computed);
  1980. },
  1981. loadRule: function loadRule() {
  1982. var _this5 = this;
  1983. this.cycleLoad = false;
  1984. this.loading = true;
  1985. if (this.pageEnd) {
  1986. this.bus.$emit('load-start');
  1987. }
  1988. this.deferSyncValue(function () {
  1989. _this5._loadRule(_this5.rules);
  1990. _this5.loading = false;
  1991. if (_this5.cycleLoad && _this5.pageEnd) {
  1992. return _this5.loadRule();
  1993. }
  1994. if (_this5.pageEnd) {
  1995. _this5.bus.$emit('load-end');
  1996. }
  1997. _this5.vm._renderRule();
  1998. _this5.$render.initOrgChildren();
  1999. _this5.syncForm();
  2000. });
  2001. },
  2002. loadChildren: function loadChildren(children, parent) {
  2003. this.cycleLoad = false;
  2004. this.loading = true;
  2005. this.bus.$emit('load-start');
  2006. this._loadRule(children, parent);
  2007. this.loading = false;
  2008. if (this.cycleLoad) {
  2009. return this.loadRule();
  2010. } else {
  2011. this.bus.$emit('load-end');
  2012. this.syncForm();
  2013. }
  2014. this.$render.clearCache(parent);
  2015. },
  2016. _loadRule: function _loadRule(rules, parent) {
  2017. var _this6 = this;
  2018. var preIndex = function preIndex(i) {
  2019. var pre = rules[i - 1];
  2020. if (!pre || !pre.__fc__) {
  2021. return i > 0 ? preIndex(i - 1) : -1;
  2022. }
  2023. var index = _this6.sort.indexOf(pre.__fc__.id);
  2024. return index > -1 ? index : preIndex(i - 1);
  2025. };
  2026. var loadChildren = function loadChildren(children, parent) {
  2027. if (is.trueArray(children)) {
  2028. _this6._loadRule(children, parent);
  2029. }
  2030. };
  2031. rules.map(function (_rule, index) {
  2032. if (parent && (is.String(_rule) || is.Undef(_rule))) return;
  2033. if (!_this6.pageEnd && !parent && index >= _this6.first) return;
  2034. if (_rule.__fc__ && _rule.__fc__.root === rules && _this6.ctxs[_rule.__fc__.id]) {
  2035. loadChildren(_rule.__fc__.rule.children, _rule.__fc__);
  2036. return _rule.__fc__;
  2037. }
  2038. var rule = getRule(_rule);
  2039. var isRepeat = function isRepeat() {
  2040. return !!(rule.field && _this6.fieldCtx[rule.field] && _this6.fieldCtx[rule.field][0] !== _rule.__fc__);
  2041. };
  2042. _this6.ruleEffect(rule, 'init', {
  2043. repeat: isRepeat()
  2044. });
  2045. if (isRepeat()) {
  2046. _this6.vm.$emit('repeat-field', _rule, _this6.api);
  2047. }
  2048. var ctx;
  2049. var isCopy = false;
  2050. var isInit = !!_rule.__fc__;
  2051. var defaultValue = rule.value;
  2052. if (isInit) {
  2053. ctx = _rule.__fc__;
  2054. defaultValue = ctx.defaultValue;
  2055. var check = !ctx.check(_this6);
  2056. if (ctx.deleted) {
  2057. if (check) {
  2058. if (isCtrl(ctx)) {
  2059. return;
  2060. }
  2061. ctx.update(_this6);
  2062. }
  2063. } else {
  2064. if (check) {
  2065. if (isCtrl(ctx)) {
  2066. return;
  2067. }
  2068. rules[index] = _rule = _rule._clone ? _rule._clone() : copyRule(_rule);
  2069. ctx = null;
  2070. isCopy = true;
  2071. }
  2072. }
  2073. }
  2074. if (!ctx) {
  2075. var _rule2 = _this6.parseRule(_rule);
  2076. ctx = new RuleContext(_this6, _rule2, isInit ? defaultValue : _rule2.value);
  2077. _this6.bindParser(ctx);
  2078. } else {
  2079. if (ctx.originType !== ctx.rule.type) {
  2080. ctx.updateType();
  2081. _this6.bindParser(ctx);
  2082. }
  2083. _this6.appendValue(ctx.rule);
  2084. }
  2085. [false, true].forEach(function (b) {
  2086. return _this6.parseEmit(ctx, b);
  2087. });
  2088. _this6.syncProp(ctx);
  2089. ctx.parent = parent || null;
  2090. ctx.root = rules;
  2091. _this6.setCtx(ctx);
  2092. !isCopy && !isInit && _this6.effect(ctx, 'load');
  2093. ctx.parser.loadChildren === false || loadChildren(ctx.rule.children, ctx);
  2094. if (!parent) {
  2095. var _preIndex = preIndex(index);
  2096. if (_preIndex > -1 || !index) {
  2097. _this6.sort.splice(_preIndex + 1, 0, ctx.id);
  2098. } else {
  2099. _this6.sort.push(ctx.id);
  2100. }
  2101. }
  2102. var r = ctx.rule;
  2103. if (!ctx.updated) {
  2104. ctx.updated = true;
  2105. if (is.Function(r.update)) {
  2106. _this6.bus.$once('load-end', function () {
  2107. _this6.refreshUpdate(ctx, r.value);
  2108. });
  2109. }
  2110. _this6.effect(ctx, 'loaded');
  2111. }
  2112. if (ctx.input) Object.defineProperty(r, 'value', _this6.valueHandle(ctx));
  2113. if (_this6.refreshControl(ctx)) _this6.cycleLoad = true;
  2114. return ctx;
  2115. });
  2116. },
  2117. refreshControl: function refreshControl(ctx) {
  2118. return ctx.input && ctx.rule.control && this.useCtrl(ctx);
  2119. },
  2120. useCtrl: function useCtrl(ctx) {
  2121. var _this7 = this;
  2122. var controls = getCtrl(ctx),
  2123. validate = [],
  2124. api = this.api;
  2125. if (!controls.length) return false;
  2126. var _loop = function _loop(i) {
  2127. var control = controls[i],
  2128. handleFn = control.handle || function (val) {
  2129. return val === control.value;
  2130. };
  2131. if (!is.trueArray(control.rule)) return "continue";
  2132. var data = _objectSpread2(_objectSpread2({}, control), {}, {
  2133. valid: invoke(function () {
  2134. return handleFn(ctx.rule.value, api);
  2135. }),
  2136. ctrl: findCtrl(ctx, control.rule),
  2137. isHidden: is.String(control.rule[0])
  2138. });
  2139. if (data.valid && data.ctrl || !data.valid && !data.ctrl && !data.isHidden) return "continue";
  2140. validate.push(data);
  2141. };
  2142. for (var i = 0; i < controls.length; i++) {
  2143. var _ret = _loop(i);
  2144. if (_ret === "continue") continue;
  2145. }
  2146. if (!validate.length) return false;
  2147. var hideLst = [];
  2148. var flag = false;
  2149. this.deferSyncValue(function () {
  2150. validate.reverse().forEach(function (_ref) {
  2151. var isHidden = _ref.isHidden,
  2152. valid = _ref.valid,
  2153. rule = _ref.rule,
  2154. prepend = _ref.prepend,
  2155. append = _ref.append,
  2156. child = _ref.child,
  2157. ctrl = _ref.ctrl;
  2158. if (isHidden) {
  2159. valid ? ctx.ctrlRule.push({
  2160. __ctrl: true,
  2161. children: rule,
  2162. valid: valid
  2163. }) : ctx.ctrlRule.splice(ctx.ctrlRule.indexOf(ctrl), 1);
  2164. hideLst[valid ? 'push' : 'unshift'](function () {
  2165. _this7.api.hidden(!valid, rule);
  2166. });
  2167. return;
  2168. }
  2169. if (valid) {
  2170. flag = true;
  2171. var ruleCon = {
  2172. type: 'fcFragment',
  2173. "native": true,
  2174. __ctrl: true,
  2175. children: rule
  2176. };
  2177. ctx.ctrlRule.push(ruleCon);
  2178. _this7.bus.$once('load-start', function () {
  2179. if (prepend) {
  2180. api.prepend(ruleCon, prepend, child);
  2181. } else if (append || child) {
  2182. api.append(ruleCon, append || ctx.id, child);
  2183. } else {
  2184. ctx.root.splice(ctx.root.indexOf(ctx.origin) + 1, 0, ruleCon);
  2185. }
  2186. });
  2187. } else {
  2188. ctx.ctrlRule.splice(ctx.ctrlRule.indexOf(ctrl), 1);
  2189. var ctrlCtx = byCtx(ctrl);
  2190. ctrlCtx && ctrlCtx.rm();
  2191. }
  2192. });
  2193. });
  2194. hideLst.length && this.vm.$nextTick(function () {
  2195. hideLst.forEach(function (v) {
  2196. return v();
  2197. });
  2198. });
  2199. this.vm.$emit('control', ctx.origin, this.api);
  2200. this.effect(ctx, 'control');
  2201. return flag;
  2202. },
  2203. reloadRule: function reloadRule(rules) {
  2204. return this._reloadRule(rules);
  2205. },
  2206. _reloadRule: function _reloadRule(rules) {
  2207. var _this8 = this;
  2208. if (!rules) rules = this.rules;
  2209. var ctxs = _objectSpread2({}, this.ctxs);
  2210. this.clearNextTick();
  2211. this.$render.clearOrgChildren();
  2212. this.initData(rules);
  2213. this.fc.rules = rules;
  2214. this.deferSyncValue(function () {
  2215. _this8.bus.$once('load-end', function () {
  2216. Object.keys(ctxs).filter(function (id) {
  2217. return _this8.ctxs[id] === undefined;
  2218. }).forEach(function (id) {
  2219. return _this8.rmCtx(ctxs[id]);
  2220. });
  2221. _this8.$render.clearCacheAll();
  2222. });
  2223. _this8.reloading = true;
  2224. _this8.loadRule();
  2225. _this8.reloading = false;
  2226. _this8.refresh();
  2227. _this8.vm.$emit('reloading', _this8.api);
  2228. });
  2229. this.bus.$off('next-tick', this.nextReload);
  2230. this.bus.$once('next-tick', this.nextReload);
  2231. this.vm.$emit('update', this.api);
  2232. },
  2233. refresh: function refresh() {
  2234. this.vm._refresh();
  2235. }
  2236. });
  2237. }
  2238. function fullRule(rule) {
  2239. var def = baseRule();
  2240. Object.keys(def).forEach(function (k) {
  2241. if (!hasProperty(rule, k)) $set(rule, k, def[k]);
  2242. });
  2243. return rule;
  2244. }
  2245. function getCtrl(ctx) {
  2246. var control = ctx.rule.control || [];
  2247. if (is.Object(control)) return [control];else return control;
  2248. }
  2249. function findCtrl(ctx, rule) {
  2250. for (var i = 0; i < ctx.ctrlRule.length; i++) {
  2251. var ctrl = ctx.ctrlRule[i];
  2252. if (ctrl.children === rule) return ctrl;
  2253. }
  2254. }
  2255. function isCtrl(ctx) {
  2256. return !!ctx.rule.__ctrl;
  2257. }
  2258. function useInput(Handler) {
  2259. extend(Handler.prototype, {
  2260. getValue: function getValue(ctx) {
  2261. if (is.Undef(ctx.cacheValue)) {
  2262. ctx.cacheValue = ctx.parser.toValue(this.getFormData(ctx), ctx);
  2263. }
  2264. return ctx.cacheValue;
  2265. },
  2266. setValue: function setValue(ctx, value, formValue, setFlag) {
  2267. if (ctx.deleted) return;
  2268. ctx.cacheValue = value;
  2269. this.changeStatus = true;
  2270. this.nextRefresh();
  2271. this.$render.clearCache(ctx);
  2272. this.setFormData(ctx, formValue);
  2273. this.syncValue();
  2274. this.valueChange(ctx, value);
  2275. this.vm.$emit('change', ctx.field, value, ctx.origin, this.api, setFlag || false);
  2276. this.effect(ctx, 'value');
  2277. },
  2278. onInput: function onInput(ctx, value) {
  2279. var val;
  2280. if (ctx.input && (this.isQuote(ctx, val = ctx.parser.toValue(value, ctx)) || this.isChange(ctx, val))) {
  2281. this.setValue(ctx, val, value);
  2282. }
  2283. },
  2284. setFormData: function setFormData(ctx, value) {
  2285. $set(this.formData, ctx.id, value === null ? undefined : value);
  2286. },
  2287. getFormData: function getFormData(ctx) {
  2288. return this.formData[ctx.id];
  2289. },
  2290. validate: function validate() {
  2291. var _this = this;
  2292. toEmpty(this.vm.validate);
  2293. this.fields().forEach(function (id) {
  2294. _this.fieldCtx[id].forEach(function (ctx) {
  2295. _this.vm.validate[ctx.id] = toArray(ctx.rule.validate);
  2296. });
  2297. });
  2298. return this.vm.validate;
  2299. },
  2300. syncForm: function syncForm() {
  2301. var _this2 = this;
  2302. toEmpty(this.form);
  2303. Object.defineProperties(this.form, this.fields().reduce(function (initial, field) {
  2304. var ctx = _this2.getFieldCtx(field);
  2305. var handle = _this2.valueHandle(ctx);
  2306. handle.configurable = true;
  2307. initial[field] = handle;
  2308. return initial;
  2309. }, this.options.appendValue ? Object.keys(this.appendData).reduce(function (initial, field) {
  2310. initial[field] = {
  2311. enumerable: true,
  2312. configurable: true,
  2313. get: function get() {
  2314. return _this2.appendData[field];
  2315. },
  2316. set: function set(val) {
  2317. _this2.appendData[field] = val;
  2318. }
  2319. };
  2320. return initial;
  2321. }, {}) : {}));
  2322. this.syncValue();
  2323. },
  2324. valueHandle: function valueHandle(ctx) {
  2325. var _this3 = this;
  2326. return {
  2327. enumerable: true,
  2328. get: function get() {
  2329. return _this3.getValue(ctx);
  2330. },
  2331. set: function set(value) {
  2332. if (_this3.isChange(ctx, value)) {
  2333. _this3.setValue(ctx, value, ctx.parser.toFormValue(value, ctx), true);
  2334. }
  2335. }
  2336. };
  2337. },
  2338. appendValue: function appendValue(rule) {
  2339. if (!rule.field || !hasProperty(this.appendData, rule.field)) return;
  2340. rule.value = this.appendData[rule.field];
  2341. delete this.appendData[rule.field];
  2342. },
  2343. addSubForm: function addSubForm(ctx, subForm) {
  2344. this.subForm[ctx.id] = subForm;
  2345. },
  2346. deferSyncValue: function deferSyncValue(fn, sync) {
  2347. if (!this.deferSyncFn) {
  2348. this.deferSyncFn = fn;
  2349. }
  2350. if (!this.deferSyncFn.sync) {
  2351. this.deferSyncFn.sync = sync;
  2352. }
  2353. invoke(fn);
  2354. if (this.deferSyncFn === fn) {
  2355. this.deferSyncFn = null;
  2356. if (fn.sync) {
  2357. this.syncValue();
  2358. }
  2359. }
  2360. },
  2361. syncValue: function syncValue() {
  2362. if (this.deferSyncFn) {
  2363. return this.deferSyncFn.sync = true;
  2364. }
  2365. this.vm._updateValue(_objectSpread2(_objectSpread2({}, this.options.appendValue ? this.appendData : {}), this.form));
  2366. },
  2367. isChange: function isChange(ctx, value) {
  2368. return JSON.stringify(ctx.rule.value, strFn) !== JSON.stringify(value === null ? undefined : value, strFn);
  2369. },
  2370. isQuote: function isQuote(ctx, value) {
  2371. return (is.Object(value) || Array.isArray(value)) && value === ctx.rule.value;
  2372. },
  2373. refreshUpdate: function refreshUpdate(ctx, val) {
  2374. var _this4 = this;
  2375. if (is.Function(ctx.rule.update)) {
  2376. var state = invoke(function () {
  2377. return ctx.rule.update(val, ctx.origin, _this4.api);
  2378. });
  2379. if (state === undefined) return;
  2380. ctx.rule.hidden = state === true;
  2381. }
  2382. },
  2383. valueChange: function valueChange(ctx, val) {
  2384. this.refreshRule(ctx, val);
  2385. this.bus.$emit('change-' + ctx.field, val);
  2386. },
  2387. refreshRule: function refreshRule(ctx, val) {
  2388. if (this.refreshControl(ctx)) {
  2389. this.$render.clearCacheAll();
  2390. this.loadRule();
  2391. this.vm.$emit('update', this.api);
  2392. this.refresh();
  2393. }
  2394. this.refreshUpdate(ctx, val);
  2395. },
  2396. appendLink: function appendLink(ctx) {
  2397. var _this5 = this;
  2398. var link = ctx.rule.link;
  2399. is.trueArray(link) && link.forEach(function (field) {
  2400. var fn = function fn() {
  2401. return _this5.refreshRule(ctx, ctx.rule.value);
  2402. };
  2403. _this5.bus.$on('change-' + field, fn);
  2404. ctx.linkOn.push(function () {
  2405. return _this5.bus.$off('change-' + field, fn);
  2406. });
  2407. });
  2408. },
  2409. fields: function fields() {
  2410. return Object.keys(this.fieldCtx);
  2411. }
  2412. });
  2413. }
  2414. function strFn(key, val) {
  2415. return typeof val === 'function' ? '' + val : val;
  2416. }
  2417. function toEmpty(obj) {
  2418. Object.keys(obj).forEach(function (k) {
  2419. return delete obj[k];
  2420. });
  2421. }
  2422. var BaseParser = {
  2423. init: function init(ctx) {},
  2424. toFormValue: function toFormValue(value, ctx) {
  2425. return value;
  2426. },
  2427. toValue: function toValue(formValue, ctx) {
  2428. return formValue;
  2429. },
  2430. mounted: function mounted(ctx) {},
  2431. render: function render(children, ctx) {
  2432. return ctx.$render.defaultRender(ctx, children);
  2433. },
  2434. preview: function preview(children, ctx) {
  2435. return this.render(children, ctx);
  2436. },
  2437. mergeProp: function mergeProp(ctx) {}
  2438. };
  2439. var noneKey = ['field', 'value', 'vm', 'template', 'name', 'config', 'control', 'inject', 'sync', 'payload', 'optionsTo', 'update', 'component', 'cache'];
  2440. function useContext(Handler) {
  2441. extend(Handler.prototype, {
  2442. getCtx: function getCtx(id) {
  2443. return this.getFieldCtx(id) || this.getNameCtx(id)[0] || this.ctxs[id];
  2444. },
  2445. getCtxs: function getCtxs(id) {
  2446. return this.fieldCtx[id] || this.nameCtx[id] || (this.ctxs[id] ? [this.ctxs[id]] : []);
  2447. },
  2448. setIdCtx: function setIdCtx(ctx, key, type) {
  2449. var field = "".concat(type, "Ctx");
  2450. if (!this[field][key]) {
  2451. this[field][key] = [ctx];
  2452. } else {
  2453. this[field][key].push(ctx);
  2454. }
  2455. },
  2456. rmIdCtx: function rmIdCtx(ctx, key, type) {
  2457. var field = "".concat(type, "Ctx");
  2458. var lst = this[field][key];
  2459. if (!lst) return false;
  2460. var flag = lst.splice(lst.indexOf(ctx) >>> 0, 1).length > 0;
  2461. if (!lst.length) {
  2462. delete this[field][key];
  2463. }
  2464. return flag;
  2465. },
  2466. getFieldCtx: function getFieldCtx(field) {
  2467. return (this.fieldCtx[field] || [])[0];
  2468. },
  2469. getNameCtx: function getNameCtx(name) {
  2470. return this.nameCtx[name] || [];
  2471. },
  2472. setCtx: function setCtx(ctx) {
  2473. var id = ctx.id,
  2474. field = ctx.field,
  2475. name = ctx.name,
  2476. rule = ctx.rule;
  2477. this.ctxs[id] = ctx;
  2478. name && this.setIdCtx(ctx, name, 'name');
  2479. if (!ctx.input) return;
  2480. this.setIdCtx(ctx, field, 'field');
  2481. this.setFormData(ctx, ctx.parser.toFormValue(rule.value, ctx));
  2482. if (this.isMounted && !this.reloading) {
  2483. this.vm.$emit('change', ctx.field, rule.value, ctx.origin, this.api);
  2484. }
  2485. },
  2486. getParser: function getParser(ctx) {
  2487. var list = this.fc.parsers;
  2488. return list[ctx.originType] || list[toCase(ctx.type)] || list[ctx.trueType] || BaseParser;
  2489. },
  2490. bindParser: function bindParser(ctx) {
  2491. ctx.setParser(this.getParser(ctx));
  2492. },
  2493. getType: function getType(alias) {
  2494. var map = this.fc.CreateNode.aliasMap;
  2495. var type = map[alias] || map[toCase(alias)] || alias;
  2496. return toCase(type);
  2497. },
  2498. noWatch: function noWatch(fn) {
  2499. if (!this.noWatchFn) {
  2500. this.noWatchFn = fn;
  2501. }
  2502. invoke(fn);
  2503. if (this.noWatchFn === fn) {
  2504. this.noWatchFn = null;
  2505. }
  2506. },
  2507. watchCtx: function watchCtx(ctx) {
  2508. var _this = this;
  2509. var vm = this.vm;
  2510. Object.keys(ctx.rule).filter(function (k) {
  2511. return k[0] !== '_' && k[0] !== '$' && noneKey.indexOf(k) === -1;
  2512. }).forEach(function (key) {
  2513. var flag = key === 'children';
  2514. ctx.watch.push(vm.$watch(function () {
  2515. return ctx.rule[key];
  2516. }, function (n, o) {
  2517. if (_this.loading || _this.noWatchFn || _this.reloading) return;
  2518. if (flag && ctx.parser.loadChildren === false) {
  2519. _this.$render.clearCache(ctx);
  2520. _this.nextRefresh();
  2521. return;
  2522. }
  2523. _this.watching = true;
  2524. if (key === 'link') {
  2525. ctx.link();
  2526. return;
  2527. } else if (['props', 'on', 'nativeOn', 'deep'].indexOf(key) > -1) {
  2528. _this.parseInjectEvent(ctx.rule, n || {});
  2529. if (key === 'props' && ctx.input) {
  2530. _this.setFormData(ctx, ctx.parser.toFormValue(ctx.rule.value, ctx));
  2531. }
  2532. } else if (['emit', 'nativeEmit'].indexOf(key) > -1) _this.parseEmit(ctx, key === 'emit');else if (['prefix', 'suffix'].indexOf(key) > -1) n && _this.loadFn(n, ctx.rule);else if (key === 'type') {
  2533. ctx.updateType();
  2534. _this.bindParser(ctx);
  2535. } else if (key === 'children') {
  2536. var _flag = is.trueArray(n);
  2537. _this.deferSyncValue(function () {
  2538. if (n !== o) {
  2539. _this.rmSub(o, ctx);
  2540. _this.$render.initOrgChildren();
  2541. }
  2542. _flag && _this.loadChildren(n, ctx);
  2543. _this.vm.$emit('update', _this.api);
  2544. });
  2545. }
  2546. _this.$render.clearCache(ctx);
  2547. _this.refresh();
  2548. _this.watching = false;
  2549. }, {
  2550. deep: !flag,
  2551. sync: flag
  2552. }));
  2553. });
  2554. this.watchEffect(ctx);
  2555. },
  2556. rmSub: function rmSub(sub, ctx) {
  2557. var _this2 = this;
  2558. is.trueArray(sub) && sub.forEach(function (r) {
  2559. r && r.__fc__ && r.__fc__.parent === ctx && _this2.rmCtx(r.__fc__);
  2560. });
  2561. },
  2562. rmCtx: function rmCtx(ctx) {
  2563. var _this3 = this;
  2564. if (ctx.deleted) return;
  2565. var id = ctx.id,
  2566. field = ctx.field,
  2567. input = ctx.input,
  2568. name = ctx.name;
  2569. if (ctx.input) {
  2570. Object.defineProperty(ctx.rule, 'value', {
  2571. value: ctx.rule.value,
  2572. writable: true
  2573. });
  2574. }
  2575. $del(this.ctxs, id);
  2576. $del(this.$render.tempList, id);
  2577. $del(this.$render.orgChildren, id);
  2578. $del(this.vm.ctxInject, id);
  2579. $del(this.formData, id);
  2580. $del(this.subForm, id);
  2581. $del(ctx, 'cacheValue');
  2582. input && this.rmIdCtx(ctx, field, 'field');
  2583. name && this.rmIdCtx(ctx, name, 'name');
  2584. if (input && !hasProperty(this.fieldCtx, field)) {
  2585. $del(this.form, field);
  2586. }
  2587. this.deferSyncValue(function () {
  2588. if (!_this3.reloading) {
  2589. if (ctx.parser.loadChildren !== false) {
  2590. if (is.trueArray(ctx.rule.children)) {
  2591. ctx.rule.children.forEach(function (h) {
  2592. return h.__fc__ && _this3.rmCtx(h.__fc__);
  2593. });
  2594. }
  2595. }
  2596. if (ctx.root === _this3.rules) {
  2597. _this3.vm._renderRule();
  2598. }
  2599. }
  2600. }, input);
  2601. var index = this.sort.indexOf(id);
  2602. if (index > -1) {
  2603. this.sort.splice(index, 1);
  2604. }
  2605. this.$render.clearCache(ctx);
  2606. ctx["delete"]();
  2607. this.effect(ctx, 'deleted');
  2608. input && !this.fieldCtx[field] && this.vm.$emit('removeField', field, ctx.rule, this.api);
  2609. ctx.rule.__ctrl || this.vm.$emit('removeRule', ctx.rule, this.api);
  2610. return ctx;
  2611. }
  2612. });
  2613. }
  2614. function useLifecycle(Handler) {
  2615. extend(Handler.prototype, {
  2616. mounted: function mounted() {
  2617. var _this = this;
  2618. var _mounted = function _mounted() {
  2619. _this.isMounted = true;
  2620. _this.lifecycle('mounted');
  2621. };
  2622. if (this.pageEnd) {
  2623. _mounted();
  2624. } else {
  2625. this.bus.$once('page-end', _mounted);
  2626. }
  2627. },
  2628. lifecycle: function lifecycle(name) {
  2629. var _this2 = this;
  2630. var fn = this.options[name];
  2631. is.Function(fn) && invoke(function () {
  2632. return fn(_this2.api);
  2633. });
  2634. this.vm.$emit(name, this.api);
  2635. }
  2636. });
  2637. }
  2638. function useEffect(Handler) {
  2639. extend(Handler.prototype, {
  2640. useProvider: function useProvider() {
  2641. var _this = this;
  2642. var ps = this.fc.providers;
  2643. Object.keys(ps).forEach(function (k) {
  2644. var prop = ps[k];
  2645. if (is.Function(prop)) {
  2646. prop = prop(_this.fc);
  2647. }
  2648. prop._c = getComponent(prop);
  2649. _this.onEffect(prop);
  2650. _this.providers[k] = prop;
  2651. });
  2652. },
  2653. onEffect: function onEffect(provider) {
  2654. var _this2 = this;
  2655. var used = [];
  2656. (provider._c || ['*']).forEach(function (name) {
  2657. var type = name === '*' ? '*' : _this2.getType(name);
  2658. if (used.indexOf(type) > -1) return;
  2659. used.push(type);
  2660. _this2.bus.$on("p:".concat(provider.name, ":").concat(type, ":").concat(provider.input ? 1 : 0), function (event, args) {
  2661. provider[event] && provider[event].apply(provider, _toConsumableArray(args));
  2662. });
  2663. });
  2664. provider._used = used;
  2665. },
  2666. watchEffect: function watchEffect(ctx) {
  2667. var _this3 = this;
  2668. var vm = this.vm;
  2669. var effect = {};
  2670. Object.keys(ctx.rule.effect || {}).forEach(function (k) {
  2671. effect[k] = function () {
  2672. return ctx.rule.effect[k];
  2673. };
  2674. });
  2675. Object.keys(ctx.rule).forEach(function (k) {
  2676. if (k[0] === '$') {
  2677. effect[k.substr(1)] = function () {
  2678. return ctx.rule[k];
  2679. };
  2680. }
  2681. });
  2682. Object.keys(effect || {}).forEach(function (k) {
  2683. ctx.watch.push(vm.$watch(effect[k], function (n) {
  2684. _this3.effect(ctx, 'watch', _defineProperty({}, k, n));
  2685. }, {
  2686. deep: true
  2687. }));
  2688. });
  2689. },
  2690. ruleEffect: function ruleEffect(rule, event, append) {
  2691. this.emitEffect({
  2692. rule: rule,
  2693. input: !!rule.field,
  2694. type: this.getType(rule.type)
  2695. }, event, append);
  2696. },
  2697. effect: function effect(ctx, event, custom) {
  2698. this.emitEffect({
  2699. rule: ctx.rule,
  2700. input: ctx.input,
  2701. type: ctx.trueType,
  2702. ctx: ctx,
  2703. custom: custom
  2704. }, event);
  2705. },
  2706. getEffect: function getEffect(rule, name) {
  2707. if (hasProperty(rule, '$' + name)) {
  2708. return rule['$' + name];
  2709. }
  2710. if (hasProperty(rule, 'effect') && hasProperty(rule.effect, name)) return rule.effect[name];
  2711. return undefined;
  2712. },
  2713. emitEffect: function emitEffect(_ref, event, append) {
  2714. var _this4 = this;
  2715. var ctx = _ref.ctx,
  2716. rule = _ref.rule,
  2717. input = _ref.input,
  2718. type = _ref.type,
  2719. custom = _ref.custom;
  2720. if (!type || type === 'fcFragment') return;
  2721. var effect = custom ? custom : Object.keys(rule).reduce(function (i, k) {
  2722. if (k[0] === '$') {
  2723. i[k.substr(1)] = rule[k];
  2724. }
  2725. return i;
  2726. }, _objectSpread2({}, rule.effect || {}));
  2727. Object.keys(effect).forEach(function (attr) {
  2728. var p = _this4.providers[attr];
  2729. if (!p || p.input && !input) return;
  2730. var _type;
  2731. if (!p._c) {
  2732. _type = '*';
  2733. } else if (p._used.indexOf(type) > -1) {
  2734. _type = type;
  2735. } else {
  2736. return;
  2737. }
  2738. var data = _objectSpread2({
  2739. value: effect[attr],
  2740. getValue: function getValue() {
  2741. return _this4.getEffect(rule, attr);
  2742. }
  2743. }, append || {});
  2744. if (ctx) {
  2745. data.getProp = function () {
  2746. return ctx.effectData(attr);
  2747. };
  2748. data.clearProp = function () {
  2749. return ctx.clearEffectData(attr);
  2750. };
  2751. data.mergeProp = function (prop) {
  2752. return mergeProps([prop], data.getProp());
  2753. };
  2754. }
  2755. _this4.bus.$emit("p:".concat(attr, ":").concat(_type, ":").concat(p.input ? 1 : 0), event, [data, rule, _this4.api]);
  2756. });
  2757. }
  2758. });
  2759. }
  2760. function unique(arr) {
  2761. return arr.filter(function (item, index, arr) {
  2762. return arr.indexOf(item, 0) === index;
  2763. });
  2764. }
  2765. function getComponent(p) {
  2766. var c = p.components;
  2767. if (Array.isArray(c)) return unique(c.filter(function (v) {
  2768. return v !== '*';
  2769. }));else if (is.String(c)) return [c];else return false;
  2770. }
  2771. function Handler(fc) {
  2772. var _this = this;
  2773. extend(this, {
  2774. fc: fc,
  2775. vm: fc.vm,
  2776. watching: false,
  2777. loading: false,
  2778. reloading: false,
  2779. noWatchFn: null,
  2780. deferSyncFn: null,
  2781. isMounted: false,
  2782. formData: {},
  2783. subForm: {},
  2784. form: {},
  2785. appendData: {},
  2786. providers: {},
  2787. cycleLoad: null,
  2788. loadedId: 1,
  2789. nextTick: null,
  2790. changeStatus: false,
  2791. pageEnd: true,
  2792. nextReload: function nextReload() {
  2793. _this.lifecycle('reload');
  2794. }
  2795. });
  2796. funcProxy(this, {
  2797. options: function options() {
  2798. return fc.options;
  2799. },
  2800. bus: function bus() {
  2801. return fc.bus;
  2802. }
  2803. });
  2804. this.initData(fc.rules);
  2805. this.$manager = new fc.manager(this);
  2806. this.$render = new Render(this);
  2807. this.api = fc.extendApi(Api(this), this);
  2808. }
  2809. extend(Handler.prototype, {
  2810. initData: function initData(rules) {
  2811. extend(this, {
  2812. ctxs: {},
  2813. fieldCtx: {},
  2814. nameCtx: {},
  2815. sort: [],
  2816. rules: rules
  2817. });
  2818. },
  2819. init: function init() {
  2820. this.appendData = _objectSpread2(_objectSpread2(_objectSpread2({}, this.fc.options.formData || {}), this.vm.value || {}), this.appendData);
  2821. this.useProvider();
  2822. this.usePage();
  2823. this.loadRule();
  2824. this.$manager.__init();
  2825. this.vm.$set(this.vm, 'formData', this.formData);
  2826. }
  2827. });
  2828. useInject(Handler);
  2829. usePage(Handler);
  2830. useRender$1(Handler);
  2831. useLoader(Handler);
  2832. useInput(Handler);
  2833. useContext(Handler);
  2834. useLifecycle(Handler);
  2835. useEffect(Handler);
  2836. function getError(action, option, xhr) {
  2837. var msg = "fail to ".concat(action, " ").concat(xhr.status, "'");
  2838. var err = new Error(msg);
  2839. err.status = xhr.status;
  2840. err.url = action;
  2841. return err;
  2842. }
  2843. function getBody(xhr) {
  2844. var text = xhr.responseText || xhr.response;
  2845. if (!text) {
  2846. return text;
  2847. }
  2848. try {
  2849. return JSON.parse(text);
  2850. } catch (e) {
  2851. return text;
  2852. }
  2853. }
  2854. function fetch(option) {
  2855. if (typeof XMLHttpRequest === 'undefined') {
  2856. return;
  2857. }
  2858. var xhr = new XMLHttpRequest();
  2859. var action = option.action;
  2860. xhr.onerror = function error(e) {
  2861. option.onError(e);
  2862. };
  2863. xhr.onload = function onload() {
  2864. if (xhr.status < 200 || xhr.status >= 300) {
  2865. return option.onError(getError(action, option, xhr), getBody(xhr));
  2866. }
  2867. option.onSuccess(getBody(xhr));
  2868. };
  2869. xhr.open(option.method || 'get', action, true);
  2870. var formData;
  2871. if (option.data) {
  2872. if ((option.dataType || '').toLowerCase() !== 'json') {
  2873. formData = new FormData();
  2874. Object.keys(option.data).map(function (key) {
  2875. formData.append(key, option.data[key]);
  2876. });
  2877. } else {
  2878. formData = JSON.stringify(option.data);
  2879. xhr.setRequestHeader('content-type', 'application/json');
  2880. }
  2881. }
  2882. if (option.withCredentials && 'withCredentials' in xhr) {
  2883. xhr.withCredentials = true;
  2884. }
  2885. var headers = option.headers || {};
  2886. Object.keys(headers).forEach(function (item) {
  2887. if (headers[item] !== null) {
  2888. xhr.setRequestHeader(item, headers[item]);
  2889. }
  2890. });
  2891. xhr.send(formData);
  2892. }
  2893. var NAME$1 = 'fcFragment';
  2894. var fragment = {
  2895. name: NAME$1,
  2896. functional: true,
  2897. props: ['vnode'],
  2898. render: function render(h, ctx) {
  2899. return ctx.props.vnode ? ctx.props.vnode : ctx.children;
  2900. }
  2901. };
  2902. function parseProp(prop) {
  2903. if (is.String(prop)) return {
  2904. domProps: {
  2905. innerHTML: prop
  2906. }
  2907. };
  2908. return prop;
  2909. }
  2910. function CreateNodeFactory() {
  2911. var aliasMap = {};
  2912. function CreateNode(vm) {
  2913. vm && this.setVm(vm);
  2914. }
  2915. extend(CreateNode.prototype, {
  2916. setVm: function setVm(vm) {
  2917. this.vm = vm;
  2918. this.$h = vm.$createElement;
  2919. },
  2920. make: function make(tag, data, children) {
  2921. if (Vue.config.isReservedTag(tag) && data.nativeOn) delete data.nativeOn;
  2922. return this.makeComponent(tag, data, children);
  2923. },
  2924. makeComponent: function makeComponent(type, data, children) {
  2925. var Node = this.$h(type, parseProp(data), children || []);
  2926. Node.context = this.vm;
  2927. return Node;
  2928. },
  2929. aliasMap: aliasMap
  2930. });
  2931. extend(CreateNode, {
  2932. aliasMap: aliasMap,
  2933. alias: function alias(_alias, name) {
  2934. aliasMap[_alias] = name;
  2935. },
  2936. use: function use(nodes) {
  2937. Object.keys(nodes).forEach(function (k) {
  2938. var line = toLine(k);
  2939. var lower = toString(k).toLocaleLowerCase();
  2940. var v = nodes[k];
  2941. [k, line, lower].forEach(function (n) {
  2942. CreateNode.alias(k, v);
  2943. CreateNode.prototype[n] = function (data, children) {
  2944. return this.make(v, data, children);
  2945. };
  2946. });
  2947. });
  2948. }
  2949. });
  2950. return CreateNode;
  2951. }
  2952. function createManager(proto) {
  2953. var CustomManager = function (_Manager) {
  2954. _inherits(CustomManager, _Manager);
  2955. var _super = _createSuper(CustomManager);
  2956. function CustomManager() {
  2957. _classCallCheck(this, CustomManager);
  2958. return _super.apply(this, arguments);
  2959. }
  2960. return CustomManager;
  2961. }(Manager);
  2962. Object.assign(CustomManager.prototype, proto);
  2963. return CustomManager;
  2964. }
  2965. function Manager(handler) {
  2966. extend(this, {
  2967. $handle: handler,
  2968. vm: handler.vm,
  2969. options: {},
  2970. ref: 'fcForm',
  2971. mergeOptionsRule: {
  2972. normal: ['form', 'row', 'info', 'submitBtn', 'resetBtn']
  2973. }
  2974. });
  2975. this.updateKey();
  2976. this.init();
  2977. }
  2978. extend(Manager.prototype, {
  2979. __init: function __init() {
  2980. var _this = this;
  2981. this.$render = this.$handle.$render;
  2982. this.$r = function () {
  2983. var _this$$render;
  2984. return (_this$$render = _this.$render).renderRule.apply(_this$$render, arguments);
  2985. };
  2986. },
  2987. updateKey: function updateKey() {
  2988. this.key = uniqueId();
  2989. },
  2990. init: function init() {},
  2991. update: function update() {},
  2992. beforeRender: function beforeRender() {},
  2993. form: function form() {
  2994. return this.vm.$refs[this.ref];
  2995. },
  2996. mergeOptions: function mergeOptions(args, opt) {
  2997. var _this2 = this;
  2998. return mergeProps(args.map(function (v) {
  2999. return _this2.tidyOptions(v);
  3000. }), opt, this.mergeOptionsRule);
  3001. },
  3002. updateOptions: function updateOptions(options) {
  3003. this.options = this.mergeOptions([options], this.getDefaultOptions());
  3004. this.update();
  3005. },
  3006. tidyOptions: function tidyOptions(options) {
  3007. return options;
  3008. },
  3009. tidyRule: function tidyRule(ctx) {},
  3010. mergeProp: function mergeProp(ctx) {},
  3011. getDefaultOptions: function getDefaultOptions() {
  3012. return {};
  3013. },
  3014. render: function render(children) {}
  3015. });
  3016. var fetch$1 = function fetch(fc) {
  3017. function parseOpt(option) {
  3018. if (is.String(option)) {
  3019. option = {
  3020. action: option,
  3021. to: 'options'
  3022. };
  3023. }
  3024. return option;
  3025. }
  3026. function run(inject, rule, api) {
  3027. var option = inject.value;
  3028. if (is.Function(option)) {
  3029. option = option(rule, api);
  3030. }
  3031. option = parseOpt(option);
  3032. if (!option || !option.action) {
  3033. return false;
  3034. }
  3035. if (!option.to) {
  3036. option.to = 'options';
  3037. }
  3038. var _onError = option.onError;
  3039. var check = function check() {
  3040. if (!inject.getValue()) {
  3041. inject.clearProp();
  3042. api.sync(rule);
  3043. return true;
  3044. }
  3045. };
  3046. var set = function set(val) {
  3047. if (val === undefined) {
  3048. inject.clearProp();
  3049. api.sync(rule);
  3050. } else {
  3051. deepSet(inject.getProp(), option.to, val);
  3052. }
  3053. };
  3054. invoke(function () {
  3055. return fc.create.fetch(_objectSpread2(_objectSpread2({}, option), {}, {
  3056. onSuccess: function onSuccess(body) {
  3057. if (check()) return;
  3058. var fn = function fn(v) {
  3059. return v.data;
  3060. };
  3061. if (is.Function(option.parse)) {
  3062. fn = option.parse;
  3063. }
  3064. set(fn(body, rule, api));
  3065. api.sync(rule);
  3066. },
  3067. onError: function onError(e) {
  3068. set(undefined);
  3069. if (check()) return;
  3070. (_onError || function (e) {
  3071. return err(e.message || 'fetch fail ' + option.action);
  3072. })(e, rule, api);
  3073. }
  3074. }), {
  3075. inject: inject,
  3076. rule: rule,
  3077. api: api
  3078. });
  3079. });
  3080. return true;
  3081. }
  3082. return {
  3083. name: 'fetch',
  3084. loaded: function loaded() {
  3085. run.apply(void 0, arguments);
  3086. },
  3087. watch: function watch(inject, rule, api) {
  3088. if (!run(inject, rule, api)) {
  3089. inject.clearProp();
  3090. api.sync(rule);
  3091. }
  3092. }
  3093. };
  3094. };
  3095. var $required = {
  3096. name: 'required',
  3097. load: function load(inject, rule, api) {
  3098. var val = parseVal(inject.getValue());
  3099. if (val.required === false) {
  3100. inject.clearProp();
  3101. } else {
  3102. var validate = _objectSpread2({
  3103. required: true,
  3104. validator: function validator(_, v, call) {
  3105. is.empty(v) ? call(validate.message) : call();
  3106. }
  3107. }, val);
  3108. if (!validate.message) {
  3109. var title = rule.title || '';
  3110. validate.message = ((_typeof(title) === 'object' ? title.title : title) || '') + '不能为空';
  3111. }
  3112. inject.getProp().validate = [validate];
  3113. }
  3114. api.sync(rule);
  3115. },
  3116. watch: function watch() {
  3117. $required.load.apply($required, arguments);
  3118. }
  3119. };
  3120. function parseVal(val) {
  3121. if (is.Boolean(val)) {
  3122. return {
  3123. required: val
  3124. };
  3125. } else if (is.String(val)) {
  3126. return {
  3127. message: val
  3128. };
  3129. } else if (is.Function(val)) {
  3130. return {
  3131. validator: val
  3132. };
  3133. } else if (!is.Object(val)) {
  3134. return {};
  3135. } else {
  3136. return val;
  3137. }
  3138. }
  3139. var $provider = {
  3140. fetch: fetch$1,
  3141. required: $required
  3142. };
  3143. var name = 'html';
  3144. var html = {
  3145. name: name,
  3146. loadChildren: false,
  3147. render: function render(children, ctx) {
  3148. if (!ctx.prop.domProps) ctx.prop.domProps = {};
  3149. ctx.prop.domProps.innerHTML = children;
  3150. return ctx.vNode.make(ctx.prop.props.tag || 'div', ctx.prop);
  3151. },
  3152. renderChildren: function renderChildren(ctx) {
  3153. if (Array.isArray(ctx.rule.children)) {
  3154. return ctx.rule.children.filter(function (v) {
  3155. return is.String(v);
  3156. }).join('');
  3157. }
  3158. return '';
  3159. }
  3160. };
  3161. var _vue = typeof window !== 'undefined' && window.Vue ? window.Vue : Vue;
  3162. function _parseProp(name, id) {
  3163. var prop;
  3164. if (arguments.length === 2) {
  3165. prop = arguments[1];
  3166. id = prop[name];
  3167. } else {
  3168. prop = arguments[2];
  3169. }
  3170. return {
  3171. id: id,
  3172. prop: prop
  3173. };
  3174. }
  3175. function nameProp() {
  3176. return _parseProp.apply(void 0, ['name'].concat(Array.prototype.slice.call(arguments)));
  3177. }
  3178. function _getEl(options) {
  3179. if (!options || !options.el) return window.document.body;
  3180. return is.Element(options.el) ? options.el : document.querySelector(options.el);
  3181. }
  3182. function mountForm(rules, option) {
  3183. var $vm = new _vue({
  3184. data: function data() {
  3185. return {
  3186. rule: rules,
  3187. option: option || {}
  3188. };
  3189. },
  3190. render: function render(h) {
  3191. return h('FormCreate', {
  3192. ref: 'fc',
  3193. props: this.$data
  3194. });
  3195. }
  3196. });
  3197. $vm.$mount();
  3198. return $vm;
  3199. }
  3200. function exportAttrs(attrs) {
  3201. var key = attrs.key || [];
  3202. var array = attrs.array || [];
  3203. var normal = attrs.normal || [];
  3204. keyAttrs.push.apply(keyAttrs, _toConsumableArray(key));
  3205. arrayAttrs.push.apply(arrayAttrs, _toConsumableArray(array));
  3206. normalAttrs.push.apply(normalAttrs, _toConsumableArray(normal));
  3207. appendProto([].concat(_toConsumableArray(key), _toConsumableArray(array), _toConsumableArray(normal)));
  3208. }
  3209. var id$1 = 1;
  3210. var instance = {};
  3211. function FormCreateFactory(config) {
  3212. var components = _defineProperty({}, fragment.name, fragment);
  3213. var parsers = {};
  3214. var directives = {};
  3215. var providers = _objectSpread2({}, $provider);
  3216. var maker = makerFactory();
  3217. var globalConfig = {
  3218. global: {}
  3219. };
  3220. var data = {};
  3221. var CreateNode = CreateNodeFactory();
  3222. exportAttrs(config.attrs || {});
  3223. function getApi(name) {
  3224. var val = instance[name];
  3225. if (Array.isArray(val)) return _toConsumableArray(val);
  3226. return val;
  3227. }
  3228. function directive() {
  3229. var data = nameProp.apply(void 0, arguments);
  3230. if (data.id && data.prop) directives[data.id] = data.prop;
  3231. }
  3232. function register() {
  3233. var data = nameProp.apply(void 0, arguments);
  3234. if (data.id && data.prop) providers[data.id] = _objectSpread2(_objectSpread2({}, data.prop), {}, {
  3235. name: data.id
  3236. });
  3237. }
  3238. function componentAlias(alias) {
  3239. CreateNode.use(alias);
  3240. }
  3241. function parser() {
  3242. var data = nameProp.apply(void 0, arguments);
  3243. if (!data.id || !data.prop) return;
  3244. var name = toCase(data.id);
  3245. var parser = data.prop;
  3246. var base = parser.merge === true ? parsers[name] : undefined;
  3247. parsers[name] = _objectSpread2(_objectSpread2({}, base || BaseParser), parser);
  3248. maker[name] = creatorFactory(name);
  3249. parser.maker && extend(maker, parser.maker);
  3250. }
  3251. function component(id, component) {
  3252. var name;
  3253. if (is.String(id)) {
  3254. name = toCase(id);
  3255. if (['form-create', 'formcreate'].indexOf(name) > -1) {
  3256. return $form();
  3257. } else if (component === undefined) {
  3258. return components[name];
  3259. }
  3260. } else {
  3261. name = toCase(id.name);
  3262. component = id;
  3263. }
  3264. if (!name || !component) return;
  3265. components[name] = component;
  3266. if (component.formCreateParser) parser(name, component.formCreateParser);
  3267. }
  3268. function $form() {
  3269. return _vue.extend($FormCreate(FormCreate));
  3270. }
  3271. function $vnode() {
  3272. return _vue.extend(fragment);
  3273. }
  3274. function use(fn, opt) {
  3275. if (is.Function(fn.install)) fn.install(create, opt);else if (is.Function(fn)) fn(create, opt);
  3276. return this;
  3277. }
  3278. function create(rules, _opt, parent) {
  3279. var $vm = mountForm(rules, _opt || {});
  3280. var _this = $vm.$refs.fc.formCreate;
  3281. _this.$parent = parent;
  3282. _getEl(_this.options).appendChild($vm.$el);
  3283. return _this.api();
  3284. }
  3285. function factory(inherit) {
  3286. var _config = _objectSpread2({}, config);
  3287. if (inherit) {
  3288. _config.inherit = {
  3289. components: components,
  3290. parsers: parsers,
  3291. directives: directives,
  3292. providers: providers,
  3293. maker: maker,
  3294. data: data
  3295. };
  3296. } else {
  3297. delete _config.inherit;
  3298. }
  3299. return FormCreateFactory(_config);
  3300. }
  3301. function FormCreate(vm, rules, options) {
  3302. extend(this, {
  3303. id: id$1++,
  3304. vm: vm,
  3305. create: create,
  3306. manager: createManager(config.manager),
  3307. parsers: parsers,
  3308. providers: providers,
  3309. rules: Array.isArray(rules) ? rules : [],
  3310. name: vm.$options.propsData.name,
  3311. inFor: vm.$options.propsData.inFor,
  3312. prop: {
  3313. components: components,
  3314. directives: directives
  3315. },
  3316. CreateNode: CreateNode,
  3317. bus: new _vue(),
  3318. unwatch: null,
  3319. options: options || {},
  3320. extendApi: config.extendApi || function (api) {
  3321. return api;
  3322. }
  3323. });
  3324. this.init();
  3325. this.initOptions(this.options);
  3326. if (this.name) {
  3327. if (this.inFor) {
  3328. if (!instance[this.name]) instance[this.name] = [];
  3329. instance[this.name].push(this.api());
  3330. } else {
  3331. instance[this.name] = this.api();
  3332. }
  3333. }
  3334. }
  3335. extend(FormCreate.prototype, {
  3336. init: function init() {
  3337. var _this2 = this;
  3338. var vm = this.vm;
  3339. var h = new Handler(this);
  3340. this.$handle = h;
  3341. vm.$f = h.api;
  3342. vm.$emit('input', h.api);
  3343. vm.$on('hook:created', function () {
  3344. if (_this2.isSub()) {
  3345. _this2.unwatch = vm.$watch(function () {
  3346. return vm.$pfc.option;
  3347. }, function () {
  3348. _this2.initOptions(_this2.options);
  3349. vm.$f.refresh();
  3350. }, {
  3351. deep: true
  3352. });
  3353. _this2.initOptions(_this2.options);
  3354. }
  3355. _this2.created();
  3356. });
  3357. vm.$on('hook:mounted', function () {
  3358. _this2.mounted();
  3359. });
  3360. vm.$on('hook:beforeDestroy', function () {
  3361. vm.destroyed = true;
  3362. _this2.unwatch && _this2.unwatch();
  3363. h.reloadRule([]);
  3364. if (_this2.name) {
  3365. if (_this2.inFor) {
  3366. var idx = instance[_this2.name].indexOf(_this2.api());
  3367. instance[_this2.name].splice(idx, 1);
  3368. } else {
  3369. delete instance[_this2.name];
  3370. }
  3371. }
  3372. });
  3373. vm.$on('hook:updated', function () {
  3374. h.bindNextTick(function () {
  3375. return _this2.bus.$emit('next-tick', h.api);
  3376. });
  3377. });
  3378. },
  3379. isSub: function isSub() {
  3380. return this.vm.$pfc && this.vm.extendOption;
  3381. },
  3382. initOptions: function initOptions(options) {
  3383. this.options = _objectSpread2({
  3384. formData: {},
  3385. submitBtn: {},
  3386. resetBtn: {}
  3387. }, deepCopy(globalConfig));
  3388. if (this.isSub()) {
  3389. this.mergeOptions(this.options, this.vm.$pfc.$f.config || {}, true);
  3390. }
  3391. this.updateOptions(options);
  3392. },
  3393. mergeOptions: function mergeOptions(target, opt, parent) {
  3394. opt = deepCopy(opt);
  3395. parent && ['page', 'onSubmit', 'mounted', 'reload', 'formData', 'el'].forEach(function (n) {
  3396. delete opt[n];
  3397. });
  3398. if (opt.global) {
  3399. target.global = mergeGlobal(target.global, opt.global);
  3400. delete opt.global;
  3401. }
  3402. this.$handle.$manager.mergeOptions([opt], target);
  3403. return target;
  3404. },
  3405. updateOptions: function updateOptions(options) {
  3406. this.mergeOptions(this.options, options);
  3407. this.$handle.$manager.updateOptions(this.options);
  3408. },
  3409. created: function created() {
  3410. this.$handle.init();
  3411. this.vm.$emit('created', this.api());
  3412. },
  3413. api: function api() {
  3414. return this.$handle.api;
  3415. },
  3416. render: function render() {
  3417. return this.$handle.render();
  3418. },
  3419. mounted: function mounted() {
  3420. this.$handle.mounted();
  3421. }
  3422. });
  3423. function useAttr(formCreate) {
  3424. extend(formCreate, {
  3425. version: config.version,
  3426. ui: config.ui,
  3427. data: data,
  3428. maker: maker,
  3429. component: component,
  3430. directive: directive,
  3431. register: register,
  3432. $vnode: $vnode,
  3433. parser: parser,
  3434. use: use,
  3435. factory: factory,
  3436. componentAlias: componentAlias,
  3437. copyRule: copyRule,
  3438. copyRules: copyRules,
  3439. fetch: fetch,
  3440. $form: $form,
  3441. parseFn: parseFn,
  3442. parseJson: parseJson,
  3443. toJson: toJson,
  3444. getApi: getApi,
  3445. init: function init(rules) {
  3446. var _opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3447. var $vm = mountForm(rules, _opt),
  3448. _this = $vm.$refs.fc.formCreate;
  3449. return {
  3450. mount: function mount($el) {
  3451. if ($el && is.Element($el)) _this.options.el = $el;
  3452. _getEl(_this.options).appendChild($vm.$el);
  3453. return _this.api();
  3454. },
  3455. remove: function remove() {
  3456. $vm.$el.parentNode && $vm.$el.parentNode.removeChild($vm.$el);
  3457. },
  3458. destroy: function destroy() {
  3459. this.remove();
  3460. $vm.$destroy();
  3461. },
  3462. $f: _this.api()
  3463. };
  3464. }
  3465. });
  3466. }
  3467. function useStatic(formCreate) {
  3468. extend(formCreate, {
  3469. create: create,
  3470. install: function install(Vue, options) {
  3471. globalConfig = _objectSpread2(_objectSpread2({}, globalConfig), options || {});
  3472. if (Vue._installedFormCreate === true) return;
  3473. Vue._installedFormCreate = true;
  3474. _vue = Vue;
  3475. var $formCreate = function $formCreate(rules) {
  3476. var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3477. return create(rules, opt, this);
  3478. };
  3479. useAttr($formCreate);
  3480. Vue.prototype.$formCreate = $formCreate;
  3481. Vue.component('FormCreate', $form());
  3482. Vue.component('FcFragment', $vnode());
  3483. }
  3484. });
  3485. }
  3486. useAttr(create);
  3487. useStatic(create);
  3488. CreateNode.use({
  3489. fragment: 'fcFragment'
  3490. });
  3491. parser(html);
  3492. if (config.install) create.use(config);
  3493. if (config.inherit) {
  3494. var inherit = config.inherit;
  3495. inherit.components && extend(components, inherit.components);
  3496. inherit.parsers && extend(parsers, inherit.parsers);
  3497. inherit.directives && extend(directives, inherit.directives);
  3498. inherit.providers && extend(providers, inherit.providers);
  3499. inherit.maker && extend(maker, inherit.maker);
  3500. inherit.data && extend(data, inherit.data);
  3501. }
  3502. return create;
  3503. }
  3504. export default FormCreateFactory;
  3505. export { Creator, Manager, copyRule, copyRules, creatorFactory, fragment, mergeRule, parseJson, toJson };