2 # Copyright Simon Lydell 2015.
4 # This file is part of VimFx.
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 translate = require('./l10n')
21 utils = require('./utils')
23 CONTAINER_ID = 'VimFxHelpDialogContainer'
26 injectHelp = (rootWindow, vimfx) ->
27 removeHelp(rootWindow)
29 { document } = rootWindow
31 container = document.createElement('box')
32 container.id = CONTAINER_ID
34 wrapper = document.createElement('box')
35 container.appendChild(wrapper)
37 header = createHeader(document, vimfx)
38 wrapper.appendChild(header)
40 content = createContent(document, vimfx)
41 wrapper.appendChild(content)
43 rootWindow.gBrowser.mCurrentBrowser.parentNode.appendChild(container)
45 # The font size of menu items is used by default, which is usually quite
46 # small. Try to increase it without causing a scrollbar.
47 computedStyle = rootWindow.getComputedStyle(container)
48 fontSize = originalFontSize =
49 parseFloat(computedStyle.getPropertyValue('font-size'))
50 while wrapper.clientHeight < container.clientHeight and
51 fontSize <= MAX_FONT_SIZE
53 container.style.fontSize = "#{ fontSize }px"
54 container.style.fontSize = "#{ Math.max(fontSize - 1, originalFontSize) }px"
56 # Uncomment this line if you want to use `gulp help.html`!
57 # utils.writeToClipboard(container.outerHTML)
59 removeHelp = (rootWindow) ->
60 rootWindow.document.getElementById(CONTAINER_ID)?.remove()
62 createHeader = (document, vimfx) ->
63 $ = utils.createBox.bind(null, document)
67 mainHeading = $('heading-main', header)
68 $('logo', mainHeading) # Content is added by CSS.
69 $('title', mainHeading, translate('help.title'))
71 closeButton = $('close-button', header, '×')
72 closeButton.onclick = removeHelp.bind(null, document.defaultView)
76 createContent = (document, vimfx) ->
77 $ = utils.createBox.bind(null, document)
79 content = $('content')
81 for mode in vimfx.getGroupedCommands({enabledOnly: true})
82 modeHeading = $('heading-mode', null, mode.name)
84 for category, index in mode.categories
85 categoryContainer = $('category', content)
87 # Append the mode heading inside the first category container, rather than
88 # before it, for layout purposes.
90 categoryContainer.appendChild(modeHeading)
91 categoryContainer.classList.add('first')
93 $('heading-category', categoryContainer, category.name) if category.name
95 for { command, enabledSequences } in category.commands
96 commandContainer = $('command', categoryContainer)
97 for sequence in enabledSequences
98 $('key-sequence', commandContainer, sequence)
99 $('description', commandContainer, command.description())