Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move JS build to Parcel #117

Merged
merged 5 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions _js/app/main.js

This file was deleted.

155 changes: 84 additions & 71 deletions _js/lib/Common.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,103 @@
define(['EditController', 'jquery'], (EditController, $) =>
function Common(index) {
const CLASS_OPEN = 'expanded'
const CLASS_CLOSED = 'collapsed'
import EditController from './EditController'
import $ from 'jquery'
import URI from 'urijs'

const editController = EditController()
function Common(index) {
const CLASS_OPEN = 'expanded'
const CLASS_CLOSED = 'collapsed'

const base = new URI('.').absoluteTo(index).href()
const editController = EditController()

const $nav = $('nav[role=toc]')
const $main = $('main[role=main]')
const base = URI('.')
.absoluteTo(index)
.href()

window.onpopstate = function(event) {
loadMain(document.location, undefined, false)
}
const $nav = $('nav[role=toc]')
const $main = $('main[role=main]')

return {
loadMain,
initializeMain,
isLocal
}
window.onpopstate = function(event) {
loadMain(document.location, undefined, false)
}

return {
loadMain,
initializeMain,
isLocal
}

function loadMain(href, $tocLink, pushState = true) {
const abs = new URI(href).absoluteTo(window.location.href).href()
if (pushState) {
history.pushState({}, '', href)
function loadMain(href, $tocLink, pushState = true) {
const abs = URI(href)
.absoluteTo(window.location.href)
.href()
if (pushState) {
history.pushState({}, '', href)
}
$.ajax({
url: abs,
success(data) {
updateToc(href, $tocLink)
updateMain(data)
}
$.ajax({
url: abs,
success(data) {
updateToc(href, $tocLink)
updateMain(data)
}
})
})

function updateToc(href, $tocLink) {
$nav.find('.active').removeClass('active')
if ($tocLink !== undefined) {
const $li = $tocLink.parent('li')
$li.addClass('active')
exposeNode($li)
} else {
const abs = new URI(href).absoluteTo(window.location.href).href()
const $li = $nav.find(`a[href="${abs}"]`).parent('li')
$li.addClass('active')
exposeNode($li)
}
function updateToc(href, $tocLink) {
$nav.find('.active').removeClass('active')
if ($tocLink !== undefined) {
const $li = $tocLink.parent('li')
$li.addClass('active')
exposeNode($li)
} else {
const abs = URI(href)
.absoluteTo(window.location.href)
.href()
const $li = $nav.find(`a[href="${abs}"]`).parent('li')
$li.addClass('active')
exposeNode($li)
}

function exposeNode($li) {
let $current = $li.parents('li:first')
while ($current.length) {
if ($current.hasClass(CLASS_CLOSED)) {
$current.addClass(CLASS_OPEN).removeClass(CLASS_CLOSED)
}
$current = $current.parents('li:first')
function exposeNode($li) {
let $current = $li.parents('li:first')
while ($current.length) {
if ($current.hasClass(CLASS_CLOSED)) {
$current.addClass(CLASS_OPEN).removeClass(CLASS_CLOSED)
}
$current = $current.parents('li:first')
}
}
}

function updateMain(data) {
const $dummy = $('<body>').append($.parseHTML(data))
$main.html($dummy.find('[role=main]:first').html())
document.title = $dummy.find('title').text()
initializeMain()
}
function updateMain(data) {
const $dummy = $('<body>').append($.parseHTML(data))
$main.html($dummy.find('[role=main]:first').html())
document.title = $dummy.find('title').text()
initializeMain()
}
}

function initializeMain() {
$main
.find('a[href]')
.filter(isLocal)
.click(mainClickHandler)
editController.createEditLink()
function initializeMain() {
$main
.find('a[href]')
.filter(isLocal)
.click(mainClickHandler)
editController.createEditLink()

function mainClickHandler(event) {
event.preventDefault()
event.stopPropagation()
function mainClickHandler(event) {
event.preventDefault()
event.stopPropagation()

const $target = $(event.currentTarget)
const href = $target.attr('href')
loadMain(href)
}
const $target = $(event.currentTarget)
const href = $target.attr('href')
loadMain(href)
}
}

function isLocal() {
const $a = $(this)
const abs = new URI($a.attr('href')).absoluteTo(window.location.href).href()
return abs.indexOf(base) !== -1
}
})
function isLocal() {
const $a = $(this)
const abs = URI($a.attr('href'))
.absoluteTo(window.location.href)
.href()
return abs.indexOf(base) !== -1
}
}

export default Common
89 changes: 46 additions & 43 deletions _js/lib/EditController.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
define(['jquery'], $ =>
function EditController() {
const OXYGEN = 'https://www.oxygenxml.com/oxygen-xml-web-author/app/oxygen.html'
const PROJECT_BASE = 'github://getFileContent/dita-ot/docs/'
const PROJECT_MAP = 'userguide-book.ditamap'
const PROJECT_BRANCH = 'develop'
import $ from 'jquery'

return {
createEditLink
}
function EditController() {
const OXYGEN = 'https://www.oxygenxml.com/oxygen-xml-web-author/app/oxygen.html'
const PROJECT_BASE = 'github://getFileContent/dita-ot/docs/'
const PROJECT_MAP = 'userguide-book.ditamap'
const PROJECT_BRANCH = 'develop'

return {
createEditLink
}

function createEditLink() {
let $link = $('#editLink')
if (location.pathname.indexOf('/dev/') != -1 && $('.generated').length === 0) {
let url
// TODO Why edit map when document is generated?
if (document.querySelector('.generated')) {
url = getEditURL(PROJECT_BRANCH, PROJECT_MAP)
} else {
const htmlURL = location.pathname.substring(location.pathname.indexOf('/dev/'))
let file = htmlURL.endsWith('.html')
? `${htmlURL.slice('/dev/'.length, htmlURL.length - '.html'.length)}.dita`
: `${htmlURL.slice('/dev/'.length, htmlURL.length)}index.dita`
if (file.indexOf('/first-build-') != -1) {
file = file.replace('first-build-', '')
}
if (file.indexOf('/build-') != -1) {
file = file.replace('build-', '')
}
url = getEditURL(PROJECT_BRANCH, file, PROJECT_MAP)
function createEditLink() {
let $link = $('#editLink')
if (location.pathname.indexOf('/dev/') != -1 && $('.generated').length === 0) {
let url
// TODO Why edit map when document is generated?
if (document.querySelector('.generated')) {
url = getEditURL(PROJECT_BRANCH, PROJECT_MAP)
} else {
const htmlURL = location.pathname.substring(location.pathname.indexOf('/dev/'))
let file = htmlURL.endsWith('.html')
? `${htmlURL.slice('/dev/'.length, htmlURL.length - '.html'.length)}.dita`
: `${htmlURL.slice('/dev/'.length, htmlURL.length)}index.dita`
if (file.indexOf('/first-build-') != -1) {
file = file.replace('first-build-', '')
}
if ($link.length === 0) {
$link = $(
'<a class="btn btn-success pull-right" id="editLink" target="_blank" title="Edit this page on GitHub to help improve the docs"><span class="glyphicon glyphicon-pencil"></span> Edit this page</a>'
)
$('.dev-docs-banner').append($link)
if (file.indexOf('/build-') != -1) {
file = file.replace('build-', '')
}
$link.attr('href', url)
} else {
$link.remove()
url = getEditURL(PROJECT_BRANCH, file, PROJECT_MAP)
}
if ($link.length === 0) {
$link = $(
'<a class="btn btn-success pull-right" id="editLink" target="_blank" title="Edit this page on GitHub to help improve the docs"><span class="glyphicon glyphicon-pencil"></span> Edit this page</a>'
)
$('.dev-docs-banner').append($link)
}
$link.attr('href', url)
} else {
$link.remove()
}
}

function getEditURL(branch, file, map) {
let url = `${OXYGEN}?url=${encodeURIComponent(`${PROJECT_BASE + branch}/${file}`)}`
if (map) {
url = `${url}&ditamap=${encodeURIComponent(`${PROJECT_BASE + branch}/${map}`)}`
}
return url
function getEditURL(branch, file, map) {
let url = `${OXYGEN}?url=${encodeURIComponent(`${PROJECT_BASE + branch}/${file}`)}`
if (map) {
url = `${url}&ditamap=${encodeURIComponent(`${PROJECT_BASE + branch}/${map}`)}`
}
})
return url
}
}

export default EditController
35 changes: 19 additions & 16 deletions _js/lib/HelpController.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
define(['jquery', 'jquery', 'bootstrap'], ($, jQuery) =>
function HelpController() {
const $help = $('#keyboardHelp')
$help.modal({ show: false })
import $ from 'jquery'

$(document).keypress(openHelp)
function HelpController() {
const $help = jQuery('#keyboardHelp')
$help.modal({ show: false })

function openHelp(event) {
const $target = $(event.target)
const key = event.which
if ($target.is(':input') || $('.modal:visible').length !== 0) {
// ignore
} else if (key === 63) {
event.preventDefault()
event.stopPropagation()
$(document).keypress(openHelp)

$help.modal('show')
}
function openHelp(event) {
const $target = $(event.target)
const key = event.which
if ($target.is(':input') || $('.modal:visible').length !== 0) {
// ignore
} else if (key === 63) {
event.preventDefault()
event.stopPropagation()

$help.modal('show')
}
})
}
}

export default HelpController
Loading