source: branches/testa-single-bookmark/js/sidemenu.js @ 59

Last change on this file since 59 was 41, checked in by anonymous, 6 years ago

add bin

File size: 5.1 KB
RevLine 
[29]1class SideMenu {
2    constructor (num , part){
3        this.part = part
4        this.num = num
5        this.menu_sel = Globalx[part].menu_sel
6        this.menu_id = Globalx[part].menu_id
7        this.textarea_sel = Globalx[part].textarea_sel
8        this.bookmark_op_sel = Globalx[part].bookmark_op_sel
9    }
10    set_editor( editor ){
11        this.editor = editor /* Globalx[part].editor */
12    }
13
[41]14    display_menu( text ) {
15        let str = text
16        let obj
17        if( str.match( /{.*}/ ) === null ){
18            console.log("sidemenu 1 num="+ this.num + " part="+ this.part +" str="+str)
19            obj = {}
20        }
21        else {
22            console.log("sidemenu 2 num="+ this.num +" part="+ this.part +" str="+str)
23            obj = JSON.parse( str )
24        }
25        Globalx.storagex.change_cache( obj, this.num , this.part )
26
27        this.setup_menu(obj)
28    }
29
30    setup_menu(obj){
31        this.make_menu_item_list( obj , Globalx[this.part].key)
32        if( Globalx[this.part].menu === null ){
33            Globalx[this.part].menu = $( this.menu_sel ).menu({
[29]34                classes: {
35                    "ui-menu": "highlight"
36                }
37            })
[41]38        }
[29]39    }
40
[41]41    menu_init( top_sel ){
42        $( top_sel ).empty()
43        let content = `<ul id="${this.menu_id}"></ul>`
44        $( top_sel ).append( content )
45    }
46
47    async setup( top_sel ){
48        this.menu_init( top_sel )
49        this.get_filelist_async( this.num , this.part , (text) => {
50            this.display_menu(text)
51        } )
52    }
53
54    async update( top_sel ){
55        this.menu_init( top_sel )
56        await Globalx.remotex.update_filelist( this.num, this.part, (text) => {
57            this.display_menu(text)
58        } )
59    }
60
[29]61    async get_filelist_async( num , part , func){
62        let realData = Globalx.storagex.get_filelist( num , part )
63
64        if( realData === undefined || realData === null ){
65            await Globalx.remotex.get_filelist_from_remote( num, part, func )
66        }
67        else{
68            const obj = Globalx.storagex.get_filelist( num , part )
[41]69            this.setup_menu( obj )
[29]70        }
71    }
72
73    menu_action( item_name , up_flag = false ) {
74        let part = this.part
75        let jsondata = Globalx.storagex.get_filelist( Globalx.num , part)
76
77        Globalx[part].key_indicate_file = false;
78        if( up_flag ){
79            this.set_globalx_item_name(part, "")
80            Globalx[part].editor.getSession().setValue( "" );
81            $( this.textarea_sel ).val( "" );
82            if( Globalx[part].key != '/' ){
83                let array = Globalx[part].key.split("/")
84                array.pop()
85                if( array.length > 1 ){
86                    Globalx[part].key_type = false;
87                    this.set_globalx_item_name( part, array[array.length - 1] )
88
89                    Globalx[part].key = array.join('/')
90                    $( Globalx[part].menu_sel ).empty()
91                }
92                else {
93                    Globalx[part].key = '/'
94                    this.set_globalx_item_name( part, "" )
95                }
96            }
97        }
98        else{
99            if( Globalx[part].key == '/' ){
100                Globalx[part].key = Globalx[part].key + item_name
101            }
102            else{
103                Globalx[part].key = Globalx[part].key + '/' + item_name
104            }
105        }
106        this.set_globalx_item_name(part, item_name)
107
108        const bookmark_cb = $( this.bookmark_op_sel )
109        if( Globalx[part].bookmarks.indexOf( Globalx[part].key ) >= 0 ){
110            bookmark_cb.prop('checked', true)
111        }
112        else{
113            bookmark_cb.prop('checked', false)
114        }
115
116        $( Globalx[part].key_sel ).val( Globalx[part].key )
117
118        $( this.menu_sel ).empty()
119
120        this.make_menu_item_list(jsondata, Globalx[part].key)
121
122        if( jsondata !== undefined && jsondata[Globalx[part].key] !== undefined ){
123            if( jsondata[Globalx[part].key].length == 0 ){
124                Globalx[part].key_indicate_file = true;
[41]125console.log("Globalx[" + part + "].key_indicate_file = " + Globalx[part].key_indicate_file)
[36]126                let url = ""
127                $( Globalx[part].download_url_sel ).val( Globalx[part].key )
128                $( Globalx[part].download_url_sel ).attr('href' , url )
129                $( Globalx[part].download_partial_url_sel ).val( Globalx[part].key + "(parial)" )
130                url = url + ".tmp"
131                $( Globalx[part].download_partial_url_sel ).attr('href' , url )
[29]132                Globalx.remotex.get_content( this.num, this.part, Globalx[part].key , (content) => {
133                    Globalx[part].editor.getSession().setValue( content );
134                    $( Globalx[part].textarea_sel ).val( content );
135                });
136            }
137        }
138    }
139
140    make_menu_item_list( realData , key){
141        let content = null
[36]142        let item = null
[29]143        let ary = realData[ key ]
144       
145        let name = '..(Up)'
[36]146        content = `<li class="ui-menu-item"><div class="ui-menu-item-wrapper hasmenu">${name}</div></li>`;
[29]147        $( this.menu_sel ).append(content);
[41]148        item = $( this.menu_sel ).children().last()
[36]149        item.on('click' , () => {
150            this.menu_action( key , true )
151        } )
[29]152
153        if( ary !== undefined ){
154            ary.map( ( item_name ) => {
[36]155                content = `<li class="ui-menu-item"><div class="ui-menu-item-wrapper hasmenu">${item_name}</div></li>`;
[29]156                $( this.menu_sel ).append(content);
[41]157                item = $( this.menu_sel ).children().last()
[36]158                item.on('click' , () => {
159                    this.menu_action( item_name )
160                } )
[29]161            })
162        }
163    }
164
[41]165    set_globalx_item_name( part , item_name ) {
166        Globalx[part].item_name = item_name
167        let dl = $( Globalx[ part ].download_sel )
168        dl.attr('download' , Globalx[part].item_name)
[29]169        dl.removeAttr('href')
170    }
171
172    contextmenux0( menu_sel ) {
173        $( menu_sel ).contextmenu({
174            delegate: ".hasmenu",
175            menu: [
176                {title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy"},
177                {title: "----"},
178                {title: "More", childern: [
179                    {title: "Sub 1", cmd: "sub1"},
180                    {title: "Sub 2", cmd: "sub1"}
181                ]}
182            ],
183            select: (event, ui) => {
184                alert("select " + ui.cmd + " on " + ui.target.text());
185            }
186        });
187    }
188}
[36]189
Note: See TracBrowser for help on using the repository browser.