2 ['app/yangman/models/baselist.model', 'app/yangman/models/collection.model'],
3 function (BaseListModel, CollectionModel){
8 * Base collection list object
10 * @param ParsingJsonService
11 * @param RequestsService
13 function CollectionListModel(ParsingJsonService, RequestsService){
15 BaseListModel.call(this, ParsingJsonService);
17 /* jshint validthis: true */
20 self.collections = [];
21 self.selectedRequests = [];
23 self.addRequestToList = addRequestToList;
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;
36 self.getCollectionInJSON = getCollectionInJSON;
40 * @param collectionName
42 function getCollectionInJSON(collectionName){
43 return JSON.stringify(self.toJSON(collectionName));
49 function deleteCollection(collObj){
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);
65 function duplicateCollection(srcColName, destColName){
66 var newCol = self.getCollection(srcColName).clone(destColName);
67 self.collections.push(newCol);
75 function setCollObjNewName(collObj, newName){
76 collObj.name = newName;
77 collObj.data.forEach(function (item){
78 item.collection = newName;
83 * Mark reqObj as selected
84 * @param reqObj HistoryRequest object to be selected
86 function toggleReqSelection(onlyOneSelected, reqObj){
88 self.collections.forEach(function (collection){
89 collection.data.forEach(function (req){
90 req.selected = reqObj === req;
100 function renameCollection(oldName, newName){
101 var col = self.getCollection(oldName);
102 setCollObjNewName(col, newName);
108 * @returns {HistoryRequest|*}
110 function createEntry(elem) {
111 return RequestsService.createHistoryRequestFromElement(elem);
116 * @param {string} colName
119 function collectionExists(colName) {
120 return self.collections.some(function (col){
121 return col.name === colName;
129 function getCollectionNames(){
130 return self.collections.map(function (elem){
140 function getCollection(colName){
141 return self.collections.filter(function (col){
142 return col.name === colName;
151 function addRequestToList(reqObj){
152 if (reqObj.collection) {
154 if (self.collectionExists(reqObj.collection)) {
155 col = self.getCollection(reqObj.collection);
158 col = new CollectionModel(reqObj.collection);
159 self.collections.push(col);
161 col.data.push(reqObj);
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);
180 self.collections = [];
185 * @param collectionName
188 function toJSON(collectionName) {
190 return self.getCollection(collectionName).data.map(function (elem){
191 return elem.toJSON();
196 self.collections.forEach(function (collection){
197 collection.data.forEach(function (elem){
198 list.push(elem.toJSON());
209 function loadListFromFile(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);
220 CollectionListModel.prototype = Object.create(BaseListModel.prototype);
222 return CollectionListModel;