Ignore:
Timestamp:
Aug 21, 2019 7:44:47 PM (6 years ago)
Author:
anonymous
Message:

use localstorage to store/restore settings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/testa/js/storage.js

    r23 r25  
    11let _globalStorage = {}
     2
     3const _items = ["num", "index", "parts"]
     4
     5function storage_test(){
     6    console.log(_items);
     7}
     8
     9function save_info_from_globalx(){
     10    save_info_from_globalx_with_items(_items)
     11}
     12
     13function convert_to_array_from_bookmark_mgr(mgr){
     14        return {
     15            paths: mgr.get_paths(),
     16            items: mgr_to_array(mgr),
     17            max_display_name_length: mgr.max_display_name_length()
     18        }
     19}
     20
     21function save_info_from_globalx_with_items(items){
     22    let info = {}
     23
     24    copy_object( info, Globalx, items )
     25    /* 
     26        info.num = Globalx.num
     27        info.index = Globalx.index
     28        info.parts = Globalx.parts
     29    */
     30    Globalx.parts.map( function (part) {
     31        save_info_from_globalx_part(info , part)
     32    } )
     33
     34    save_info_to_local_storage(info)
     35}
     36function convert_to_plain_object_from_bookmark_mgr(mgr){
     37//    console.log( "mgr=" + mgr )
     38//    console.log( "mgr.items()=" + mgr.items )
     39    const paths = mgr.get_paths()
     40    const new_bk_items = paths.map( function( path ){
     41        let ret = {
     42            path: "",
     43            display_name: ""
     44        }
     45        item = mgr.get(path)
     46        if( item != null ){
     47            ret.path = path
     48            ret.display_name = item.display_name
     49        }
     50        return ret
     51    } )
     52
     53    return {
     54        max_display_name_length: mgr.max_display_name_length,
     55        items: new_bk_items
     56    }
     57}
     58
     59function save_info_from_globalx_part(info, part){
     60    info[part] = {}
     61    copy_obj_with_part(info, Globalx, part)
     62
     63    info[part].bookmark_mgr = convert_to_plain_object_from_bookmark_mgr(Globalx[part].bookmark_mgr)
     64}
     65
     66function copy_obj_with_part(to_obj, from_obj, part){
     67/*    const items = ["editor_id", "editor", "textarea_sel" , "menu_id", "bookmark_id", "bookmark_op_id", "item_name", "bookmarks", "bookmark_displayname_max_length", "key_indicate_file", "key", "key_sel", "download_sel", "menu_sel", "bookmark_sel", "bookmark_op_sel", "bookmark_mgr" ]*/
     68    const items = ["editor_id", "textarea_sel" , "menu_id", "bookmark_id", "bookmark_op_id", "item_name", "bookmarks", "bookmark_displayname_max_length", "key_sel", "download_sel", "menu_sel", "bookmark_sel", "bookmark_op_sel", "bookmark_mgr" ]
     69    const init_value_items = ["editor", "key_indicate_file", "key"]
     70
     71    if( to_obj[part] === undefined ){
     72        to_obj[part] = {}
     73    }
     74    copy_object( to_obj[part], from_obj[part], items )
     75    copy_object( to_obj[part], GlobalxInitValue, init_value_items )
     76}
     77
     78function copy_object( to_obj, from_obj, items ){
     79    let x = null
     80    items.map( function(item) {
     81        x = from_obj[item]
     82        to_obj[item] = x
     83    } )
     84}
     85
     86function restore_info_to_globalx(){
     87    const info = restore_info()
     88    if( info === undefined ){
     89        console.log("call init_globalx()")
     90        init_globalx()
     91    }
     92    else {
     93        console.log("call restore_info_to_globalx_with_items")
     94        restore_info_to_globalx_with_items(info, _items)
     95    }
     96}
     97
     98function convert_to_bookmark_mgr_from_plain_object(mgr) {
     99    let new_mgr = new BookmarkMgr( mgr.max_display_name_length )
     100   
     101    const bk_items = mgr.items
     102    bk_items.map( function(bk_item) {
     103        return new_mgr.add( bk_item.path , bk_item.display_name)
     104    })
     105    return new_mgr
     106}
     107
     108function restore_info_to_globalx_with_items(info, items){
     109   
     110    copy_object( Globalx, info, items )
     111
     112    info.parts.map( function (part) {
     113        restore_info_to_globalx_part(info , part)
     114    } )
     115}
     116
     117function restore_info_to_globalx_part(info , part){
     118    info[part].bookmark_mgr = convert_to_bookmark_mgr_from_plain_object(info[part].bookmark_mgr)
     119    copy_obj_with_part(Globalx, info, part)
     120}
    2121
    3122function save_info_to_local_storage(info){
     
    18137function get_filelist(num) {
    19138    let num_s = num + ''
    20     if( _globalStorage[num_s] === undefined ){
    21         oldData = localStorage.getItem(num_s)
     139    return restore_from_localstorage(num_s)
     140}
     141
     142
     143function restore_info(){
     144    const item_name = "info"
     145    return restore_from_localstorage(item_name)
     146}
     147
     148function restore_from_localstorage(item_name){
     149   if( _globalStorage[item_name] === undefined ){
     150        oldData = localStorage.getItem(item_name)
    22151        if( oldData ){
    23             _globalStorage[num_s] = JSON.parse(oldData)
     152            _globalStorage[item_name] = JSON.parse(oldData)
    24153        }
    25154    }
    26     return _globalStorage[num_s]
     155    return _globalStorage[item_name]
    27156}
    28157
     158function reset_info(){
     159    let item_name = 'info'
     160    reset_localstorage(item_name)
     161}
     162
     163function reset_localstorage(item_name){
     164    delete _globalStorage[item_name]
     165    localStorage.removeItem(item_name)
     166}
     167
     168function clear_localstorage(){
     169    let keys = Object.keys(_globalStorage)
     170    keys.map( (v) => { delete _globalStorage[v] } )
     171
     172    localStorage.clear()
     173}
Note: See TracChangeset for help on using the changeset viewer.