Wiki navigation bar
This commit is contained in:
		
							
								
								
									
										37
									
								
								js/WikiDropdownHelper.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								js/WikiDropdownHelper.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,37 @@
 | 
			
		||||
'use strict';
 | 
			
		||||
 | 
			
		||||
class WikiDropdownHelper {
 | 
			
		||||
    constructor(element) {
 | 
			
		||||
        this.dd = element;
 | 
			
		||||
    }
 | 
			
		||||
    clear() {
 | 
			
		||||
        while (this.dd.hasChildNodes()) {
 | 
			
		||||
            this.dd.removeChild(this.dd.firstChild);
 | 
			
		||||
        }
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    add(text, value, set=false) {
 | 
			
		||||
        var option = document.createElement("option");
 | 
			
		||||
        option.text = text;
 | 
			
		||||
        option.value = value;
 | 
			
		||||
        this.dd.appendChild(option);
 | 
			
		||||
        if ( set ) {
 | 
			
		||||
            this.set(value);
 | 
			
		||||
        }
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    rename(value, text) {
 | 
			
		||||
        this.dd.querySelector(`option[value="${value}"]`).innerHTML=text;
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    get() {
 | 
			
		||||
        return {
 | 
			
		||||
                 text:this.dd.parentElement.value,
 | 
			
		||||
                 value:this.dd.parentElement.text
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
    set(value) {
 | 
			
		||||
        this.dd.parentElement.value=value;
 | 
			
		||||
        // this.onSelectWiki(value);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										123
									
								
								js/WikiNavigation.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										123
									
								
								js/WikiNavigation.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,123 @@
 | 
			
		||||
'use strict';
 | 
			
		||||
 | 
			
		||||
class WikiNavigation {
 | 
			
		||||
    dd = null;
 | 
			
		||||
    onSelectWiki = null;
 | 
			
		||||
 | 
			
		||||
    constructor(container, onSelectWiki){
 | 
			
		||||
        let self = this;
 | 
			
		||||
        this.container = container;
 | 
			
		||||
        this.onSelectWiki = onSelectWiki;
 | 
			
		||||
 | 
			
		||||
        let wikiSelector = container.getElementsByTagName('select')[0];
 | 
			
		||||
        this.dd = new WikiDropdownHelper(wikiSelector);
 | 
			
		||||
        wikiSelector.addEventListener('change', e=>{
 | 
			
		||||
            if(self.onSelectWiki) {
 | 
			
		||||
                self.onSelectWiki(+e.target.value||0);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        this.loadWikis();
 | 
			
		||||
 | 
			
		||||
        // Popup menu
 | 
			
		||||
        let appNavigationMenu = container.getElementsByClassName('app-navigation-entry-menu')[0];
 | 
			
		||||
        let button = container.querySelector('.app-navigation-entry-utils-menu-button button');
 | 
			
		||||
        button.addEventListener('click', ()=>appNavigationMenu.classList.toggle("open") );
 | 
			
		||||
        document.addEventListener('click', e=>{if(e.target!==button)appNavigationMenu.classList.remove("open");})
 | 
			
		||||
 | 
			
		||||
        appNavigationMenu.querySelector('[data-id="add"]').addEventListener('click', ()=>self.wikiChooseFolder() );
 | 
			
		||||
        appNavigationMenu.querySelector('[data-id="rename"]').addEventListener('click', ()=>self.wikiRename() );
 | 
			
		||||
        appNavigationMenu.querySelector('[data-id="delete"]').addEventListener('click', ()=>self.wikiDelete() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wikiRename() {
 | 
			
		||||
        let self=this;
 | 
			
		||||
        OC.dialogs.prompt(
 | 
			
		||||
                            t(appName, 'This allow you to rename the displayed name for the selected wiki. (The folder will remain unchanged)'),
 | 
			
		||||
                            t(appName, 'Rename Wiki'),
 | 
			
		||||
                            (ok,value)=>{
 | 
			
		||||
                                if(ok) {
 | 
			
		||||
                                    value = value.trim();
 | 
			
		||||
                                    if(value!='') {
 | 
			
		||||
                                        let wiki = self.dd.get();
 | 
			
		||||
                                        var baseUrl = OC.generateUrl('/apps/mywiki/wikis');
 | 
			
		||||
                                        $.ajax({
 | 
			
		||||
                                            url: baseUrl,
 | 
			
		||||
                                            type: 'PUT',
 | 
			
		||||
                                            contentType: 'application/json',
 | 
			
		||||
                                            data: JSON.stringify({id:wiki.value, title:value})
 | 
			
		||||
                                        }).done(function (response) {
 | 
			
		||||
                                            console.info('JDG :: Wiki renamed', response);
 | 
			
		||||
                                            // ToDo :: Rename in the dropdown
 | 
			
		||||
                                        }).fail(function (response, code) {
 | 
			
		||||
                                            OC.dialogs.alert('Error', t(appName,'Error renaming wiki'));
 | 
			
		||||
                                            console.error('JDG :: Error renaming wiki', response);
 | 
			
		||||
                                        });                                        
 | 
			
		||||
                                    }
 | 
			
		||||
                                }
 | 
			
		||||
                            },
 | 
			
		||||
                            false,
 | 
			
		||||
                            t(appName, 'New name:'),
 | 
			
		||||
                            false
 | 
			
		||||
                        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    loadWikis() {
 | 
			
		||||
        let self = this;
 | 
			
		||||
 | 
			
		||||
        this.dd.clear().add(t(appName, 'Loading...'), '', true);
 | 
			
		||||
 | 
			
		||||
        var baseUrl = OC.generateUrl('/apps/mywiki/wikis');
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            url: baseUrl,
 | 
			
		||||
            type: 'GET',
 | 
			
		||||
            contentType: 'application/json'
 | 
			
		||||
        }).done(function (response) {
 | 
			
		||||
            console.info('JDG :: Wikis loaded', response);
 | 
			
		||||
            self.dd.clear().add('','');
 | 
			
		||||
            response.forEach( x=>self.dd.add(x.title, x.id) );
 | 
			
		||||
        }).fail(function (response, code) {
 | 
			
		||||
            OC.dialogs.alert('Error', t(appName,'Error getting the list of wikis'));
 | 
			
		||||
            console.error('JDG :: Error getting the wikis', response);
 | 
			
		||||
            self.dd.clear();
 | 
			
		||||
        });
 | 
			
		||||
    }    
 | 
			
		||||
    
 | 
			
		||||
    wikiChooseFolder() {
 | 
			
		||||
        let self = this;
 | 
			
		||||
        this.dd.set('');
 | 
			
		||||
        window.OC.dialogs.filepicker(
 | 
			
		||||
                                        t(appName, 'Select Wiki folder'), 
 | 
			
		||||
                                        (path, type) => {
 | 
			
		||||
                                                            if (type === OC.dialogs.FILEPICKER_TYPE_CHOOSE) {
 | 
			
		||||
                                                                self.wikiAdd(path, path.split('/').pop());
 | 
			
		||||
                                                            }
 | 
			
		||||
                                                        }, 
 | 
			
		||||
                                        false, 
 | 
			
		||||
                                        ['httpd/unix-directory'], 
 | 
			
		||||
                                        true, 
 | 
			
		||||
                                        OC.dialogs.FILEPICKER_TYPE_CHOOSE, 
 | 
			
		||||
                                        '', // Path
 | 
			
		||||
                                        { allowDirectoryChooser: true }
 | 
			
		||||
                                    );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wikiAdd(folderPath, title) {
 | 
			
		||||
        let self = this;
 | 
			
		||||
 | 
			
		||||
        var baseUrl = OC.generateUrl('/apps/mywiki/wikis');
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            url: baseUrl,
 | 
			
		||||
            type: 'POST',
 | 
			
		||||
            data: JSON.stringify({title:title, folderPath:folderPath}),
 | 
			
		||||
            contentType: 'application/json'
 | 
			
		||||
        }).done(function (response) {
 | 
			
		||||
            console.info('JDG :: wikiAdd :: Wiki added', response);
 | 
			
		||||
            if ( response.id>0 ) {
 | 
			
		||||
                self.dd.add(response.title, response.id, true);
 | 
			
		||||
            }
 | 
			
		||||
        }).fail(function (response, code) {
 | 
			
		||||
            OC.dialogs.alert('Error', t(appName,'It has not been possible to add the new wiki'));
 | 
			
		||||
            console.error('JDG :: wikiAdd :: Error adding the wiki', response);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user