Repository navigation
Expand file tree
/
Copy pathlistBuilder.js
More file actions
196 lines (129 loc) · 4.19 KB
/
Copy pathlistBuilder.js
File metadata and controls
196 lines (129 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* EasyAutocomplete - ListBuilderService
*
* @author Łukasz Pawełczak
*
*/
var EasyAutocomplete = (function(scope) {
scope.ListBuilderService = function ListBuilderService(configuration, proccessResponseData) {
this.init = function(data) {
var listBuilder = [],
builder = {};
builder.data = configuration.get("listLocation")(data);
builder.getValue = configuration.get("getValue");
builder.maxListSize = configuration.get("list").maxNumberOfElements;
listBuilder.push(builder);
return listBuilder;
};
this.updateCategories = function(listBuilder, data) {
if (configuration.get("categoriesAssigned")) {
listBuilder = [];
for(var i = 0; i < configuration.get("categories").length; i += 1) {
var builder = convertToListBuilder(configuration.get("categories")[i], data);
listBuilder.push(builder);
}
}
return listBuilder;
};
this.convertXml = function(listBuilder) {
if(configuration.get("dataType").toUpperCase() === "XML") {
for(var i = 0; i < listBuilder.length; i += 1) {
listBuilder[i].data = convertXmlToList(listBuilder[i]);
}
}
return listBuilder;
};
this.processData = function(listBuilder, inputPhrase) {
for(var i = 0, length = listBuilder.length; i < length; i+=1) {
listBuilder[i].data = proccessResponseData(configuration, listBuilder[i], inputPhrase);
}
return listBuilder;
};
this.checkIfDataExists = function(listBuilders) {
for(var i = 0, length = listBuilders.length; i < length; i += 1) {
if (listBuilders[i].data !== undefined && listBuilders[i].data instanceof Array) {
if (listBuilders[i].data.length > 0) {
return true;
}
}
}
return false;
};
function convertToListBuilder(category, data) {
var builder = {};
if(configuration.get("dataType").toUpperCase() === "XML") {
builder = convertXmlToListBuilder();
} else {
builder = convertDataToListBuilder();
}
if (category.header !== undefined) {
builder.header = category.header;
}
if (category.maxNumberOfElements !== undefined) {
builder.maxNumberOfElements = category.maxNumberOfElements;
}
if (configuration.get("list").maxNumberOfElements !== undefined) {
builder.maxListSize = configuration.get("list").maxNumberOfElements;
}
if (category.getValue !== undefined) {
if (typeof category.getValue === "string") {
var defaultsGetValue = category.getValue;
builder.getValue = function(element) {
return element[defaultsGetValue];
};
} else if (typeof category.getValue === "function") {
builder.getValue = category.getValue;
}
} else {
builder.getValue = configuration.get("getValue");
}
return builder;
function convertXmlToListBuilder() {
var builder = {},
listLocation;
if (category.xmlElementName !== undefined) {
builder.xmlElementName = category.xmlElementName;
}
if (category.listLocation !== undefined) {
listLocation = category.listLocation;
} else if (configuration.get("listLocation") !== undefined) {
listLocation = configuration.get("listLocation");
}
if (listLocation !== undefined) {
if (typeof listLocation === "string") {
builder.data = $(data).find(listLocation);
} else if (typeof listLocation === "function") {
builder.data = listLocation(data);
}
} else {
builder.data = data;
}
return builder;
}
function convertDataToListBuilder() {
var builder = {};
if (category.listLocation !== undefined) {
if (typeof category.listLocation === "string") {
builder.data = data[category.listLocation];
} else if (typeof category.listLocation === "function") {
builder.data = category.listLocation(data);
}
} else {
builder.data = data;
}
return builder;
}
}
function convertXmlToList(builder) {
var simpleList = [];
if (builder.xmlElementName === undefined) {
builder.xmlElementName = configuration.get("xmlElementName");
}
$(builder.data).find(builder.xmlElementName).each(function() {
simpleList.push(this);
});
return simpleList;
}
};
return scope;
})(EasyAutocomplete || {});