customformDetailCtrl.2.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. var appFormly = angular.module('formlyDetail', [ 'formlyExample' ]);
  2. app.requires.push('formlyDetail');
  3. appFormly.controller('CustomformDetailCtrl', ['$scope','i18nService','$rootScope', '$parse', '$injector', '$http','$q', '$state', '$stateParams', '$modal', '$timeout', '$interval', 'SweetAlert', 'FileUploader', 'Restangular', 'BpmRestangular','api_configure_form', 'api_bpm_domain', 'api_bpm_data', 'api_user_data',
  4. function ( $scope,i18nService,$rootScope, $parse, $injector, $http, $q, $state, $stateParams, $modal, $timeout, $interval, SweetAlert, FileUploader, Restangular,BpmRestangular, api_configure_form, api_bpm_domain, api_bpm_data, api_user_data) {
  5. //console.log($parse('Restangular')($scope));
  6. //console.log($injector.get('Restangular'));
  7. var vm = this;
  8. vm.options = {
  9. formState: {
  10. horizontalLabelClass: 'col-sm-5',
  11. horizontalFieldClass: 'col-sm-5',
  12. readOnly: true
  13. }
  14. };
  15. vm.exampleTitle = ['expressionProperties','model property'];
  16. vm.fields=[];
  17. vm.model={};
  18. $scope.langs=i18nService.getAllLangs();
  19. $scope.lang = 'zh-cn';
  20. i18nService.setCurrentLang($scope.lang);
  21. $scope.backFunction = function(data){
  22. $state.go('app.incident',{});
  23. };
  24. var that=$injector;
  25. var parse=$parse;
  26. var formKey = "";
  27. var pdKey = "";
  28. if(angular.isDefined($state.current.pdKey)&&$state.current.pdKey!=""){
  29. pdKey = $state.current.pdKey;
  30. // console.log("pdKey::"+$state.current.pdKey);
  31. }
  32. if(angular.isDefined($stateParams.formKey)&&$stateParams.formKey!=""){
  33. formKey = $stateParams.formKey;
  34. // console.log("formKey::"+$stateParams.formKey);
  35. }
  36. //用户测试数据,后续从header的auth中获取
  37. var userId = 2;
  38. //==============处理表单设计数据 开始====================
  39. //处理组件加载后台数据选项的方法
  40. function refreshSelectOptions(searchVal, field) {
  41. if(field.templateOptions.optionsUrl){
  42. var process = BpmRestangular.all("");
  43. process.customPOST({"assignee":2,"idx":0,"sum":1000},field.templateOptions.optionsUrl).then(function(result){
  44. if(!field.templateOptions.options){
  45. field.templateOptions.options=[];
  46. }
  47. if(field.templateOptions.optionsDataKey){
  48. field.templateOptions.options =result[field.templateOptions.optionsDataKey];
  49. }else{
  50. field.templateOptions.options =result;
  51. }
  52. });
  53. }
  54. }
  55. //解析自定义表单设计数据
  56. function decodeVMForm(vmForm, mdata){
  57. var result = {model:{},fields:[]};
  58. //设置模型实体数据 begin
  59. // var mdata = vmForm.model;
  60. //解析数据实体
  61. //console.log("mdata="+JSON.stringify(mdata));
  62. var mdata = angular.fromJson(vmForm.model);
  63. angular.extend(result.model,mdata);
  64. //设置模型实体数据 end
  65. //解析设计数据生成表单项 begin
  66. var fields = [];
  67. //处理修改设计数据中展示设置
  68. angular.forEach(vmForm.fields, function(field){
  69. if(field.key==""){
  70. delete field.key;
  71. }
  72. if(angular.isDefined(field.extjson)){
  73. var extObj = angular.fromJson(field.extjson);//JSON.parse(field.extjson);
  74. angular.extend(field.templateOptions, extObj.templateOptions);
  75. delete extObj.templateOptions;
  76. for(var prop in extObj){
  77. if(new RegExp("Expression").test(prop)){
  78. //var obj = $scope.$eval(extObj[prop]);
  79. //extObj[prop] = $scope.$eval(extObj[prop]);
  80. if(extObj[prop]!=null){
  81. if(new RegExp("function").test(extObj[prop])){
  82. var propValue = eval(extObj[prop]);
  83. extObj[prop] = propValue;
  84. }else{
  85. // console.log(extObj[prop]);
  86. var obj = $scope.$eval(extObj[prop]);
  87. extObj[prop] = obj;
  88. // console.log(obj);
  89. }
  90. }
  91. }else if(new RegExp("expressionProperties").test(prop)){
  92. for(var p in extObj[prop]){
  93. if(new RegExp("function").test(extObj[prop][p])){
  94. var propValue = eval(extObj[prop][p]);
  95. extObj[prop][p] = propValue;
  96. }else{
  97. if(p.indexOf("'")>=0){
  98. var obj = $scope.$eval(extObj[prop][p]);
  99. extObj[prop][$scope.$eval(p)]=obj;
  100. }
  101. }
  102. }
  103. }else if("watcher"==prop){
  104. if(angular.isArray(extObj[prop])){
  105. angular.forEach(extObj[prop],function(item,index){
  106. for(var p in item){
  107. if(new RegExp("function").test(item[p])){
  108. var propValue = eval(item[p]);
  109. extObj[prop][index][p] = propValue;
  110. }
  111. }
  112. });
  113. }else if(angular.isObject(extObj[prop])){
  114. for(var p in extObj[prop]){
  115. if(new RegExp("function").test(extObj[prop][p])){
  116. var propValue = eval(extObj[prop][p]);
  117. extObj[prop][p] = propValue;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. angular.extend(field,extObj);
  124. delete field.extjson;
  125. }
  126. if(angular.isDefined(field.templateOptions)){
  127. var templateOs = field.templateOptions;
  128. for(var property in templateOs){
  129. // console.log(property);
  130. if(angular.isString(templateOs[property])&& (!(new RegExp("[\u4e00-\u9fa5]").test(templateOs[property]))||new RegExp("function").test(templateOs[property]))){
  131. if(new RegExp("function").test(templateOs[property])){
  132. var propValue = eval(templateOs[property]);
  133. field.templateOptions[property] = propValue;
  134. }else{
  135. if(that.has(templateOs[property])){
  136. field.templateOptions[property] = that.get(templateOs[property]);
  137. }else{
  138. field.templateOptions[property] = templateOs[property];
  139. }
  140. }
  141. }else if(templateOs[property]==null){
  142. //delete field.templateOptions[property];
  143. }else{
  144. }
  145. }
  146. }
  147. // console.log(field);
  148. if(angular.isDefined(field.templateOptions) && angular.isDefined(field.templateOptions.extjson)){
  149. var extObj = angular.fromJson(field.templateOptions.extjson);//JSON.parse(field.extjson);
  150. angular.extend(field.templateOptions,extObj);
  151. delete field.templateOptions.extjson;
  152. }
  153. //...
  154. if(field.templateOptions){
  155. //处理远程获取数据控件方法调用
  156. if(field.templateOptions.optionsUrl){
  157. field.templateOptions.refresh = refreshSelectOptions;
  158. }
  159. //处理嵌套属性数据绑定/
  160. if(field.templateOptions.pkey){
  161. var pmodel,i= 0;
  162. angular.forEach(field.templateOptions.pkey.split("."),function(p){
  163. if(i==0){
  164. if(result.model[p]==null){
  165. result.model[p] = {};
  166. }
  167. pmodel = result.model[p];
  168. i++;
  169. }else{
  170. if(pmodel[p]==null){
  171. pmodel[p] = {};
  172. }
  173. pmodel = pmodel[p];
  174. }
  175. });
  176. if(pmodel!=null){
  177. field.model = pmodel;
  178. if(pmodel[field.key]==null){
  179. pmodel[field.key] = null;
  180. }
  181. }
  182. }else{
  183. if(result.model[field.key]==null){
  184. result.model[field.key] = null;
  185. }
  186. }
  187. //处理弹出框组件初始化
  188. if(field.type=="ui-input-selectmodal"){
  189. field.templateOptions.modal = $modal;
  190. //field.templateOptions.Restangular = Restangular;
  191. }else if(field.type=="ui-userselect"){
  192. field.templateOptions.modal = $modal;
  193. //field.templateOptions.Restangular = Restangular;
  194. }else if(field.type=="ui-multiuserselect"){
  195. field.templateOptions.modal = $modal;
  196. }else if(field.type=="ui-modelselect"){
  197. field.templateOptions.modal = $modal;
  198. //field.templateOptions.Restangular = Restangular;
  199. }else if(field.type=="ui-dropfile"){
  200. field.templateOptions.fileUploader = new FileUploader({
  201. url:'/uploader'
  202. });
  203. field.templateOptions.taskId = $stateParams.taskId;
  204. field.templateOptions.processInstanceId = $stateParams.processInstanceId;
  205. field.templateOptions.userId = $rootScope.user.id;
  206. //field.ApiService=api_bpm_domain;
  207. }else if(field.type=="ui-dropfiletable"){
  208. field.templateOptions.processInstanceId = $stateParams.processInstanceId;
  209. }
  210. }
  211. // console.log(field);
  212. result.fields.push(field);
  213. });
  214. angular.extend($scope.vm.fields,result.fields);
  215. angular.extend($scope.vm.model,result.model);
  216. $scope.formData = vmForm;
  217. //console.log($scope.vm);
  218. //解析设计数据生成表单项 end
  219. return result;
  220. }
  221. //======================处理表单设计数据 结束========================
  222. $scope.closeModel = function(){
  223. console.log("pdKey="+pdKey);
  224. if(pdKey=="incident") {
  225. $state.go('app.incident.list',{ });
  226. }else if(pdKey=="change"){
  227. $state.go('app.change.list',{ });
  228. }else if(pdKey=="problem"){
  229. $state.go('app.problem.list',{ });
  230. }else if(pdKey=="release"){
  231. $state.go('app.release.list',{ });
  232. }else if(pdKey=="schedule"){
  233. $state.go('app.scheduling.list',{ });
  234. }else if(pdKey=="configure"){
  235. $state.go('app.cmdb.plist',{ });
  236. }else if(pdKey=="serviceCatalogueProcessActual"){
  237. $state.go('app.reService.manager',{ });
  238. };
  239. event.preventDefault();
  240. } ;
  241. vm.submit = function(data, style){
  242. };
  243. $scope.vm = vm;
  244. var dataId = $stateParams.dataId;
  245. var pdKey = $stateParams.pdKey;
  246. var detailFormKey = $stateParams.formKey;
  247. if(angular.isDefined(detailFormKey)&&detailFormKey!=""){
  248. //console.log("formKey>>"+detailFormKey);
  249. // editor process task form
  250. var dataId = parseInt(dataId);
  251. if(angular.isDefined(dataId)&&dataId!=""){
  252. api_bpm_data.fetchData(pdKey, dataId).then(function(response){
  253. if(response){
  254. var myData = Restangular.stripRestangular(response);
  255. var processInstanceId = myData.data.processInstanceId;
  256. //$scope.vm.model[pdKey]=myData.data;
  257. api_configure_form.renderForm(detailFormKey,'00000',processInstanceId).then(function(responseData){
  258. if(responseData){
  259. // console.log("responseData>>>");console.log(responseData);
  260. var vmForm = Restangular.stripRestangular(responseData);
  261. if(vmForm){
  262. //console.log("vmForm>>>");console.log(vmForm);
  263. var modelData = {};
  264. modelData[pdKey]=vmForm.model;
  265. decodeVMForm(vmForm, modelData);
  266. }
  267. }
  268. });
  269. }
  270. })
  271. }
  272. }else{
  273. // create process instance
  274. if(pdKey!=""){//for process
  275. api_bpm_domain.startformkey(pdKey).then(function(response){
  276. if(response){
  277. var myData = Restangular.stripRestangular(response);
  278. api_configure_form.renderForm(myData.data).then(function(responseData){
  279. if(responseData){
  280. var vmForm = Restangular.stripRestangular(responseData);
  281. if(vmForm){
  282. decodeVMForm(vmForm);
  283. console.log($scope);
  284. }
  285. }
  286. });
  287. }
  288. });
  289. }else{//for form
  290. //TODO
  291. //测试数据 开始
  292. var testJson = {
  293. fields : [
  294. {
  295. key: 'formtitle',
  296. type: 'ui-title',
  297. noFormControl: true,
  298. templateOptions: {
  299. label: 'title is ',
  300. placeholder: 'Formly is terrific!'
  301. }
  302. },
  303. {
  304. key:'a',type:'ui-input',className:'col-xs-4',
  305. templateOptions: {
  306. type: 'input',label: 'input a',placeholder:'search a...',
  307. lBtn:{
  308. label:'input',
  309. onClick: function(val,opt,el,ev,model){alert(val);}
  310. },
  311. rAddon:'@111.cc'
  312. }
  313. },
  314. {
  315. key:'b',type:'ui-input',className:'col-xs-4',
  316. templateOptions: {
  317. type: 'input',label: 'input a',placeholder:'search a...',
  318. rBtn:{
  319. icon:'glyphicon-search',
  320. onClick: function(val,opt,el,ev,model){alert(val);}
  321. },
  322. lAddon:'@'
  323. }
  324. },
  325. {
  326. key:'first',type:'ui-input',className:'col-xs-2',
  327. templateOptions: {
  328. type: 'input',
  329. label: 'first name',
  330. pkey:'name'
  331. }
  332. },
  333. {
  334. key:'last',type:'ui-input',className:'col-xs-2',
  335. templateOptions: {type: 'input',label: 'last name',pkey:'name'}},
  336. {
  337. key:'username',
  338. type:'ui-input',
  339. className:'col-xs-6',
  340. templateOptions: {
  341. type: 'input',
  342. label: 'User Name',
  343. placeholder: 'Enter name',
  344. required:true,
  345. maxLength:10
  346. }
  347. },
  348. {
  349. key: 'password',
  350. type: 'ui-input',
  351. className:'col-xs-6',
  352. templateOptions: {
  353. type: 'password',
  354. label: 'Password',
  355. placeholder: 'Password',
  356. required:true
  357. },
  358. expressionProperties: {
  359. 'templateOptions.disabled': '!model.username'
  360. }
  361. },
  362. {
  363. key: 'selectmodal',
  364. type: 'ui-input-selectmodal',
  365. className:'col-xs-6',
  366. templateOptions:{
  367. label: 'selectmodal',
  368. btnLabel:'search',
  369. btnClassName:'btn-o',
  370. modalParam:{
  371. items: function(){
  372. return ['a','b','c','d'];
  373. },
  374. title: function(){
  375. return '测试标题';
  376. }
  377. }
  378. }
  379. },
  380. {
  381. key: 'email',
  382. type: 'ui-input',
  383. className:'col-xs-6',
  384. templateOptions:{
  385. type: 'email',
  386. maxlength: 10,
  387. minlength: 6,
  388. label: 'EMail'
  389. }
  390. },
  391. {
  392. key: 'date',
  393. type: 'ui-datepicker',
  394. className:'col-xs-6',
  395. templateOptions: {
  396. label: 'Date',
  397. type: 'text',
  398. datepickerPopup: 'yyyy-MM-dd hh:mm:ss',
  399. initDate : function(){
  400. return new Date();
  401. }
  402. }
  403. },
  404. {
  405. key: 'multiselectItem',
  406. type: 'ui-multi-select-tree',
  407. className: 'col-xs-6',
  408. templateOptions:{
  409. label: 'multiSelect',
  410. placeholder:'请选择...',
  411. isMultiSelect:false,
  412. selLeafs:false,//只选叶子节点
  413. refreshData: function(APIService){
  414. return APIService.fetchDataList('changeclassify',{'idx':0,'sum':100});
  415. },
  416. APIService:api_bpm_data,
  417. optionsDataKey:'list',
  418. refreshDelay:1000,
  419. onDataCallback:function(item, selectItems, options, field, model){
  420. model[options.key]={id:item.id};
  421. }
  422. }
  423. },
  424. {
  425. key:'selectItem',
  426. type:'ui-select',
  427. className:'col-xs-4',
  428. templateOptions:{
  429. label:'ui-select',
  430. valueProp: 'code',//对应value
  431. labelProp: 'source',//对应key
  432. optionsUrl:'data/fetchDataList/incidentsource',//对应后台地址,api/之前的部分省略
  433. optionsDataKey:'list',//对应返回数组数据的键值,
  434. required:true,
  435. onChange:function(val, options, field, event ,model){
  436. model.start_code = val;
  437. }
  438. }
  439. },
  440. {
  441. key:'multiselectItem',
  442. type:'ui-multiselect',
  443. className:'col-xs-4',
  444. templateOptions:{
  445. label:'multi-select',
  446. valueProp: 'code',//对应value
  447. labelProp: 'source',//对应key
  448. optionsUrl:'data/fetchDataList/incidentsource',//对应后台地址,api/之前的部分省略
  449. optionsDataKey:'list',//对应返回数组数据的键值,
  450. required:true,
  451. onChange:function(val, options, field, event ,model){
  452. console.log(val);
  453. console.log(model);
  454. //model.start_code = val;
  455. }
  456. }
  457. },
  458. {
  459. key:'checklistItem',
  460. type:'ui-checklist',
  461. className:'col-xs-4',
  462. templateOptions: {
  463. label:'checklistItem',
  464. options: [
  465. {name: 'Iron Man', value: 'iron_man'},
  466. {name: 'Captain America', value: 'captain_america'},
  467. {name: 'Black Widow', value: 'black_widow'},
  468. {name: 'Hulk', value: 'hulk'},
  469. {name: 'Captain Marvel', value: 'captain_marvel'}
  470. ]
  471. }
  472. },
  473. {
  474. key:'radioItem',
  475. type:'ui-radio',
  476. className:'col-xs-4',
  477. templateOptions:{
  478. label:'radioItem',
  479. options: [
  480. {name: 'Iron Man', value: 'iron_man'},
  481. {name: 'Captain America', value: 'captain_america'},
  482. {name: 'Black Widow', value: 'black_widow'},
  483. {name: 'Hulk', value: 'hulk'},
  484. {name: 'Captain Marvel', value: 'captain_marvel'}
  485. ],type:'number'
  486. }
  487. },
  488. {
  489. key:'selectUser',
  490. type:'ui-userselect',
  491. className:'col-xs-12',
  492. templateOptions:{
  493. label:'请求人信息',
  494. modalTitle:'请求人列表',
  495. fetchItems:function(filterData, APIService){
  496. return APIService.fetchDataList('user',filterData);
  497. },
  498. Restangular:Restangular,
  499. ApiService:api_user_data,
  500. onClick:function(val, options, field, event , model){
  501. //model.start_code++;
  502. console.log(options);
  503. }
  504. }
  505. },
  506. {
  507. key:'cabUser',
  508. type:'ui-multiuserselect',
  509. className:'col-xs-12',
  510. templateOptions:{
  511. label:'CAB评审成员',
  512. modalTitle:'CAB成员列表',
  513. fetchItems:function(filterData, APIService){
  514. return APIService.fetchDataList('user',filterData);
  515. },
  516. Restangular:Restangular,
  517. ApiService:api_user_data,
  518. onClick:function(val, options, field, event , model){
  519. console.log(model);
  520. }
  521. }
  522. },
  523. {
  524. key:'incident',
  525. type:'ui-modelselect',
  526. className:'col-xs-3',
  527. templateOptions:{
  528. label:'关联事件工单',
  529. modalTitle:'事件列表',
  530. fetchItems:function(filterData,APIService){
  531. return APIService.fetchDataList('incident',filterData);
  532. },
  533. Restangular:"Restangular",
  534. ApiService:"api_bpm_data",
  535. columnDefs:[
  536. { name:'id', width:80, enableFiltering:false},
  537. { name:'title', displayName:'事件主题', width:140},
  538. { name:'type.name', displayName:'事件类型', width:140},
  539. { name:'emergency.name', displayName:'紧急度', width:80},
  540. { name:'state.name', displayName:'状态', width:100},
  541. { name:'emergency.name', displayName:'紧急度', width:80},
  542. { name:'influence.name', displayName:'影响度', width:80},
  543. { name:'priority.name', displayName:'优先级', width:80},
  544. { name:'handlerUser.name', displayName:'当前处理人', width:100},
  545. { name:'acceptDate', displayName:'创建时间', width:100}
  546. ],
  547. displayName:'title',
  548. onClick:function(val, options, field, event , model){
  549. //model.start_code++;
  550. console.log(options);
  551. }
  552. }
  553. },
  554. {
  555. key: 'worknumber',
  556. type: 'ui-workernumber',
  557. className:'col-xs-3',
  558. templateOptions: {
  559. label: 'work number',
  560. Restangular:Restangular,
  561. ApiService:api_bpm_domain,
  562. getWorkernumber: function(APIService){
  563. return APIService.workernumber('bg');
  564. }
  565. }
  566. },
  567. {
  568. key: 'chkme',
  569. type: 'ui-checkbox',
  570. className:'col-xs-3',
  571. templateOptions: {
  572. label: 'Check me out'
  573. }
  574. },
  575. {
  576. key: 'droplink',
  577. type: 'ui-link',
  578. className: 'col-xs-3',
  579. templateOptions:{
  580. label:'添加附件',
  581. onClick:function(val, options, field, event ,model){
  582. if(angular.isUndefined(field.form.dropState)){
  583. field.form.dropState=false;
  584. }
  585. field.form.dropState=!field.form.dropState;
  586. console.log(field);
  587. }
  588. }
  589. },
  590. {
  591. key: 'dropfile',
  592. type: 'ui-dropfile',
  593. className: 'col-xs-12',
  594. templateOptions:{
  595. label:'附件',
  596. uploadUrl:'http://127.0.0.1:9008/saveAttachments'
  597. }
  598. },
  599. {
  600. key: 'droplist',
  601. type: 'ui-dropfiletable',
  602. className: 'col-xs-12',
  603. templateOptions:{
  604. label:'附件下载列表',
  605. Restangular:Restangular,
  606. ApiService:api_bpm_domain
  607. }
  608. },
  609. {
  610. key: 'start_code',
  611. className:'col-xs-6',
  612. type: 'ui-hidden'
  613. },
  614. {
  615. key:'remark',
  616. type:'ui-textarea',
  617. className:'col-xs-12',
  618. templateOptions:{
  619. label:'remark is ',
  620. rows:3
  621. }
  622. },
  623. {
  624. key:'setStartCode',
  625. type:'ui-button',
  626. className:'col-xs-4',
  627. templateOptions:{
  628. label:'customButton',
  629. onClick:function(val, options, field, event ,model){
  630. model.start_code++;
  631. console.log(options);
  632. }
  633. }
  634. }
  635. ],
  636. model : {
  637. username:"robin lau",
  638. password:"123456",
  639. chkme:false,start_code:0,
  640. formtitle:"Hey!I am title value!",
  641. name:{
  642. first:"robin",last:"lau"
  643. },
  644. multiselectItem:[]
  645. //date:"2015-09-15 00:00:00"
  646. }
  647. };
  648. setTimeout(function(){
  649. decodeVMForm(testJson);
  650. $scope.$apply();
  651. },500);
  652. //测试数据结束
  653. }
  654. }
  655. }]);