Merge "Yangman - show history with html response correctly"
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / models / collectionlist.model.js
1 define(
2     ['app/yangman/models/baselist.model', 'app/yangman/models/collection.model'],
3     function (BaseListModel, CollectionModel){
4
5         'use strict';
6
7         /**
8          * Base collection list object
9          * @constructor
10          * @param ParsingJsonService
11          * @param RequestsService
12          */
13         function CollectionListModel($filter, ParsingJsonService, RequestsService){
14
15             BaseListModel.call(this, $filter, ParsingJsonService);
16
17             /* jshint validthis: true */
18             var self = this;
19
20             /**
21              * Array of collections in which are requests from self.list groupped
22              * @type {Array}
23              */
24             self.collections = [];
25
26             self.addItemToList = addItemToList;
27             self.clear = clear;
28             self.collectionExists = collectionExists;
29             self.createItem = createItem;
30             self.deleteCollection = deleteCollection;
31             self.deleteRequestItem = deleteRequestItem;
32             self.duplicateCollection = duplicateCollection;
33             self.getCollection = getCollection;
34             self.getCollectionNames = getCollectionNames;
35             self.loadListFromFile = loadListFromFile;
36             self.renameCollection = renameCollection;
37             self.toggleReqSelection = toggleReqSelection;
38             self.toJSON = toJSON;
39             self.getCollectionInJSON = getCollectionInJSON;
40             self.getExpandedCollectionNames = getExpandedCollectionNames;
41             self.expandCollectionByNames = expandCollectionByNames;
42             self.getCollectionInRawJSON = getCollectionInRawJSON;
43
44             /**
45              *
46              * @param collectionName
47              */
48             function getCollectionInJSON(collectionName){
49                 return JSON.stringify(self.toJSON(collectionName));
50             }
51
52             function getCollectionInRawJSON(collectionName){
53                 return self.toJSON(collectionName);
54             }
55
56
57             /**
58              * @param collObj
59              */
60             function deleteCollection(collObj){
61
62                 var colIndex = self.collections.indexOf(collObj);
63                 // first check if collObj is the same as got by index to prevent delete wrong collection because of the
64                 // asynchron processes (when user is pressing enter many times, this method could be called more times
65                 // and in some other proceeding might be this collection already deleted)
66                 if (self.collections[colIndex] && self.collections[colIndex].name === collObj.name){
67                     self.collections.splice(colIndex, 1);
68                 }
69             }
70
71             /**
72              *
73              * @param srcColName
74              * @param destColName
75              */
76             function duplicateCollection(srcColName, destColName){
77                 var newCol = self.getCollection(srcColName).clone(destColName);
78                 self.collections.push(newCol);
79             }
80
81             /**
82              *
83              * @param collObj
84              * @param name
85              */
86             function setCollObjNewName(collObj, newName){
87                 collObj.name = newName;
88                 collObj.data.forEach(function (item){
89                     item.collection = newName;
90                 });
91             }
92
93             /**
94              * Mark reqObj as selected
95              * @param reqObj HistoryRequest object to be selected
96              */
97             function toggleReqSelection(onlyOneSelected, reqObj){
98
99                 //self.collections.forEach(function (collection){
100                 //    collection.data.forEach(function (req){
101                 //        req.selected = reqObj === req;
102                 //    });
103                 //});
104
105                 if (onlyOneSelected){
106                     self.selectedItems.forEach(function (req){
107                         req.selected = false;
108                     });
109                     self.selectedItems = [];
110                 }
111
112                 if (reqObj.selected && !onlyOneSelected){
113                     self.selectedItems.splice(self.selectedItems.indexOf(reqObj), 1);
114                 }
115
116                 reqObj.selected = (reqObj.selected && onlyOneSelected) || !reqObj.selected;
117                 if (reqObj.selected){
118                     self.selectedItems.push(reqObj);
119                 }
120             }
121
122             /**
123              *
124              * @param oldName
125              * @param newName
126              */
127             function renameCollection(oldName, newName){
128                 var col = self.getCollection(oldName);
129                 setCollObjNewName(col, newName);
130             }
131
132             /**
133              *
134              * @param elem
135              * @returns {HistoryRequest|*}
136              */
137             function createItem(elem) {
138                 return RequestsService.createHistoryRequestFromElement(elem);
139             }
140
141             /**
142              *
143              * @param {string} colName
144              * @returns {boolean}
145              */
146             function collectionExists(colName) {
147                 return self.collections.some(function (col){
148                     return col.name === colName;
149                 });
150             }
151
152             /**
153              *
154              * @returns {Array}
155              */
156             function getCollectionNames(){
157                 return self.collections.map(function (elem){
158                     return elem.name;
159                 });
160             }
161
162             /**
163              *
164              * @param colName
165              * @returns {T}
166              */
167             function getCollection(colName){
168                 return self.collections.filter(function (col){
169                     return col.name === colName;
170                 })[0];
171             }
172
173
174             /**
175              *
176              * @param reqObj
177              */
178             function addItemToList(reqObj){
179                 self.list.push(reqObj);
180                 if (reqObj.collection) {
181                     var col = null;
182                     if (self.collectionExists(reqObj.collection)) {
183                         col = self.getCollection(reqObj.collection);
184                     }
185                     else {
186                         col = new CollectionModel(reqObj.collection);
187                         self.collections.push(col);
188                     }
189                     col.data.push(reqObj);
190                 }
191             }
192
193
194             /**
195              *
196              * @param elem
197              */
198             function deleteRequestItem(elem){
199                 var col = self.getCollection(elem.collection);
200                 col.data.splice(col.data.indexOf(elem), 1);
201                 if (col.data.length === 0){
202                     self.collections.splice(self.collections.indexOf(col), 1);
203                 }
204
205             }
206
207             function clear() {
208                 self.collections = [];
209                 self.selectedItems = [];
210                 self.list = [];
211             }
212
213             /**
214              *
215              * @param collectionName
216              * @returns {*}
217              */
218             function toJSON(collectionName) {
219                 if (collectionName){
220                     return self.getCollection(collectionName).data.map(function (elem){
221                         return elem.toJSON();
222                     });
223                 }
224                 else {
225                     var list = [];
226                     self.collections.forEach(function (collection){
227                         collection.data.forEach(function (elem){
228                             list.push(elem.toJSON());
229                         });
230                     });
231                     return list;
232                 }
233             }
234
235             /**
236              *
237              * @param data
238              */
239             function loadListFromFile(data){
240                 if (data){
241                     ParsingJsonService.parseJson(data).map(function (elem) {
242                         return RequestsService.createHistoryRequest(elem.sentData, elem.receivedData, elem.path,
243                             elem.method, elem.status, elem.name, elem.collection);
244                     }).forEach(function (elem) {
245                         self.addItemToList(elem);
246                     });
247                 }
248             }
249
250             /**
251              * Get expanded collection names
252              */
253             function getExpandedCollectionNames(){
254                 var expandCollectionNames = [];
255                 self.collections.forEach(function(collection){
256                     if (collection.expanded) {
257                         expandCollectionNames.push(collection.name);
258                     }
259                 });
260                 return expandCollectionNames;
261             }
262
263             /**
264              * Expand collections by expandCollectionNames
265              */
266             function expandCollectionByNames(expandCollectionNames){
267
268                 self.collections.forEach(function(collection){
269                     if (findName(collection.name)) {
270                         collection.expanded = true;
271                     }
272                 });
273
274                 function findName(name) {
275                     var found = false;
276                     expandCollectionNames.forEach(function(collectionName){
277                         if (name === collectionName) {
278                             found = true;
279                             return null;
280                         }
281                     });
282                     return found;
283                 }
284
285             }
286
287         }
288         CollectionListModel.prototype = Object.create(BaseListModel.prototype);
289
290         return CollectionListModel;
291     }
292 );