1// Copyright (C) 2018 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import * as m from 'mithril'; 16 17import {Actions} from '../common/actions'; 18 19import {onClickCopy} from './clipboard'; 20import {CookieConsent} from './cookie_consent'; 21import {globals} from './globals'; 22import {Sidebar} from './sidebar'; 23import {Topbar} from './topbar'; 24 25function renderPermalink(): m.Children { 26 const permalink = globals.state.permalink; 27 if (!permalink.requestId || !permalink.hash) return null; 28 const url = `${self.location.origin}/#!/?s=${permalink.hash}`; 29 const linkProps = {title: 'Click to copy the URL', onclick: onClickCopy(url)}; 30 31 return m('.alert-permalink', [ 32 m('div', 'Permalink: ', m(`a[href=${url}]`, linkProps, url)), 33 m('button', 34 { 35 onclick: () => globals.dispatch(Actions.clearPermalink({})), 36 }, 37 m('i.material-icons.disallow-selection', 'close')), 38 ]); 39} 40 41class Alerts implements m.ClassComponent { 42 view() { 43 return m('.alerts', renderPermalink()); 44 } 45} 46 47/** 48 * Wrap component with common UI elements (nav bar etc). 49 */ 50export function createPage(component: m.Component<PageAttrs>): 51 m.Component<PageAttrs> { 52 const pageComponent = { 53 view({attrs}: m.Vnode<PageAttrs>) { 54 const children = [ 55 m(Sidebar), 56 m(Topbar), 57 m(Alerts), 58 m(component, attrs), 59 m(CookieConsent), 60 ]; 61 if (globals.state.perfDebug) { 62 children.push(m('.perf-stats')); 63 } 64 return children; 65 }, 66 }; 67 68 return pageComponent; 69} 70 71export interface PageAttrs { 72 subpage?: string; 73} 74