// The CoqLayoutClassic class. // Copyright (C) 2015-2017 Mines ParisTech/ARMINES // // This class provides a plugabble side panel with proof and query // buffers. import $ from 'jquery'; import { SettingsPanel } from './settings.js'; // Bootstrap import 'bootstrap/dist/css/bootstrap.min.css'; import 'bootstrap/dist/js/bootstrap.bundle.min.js'; // Medias import "../css/landing-page.css"; import "../css/kbd.css"; import '../css/coq-log.css'; import '../css/coq-base.css'; import '../css/coq-light.css'; import '../css/coq-dark.css'; import '../css/settings.css'; /***********************************************************************/ /* The CoqLayout class contains the goal, query, and packages buffer */ /***********************************************************************/ /** * Classical layout: a right panel containing a toolbar with buttons at the * top, and a main area divided vertically into three collapsible panes. * Also shows a power button that hides or shows the panel. * * @class CoqLayoutClassic */ export class CoqLayoutClassic { options : any; ide : HTMLElement; panel : HTMLDivElement; proof : HTMLDivElement; query : HTMLDivElement; packages : HTMLDivElement; buttons : HTMLSpanElement; menubtn : SVGElement settings : SettingsPanel; onToggle : (evt : any) => void; onAction : (evt : any) => void; log_levels : string[]; log_level : number; outline : HTMLDivElement; scrollTimeout? : any; // timeout html(params) { var {base_path, backend, kb} = params; return `
+
Goals
Help
Messages
`; } /** * Initializes the UI layout. * @param {object} options jsCoq options; used keys are: * - wrapper_id: element id of the IDE container * - base_path: URL for the root directory of jsCoq * - theme: jsCoq theme to use for the panel ('light' or 'dark') * @param {object} params HTML template parameters; used keys are: * - kb: key-binding tooltips for actions {up, down, cursor} */ constructor(options, params) { this.options = options; // Our reference to the IDE, goal display & query buffer. this.ide = document.getElementById(options.wrapper_id); this.ide.classList.add('jscoq-ide', `layout-${options.layout || 'flex'}`); this.panel = document.createElement('div'); this.panel.id = 'panel-wrapper'; this.panel.innerHTML = this.html({base_path: options.base_path, backend: this.options.backend, ...params}); this.ide.appendChild(this.panel); // UI setup. this.proof = this.panel.querySelector('#goal-text'); this.query = this.panel.querySelector('#query-panel'); this.packages = this.panel.querySelector('#packages-panel'); this.buttons = this.panel.querySelector('#buttons'); this.menubtn = this.panel.querySelector('.app-menu-button'); this.settings = new SettingsPanel(); var flex_container = this.panel.getElementsByClassName('flex-container')[0]; flex_container.addEventListener('click', evt => { this.panelClickHandler(evt); }); this.onToggle = evt => {}; this.panel.querySelector('#hide-panel') .addEventListener('click', evt => this.toggle() ); this._setButtons(false); // starts disabled this.onAction = evt => {}; this.buttons.addEventListener('click', evt => this.onAction(evt)); this.menubtn.addEventListener('mousedown', () => this.settings.toggle()); this.settings.active.observe(active => this.menubtn.classList.toggle('active', active)); this.panel.querySelector('#help-panel button.btn-close') .addEventListener('click', () => this.hideHelp()); // Configure log this.log_levels = ['Error', 'Warning', 'Notice', 'Info', 'Debug'] let optionList : JQuery = $(this.panel).find('select[name=msg_filter]') as JQuery; optionList.on('change', ev => this.filterLog(parseInt(ev.target.value))); this.filterLog(3); // Info this.configure(options); this._setupSettings(); this._preloadImages(); } /** * Configure or re-configure UI based on CoqManager options. * @param {object} options */ configure(options) { if (options.theme) { this.panel.classList.remove(...[...this.panel.classList] .filter(c => c.startsWith('jscoq-theme-'))); this.panel.classList.add(`jscoq-theme-${options.theme}`); // - configure help which is in an iframe let ipanel : HTMLIFrameElement = this.panel.querySelector('#help-panel iframe'); ipanel.contentDocument.body.setAttribute('theme', options.theme); } this.settings.configure({ theme: options.theme, company: options.editor && options.editor.mode && options.editor.mode['company-coq'] }); } show() { this.ide.classList.add('goals-active'); this.ide.classList.remove('toggled'); this.onToggle({target: this, shown: true}); } hide() { this.ide.classList.remove('goals-active'); this.ide.classList.add('toggled'); this.onToggle({target: this, shown: false}); } isVisible() { return !this.ide.classList.contains('toggled'); } toggle() { this.isVisible() ? this.hide() : this.show(); } showHelp() { this.panel.classList.add('show-help'); this.panel.querySelector('#help-panel').classList.remove('collapsed'); } hideHelp() { this.panel.classList.remove('show-help'); } toggleHelp() { this.panel.classList.toggle('show-help'); if (this.panel.classList.contains('show-help')) this.panel.querySelector('#help-panel').classList.remove('collapsed'); } splash(version_info, msg, mode='wait') { var above = $(this.proof).find('.splash-above'), image = $(this.proof).find('.splash-image'), below = $(this.proof).find('.splash-below'); var overlay = `${this.options.base_path}/frontend/classic/images/${mode}.gif`; if (!(above.length && image.length && below.length)) { $(this.proof).empty().append( above = $('

').addClass('splash-above'), $('

').addClass('splash-middle').append( image = $('
').append($('')) ), below = $('

').addClass('splash-below') ) } if (version_info) above.text(version_info); if (msg) below.text(msg); image[0].className = ""; image.addClass(['splash-image', mode]); var img = image.find('img'); if (img.attr('src') !== overlay) img.attr('src', overlay); if (mode !== 'wait') this.splashLinks(); } splashLinks() { var bar = $(`

`); // Set icons let icon = fn => `${this.options.base_path}/frontend/classic/images/${fn}`; bar.find('a[href="#quick-help"] img').attr('src', icon('help.svg')).height("1em"); bar.find('a[href="#scratchpad"] img').attr('src', icon('scratchpad.png')).height("1em"); // Set quick-help action bar.find('a[href="#quick-help"]').on('click', ev => { ev.preventDefault(); this.showHelp(); }); // Set scratchpad action bar.find('a[href="#scratchpad"]').attr('href', this.options.links?.scratchpad ?? `${this.options.base_path}/examples/scratchpad.html`); // Ship it bar.prependTo($(this.proof).find('.splash-below')); } createOutline() { var outline_pane : JQuery = $('
').attr('id', 'outline-pane') as JQuery; $(this.ide).prepend(outline_pane); requestAnimationFrame(() => $(this.ide).addClass('outline-active')); return this.outline = outline_pane[0]; } /** * Shows a notice in the main goal pane (reserved for important messages, * such as during startup). * @param {string} msg message text */ systemNotification(msg) { $(this.proof).append($('

').addClass('system').text(msg)); } _setButtons(enabled) { if(enabled) { $(this.buttons).find('button').removeAttr('disabled'); this.buttons.classList.remove('disabled') } else { $(this.buttons).find('button').attr('disabled'); this.buttons.classList.add('disabled'); } } toolbarOn() { // Enable the button actions and show them. this._setButtons(true); this.ide.classList.remove('on-hold'); } toolbarOff() { // Disable the button actions and dim them. this._setButtons(false); this.ide.classList.add('on-hold'); } // This is still not optimal. update_goals(content) { // TODO: Add diff/history of goals. // XXX: should send a message. $(this.proof).html(content); } // Add a log event received from Coq. log(text, level, attrs : any = {}) { // Levels are taken from Coq itself: // | Debug | Info | Notice | Warning | Error var item = $('

').addClass(level).html(text).attr(attrs), prev = $(this.query).children(':visible').last(); if (attrs['data-coq-sid'] !== undefined) this.logSep(attrs['data-coq-sid'], item, prev); $(this.query).append(item); if (this.isLogVisible(level)) { if (this.scrollTimeout) clearTimeout(this.scrollTimeout); if (attrs.fast) this.query.scrollTo({top: this.query.scrollHeight}); else this.scrollTimeout = setTimeout( () => { this.query.scrollTo({top: this.query.scrollHeight, behavior: 'smooth'}); }, 1 ); } return item; } logSep(sid, item, prev) { if (sid === prev.attr('data-coq-sid')) { prev.removeClass('coq-sid-end'); item.addClass('coq-sid-end'); } else { item.addClass(['coq-sid-start', 'coq-sid-end'].concat( prev.hasClass('coq-sid-end') ? ['coq-prev-end'] : [])); } } /** * Readjusts separators for the entire log when the level changes. * (called from filterLog) */ logSepReadjust() { for (let item of $(this.query).find('.coq-sid-start')) { // XXX not very efficient :( if ($(item).prevUntil(':visible').prev('.coq-sid-end:visible').length) $(item).addClass('coq-prev-end'); else $(item).removeClass('coq-prev-end'); } } filterLog(level_select) { var i; if (typeof level_select == 'string') level_select = this.log_levels.indexOf(level_select); console.log('setting log level', level_select); for(i = 0 ; i <= level_select ; i++) this.query.classList.add(`show-${this.log_levels[i]}`); for(i = level_select+1 ; i < this.log_levels.length ; i++) this.query.classList.remove(`show-${this.log_levels[i]}`); this.log_level = level_select; requestAnimationFrame(() => { this.query.scrollTo({top: this.query.scrollHeight}); // only reasonable thing to do this.logSepReadjust(); }); } isLogVisible(level) { if (typeof level == 'string') level = this.log_levels.indexOf(level); return level <= this.log_level; } panelClickHandler(evt) { var target = evt.target; if(target.classList.contains('caption') && target.parentNode.classList.contains('flex-panel')) { var panel = target.parentNode; if(panel.classList.contains('collapsed')) { panel.classList.remove('collapsed'); } else { var panels_cpt = this.panel.getElementsByClassName('flex-panel').length; var collapsed_panels_cpt = this.panel.getElementsByClassName('collapsed').length; if(collapsed_panels_cpt + 1 >= panels_cpt) // at least one panel opened return; panel.classList.add('collapsed'); } } } /** * Set up hooks for when user changes settings. */ _setupSettings() { this.settings.model.theme.observe(theme => { this.configure({theme}); }); this.settings.onAction = ev => { switch (ev.action) { case 'quick-help': this.showHelp(); break; } } } /** * Auxiliary function to improve UX by preloading images. */ _preloadImages() { var imgs_dir = `${this.options.base_path}/frontend/classic/images`, img_fns = ['jscoq-splash.png', 'egg.png']; for (let fn of img_fns) { new Image().src = `${imgs_dir}/${fn}`; } } } // Local Variables: // js-indent-level: 4 // End: