5c711425bb72adef368d874e779b4f5781bd4a62
[portal/sdk.git] /
1 (function(){
2    var stepFormFactory = function($http, $compile, $q, $log) {   
3            
4         var factory = {};
5         
6                 factory.getStepJSONData = function(jsonSrcName) {                       
7                         $http({
8                                 method: "GET",
9                 url: jsonSrcName,                       
10                         }).then(function(response) {
11                                 if (typeof response.data === 'object') {
12                                         return response.data;
13                                 } else {
14                                         return $q.reject(response.data);
15                                 }
16                         }, function(response) {
17                                 // something went wrong
18                                 return $q.reject(response.data);
19                         });                     
20                 }                                       
21                 
22                 
23                 factory.renderColumnForm = function(scope) {
24                         
25                 }
26         factory.renderForm = function(jsonSrcName, elem, scope) {
27                 let defer = $q.defer();
28                 var divStepForm = document.getElementById('stepView'); 
29                 var textStyle = "width:300px"
30                 var textAreaStyle = "width:300px"
31                 if(divStepForm) 
32                 while(divStepForm && divStepForm.firstChild){
33                     divStepForm.removeChild(divStepForm.firstChild);
34                 }
35                     $http({
36                     method: 'GET',
37                     url: jsonSrcName
38                     }).then(function successCallback(response) {
39                         // this callback will be called asynchronously
40                         // when the response is available
41                         if (typeof response.data === 'object') {
42                                 var json = response.data;
43                                 var step = json.step;
44                                 var div =d3.select(elem[0]);
45                                 var modelNameDict = {};
46                                 
47                                 div.append("h1").attr({"class":"font-color:blue"}).text(json.content.title);
48                                 div.append("div").html(json.content.contentHtml);
49                                 var sections = json.content.sections;
50                                 sections.forEach(function(d, i) {
51                                     var sectionDiv = div.append("div");
52                                     sectionDiv.attr("id", "section_"+i);
53                                     if(d.title != "finalButton")
54                                         sectionDiv.append("h4").text(d.title);
55                                     var proj_id_hidden = sectionDiv.append("input");
56                                     proj_id_hidden.attr({"type":"hidden", "value":"", "name": "proj_id", "ng-model":"proj_id", "style": "" });
57         
58                                     var elements = d.elements;
59                                     var buttons = d.buttons;
60                                     var elementTable ;
61                                     if(elements) {
62                                     elements.forEach(function(element, elementIndex) {
63                                         
64                                         var elementBody ;
65                                         if(elementIndex == 0) {
66                                             elementTable = sectionDiv.append("table").attr({"border":"0", "cellpadding":"4", "cellspacing":"0"});   
67                                             elementBody = elementTable.append("tbody");
68                                         } else {
69                                             elementBody = elementTable = sectionDiv.select("tbody");
70                                         }
71                                         if(element.input == 'hidden') {
72                                             
73                                         } else {
74                                         var tr = elementBody.append("tr")
75                                         var cellWidthStyle = {"width":"50%"}
76         
77                                         if (!(element.spanOverTwoColumns)){
78                                             var td = tr.append("td").attr({"width":"30%","class":"tdLeftColumn"});                                      
79                                                 if (element.input=="checkboxGroup") {
80                                                         td = td.attr({"rowspan":element.checkboxes.length})
81                                                 }
82                                                 td.text(element.displayName);
83                                                 var cellWidthStyle = {"width":"500px;"}
84                                         }
85                                         td = tr.append("td").attr(cellWidthStyle); 
86                                         var model_name_appendix='';
87                                             if (element.name in modelNameDict) {
88                                                 modelNameDict[element.name]=modelNameDict[element.name]+1
89                                                 model_name_appendix ='_'+modelNameDict[element.name]
90                                             } else {
91                                                 modelNameDict[element.name]=0
92                                             } 
93                                         if(element.input == 'text') {
94                                             var inp_text = td.append("input");
95                                             inp_text.attr({"type":"text", "value":element.defaultValue, "name": element.name, "ng-model":element.name+model_name_appendix, "style": (element.style?element.style:textStyle) });
96                                             }
97                                         else if(element.input === 'checkbox') {
98                                                 var temptd= td.append("label").attr({"class":"checkbox"});
99                                                 var temptd2= temptd.append("input").attr({"type":"checkbox", "ng-model": element.checkbox.value});
100                                                 var temptd3= temptd.append("i").attr({"class":"skin"})
101                                                 var temptd3= temptd.append("span").text(element.checkbox.text);
102                                             
103                                         } else if (element.input === 'radio') {
104                                                 element.options.forEach(function(d,i) {
105                                                     td.append("input").attr({"type":"radio", "id":element.name+"_"+d.id, "ng-model":element.name+model_name_appendix, "name": element.name, "value": d.id});
106                                                     td.append("label").text(d.name); 
107                                                 })
108                                         } else if (element.input === 'checkboxGroup') {
109                                                 
110                                                 element.checkboxes.forEach(function(d,i) {
111                                                         if (i==0) {
112                                                         var temptd= td.append("label").attr({"class":"checkbox"});
113                                                         var temptd2= temptd.append("input").attr({"type":"checkbox", "ng-model": d.value});
114                                                         var temptd3= temptd.append("i").attr({"class":"skin"})
115                                                         var temptd3= temptd.append("span").text(d.text);
116                                                         } else {
117                                                     var tr = elementBody.append("tr")
118                                                     td = tr.append("td").attr({"width":"50%"}); 
119                                                     var temptd= td.append("label").attr({"class":"checkbox"});
120                                                         var temptd2= temptd.append("input").attr({"type":"checkbox", "ng-model": d.value});
121                                                         var temptd3= temptd.append("i").attr({"class":"skin"})
122                                                         var temptd3= temptd.append("span").text(d.text);                                            
123                                                         }
124                                                 })
125                                         } else if (element.input === 'textarea') {
126                                                     td.append("textarea").attr({"rows":"4", "cols":"50", "ng-model":element.name+model_name_appendix, "name": element.name, "style": (element.style?element.style:textAreaStyle)});                          
127                                         } 
128                                         else if (element.input === 'select') {
129                                             var select = td.append("select");
130                                             // need to store options into element name;
131                                             select
132                                             .attr({"name": element.name,"b2b-dropdown":"","ng-model":element.ngModelName,"style":"width:300px"})
133                                             .attr("placeholder-text","Select");
134                                             
135                                             
136         /*                                    var data = ["Option 1", "Option 2", "Option 3"]*/
137                                             var options = select
138                                                 .selectAll('option')
139                                                 .data(element.options).enter()
140                                                 .append('option').attr("value", function(d) {return d.value})
141                                                         .attr("b2b-dropdown-list","")
142                                                     .text(function (d) { return d.text; })
143                                                     ;
144                                         } else if (element.input === 'tabletext') {
145                                             var colArray = element.columns.split(',');
146                                             var table = td.append("table");
147                                             table.attr("border", "1");
148         
149                                             colArray.forEach(function(d) {
150                                                 table.append("th").text(d);
151                                             }) 
152         
153                                         }
154                                        }
155                                     })
156                                     if (buttons) {
157                                                 buttons.forEach(function(button, buttonIndex) {
158                                                 sectionDiv.append("input").attr({"type":"button", "value": button.text, "ng-click":button.ngFunction,"class":"btn btn-alt btn-small"});
159                                             })
160                                             sectionDiv.append("br");
161                                         }
162                                     } 
163                                         else { //if elements
164                                         if((json.step != "1")&&(!json.hideBackButton))
165                                             sectionDiv.append("input").attr({"type":"button", "value": "Back", "ng-click":"previous()","class":"btn btn-alt btn-small"});
166                                         if((d.title == "finalButton")&(!json.hideSaveButton)) 
167                                             sectionDiv.append("input").attr({"type":"button", "value": "Save", "ng-click":"save()", "class":"btn btn-alt btn-small"});
168                                         if(!json.last_step)
169                                             sectionDiv.append("input").attr({"type":"button", "value": "Next", "ng-click":"next()", "class":"btn btn-alt btn-small"});
170         
171                                     }
172                                 })
173                                 elem.html(div.html());
174                                 elem.removeAttr("step-form");
175                                 $compile(elem.contents())(scope);
176                                 defer.resolve();
177                          } else {
178                                 $log.error
179                         return $q.reject(response.data);
180                     }
181                    }, function errorCallback(response) {
182                         // called asynchronously if an error occurs
183                         // or server returns response with an error status.
184                             $log.error("reponse data is not a valid JSON object");
185                         defer.resolve();        
186                     });
187                 //}
188                 return defer.promise;
189         };
190        
191         return factory;
192    };
193    
194     stepFormFactory.$inject = ['$http', '$compile', '$q'];
195     
196     appDS2.factory('stepFormFactory', stepFormFactory);
197     
198 }())