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($filter, ParsingJsonService, RequestsService){
15 BaseListModel.call(this, $filter, ParsingJsonService);
17 /* jshint validthis: true */
21 * Array of collections in which are requests from self.list groupped
24 self.collections = [];
26 self.addItemToList = addItemToList;
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;
39 self.getCollectionInJSON = getCollectionInJSON;
40 self.getExpandedCollectionNames = getExpandedCollectionNames;
41 self.expandCollectionByNames = expandCollectionByNames;
42 self.getCollectionInRawJSON = getCollectionInRawJSON;
46 * @param collectionName
48 function getCollectionInJSON(collectionName){
49 return JSON.stringify(self.toJSON(collectionName));
52 function getCollectionInRawJSON(collectionName){
53 return self.toJSON(collectionName);
60 function deleteCollection(collObj){
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);
76 function duplicateCollection(srcColName, destColName){
77 var newCol = self.getCollection(srcColName).clone(destColName);
78 self.collections.push(newCol);
86 function setCollObjNewName(collObj, newName){
87 collObj.name = newName;
88 collObj.data.forEach(function (item){
89 item.collection = newName;
94 * Mark reqObj as selected
95 * @param reqObj HistoryRequest object to be selected
97 function toggleReqSelection(onlyOneSelected, reqObj){
99 //self.collections.forEach(function (collection){
100 // collection.data.forEach(function (req){
101 // req.selected = reqObj === req;
105 if (onlyOneSelected){
106 self.selectedItems.forEach(function (req){
107 req.selected = false;
109 self.selectedItems = [];
112 if (reqObj.selected && !onlyOneSelected){
113 self.selectedItems.splice(self.selectedItems.indexOf(reqObj), 1);
116 reqObj.selected = (reqObj.selected && onlyOneSelected) || !reqObj.selected;
117 if (reqObj.selected){
118 self.selectedItems.push(reqObj);
127 function renameCollection(oldName, newName){
128 var col = self.getCollection(oldName);
129 setCollObjNewName(col, newName);
135 * @returns {HistoryRequest|*}
137 function createItem(elem) {
138 return RequestsService.createHistoryRequestFromElement(elem);
143 * @param {string} colName
146 function collectionExists(colName) {
147 return self.collections.some(function (col){
148 return col.name === colName;
156 function getCollectionNames(){
157 return self.collections.map(function (elem){
167 function getCollection(colName){
168 return self.collections.filter(function (col){
169 return col.name === colName;
178 function addItemToList(reqObj){
179 self.list.push(reqObj);
180 if (reqObj.collection) {
182 if (self.collectionExists(reqObj.collection)) {
183 col = self.getCollection(reqObj.collection);
186 col = new CollectionModel(reqObj.collection);
187 self.collections.push(col);
189 col.data.push(reqObj);
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);
208 self.collections = [];
209 self.selectedItems = [];
215 * @param collectionName
218 function toJSON(collectionName) {
220 return self.getCollection(collectionName).data.map(function (elem){
221 return elem.toJSON();
226 self.collections.forEach(function (collection){
227 collection.data.forEach(function (elem){
228 list.push(elem.toJSON());
239 function loadListFromFile(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);
251 * Get expanded collection names
253 function getExpandedCollectionNames(){
254 var expandCollectionNames = [];
255 self.collections.forEach(function(collection){
256 if (collection.expanded) {
257 expandCollectionNames.push(collection.name);
260 return expandCollectionNames;
264 * Expand collections by expandCollectionNames
266 function expandCollectionByNames(expandCollectionNames){
268 self.collections.forEach(function(collection){
269 if (findName(collection.name)) {
270 collection.expanded = true;
274 function findName(name) {
276 expandCollectionNames.forEach(function(collectionName){
277 if (name === collectionName) {
288 CollectionListModel.prototype = Object.create(BaseListModel.prototype);
290 return CollectionListModel;