Merge "Yangman - select all requests from history tab"
[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              * @param collObj
48              */
49             function deleteCollection(collObj){
50
51                 var colIndex = self.collections.indexOf(collObj);
52                 // first check if collObj is the same as got by index to prevent delete wrong collection because of the
53                 // asynchron processes (when user is pressing enter many times, this method could be called more times
54                 // and in some other proceeding might be this collection already deleted)
55                 if (self.collections[colIndex] && self.collections[colIndex].name === collObj.name){
56                     self.collections.splice(colIndex, 1);
57                 }
58             }
59
60             /**
61              *
62              * @param srcColName
63              * @param destColName
64              */
65             function duplicateCollection(srcColName, destColName){
66                 var newCol = self.getCollection(srcColName).clone(destColName);
67                 self.collections.push(newCol);
68             }
69
70             /**
71              *
72              * @param collObj
73              * @param name
74              */
75             function setCollObjNewName(collObj, newName){
76                 collObj.name = newName;
77                 collObj.data.forEach(function (item){
78                     item.collection = newName;
79                 });
80             }
81
82             /**
83              * Mark reqObj as selected
84              * @param reqObj HistoryRequest object to be selected
85              */
86             function toggleReqSelection(onlyOneSelected, reqObj){
87
88                 self.collections.forEach(function (collection){
89                     collection.data.forEach(function (req){
90                         req.selected = reqObj === req;
91                     });
92                 });
93             }
94
95             /**
96              *
97              * @param oldName
98              * @param newName
99              */
100             function renameCollection(oldName, newName){
101                 var col = self.getCollection(oldName);
102                 setCollObjNewName(col, newName);
103             }
104
105             /**
106              *
107              * @param elem
108              * @returns {HistoryRequest|*}
109              */
110             function createEntry(elem) {
111                 return RequestsService.createHistoryRequestFromElement(elem);
112             }
113
114             /**
115              *
116              * @param {string} colName
117              * @returns {boolean}
118              */
119             function collectionExists(colName) {
120                 return self.collections.some(function (col){
121                     return col.name === colName;
122                 });
123             }
124
125             /**
126              *
127              * @returns {Array}
128              */
129             function getCollectionNames(){
130                 return self.collections.map(function (elem){
131                     return elem.name;
132                 });
133             }
134
135             /**
136              *
137              * @param colName
138              * @returns {T}
139              */
140             function getCollection(colName){
141                 return self.collections.filter(function (col){
142                     return col.name === colName;
143                 })[0];
144             }
145
146
147             /**
148              *
149              * @param reqObj
150              */
151             function addRequestToList(reqObj){
152                 if (reqObj.collection) {
153                     var col = null;
154                     if (self.collectionExists(reqObj.collection)) {
155                         col = self.getCollection(reqObj.collection);
156                     }
157                     else {
158                         col = new CollectionModel(reqObj.collection);
159                         self.collections.push(col);
160                     }
161                     col.data.push(reqObj);
162                 }
163             }
164
165
166             /**
167              *
168              * @param elem
169              */
170             function deleteRequestItem(elem){
171                 var col = self.getCollection(elem.collection);
172                 col.data.splice(col.data.indexOf(elem), 1);
173                 if (col.data.length === 0){
174                     self.collections.splice(self.collections.indexOf(col), 1);
175                 }
176
177             }
178
179             function clear() {
180                 self.collections = [];
181             }
182
183             /**
184              *
185              * @param collectionName
186              * @returns {*}
187              */
188             function toJSON(collectionName) {
189                 if (collectionName){
190                     return self.getCollection(collectionName).data.map(function (elem){
191                         return elem.toJSON();
192                     });
193                 }
194                 else {
195                     var list = [];
196                     self.collections.forEach(function (collection){
197                         collection.data.forEach(function (elem){
198                             list.push(elem.toJSON());
199                         });
200                     });
201                     return list;
202                 }
203             }
204
205             /**
206              *
207              * @param data
208              */
209             function loadListFromFile(data){
210                 if (data){
211                     ParsingJsonService.parseJson(data).map(function (elem) {
212                         return RequestsService.createHistoryRequest(elem.sentData, elem.receivedData, elem.path,
213                             elem.method, elem.status, elem.name, elem.collection);
214                     }).forEach(function (elem) {
215                         self.addRequestToList(elem);
216                     });
217                 }
218             }
219         }
220         CollectionListModel.prototype = Object.create(BaseListModel.prototype);
221
222         return CollectionListModel;
223     }
224 );