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));
50 function deleteCollection(collObj){
51 self.collections.splice(self.collections.indexOf(collObj), 1);
59 function duplicateCollection(srcColName, destColName){
60 var newCol = self.getCollection(srcColName).clone(destColName);
61 self.collections.push(newCol);
69 function setCollObjNewName(collObj, newName){
70 collObj.name = newName;
71 collObj.data.forEach(function (item){
72 item.collection = newName;
77 * Mark reqObj as selected
78 * @param reqObj HistoryRequest object to be selected
80 function toggleReqSelection(onlyOneSelected, reqObj){
82 self.collections.forEach(function (collection){
83 collection.data.forEach(function (req){
84 req.selected = reqObj === req;
94 function renameCollection(oldName, newName){
95 var col = self.getCollection(oldName);
96 setCollObjNewName(col, newName);
102 * @returns {HistoryRequest|*}
104 function createEntry(elem) {
105 return RequestsService.createHistoryRequestFromElement(elem);
110 * @param {string} colName
113 function collectionExists(colName) {
114 return self.collections.some(function (col){
115 return col.name === colName;
123 function getCollectionNames(){
124 return self.collections.map(function (elem){
134 function getCollection(colName){
135 return self.collections.filter(function (col){
136 return col.name === colName;
145 function addRequestToList(reqObj){
146 if (reqObj.collection) {
148 if (self.collectionExists(reqObj.collection)) {
149 col = self.getCollection(reqObj.collection);
152 col = new CollectionModel(reqObj.collection);
153 self.collections.push(col);
155 col.data.push(reqObj);
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);
174 self.collections = [];
179 * @param collectionName
182 function toJSON(collectionName) {
184 return self.getCollection(collectionName).data.map(function (elem){
185 return elem.toJSON();
190 self.collections.forEach(function (collection){
191 collection.data.forEach(function (elem){
192 list.push(elem.toJSON());
203 function loadListFromFile(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);
214 CollectionListModel.prototype = Object.create(BaseListModel.prototype);
216 return CollectionListModel;