| [29] | 1 | class Storagex { |
|---|
| 2 | constructor () { |
|---|
| 3 | this._globalStorage = {} |
|---|
| 4 | this._items = ["num", "index", "parts"] |
|---|
| 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[part].bookmark_mgr = this.convert_to_plain_object_from_bookmark_mgr(Globalx[part].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( obj , num , part ){ |
|---|
| 47 | const key = `${num}/${part}` |
|---|
| 48 | this._globalStorage[key] = obj |
|---|
| 49 | localStorage.setItem(key , JSON.stringify(this._globalStorage[key]) ) |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | convert_to_array_from_bookmark_mgr(mgr){ |
|---|
| 53 | return { |
|---|
| 54 | paths: mgr.get_paths(), |
|---|
| 55 | items: mgr_to_array(mgr), |
|---|
| 56 | max_display_name_length: mgr.max_display_name_length() |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | convert_to_plain_object_from_bookmark_mgr(mgr){ |
|---|
| 61 | const paths = mgr.get_paths() |
|---|
| 62 | const new_bk_items = paths.map( ( path ) => { |
|---|
| 63 | let ret = { |
|---|
| 64 | path: "", |
|---|
| 65 | display_name: "" |
|---|
| 66 | } |
|---|
| 67 | item = mgr.get(path) |
|---|
| 68 | if( item != null ){ |
|---|
| 69 | ret.path = path |
|---|
| 70 | ret.display_name = item.display_name |
|---|
| 71 | } |
|---|
| 72 | return ret |
|---|
| 73 | } ) |
|---|
| 74 | |
|---|
| 75 | return { |
|---|
| 76 | max_display_name_length: mgr.max_display_name_length, |
|---|
| 77 | items: new_bk_items |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | copy_obj_with_part(to_obj, from_obj, part){ |
|---|
| 82 | const items = ["editor_id", "textarea_sel" , "menu_id", "bookmark_id", "bookmark_op_id", "item_name", "bookmarks", "key_sel", "download_sel", "menu_sel", "bookmark_sel", "bookmark_op_sel", "bookmark_mgr" ] |
|---|
| 83 | const init_value_items = ["editor", "key_indicate_file", "key"] |
|---|
| 84 | |
|---|
| 85 | if( to_obj[part] === undefined ){ |
|---|
| 86 | to_obj[part] = {} |
|---|
| 87 | } |
|---|
| 88 | this.copy_object( to_obj[part], from_obj[part], items ) |
|---|
| 89 | this.copy_object( to_obj[part], GlobalxInitValue, init_value_items ) |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | copy_object( to_obj, from_obj, items ){ |
|---|
| 93 | let x = null |
|---|
| 94 | items.map( (item) => { |
|---|
| 95 | x = from_obj[item] |
|---|
| 96 | to_obj[item] = x |
|---|
| 97 | } ) |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | restore_info(){ |
|---|
| 101 | const item_name = "info" |
|---|
| 102 | return this.restore_from_localstorage(item_name) |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | restore_from_localstorage(num, part= null){ |
|---|
| 106 | let key = null |
|---|
| 107 | let ret = null |
|---|
| 108 | if( part === null ){ |
|---|
| 109 | key = num |
|---|
| 110 | } |
|---|
| 111 | else{ |
|---|
| 112 | key = `${num}/${part}` |
|---|
| 113 | } |
|---|
| 114 | const str = localStorage.getItem(key) |
|---|
| 115 | if( str ){ |
|---|
| 116 | ret = this._globalStorage[key] = JSON.parse(str) |
|---|
| 117 | // console.log("rfl 1 key=" + key + " str=" + str) |
|---|
| 118 | } |
|---|
| 119 | else{ |
|---|
| 120 | if( this._globalStorage[key] !== undefined ){ |
|---|
| 121 | delete this._globalStorage[key] |
|---|
| 122 | } |
|---|
| 123 | ret = this._globalStorage[key] |
|---|
| 124 | console.log("rfl 2 key=" + key + " undefined") |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | return ret |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | restore_globalx_from_info(){ |
|---|
| 131 | let need_to_save = false |
|---|
| 132 | const info = this.restore_info() |
|---|
| 133 | if( info !== undefined && info !== null ){ |
|---|
| 134 | this.restore_info_to_globalx_with_items(info, this._items) |
|---|
| 135 | } |
|---|
| 136 | else{ |
|---|
| 137 | need_to_save = true |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | return need_to_save |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | convert_to_bookmark_mgr_from_plain_object(mgr) { |
|---|
| 144 | let new_mgr = new BookmarkMgr( mgr.max_display_name_length ) |
|---|
| 145 | |
|---|
| 146 | const bk_items = mgr.items |
|---|
| 147 | bk_items.map( (bk_item) => { |
|---|
| 148 | return new_mgr.add( bk_item.path , bk_item.display_name) |
|---|
| 149 | }) |
|---|
| 150 | return new_mgr |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | restore_info_to_globalx_with_items(info, items){ |
|---|
| 154 | this.copy_object( Globalx, info, items ) |
|---|
| 155 | |
|---|
| 156 | info.parts.map( (part) => { |
|---|
| 157 | this.restore_globalx_from_info_part(info , part) |
|---|
| 158 | } ) |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | restore_globalx_from_info_part(info , part){ |
|---|
| 162 | info[part].bookmark_mgr = this.convert_to_bookmark_mgr_from_plain_object(info[part].bookmark_mgr) |
|---|
| 163 | this.copy_obj_with_part(Globalx, info, part) |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | get_filelist(num, part) { |
|---|
| 167 | return this.restore_from_localstorage(num, part) |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | reset_info = () => { |
|---|
| 173 | let item_name = 'info' |
|---|
| 174 | reset_localstorage(item_name) |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | reset_localstorage(item_name){ |
|---|
| 178 | delete this._globalStorage[item_name] |
|---|
| 179 | localStorage.removeItem(item_name) |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | clear_localstorage = () => { |
|---|
| 183 | let keys = Object.keys(this._globalStorage) |
|---|
| 184 | keys.map( (v) => { delete this._globalStorage[v] } ) |
|---|
| 185 | |
|---|
| 186 | localStorage.clear() |
|---|
| 187 | } |
|---|
| 188 | show_localstorage() { |
|---|
| 189 | window.open('local_storage.html', '_blank') |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | show_globalx() { |
|---|
| 193 | localStorage.setItem('globalx' , _globalStorage[0] ) |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | |
|---|