class Storagex { constructor () { this._globalStorage = {} this._items = GlobalxItems } ensure_data_structure ( item , parts ) { parts.map( (part) => { if( item[part] === undefined ){ item[part] = {} } } ) } save_as_info_from_globalx() { this.save_as_info_from_globalx_with_items(this._items) } save_as_info_from_globalx_with_items(items){ let info = {} this.copy_object( info, Globalx, items ) Globalx.parts.map( (part) => { this.save_as_info_from_globalx_part(part , info) } ) this.save_as_info_to_local_storage(info) } save_as_info_to_local_storage(obj){ this.save_to_local_storage("info" , obj) } save_as_info_from_globalx_part(part , info){ info[part] = {} this.copy_obj_with_part(info, Globalx, part) info.bookmark_mgr = this.convert_to_plain_object_from_bookmark_mgr(Globalx.bookmark_mgr) } save_to_local_storage(key , obj) { this._globalStorage[key] = obj localStorage.setItem(key , JSON.stringify(obj) ) } change_cache_x( obj ){ this._globalStorage[key] = obj // localStorage.setItem(key , JSON.stringify(this._globalStorage[key]) ) } change_cache( obj , num , part ){ const key = `${num}/${part}` this._globalStorage[key] = obj localStorage.setItem(key , JSON.stringify(this._globalStorage[key]) ) } async convert_mgr_object_async( mgr, array ){ return await array.map( (path) => { const item = mgr.get(part , path) return { part: part, path: item.path, display_name: item.display_name } }) } convert_to_plain_object_from_bookmark_mgr(mgr){ let obj = {} obj.bms = {} Object.entries( mgr.paths ).map( ([part, array]) => { obj.bms[part] = this.convert_mgr_object_async(mgr, array) } ) obj.max_display_name_length = mgr.max_display_name_length obj.parts = mgr.parts return obj } convert_to_plain_object_from_bookmark_mgr0(mgr){ let obj = {} obj.bms = Object.fromEntries( Object.entries( mgr.paths ).map( ([part, array]) => { const bk_items = this.convert_mgr_object_async(mgr, array) ([part, bk_items]) } ) ) obj.max_display_name_length = mgr.max_display_name_length obj.parts = mgr.parts return obj } copy_obj_with_part(to_obj, from_obj, part){ const items = [ "editor_id", "textarea_sel" , "menu_id", "bookmark_id", "bookmark_op_id", "item_name", "bookmarks", "key", "key_sel", "download_sel", "download_url_sel", "download_partial_url_sel", "menu_sel" , "bookmark_mgr" , "bookmark_op_sel" ] if( to_obj[part] === undefined ){ to_obj[part] = {} } this.copy_object( to_obj[part], from_obj[part], items ) this.copy_object( to_obj[part], GlobalxInitValue, GlobalxInitValueItems ) } copy_object( to_obj, from_obj, items ){ let x = null items.map( (item) => { x = from_obj[item] to_obj[item] = x } ) } get_filelist(num, part) { return this.restore_from_localstorage(num, part) } restore_info(){ const item_name = "info" return this.restore_from_localstorage(item_name) } restore_from_localstorage(num, part= null){ let key = null let ret = null if( part === null ){ key = num } else{ key = `${num}/${part}` } const str = localStorage.getItem(key) if( str ){ ret = this._globalStorage[key] = JSON.parse(str) // console.log("### rfl 1 key=" + key + " str=" + str) } else{ if( this._globalStorage[key] !== undefined ){ delete this._globalStorage[key] } ret = this._globalStorage[key] // console.log("### rfl 2 key=" + key + " undefined") } return ret } restore_globalx_from_info(){ let need_to_save = false const info = this.restore_info() if( info !== undefined && info !== null ){ need_to_save = this.restore_info_to_globalx_with_items(info, this._items) } else{ need_to_save = true } return need_to_save } convert_to_bookmark_mgr_from_plain_object(mgr) { let new_mgr = new BookmarkMgr( mgr.max_display_name_length , mgr.parts) const bk_items = mgr.bms Object.entries( ([part, bk_items]) => { new_bk_items = bk_items.map( (bk_item) => { return new_mgr.add( bk_item.part , bk_item.path , bk_item.display_name) } ) [part, new_bk_items] } ) return new_mgr } restore_info_to_globalx_with_items(info, items){ let need_to_save = true this.copy_object( Globalx, info, items ) if( info.bookmark_mgr !== undefined ){ info.bookmark_mgr = this.convert_to_bookmark_mgr_from_plain_object(info.bookmark_mgr) if( info.parts !== undefined ){ info.parts.map( (part) => { this.restore_globalx_from_info_part(info , part) } ) need_to_save = false } } else{ Globalx.bookmark_mgr = null } return need_to_save } restore_globalx_from_info_part(info , part){ this.copy_obj_with_part(Globalx, info, part) } reset_info() { reset_localstorage('info') } reset_localstorage(item_name){ delete this._globalStorage[item_name] localStorage.removeItem(item_name) } clear_localstorage() { let keys = Object.keys(this._globalStorage) keys.map( (v) => { delete this._globalStorage[v] } ) localStorage.clear() } show_localstorage() { window.open('local_storage.html', '_blank') } show_globalx() { localStorage.setItem('globalx' , _globalStorage[0] ) } }