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

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

set_anchor_url

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