source: branches/testa-single-bookmark/js/storagex.js @ 38

Last change on this file since 38 was 36, checked in by anonymous, 6 years ago

hide unused menu by css

File size: 5.4 KB
Line 
1class Storagex {
2    constructor () {
3        this._globalStorage = {}
4        this._items = GlobalxItems
5    }
6
7    ensure_data_structure ( item , parts ) {
8        parts.map( (part) => {
9            if( item[part] === undefined ){
10                item[part] = {}
11            }
12        } )
13    }
14
15    save_as_info_from_globalx() {
16        this.save_as_info_from_globalx_with_items(this._items)
17    }
18
19    save_as_info_from_globalx_with_items(items){
20        let info = {}
21
22        this.copy_object( info, Globalx, items )
23        Globalx.parts.map( (part) => {
24            this.save_as_info_from_globalx_part(part , info)
25        } )
26
27        this.save_as_info_to_local_storage(info)
28    }
29
30    save_as_info_to_local_storage(obj){
31        this.save_to_local_storage("info" , obj)
32    }
33
34    save_as_info_from_globalx_part(part , info){
35        info[part] = {}
36        this.copy_obj_with_part(info, Globalx, part)
37       
38        info.bookmark_mgr = this.convert_to_plain_object_from_bookmark_mgr(Globalx.bookmark_mgr)
39    }
40
41    save_to_local_storage(key , obj) {
42        this._globalStorage[key] = obj
43        localStorage.setItem(key , JSON.stringify(obj) )
44    }
45
46    change_cache_x( obj ){
47        this._globalStorage[key] = obj
48//      localStorage.setItem(key , JSON.stringify(this._globalStorage[key]) )
49    }
50
51    change_cache( obj , num , part ){
52        const key = `${num}/${part}`
53        this._globalStorage[key] = obj
54        localStorage.setItem(key , JSON.stringify(this._globalStorage[key]) )
55    }
56
57    async convert_mgr_object_async( mgr, array ){
58        return await array.map( (path) => {
59            const item = mgr.get(part , path)
60            return {
61                part: part,
62                path: item.path,
63                display_name: item.display_name
64            }
65        })
66    }
67
68    convert_to_plain_object_from_bookmark_mgr(mgr){
69        let obj = {}
70        obj.bms = {}
71
72        Object.entries( mgr.paths ).map( ([part, array]) => {
73            obj.bms[part] = this.convert_mgr_object_async(mgr, array)
74        } )
75
76        obj.max_display_name_length = mgr.max_display_name_length
77        obj.parts = mgr.parts
78
79        return obj
80    }
81
82    convert_to_plain_object_from_bookmark_mgr0(mgr){
83        let obj = {}
84        obj.bms = Object.fromEntries( Object.entries( mgr.paths ).map( ([part, array]) => {
85            const bk_items = this.convert_mgr_object_async(mgr, array)
86
87            ([part, bk_items])
88        } ) )
89        obj.max_display_name_length = mgr.max_display_name_length
90        obj.parts = mgr.parts
91
92        return obj
93    }
94
95    copy_obj_with_part(to_obj, from_obj, part){
96        const items = [
97            "editor_id",
98            "textarea_sel" ,
99            "menu_id",
100            "bookmark_id",
101            "bookmark_op_id",
102            "item_name",
103            "bookmarks",
104            "key",
105            "key_sel",
106            "download_sel",
107            "download_url_sel",
108            "download_partial_url_sel",
109            "menu_sel" ,
110            "bookmark_mgr" ,
111            "bookmark_op_sel"
112        ]
113        if( to_obj[part] === undefined ){
114            to_obj[part] = {}
115        }
116        this.copy_object( to_obj[part], from_obj[part], items )
117        this.copy_object( to_obj[part], GlobalxInitValue, GlobalxInitValueItems )
118    }
119
120    copy_object( to_obj, from_obj, items ){
121        let x = null
122        items.map( (item) => {
123            x = from_obj[item]
124            to_obj[item] = x
125        } )
126    }
127
128    restore_info(){
129        const item_name = "info"
130        return this.restore_from_localstorage(item_name)
131    }
132
133    restore_from_localstorage(num, part= null){
134        let key = null
135        let ret = null
136        if( part === null ){
137            key = num
138        }
139        else{
140            key = `${num}/${part}`
141        }
142        const str = localStorage.getItem(key)
143        if( str ){
144            ret = this._globalStorage[key] = JSON.parse(str)
145//          console.log("### rfl 1 key=" + key + " str=" + str)
146        }
147        else{
148            if( this._globalStorage[key] !== undefined ){
149                delete this._globalStorage[key]
150            }
151            ret = this._globalStorage[key]
152//          console.log("### rfl 2 key=" + key + " undefined")
153        }
154
155        return ret
156    }
157
158    restore_globalx_from_info(){
159        let need_to_save = false
160        const info = this.restore_info()
161        if( info !== undefined && info !== null ){
162            need_to_save = this.restore_info_to_globalx_with_items(info, this._items)
163        }
164        else{
165            need_to_save = true
166        }
167
168        return need_to_save
169    }
170
171    convert_to_bookmark_mgr_from_plain_object(mgr) {
172        let new_mgr = new BookmarkMgr( mgr.max_display_name_length , mgr.parts)
173       
174        const bk_items = mgr.bms
175        Object.entries( ([part, bk_items]) => {
176            new_bk_items = bk_items.map( (bk_item) => {
177                return new_mgr.add( bk_item.part , bk_item.path , bk_item.display_name)
178            } )
179            [part, new_bk_items]
180        } )
181       
182        return new_mgr
183    }
184
185    restore_info_to_globalx_with_items(info, items){
186        let need_to_save = true
187
188        this.copy_object( Globalx, info, items )
189
190        if( info.bookmark_mgr !== undefined ){
191            info.bookmark_mgr = this.convert_to_bookmark_mgr_from_plain_object(info.bookmark_mgr)
192       
193            if( info.parts !== undefined ){
194                info.parts.map( (part) => {
195                    this.restore_globalx_from_info_part(info , part)
196                } )
197                need_to_save = false
198            }
199        }
200        else{
201            Globalx.bookmark_mgr = null
202        }
203        return need_to_save
204    }
205
206    restore_globalx_from_info_part(info , part){
207        this.copy_obj_with_part(Globalx, info, part)
208    }
209
210    get_filelist(num, part) {
211        return this.restore_from_localstorage(num, part)
212    }
213
214    get_output_url() {
215        return this._globalStorage.output_url
216    }
217
218    reset_info = () => {
219        let item_name = 'info'
220        reset_localstorage(item_name)
221    }
222   
223    reset_localstorage(item_name){
224        delete this._globalStorage[item_name]
225        localStorage.removeItem(item_name)
226    }
227
228    clear_localstorage = () => {
229        let keys = Object.keys(this._globalStorage)
230        keys.map( (v) => { delete this._globalStorage[v] } )
231       
232        localStorage.clear()
233    }
234
235    show_localstorage() {
236        window.open('local_storage.html', '_blank')
237    }
238
239    show_globalx() {
240        localStorage.setItem('globalx' , _globalStorage[0] )
241    }
242}
243
Note: See TracBrowser for help on using the repository browser.