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

Last change on this file since 81 was 66, checked in by anonymous, 6 years ago

set_anchor_url

File size: 5.3 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    get_filelist(num, part) {
129        return this.restore_from_localstorage(num, part)
130    }
131
132    restore_info(){
133        const item_name = "info"
134        return this.restore_from_localstorage(item_name)
135    }
136
137    restore_from_localstorage(num, part= null){
138        let key = null
139        let ret = null
140        if( part === null ){
141            key = num
142        }
143        else{
144            key = `${num}/${part}`
145        }
146        const str = localStorage.getItem(key)
147        if( str ){
148            ret = this._globalStorage[key] = JSON.parse(str)
149//          console.log("### rfl 1 key=" + key + " str=" + str)
150        }
151        else{
152            if( this._globalStorage[key] !== undefined ){
153                delete this._globalStorage[key]
154            }
155            ret = this._globalStorage[key]
156//          console.log("### rfl 2 key=" + key + " undefined")
157        }
158
159        return ret
160    }
161
162    restore_globalx_from_info(){
163        let need_to_save = false
164        const info = this.restore_info()
165        if( info !== undefined && info !== null ){
166            need_to_save = this.restore_info_to_globalx_with_items(info, this._items)
167        }
168        else{
169            need_to_save = true
170        }
171
172        return need_to_save
173    }
174
175    convert_to_bookmark_mgr_from_plain_object(mgr) {
176        let new_mgr = new BookmarkMgr( mgr.max_display_name_length , mgr.parts)
177       
178        const bk_items = mgr.bms
179        Object.entries( ([part, bk_items]) => {
180            new_bk_items = bk_items.map( (bk_item) => {
181                return new_mgr.add( bk_item.part , bk_item.path , bk_item.display_name)
182            } )
183            [part, new_bk_items]
184        } )
185       
186        return new_mgr
187    }
188
189    restore_info_to_globalx_with_items(info, items){
190        let need_to_save = true
191
192        this.copy_object( Globalx, info, items )
193
194        if( info.bookmark_mgr !== undefined ){
195            info.bookmark_mgr = this.convert_to_bookmark_mgr_from_plain_object(info.bookmark_mgr)
196       
197            if( info.parts !== undefined ){
198                info.parts.map( (part) => {
199                    this.restore_globalx_from_info_part(info , part)
200                } )
201                need_to_save = false
202            }
203        }
204        else{
205            Globalx.bookmark_mgr = null
206        }
207        return need_to_save
208    }
209
210    restore_globalx_from_info_part(info , part){
211        this.copy_obj_with_part(Globalx, info, part)
212    }
213
214    reset_info() {
215        reset_localstorage('info')
216    }
217   
218    reset_localstorage(item_name){
219        delete this._globalStorage[item_name]
220        localStorage.removeItem(item_name)
221    }
222
223    clear_localstorage() {
224        let keys = Object.keys(this._globalStorage)
225        keys.map( (v) => { delete this._globalStorage[v] } )
226       
227        localStorage.clear()
228    }
229
230    show_localstorage() {
231        window.open('local_storage.html', '_blank')
232    }
233
234    show_globalx() {
235        localStorage.setItem('globalx' , _globalStorage[0] )
236    }
237}
238
Note: See TracBrowser for help on using the repository browser.