mpFormBuilder-compiled.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. (function (global, factory) {
  2. define && define.amd && define(['mpBuilder', 'axios'], factory);
  3. })(this, function (r, axios) {
  4. var FormBuilderName = 'form-builder';
  5. var api = {};
  6. var opt = {};
  7. var formBuilderInstall = function formBuilderInstall(Vue, _api, rules, _opt) {
  8. api = _api;
  9. opt = _opt;
  10. var builder = new formBuilder(rules),
  11. _render;
  12. Vue.component(FormBuilderName, {
  13. data: function data() {
  14. return {
  15. formValidate: builder.metaData(),
  16. formStatus: {
  17. loading: false,
  18. form: 'formValidate'
  19. }
  20. };
  21. },
  22. render: function render(h) {
  23. window.__vm = this;
  24. _render = builder.createRender(this, h);
  25. window._fb = _render;
  26. return _render.parse();
  27. },
  28. watch: {},
  29. mounted: function mounted() {
  30. // render._bindWatch(this);
  31. _render._mountedCall(this);
  32. }
  33. });
  34. };
  35. var formBuilder = function formBuilder(rules) {
  36. this.original = rules;
  37. this.rules = this._handleRules();
  38. this.fields = this._getFields();
  39. };
  40. formBuilder.prototype = {
  41. //创建表单生成器
  42. createRender: function createRender(vm, h) {
  43. return new formRender(this.rules, vm, h);
  44. },
  45. //获得表单字段
  46. _getFields: function _getFields() {
  47. var field = [];
  48. this.rules.map(function (rule) {
  49. field.push(rule.field);
  50. });
  51. return field;
  52. },
  53. field: function field() {
  54. return this.fields;
  55. },
  56. //获得表单键值对
  57. metaData: function metaData() {
  58. var metaData = {};
  59. this.rules.map(function (rule) {
  60. metaData[rule.field] = {
  61. value: rule.value,
  62. type: rule.type,
  63. select: rule.select
  64. };
  65. });
  66. return metaData;
  67. },
  68. //初始化参数
  69. _handleRules: function _handleRules() {
  70. return this.original.filter(function (rule) {
  71. return !!rule.field;
  72. }).map(function (rule) {
  73. rule.props || (rule.props = {});
  74. rule.type = rule.type.toLowerCase();
  75. return rule;
  76. });
  77. }
  78. };
  79. var formRender = function formRender(rules, vm, h) {
  80. this.vm = vm;
  81. this.h = h;
  82. this.rules = rules;
  83. this.r = new r(h);
  84. this.t = this.r.$t();
  85. this._mountedCallList = [];
  86. };
  87. formRender.prototype = {
  88. _mounted: function _mounted(call) {
  89. this._mountedCallList.push(call);
  90. },
  91. _mountedCall: function _mountedCall(vm) {
  92. this._mountedCallList.map(function (call) {
  93. call(vm);
  94. });
  95. },
  96. //绑定表单监听事件
  97. _bindWatch: function _bindWatch(vm) {
  98. var _this2 = this;
  99. this.rules.map(function (rule) {
  100. _this2._bindMetaWatch(vm, rule.field);
  101. });
  102. },
  103. //绑定字段监听事件
  104. _bindMetaWatch: function _bindMetaWatch(vm, field) {
  105. var _this3 = this;
  106. return this.vm.$watch('formValidate.' + field, function (n) {
  107. _this3.vm.$refs[_this3.metaRef(field)].currentValue && (_this3.vm.$refs[_this3.metaRef(field)].currentValue = n);
  108. });
  109. },
  110. _bindInput: function _bindInput(field, value) {
  111. this.setFieldValue(field, value);
  112. this.vm.$emit('input', value);
  113. },
  114. getFieldValue: function getFieldValue(field) {
  115. return this.vm.formValidate[field].value;
  116. },
  117. setFieldValue: function setFieldValue(field, value) {
  118. this.vm.formValidate[field].value = value;
  119. },
  120. //获得表单的ref名称
  121. metaRef: function metaRef(field) {
  122. return 'mp_' + field;
  123. },
  124. getRef: function getRef(field) {
  125. return this.vm.$refs[this.metaRef(field)];
  126. },
  127. getFormRef: function getFormRef() {
  128. return this.vm.$refs[this.vm.formStatus.form];
  129. },
  130. getParseFormData: function getParseFormData() {
  131. var parseData = {},
  132. formData = this.vm.formValidate;
  133. var _iteratorNormalCompletion = true;
  134. var _didIteratorError = false;
  135. var _iteratorError = undefined;
  136. try {
  137. var _loop = function _loop() {
  138. var it = _step.value;
  139. var item = formData[it];
  140. if (['datepicker', 'timepicker'].indexOf(item.type) != -1) {
  141. if (Object.prototype.toString.call(item.value) == '[object Array]') {
  142. c = [];
  143. item.value.map(function (value) {
  144. c.push((isNaN(Date.parse(value)) ? Date.parse(new Date()) : Date.parse(value)) / 1000);
  145. });
  146. } else {
  147. c = parseData[it] = (isNaN(Date.parse(item.value)) ? Date.parse(new Date()) : Date.parse(item.value)) / 1000;
  148. }
  149. parseData[it] = c;
  150. } else if (['checkbox', 'select', 'radio'].indexOf(item.type) != -1) {
  151. if (Object.prototype.toString.call(item.value) == '[object Array]') {
  152. c = [];
  153. item.value.map(function (value) {
  154. item.select[value] != undefined && c.push(item.select[value]);
  155. });
  156. } else {
  157. c = item.select[item.value];
  158. }
  159. parseData[it] = c;
  160. } else parseData[it] = item.value;
  161. };
  162. for (var _iterator = Object.keys(formData)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  163. var c;
  164. var c;
  165. _loop();
  166. }
  167. } catch (err) {
  168. _didIteratorError = true;
  169. _iteratorError = err;
  170. } finally {
  171. try {
  172. if (!_iteratorNormalCompletion && _iterator.return) {
  173. _iterator.return();
  174. }
  175. } finally {
  176. if (_didIteratorError) {
  177. throw _iteratorError;
  178. }
  179. }
  180. }
  181. return parseData;
  182. },
  183. makeForm: function makeForm(VNodeFn) {
  184. var _this4 = this;
  185. var t = new this.t();
  186. t.props({ 'label-width': 125 }).ref(this.vm.formStatus.form).attrs({ method: 'POST', action: opt.router + '/save' }).nativeOn('submit', function (e) {
  187. e.preventDefault();
  188. var _this = _this4.getFormRef();
  189. var parseData = _this4.getParseFormData();
  190. axios.post(_this.$attrs['action'], parseData).then(function (res) {
  191. if (res.status && res.data.code == 200) return Promise.resolve(res.data);else return Promise.reject(res.data.msg || '添加失败,请稍候再试!');
  192. }).then(function (res) {
  193. api.message('success', res.msg || '操作成功!');
  194. api.closeModalFrame(window.name);
  195. }).catch(function (err) {
  196. api.message('error', err);
  197. });
  198. console.log(parseData);
  199. });
  200. return this.r.form(t.get(), VNodeFn);
  201. },
  202. makeFormItem: function makeFormItem(field, label, VNodeFn) {
  203. return this.r.formItem({
  204. props: {
  205. 'props': field,
  206. 'label': label || ''
  207. }
  208. }, VNodeFn);
  209. },
  210. makeInput: function makeInput(rule) {
  211. var _this5 = this;
  212. _vm = this.vm;
  213. var t = new this.t(),
  214. field = rule.field,
  215. ref = this.metaRef(field);
  216. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  217. return _this5._bindInput(field, value);
  218. });
  219. return this.r.input(t.get());
  220. },
  221. makeInputNumber: function makeInputNumber(rule) {
  222. var _this6 = this;
  223. var t = new this.t(),
  224. field = rule.field,
  225. ref = this.metaRef(field);
  226. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  227. return _this6._bindInput(field, value);
  228. });
  229. return this.r.inputNumber(t.get());
  230. },
  231. makeRadio: function makeRadio(rule) {
  232. var _this7 = this;
  233. var t = new this.t(),
  234. field = rule.field,
  235. ref = this.metaRef(field);
  236. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  237. return _this7._bindInput(field, value);
  238. });
  239. return this.r.radioGroup(t.get(), function () {
  240. return rule.options.map(function (option) {
  241. return _this7.r.radio({ props: option.props });
  242. });
  243. });
  244. },
  245. makeCheckBox: function makeCheckBox(rule) {
  246. var _this8 = this;
  247. var t = new this.t(),
  248. field = rule.field,
  249. ref = this.metaRef(field);
  250. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  251. return _this8._bindInput(field, value);
  252. });
  253. return this.r.checkboxGroup(t.get(), function () {
  254. return rule.options.map(function (option) {
  255. return _this8.r.checkbox({ props: option.props });
  256. });
  257. });
  258. },
  259. markSelect: function markSelect(rule) {
  260. var _this9 = this;
  261. var t = new this.t(),
  262. field = rule.field,
  263. ref = this.metaRef(field);
  264. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  265. return _this9._bindInput(field, value);
  266. });
  267. return this.r.select(t.get(), this.markSelectOptions(rule.options));
  268. },
  269. markSelectOptions: function markSelectOptions(options) {
  270. var _this10 = this;
  271. return options.map(function (option) {
  272. return _this10.r.option({ props: option.props });
  273. });
  274. },
  275. stringToDate: function stringToDate(field) {
  276. var val = this.getFieldValue(field);
  277. if (Object.prototype.toString.call(val) == '[object Array]') {
  278. val.map(function (v, k) {
  279. Object.prototype.toString.call(v) == '[object Date]' || (val[k] = new Date(v * 1000));
  280. });
  281. } else {
  282. Object.prototype.toString.call(val) == '[object Date]' || (val = new Date(v * 1000));
  283. }
  284. },
  285. stringToTime: function stringToTime(field) {
  286. var val = this.getFieldValue(field),
  287. today = this.today();
  288. if (Object.prototype.toString.call(val) == '[object Array]') {
  289. val.map(function (v, k) {
  290. Object.prototype.toString.call(v) == '[object Date]' || (val[k] = new Date(v * 1000));
  291. });
  292. } else {
  293. Object.prototype.toString.call(val) == '[object Date]' || (val = new Date(v * 1000));
  294. }
  295. },
  296. today: function today() {
  297. var date = new Date();
  298. var y = date.getFullYear();
  299. var m = date.getMonth() + 1;
  300. m = m < 10 ? '0' + m : m;
  301. var d = date.getDate();
  302. d = d < 10 ? '0' + d : d;
  303. return y + '-' + m + '-' + d;
  304. },
  305. makeDatePicker: function makeDatePicker(rule) {
  306. var _this11 = this;
  307. var t = new this.t(),
  308. field = rule.field,
  309. ref = this.metaRef(field);
  310. rule.props.type || (rule.props.type = 'date');
  311. this.stringToDate(field);
  312. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  313. return _this11._bindInput(field, value);
  314. });
  315. return this.r.datePicker(t.get());
  316. },
  317. makeTimePicker: function makeTimePicker(rule) {
  318. var _this12 = this;
  319. var t = new this.t(),
  320. field = rule.field,
  321. ref = this.metaRef(field);
  322. rule.props.type || (rule.props.type = 'time');
  323. this.stringToTime(field);
  324. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  325. return _this12._bindInput(field, value);
  326. });
  327. return this.r.timePicker(t.get());
  328. },
  329. makeColorPicker: function makeColorPicker(rule) {
  330. var _this13 = this;
  331. var t = new this.t(),
  332. field = rule.field,
  333. ref = this.metaRef(field);
  334. t.props(rule.props).props('value', this.getFieldValue(field)).ref(ref).on('input', function (value) {
  335. return _this13._bindInput(field, value);
  336. });
  337. return this.r.colorPicker(t.get());
  338. },
  339. makeUpload: function makeUpload(rule) {
  340. var _this14 = this;
  341. var t = new this.t(),
  342. field = rule.field,
  343. ref = this.metaRef(field);
  344. t.props(rule.props).props('value', this.getFieldValue(field));
  345. //上传文件之前的钩子,参数为上传的文件,若返回 false 或者 Promise 则停止上传
  346. t.props('before-upload', function () {
  347. if (rule.props['max-length'] && rule.props['max-length'] <= _this14.getFieldValue(field).length) {
  348. api.message('最多可上传' + rule.props['max-length'] + '张图片');
  349. return false;
  350. }
  351. });
  352. //文件上传时的钩子,返回字段为 event, file, fileList
  353. t.props('on-progress', function (event, file, fileList) {});
  354. //文件上传成功时的钩子,返回字段为 response, file, fileList
  355. t.props('on-success', function (response, file, fileList) {
  356. if (response.code == 200) {
  357. api.message('success', file.name + '图片上传成功');
  358. _this14.getFieldValue(field).push(response.data.url);
  359. } else {
  360. api.message('error', file.name + '图片上传失败,' + response.msg);
  361. }
  362. });
  363. //点击已上传的文件链接时的钩子,返回字段为 file, 可以通过 file.response 拿到服务端返回数据
  364. t.props('on-preview', function (file) {});
  365. //文件列表移除文件时的钩子,返回字段为 file, fileList
  366. t.props('on-remove', function (file) {});
  367. //文件格式验证失败时的钩子,返回字段为 file, fileList
  368. t.props('on-format-error', function (file, fileList) {
  369. api.message('error', file.name + '格式不正确,请上传 jpg 或 png 格式的图片。');
  370. });
  371. //文件超出指定大小限制时的钩子,返回字段为 file, fileList
  372. t.props('on-exceeded-size', function (file, fileList) {
  373. api.message('error', file.name + '太大,不能超过 ' + rule.props['max-size'] + 'kb');
  374. });
  375. //文件上传失败时的钩子,返回字段为 error, file, fileList
  376. t.props('on-error', function (error, file, fileList) {
  377. api.message('error', file.name + '上传失败,' + error);
  378. });
  379. t.class('mp-upload', true);
  380. t.ref(ref);
  381. var data = t.get();
  382. return function () {
  383. var render = [];
  384. if (data.props['mp-show-upload-list'] == true) render.push(function () {
  385. return data.props.value.map(function (img) {
  386. return _this14.r.make('div', { class: { 'demo-upload-list': true } }, [_this14.r.make('img', { attrs: { src: img } }), _this14.r.make('div', { class: { 'demo-upload-list-cover': true } }, [_this14.r.icon({ props: { type: 'ios-eye-outline' }, nativeOn: { 'click': function click() {
  387. api.layer.open({
  388. type: 1,
  389. title: false,
  390. closeBtn: 0,
  391. shadeClose: true,
  392. content: '<img src="' + img + '" style="display: block;width: 100%;" />'
  393. });
  394. } } }), _this14.r.icon({ props: { type: 'ios-trash-outline' }, nativeOn: { 'click': function click() {
  395. data.props.value.splice(data.props.value.indexOf(img), 1);
  396. } } })])]);
  397. });
  398. }());
  399. if (!rule.props['max-length'] || rule.props['max-length'] > _this14.getFieldValue(rule.field).length) render.push(function () {
  400. return _this14.r.upload(data, function () {
  401. return [_this14.r.make('div', { class: { 'mp-upload-btn': true } }, [_this14.r.icon({ props: { type: "camera", size: 20 } })])];
  402. });
  403. }());
  404. return render;
  405. }();
  406. },
  407. makeSubmit: function makeSubmit() {
  408. var _this15 = this;
  409. var t = new this.t();
  410. t.props({ type: 'primary', 'html-type': 'submit', long: true, size: "large", loading: this.vm.formStatus.loading }).on('click', function () {
  411. _this15.vm.formStatus.loading = true;
  412. _this15.getFormRef().validate(function (valid) {
  413. console.log(valid);
  414. });
  415. });
  416. return this.r.formItem({ class: { 'add-submit-item': true } }, [this.r.button(t.get(), function () {
  417. return [_this15.r.span('提交')];
  418. })]);
  419. },
  420. parse: function parse() {
  421. var _this16 = this;
  422. return this.makeForm(function () {
  423. var form = _this16.rules.filter(function (rule) {
  424. return !!rule.field;
  425. }).map(function (rule) {
  426. return _this16.makeFormItem(rule.field, rule.title, function () {
  427. return _this16[rule.type].call(_this16, rule);
  428. });
  429. });
  430. form.push(_this16.makeSubmit());
  431. return form;
  432. });
  433. },
  434. text: function text(rule) {
  435. return [this.makeInput(rule)];
  436. },
  437. radio: function radio(rule) {
  438. return [this.makeRadio(rule)];
  439. },
  440. checkbox: function checkbox(rule) {
  441. return [this.makeCheckBox(rule)];
  442. },
  443. select: function select(rule) {
  444. return [this.markSelect(rule)];
  445. },
  446. inputnumber: function inputnumber(rule) {
  447. return [this.makeInputNumber(rule)];
  448. },
  449. datepicker: function datepicker(rule) {
  450. return [this.makeDatePicker(rule)];
  451. },
  452. timepicker: function timepicker(rule) {
  453. return [this.makeTimePicker(rule)];
  454. },
  455. colorpicker: function colorpicker(rule) {
  456. return [this.makeColorPicker(rule)];
  457. },
  458. upload: function upload(rule) {
  459. return this.makeUpload(rule);
  460. }
  461. };
  462. return {
  463. install: formBuilderInstall
  464. };
  465. });
  466. //# sourceMappingURL=mpFormBuilder-compiled.js.map