mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Syntax editor: can now load minibar (in-place) from a linearised tree
This commit is contained in:
@@ -20,6 +20,15 @@ if(!Array.isArray) {
|
||||
};
|
||||
}
|
||||
|
||||
// Create a clone of an array
|
||||
// http://davidwalsh.name/javascript-clone-array
|
||||
// Note that iterating over an array with for...in will include "clone" as a key!
|
||||
// if(!Array.clone) {
|
||||
// Array.prototype.clone = function() {
|
||||
// return this.slice(0);
|
||||
// };
|
||||
// }
|
||||
|
||||
/* --- JSONP ---------------------------------------------------------------- */
|
||||
|
||||
// Inspired by the function jsonp from
|
||||
|
||||
@@ -7,7 +7,7 @@ body.syntax_editor {
|
||||
display:none;
|
||||
}
|
||||
|
||||
#editor select#to_menu
|
||||
.editor select#to_menu
|
||||
{
|
||||
height: 10em;
|
||||
position: absolute;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
</head>
|
||||
<body class=syntax_editor>
|
||||
<h2>Syntax Editor</h2>
|
||||
<div id="editor"></div>
|
||||
<div id="editor" class="test"></div>
|
||||
<noscript>This page doesn't works unless JavaScript is enabled.</noscript>
|
||||
|
||||
<hr />
|
||||
@@ -17,26 +17,23 @@
|
||||
John J. Camilleri, November 2012
|
||||
</small>
|
||||
|
||||
<!-- Common -->
|
||||
<script type="text/javascript" src="../js/support.js"></script>
|
||||
<script type="text/javascript" src="../js/pgf_online.js"></script>
|
||||
|
||||
<!-- Editor -->
|
||||
<script type="text/javascript" src="ast.js"></script>
|
||||
<script type="text/javascript" src="editor_menu.js"></script>
|
||||
<script type="text/javascript" src="editor.js"></script>
|
||||
<script type="text/javascript">
|
||||
var server_options = {
|
||||
// grammars_url: "http://www.grammaticalframework.org/grammars/",
|
||||
grammars_url: "http://localhost:41296/grammars/",
|
||||
}
|
||||
var editor_options = {
|
||||
target: "editor"
|
||||
}
|
||||
if(/^\?\/tmp\//.test(location.search)) {
|
||||
var args=decodeURIComponent(location.search.substr(1)).split(" ")
|
||||
if(args[0]) server_options.grammars_url=args[0];
|
||||
}
|
||||
var server = pgf_online(server_options);
|
||||
var editor = new Editor(server, editor_options);
|
||||
</script>
|
||||
|
||||
<!-- Minibar -->
|
||||
<script type="text/JavaScript" src="../minibar/minibar.js"></script>
|
||||
<script type="text/JavaScript" src="../minibar/minibar_input.js"></script>
|
||||
<script type="text/JavaScript" src="../minibar/minibar_translations.js"></script>
|
||||
<script type="text/JavaScript" src="../minibar/minibar_support.js"></script>
|
||||
|
||||
<!-- Get us rolling! -->
|
||||
<script type="text/javascript" src="editor_online.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,11 +1,3 @@
|
||||
|
||||
/* --- Some enhancements --------------------------------------------------- */
|
||||
|
||||
// http://www.xenoveritas.org/blog/xeno/the-correct-way-to-clone-javascript-arrays
|
||||
// Array.prototype.clone = function(){
|
||||
// return this.slice(0);
|
||||
// }
|
||||
|
||||
/* --- Main Editor object --------------------------------------------------- */
|
||||
/* When creating the object, stuff gets called in this order:
|
||||
new EditorMenu
|
||||
@@ -49,16 +41,20 @@ function Editor(server,opts) {
|
||||
if(opts) for(var o in opts) this.options[o]=opts[o];
|
||||
|
||||
/* --- Creating UI components ------------------------------------------- */
|
||||
var main = document.getElementById(this.options.target);
|
||||
this.container = document.getElementById(this.options.target);
|
||||
this.container.classList.add("editor");
|
||||
this.ui = {
|
||||
menubar: div_class("menu"),
|
||||
tree: div_id("tree"),
|
||||
refinements: div_id("refinements"),
|
||||
lin: div_id("linearisations")
|
||||
};
|
||||
with(this.ui) {
|
||||
appendChildren(main, [menubar, tree, refinements, lin]);
|
||||
}
|
||||
appendChildren(this.container, [
|
||||
this.ui.menubar,
|
||||
this.ui.tree,
|
||||
this.ui.refinements,
|
||||
this.ui.lin
|
||||
]);
|
||||
|
||||
/* --- Client state initialisation -------------------------------------- */
|
||||
this.server = server;
|
||||
@@ -70,8 +66,11 @@ function Editor(server,opts) {
|
||||
/* --- Main program, this gets things going ----------------------------- */
|
||||
this.menu = new EditorMenu(this, this.options);
|
||||
|
||||
/* --- Apply supplied initial settings (if any) ------------------------- */
|
||||
// if (this.options.initial.grammar)
|
||||
/* --- Shutdown the editor nicely --------------------------------------- */
|
||||
this.shutdown = function() {
|
||||
clear(this.container);
|
||||
this.container.classList.remove("editor");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -237,32 +236,34 @@ Editor.prototype.redraw_tree=function() {
|
||||
}
|
||||
|
||||
Editor.prototype.update_linearisation=function(){
|
||||
|
||||
var t = this;
|
||||
function langpart(conc,abs) { // langpart("FoodsEng","Foods") == "Eng"
|
||||
return hasPrefix(conc,abs) ? conc.substr(abs.length) : conc;
|
||||
}
|
||||
|
||||
var t = this;
|
||||
with (this) {
|
||||
var args = {
|
||||
tree: ast.toString()
|
||||
};
|
||||
server.linearize(args, function(data){
|
||||
clear(t.ui.lin);
|
||||
for (i in data) {
|
||||
var lang = data[i].to;
|
||||
var langname = langpart(lang, t.grammar.name);
|
||||
if (t.languages.length < 1 || elem(lang, t.languages)) {
|
||||
var div_lang = empty("div");
|
||||
div_lang.appendChild(span_class("lang", text(langname)));
|
||||
div_lang.appendChild(
|
||||
span_class("lin", [text(data[i].text)])
|
||||
);
|
||||
t.ui.lin.appendChild(div_lang);
|
||||
}
|
||||
}
|
||||
function row(lang, lin) {
|
||||
var langname = langpart(lang, t.grammar.name);
|
||||
var btn = button(langname, function(){
|
||||
t.options.lin_action(lin,lang);
|
||||
});
|
||||
var c1 = th(btn);
|
||||
var c2 = td(text(lin));
|
||||
var row = tr([c1,c2]);
|
||||
return row;
|
||||
}
|
||||
var args = {
|
||||
tree: t.ast.toString()
|
||||
};
|
||||
t.server.linearize(args, function(data){
|
||||
clear(t.ui.lin);
|
||||
var tbody=empty("tbody");
|
||||
for (i in data) {
|
||||
var lang = data[i].to;
|
||||
if (t.languages.length < 1 || elem(lang, t.languages)) {
|
||||
tbody.appendChild(row(lang, data[i].text))
|
||||
}
|
||||
}
|
||||
t.ui.lin.appendChild(wrap("table",tbody));
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
41
src/www/syntax-editor/editor_online.js
Normal file
41
src/www/syntax-editor/editor_online.js
Normal file
@@ -0,0 +1,41 @@
|
||||
var server_options = {
|
||||
// grammars_url: "http://www.grammaticalframework.org/grammars/",
|
||||
// grammars_url: "http://localhost:41296/grammars/",
|
||||
}
|
||||
var editor_options = {
|
||||
target: "editor",
|
||||
// initial: {
|
||||
// grammar: "http://localhost:41296/grammars/Phrasebook.pgf",
|
||||
// startcat: "Proposition",
|
||||
// languages: ["Eng","Swe","Ita"],
|
||||
// abstr: "PropOpenDate (SuperlPlace TheMostExpensive School) Tomorrow",
|
||||
// node_id: null
|
||||
// },
|
||||
show: {
|
||||
grammar_menu: true,
|
||||
startcat_menu: true,
|
||||
to_menu: true,
|
||||
random_button: true
|
||||
}
|
||||
}
|
||||
if(window.Minibar) // Minibar loaded?
|
||||
editor_options.lin_action=function(s,langFrom) {
|
||||
editor.shutdown();
|
||||
var minibar_options = {
|
||||
target: "editor",
|
||||
initial_grammar: editor.menu.ui.grammar_menu.value, // hmm
|
||||
initial: {
|
||||
from: langFrom,
|
||||
input: s.split(" ") // is it that easy?
|
||||
}
|
||||
}
|
||||
editor.minibar=new Minibar(server,minibar_options);
|
||||
}
|
||||
if(/^\?\/tmp\//.test(location.search)) {
|
||||
var args=decodeURIComponent(location.search.substr(1)).split(" ")
|
||||
if(args[0]) server_options.grammars_url=args[0];
|
||||
}
|
||||
var server = pgf_online(server_options);
|
||||
var editor = new Editor(server, editor_options);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user