Merge "Yangman - Deselect history requests"
[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(ParsingJsonService, RequestsService){
14
15             BaseListModel.call(this, ParsingJsonService);
16
17             /* jshint validthis: true */
18             var self = this;
19
20             self.collections = [];
21             self.selectedRequests = [];
22
23             self.addRequestToList = addRequestToList;
24             self.clear = clear;
25             self.collectionExists = collectionExists;
26             self.createEntry = createEntry;
27             self.deleteCollection = deleteCollection;
28             self.deleteRequestItem = deleteRequestItem;
29             self.duplicateCollection = duplicateCollection;
30             self.getCollection = getCollection;
31             self.getCollectionNames = getCollectionNames;
32             self.loadListFromFile = loadListFromFile;
33             self.renameCollection = renameCollection;
34             self.toggleReqSelection = toggleReqSelection;
35             self.toJSON = toJSON;
36             self.getCollectionInJSON = getCollectionInJSON;
37
38             /**
39              *
40              * @param collectionName
41              */
42             function getCollectionInJSON(collectionName){
43                 return JSON.stringify(self.toJSON(collectionName));
44             }
45
46             /**
47              *
48              * @param collObj
49              */
50             function deleteCollection(collObj){
51                 self.collections.splice(self.collections.indexOf(collObj), 1);
52             }
53
54             /**
55              *
56              * @param srcColName
57              * @param destColName
58              */
59             function duplicateCollection(srcColName, destColName){
60                 var newCol = self.getCollection(srcColName).clone(destColName);
61                 self.collections.push(newCol);
62             }
63
64             /**
65              *
66              * @param collObj
67              * @param name
68              */
69             function setCollObjNewName(collObj, newName){
70                 collObj.name = newName;
71                 collObj.data.forEach(function (item){
72                     item.collection = newName;
73                 });
74             }
75
76             /**
77              * Mark reqObj as selected
78              * @param reqObj HistoryRequest object to be selected
79              */
80             function toggleReqSelection(onlyOneSelected, reqObj){
81
82                 self.collections.forEach(function (collection){
83                     collection.data.forEach(function (req){
84                         req.selected = reqObj === req;
85                     });
86                 });
87             }
88
89             /**
90              *
91              * @param oldName
92              * @param newName
93              */
94             function renameCollection(oldName, newName){
95                 var col = self.getCollection(oldName);
96                 setCollObjNewName(col, newName);
97             }
98
99             /**
100              *
101              * @param elem
102              * @returns {HistoryRequest|*}
103              */
104             function createEntry(elem) {
105                 return RequestsService.createHistoryRequestFromElement(elem);
106             }
107
108             /**
109              *
110              * @param {string} colName
111              * @returns {boolean}
112              */
113             function collectionExists(colName) {
114                 return self.collections.some(function (col){
115                     return col.name === colName;
116                 });
117             }
118
119             /**
120              *
121              * @returns {Array}
122              */
123             function getCollectionNames(){
124                 return self.collections.map(function (elem){
125                     return elem.name;
126                 });
127             }
128
129             /**
130              *
131              * @param colName
132              * @returns {T}
133              */
134             function getCollection(colName){
135                 return self.collections.filter(function (col){
136                     return col.name === colName;
137                 })[0];
138             }
139
140
141             /**
142              *
143              * @param reqObj
144              */
145             function addRequestToList(reqObj){
146                 if (reqObj.collection) {
147                     var col = null;
148                     if (self.collectionExists(reqObj.collection)) {
149                         col = self.getCollection(reqObj.collection);
150                     }
151                     else {
152                         col = new CollectionModel(reqObj.collection);
153                         self.collections.push(col);
154                     }
155                     col.data.push(reqObj);
156                 }
157             }
158
159
160             /**
161              *
162              * @param elem
163              */
164             function deleteRequestItem(elem){
165                 var col = self.getCollection(elem.collection);
166                 col.data.splice(col.data.indexOf(elem), 1);
167                 if (col.data.length === 0){
168                     self.collections.splice(self.collections.indexOf(col), 1);
169                 }
170
171             }
172
173             function clear() {
174                 self.collections = [];
175             }
176
177             /**
178              *
179              * @param collectionName
180              * @returns {*}
181              */
182             function toJSON(collectionName) {
183                 if (collectionName){
184                     return self.getCollection(collectionName).data.map(function (elem){
185                         return elem.toJSON();
186                     });
187                 }
188                 else {
189                     var list = [];
190                     self.collections.forEach(function (collection){
191                         collection.data.forEach(function (elem){
192                             list.push(elem.toJSON());
193                         });
194                     });
195                     return list;
196                 }
197             }
198
199             /**
200              *
201              * @param data
202              */
203             function loadListFromFile(data){
204                 if (data){
205                     ParsingJsonService.parseJson(data).map(function (elem) {
206                         return RequestsService.createHistoryRequest(elem.sentData, elem.receivedData, elem.path,
207                             elem.method, elem.status, elem.name, elem.collection);
208                     }).forEach(function (elem) {
209                         self.addRequestToList(elem);
210                     });
211                 }
212             }
213         }
214         CollectionListModel.prototype = Object.create(BaseListModel.prototype);
215
216         return CollectionListModel;
217     }
218 );