|
<script> OSCEUserSpaces = { storeKey: "osce-user-spaces" , rootNode: "#main-content div:nth-child(1) div:nth-child(1)" , webServiceUrl : "https://communities.osce.org/rest/api/space?expand=icon" , init() { const cached = localStorage.getItem( this.storeKey ); cached && this.draw( JSON.parse( cached )); this.read(); } , async read() { let response = await fetch(this.webServiceUrl); let data = await response.text(); if( data ) { try{ this.draw( JSON.parse( data ) ); localStorage.setItem( this.storeKey, data ); } catch(e) { console.warn(e) } } } , draw( result ) { const spaces = result.results.filter( space => ( space.type == 'global' && ! ["WTOC","OBWS","CTHBSOURCE"].includes( space.key ) ) ); const root = document.querySelector( this.rootNode ); const main = document.createElement( "div"); const oldNode = document.getElementById( this.storeKey); oldNode && oldNode.remove(); main.id = this.storeKey; html = ` <section spaces> <main spaces> <div header>Your Communities</div> `; if( spaces.length > 0) { spaces.forEach( space => { html += ` <a href="https://communities.osce.org/display/${space.key}" class="grow-on-hover"> <div space> <img src="https://communities.osce.org${space.icon.path}" /> <div name>${space.name}</div> </div> </a> `; }); } else { html += ` <a href="https://communities.osce.org/login.action" class="grow-on-hover"> <div space> <div filler></div> <div name>Login to access your communities</div> </div> </a> `; } html += ` </main> </section> `; main.innerHTML = html; root.appendChild( main ); } } OSCEUserSpaces.init(); </script> |