noide
Version:
Web Editor built on hapi and nes websockets using browserify, superviews.js, bootstrap and the ACE editor.
132 lines (103 loc) • 2.96 kB
JavaScript
var page = require('page')
var qs = require('querystring')
var fs = require('./fs')
var client = require('./client')
var util = require('./util')
var splitter = require('./splitter')
var editor = require('./editor')
var linter = require('./standard')
var noide = require('./noide')
var watch = require('./watch')
var workspacesEl = document.getElementById('workspaces')
window.onbeforeunload = function () {
if (noide.dirty.length) {
return 'Unsaved changes will be lost - are you sure you want to leave?'
}
}
client.connect(function (err) {
if (err) {
return util.handleError(err)
}
client.request('/watched', function (err, payload) {
if (err) {
return util.handleError(err)
}
noide.load(payload)
// Save state on page unload
window.onunload = function () {
noide.saveState()
}
// Build the tree pane
require('./tree')
// Build the recent list pane
require('./recent')
// Build the procsses pane
var processesView = require('./processes')
// Subscribe to watched file changes
// that happen on the file system
watch()
// Subscribe to editor changes and
// update the recent files views
editor.on('input', function () {
noide.onInput()
})
/* Initialize the splitters */
function resizeEditor () {
editor.resize()
processesView.editor.resize()
}
splitter(document.getElementById('sidebar-workspaces'), false, resizeEditor)
splitter(document.getElementById('workspaces-info'), true, resizeEditor)
splitter(document.getElementById('main-footer'), true, resizeEditor)
/* Initialize the standardjs linter */
linter()
function setWorkspace (className) {
workspacesEl.className = className || 'welcome'
}
page('*', function (ctx, next) {
// Update current file state
noide.current = null
next()
})
page('/', function (ctx) {
setWorkspace()
})
page('/file', function (ctx, next) {
var relativePath = qs.parse(ctx.querystring).path
var file = noide.getFile(relativePath)
if (!file) {
return next()
}
var session = noide.getSession(file)
function setSession () {
setWorkspace('editor')
// Update state
noide.current = file
if (!noide.hasRecent(file)) {
noide.addRecent(file)
}
// Set the editor session
editor.setSession(session.editSession)
editor.resize()
editor.focus()
}
if (session) {
setSession()
} else {
fs.readFile(relativePath, function (err, payload) {
if (err) {
return util.handleError(err)
}
session = noide.addSession(file, payload.contents)
setSession()
})
}
})
page('*', function (ctx) {
setWorkspace('not-found')
})
page({
hashbang: true
})
})
})