view src/editor.tp @ 127:2b25d0ce2946

Add fullscreen support and improve experience on tablet browsers by tweaking button text size and disabling zooming.
author Mike Pavone <pavone@retrodev.com>
date Tue, 06 Aug 2013 00:06:41 -0700
parents 1157639353e7
children 8285784f5ff5
line wrap: on
line source

#{
//mquery functions
q <- foreign: :query {}
qall <- foreign: :query {}
each <- foreign: :iterable fun {}
addClass <- foreign: :node className {}
removeClass <- foreign: :node className {}
hasClass <- foreign: :node className {}
get <- foreign: :url onSuccess onFail onOther {}
newEl <- foreign: :tagname props {}

//editor.js functions
getEl <- foreign: :from idx {}
setEl <- foreign: :to idx val {}
goFullScreen <- foreign: {}

//TP Parser
parser <- foreign: #{
	parse <- foreign: :str {}
}
isLambda <- foreign: :astnode {}

//js builtins
console <- foreign: #{
	log <- foreign: :val {}
}
window <- foreign: #{}
Object <- foreign: #{
	keys <- foreign: :object {}
}

filter <- :arr pred {
	output <- arr slice: 0 0
	each: arr :idx el {
		if: (pred: el) {
			output push: el
		} else: {}
	}
	output
}

//editor code
selection <- #{
	valid? <- false
}

setSelection:withInNode <- :astnode :innode {
	fakeEvent <- #{
		stopPropagation <- :Blah {
		}
	}
	selection <- #{
		valid? <- true
		in <- {
			(innode domNode) onclick: fakeEvent
		}
		out <- {
			fakeEvent <- #{
				stopPropagation <- :Blah {
				}
			}
			((astnode up) domNode) onclick: fakeEvent
		}
	}
}

setSelection <- :astnode {
	fakeEvent <- #{
		stopPropagation <- :Blah {
		}
	}
	selection <- #{
		valid? <- true
		in <- {
		}
		out <- {
			fakeEvent <- #{
				stopPropagation <- :Blah {
				}
			}
			((astnode up) domNode) onclick: fakeEvent
		}
	}
}

editFile <- :path {
	get: path :request {
		addClass: (q: "body") "editorMode"
		src <- request responseText
		ast <- parser parse: src
		ast populateSymbols: (foreign: null)
		ast toHTML: (q: "#src")
	}
}

selectNode <- :node {
	each: (qall: ".selected") :idx el {
		removeClass: el "selected"
	}
	addClass: node "selected"
}

selectQuery <- :selector {
	selectQuery: selector in: (foreign: undefined)
}

selectQuery:in <- :selector :context {
	each: (qall: ".selected") :idx el {
		removeClass: el "selected"
	}
	each: (qall: selector context) :idx el {
		addClass: el "selected"
	}
}

selectParent <- :node {
	each: (qall: ".selectParent") :idx el {
		removeClass: el "selectParent"
	}
	addClass: node "selectParent"
}

popInscope:onClick <- :syms :handler {
	inscope <- q: "#inscope"
	inscope innerHTML!: ""
	each: syms :idx key {
		inscope appendChild: (newEl: "li" #{
			textContent <- key
			onclick <- :Event { handler: key }
		})
	}
}

scalarClick <- :domnode astnode event {
	selectNode: domnode
	setSelection: astnode
	event stopPropagation: (foreign: undefined)
	//TODO: set focus
}

symbolClick <- :domnode astnode event {
	selectNode: domnode
	popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
		domnode textContent!: key
		astnode name!: key
	}
	setSelection: astnode
	event stopPropagation: (foreign: undefined)
}

assignClick <- :domnode astnode event {
	selectParent: domnode
	selectQuery: ".selectParent > .varname" in: domnode
	popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
		(domnode firstChild) textContent!: key
		(astnode symbol) name!: key
	}
	setSelection: astnode withInNode: (astnode expression)
	event stopPropagation: (foreign: undefined)
}

opClick <- :domnode astnode event {
	selectParent: domnode
	selectQuery: ".selectParent > .opname" in: domnode
	showOps
	setSelection: astnode withInNode: (astnode left)
	event stopPropagation: (foreign: undefined)
}

funClick <- :domnode astnode event {
	selectParent: domnode
	selectQuery: ".selectParent > .funpart" in: domnode
	symtable <- astnode symbols
	syms <- filter: (symtable allSymbols: (foreign: undefined)) :sym {
		isLambda: ((symtable find: sym) def)
	}
	inner <- if: (astnode receiver) != (foreign: null) {
		astnode receiver
	} else: {
		(astnode args) getEl: 0
	}
	setSelection: astnode withInNode: inner
	popInscope: syms onClick: :key {
		astnode name!: key
		parts <- key split: ":"
		nodes <- []
		each: (domnode children) :idx val{
			nodes push: val
		}
		partIdx <- 0
		nodeIdx <- 0
		lastWasNamePart <- true
		while: { partIdx < (parts length) || nodeIdx < (nodes length) } do: {
			if: nodeIdx < (nodes length) {
				node <-getEl: nodes nodeIdx
				nodeIdx <- nodeIdx + 1
				if: (hasClass: node "funpart") {
					if: partIdx < (parts length) {
						postfix <- if: partIdx = 0 && nodeIdx = 2 && (parts length) = 1 && (nodes length) = 2 { "" } else: { ":" }
						t <- (getEl: parts partIdx)
						node textContent!: (getEl: parts partIdx) . postfix
						partIdx <- partIdx + 1
					} else: {
						domnode removeChild: node
					}
					lastWasNamePart <- true
				} else: {
					if: (not: lastWasNamePart) && partIdx < (parts length) && nodeIdx > 0 {
						domnode insertBefore: (newEl: "span" #{
							className <- "funpart selected"
							textContent <- (getEl: parts partIdx) . ":"
						}) node
						partIdx <- partIdx + 1
					}
					lastWasNamePart <- false
				}
			} else: {
				console log: "part: " . (getEl: parts partIdx)
				domnode appendChild: (newEl: "span" #{
					className <- "funpart selected"
					textContent <- (getEl: parts partIdx) . ":"
				})
				partIdx <- partIdx + 1
			}
		}
	}
	event stopPropagation: (foreign: undefined)
}

lambdaClick <- :domnode astnode event {
	selectNode: domnode
	popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
		console log: "foooobar!"
	}
	inner <- if: ((astnode args) length) > 0 {
		(astnode args) getEl: 0
	} else: {
		(astnode expressions) getEl: 0
	}
	setSelection: astnode withInNode: inner
	event stopPropagation: (foreign: undefined)
}

objectClick <- :domnode astnode event {
	selectNode: domnode
	popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
		console log: "fooobar!"
	}
	setSelection: astnode withInNode: ((astnode messages) getEl: 0)
	event stopPropagation: (foreign: undefined)
}

visible <- "showops"

showOps <- {
	each: (qall: ".controls") :idx el {
		removeClass: el visible
		addClass: el "showops"
	}
	visible <- "showops"
}

showLit <- {
	each: (qall: ".controls") :idx el {
		removeClass: el visible
		addClass: el "showlit"
	}
	visible <- "showlit"
}

main <- {
	get: "/src/" :data {
		fakeEl <- newEl: "div" #{
			innerHTML <- data response
		}
		each: (qall: "a" fakeEl) :idx el {
			if: ((el textContent) = "../") {} else: {
				nel <- newEl: "a" #{
					href <- "/edit/src/" + (el textContent)
					textContent <- el textContent
				}
				nel onclick!: :event {
					link <- foreign: this
					path <- link href
					path <- path substr: (path indexOf: "/edit/") + 5
					editFile: path
					foreign: false
				}
				li <- newEl: "li"
				li appendChild: nel
				(q: "#browser ul") appendChild: li
			}
		}
	}

	//bind handlers for editor buttons
	each: (qall: ".controls li") :idx el {
		el onclick!: :event {
			srcel <- (q: "#src")
			srcel textContent!: (srcel textContent) + (el textContent)
		}
	}
	(q: "#ops_button") onclick!: :event {
		showOps
	}
	(q: "#lit_button") onclick!: :event {
		showLit
	}

	(q: "#in") onclick!: :event {
		console log: "inwards"
		if: (selection valid?) {
			selection in
		}
	}

	(q: "#out") onclick!: :event {
		console log: "outwards"
		if: (selection valid?) {
			selection out
		}
	}

	path <- (window location) pathname
	if: (path indexOf: "/edit/") = 0 {
		editFile: (path substr: 5)
	}
	(q: "#fullscreen") onclick!: :event {
		goFullScreen:
	}
}

}