Merge "Yangman - Deselect history requests"
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / models / baselist.model.js
1 define([], function (){
2     'use strict';
3
4
5     /**
6      * Base list object for extending history and collection object
7      * @constructor
8      * @param ParsingJsonService
9      */
10     function BaseListModel(ParsingJsonService) {
11
12         var self = this;
13
14         self.addFromJSON = addFromJSON;
15         self.addRequestToList = addRequestToList;
16         self.createEntry = createEntry;
17         self.errorEditCbk = errorEditCbk;
18         self.loadListFromStorage = loadListFromStorage;
19         self.refresh = refresh;
20         self.saveToStorage = saveToStorage;
21         self.successfullEditCbk = successfullEditCbk;
22         self.setName = setName;
23
24         function setName(name) {
25             self.name = name;
26         }
27
28         function createEntry(elem) {
29             return elem;
30         }
31
32         function addRequestToList(){}
33
34         function refresh(){}
35
36         function successfullEditCbk(){}
37
38         function errorEditCbk(){}
39
40         /**
41          * Loading from localStorage
42          */
43         function loadListFromStorage(){
44             var storageList = localStorage.getItem(self.name);
45
46             if (storageList){
47                 self.clear();
48                 ParsingJsonService.parseJson(storageList).map(function (elem) {
49                     return self.createEntry(elem);
50                 }).forEach(function (elem) {
51                     self.addRequestToList(elem);
52                 });
53             }
54         }
55
56         /**
57          * Saving to local storage
58          */
59         function saveToStorage(){
60             try {
61                 localStorage.setItem(self.name, JSON.stringify(self.toJSON()));
62             } catch (e) {
63                 // console.info('DataStorage error:', e);
64             }
65         }
66
67         /**
68          * Add each request from json
69          * @param json
70          */
71         function addFromJSON(json) {
72             json.forEach(function (elem) {
73                 var req = self.createEntry(elem);
74                 self.addRequestToList(req);
75             });
76         }
77     }
78
79     return BaseListModel;
80 });