Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.8
1.1 damieng 1: /*
2: This file is part of LON-CAPA.
3:
4: LON-CAPA is free software: you can redistribute it and/or modify
5: it under the terms of the GNU General Public License as published by
6: the Free Software Foundation, either version 3 of the License, or
7: (at your option) any later version.
8:
9: LON-CAPA is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with LON-CAPA. If not, see <http://www.gnu.org/licenses/>.
16: */
17:
18: library loncapa_daxe;
19:
20: import 'dart:async';
21: import 'dart:collection';
22: import 'dart:html' as h;
23: import 'package:daxe/daxe.dart';
24: import 'package:daxe/src/xmldom/xmldom.dart' as x;
25: import 'package:daxe/src/strings.dart';
26: import 'package:daxe/src/nodes/nodes.dart' show DNCData, DNText, SimpleTypeControl, ParentUpdatingDNText;
27: import 'dart:js' as js;
28:
29: import 'lcd_strings.dart';
30: part 'nodes/lcd_block.dart';
31: part 'nodes/lcd_parameter.dart';
32: part 'nodes/tex_mathjax.dart';
33: part 'nodes/lm.dart';
1.2 damieng 34: part 'nodes/numerical_response.dart';
1.3 damieng 35: part 'nodes/formula_response.dart';
36: part 'nodes/string_response.dart';
1.1 damieng 37: part 'nodes/radio_response.dart';
38: part 'nodes/radio_foilgroup.dart';
39: part 'nodes/radio_foil.dart';
40: part 'nodes/option_response.dart';
41: part 'nodes/option_foilgroup.dart';
42: part 'nodes/option_foil.dart';
1.6 damieng 43: part 'nodes/match_response.dart';
44: part 'nodes/match_foilgroup.dart';
45: part 'nodes/match_foil.dart';
46: part 'nodes/itemgroup.dart';
47: part 'nodes/match_item.dart';
1.1 damieng 48: part 'nodes/rank_response.dart';
49: part 'nodes/rank_foilgroup.dart';
50: part 'nodes/rank_foil.dart';
1.5 damieng 51: part 'nodes/textline.dart';
1.1 damieng 52: part 'nodes/hintgroup.dart';
53: part 'nodes/simple_ui_text.dart';
54: part 'nodes/script_block.dart';
1.2 damieng 55: part 'nodes/simple_ui_exception.dart';
1.7 damieng 56: part 'nodes/gnuplot.dart';
57: part 'nodes/fake_attribute_element.dart';
58: part 'nodes/plot_axis.dart';
59: part 'nodes/plot_curve.dart';
1.1 damieng 60: part 'lcd_button.dart';
61:
62:
63: void main() {
64: NodeFactory.addCoreDisplayTypes();
65:
66: addDisplayType('lcdblock',
67: (x.Element ref) => new LCDBlock.fromRef(ref),
68: (x.Node node, DaxeNode parent) => new LCDBlock.fromNode(node, parent)
69: );
70:
71: addDisplayType('texmathjax',
72: (x.Element ref) => new TeXMathJax.fromRef(ref),
73: (x.Node node, DaxeNode parent) => new TeXMathJax.fromNode(node, parent)
74: );
75:
76: addDisplayType('lm',
77: (x.Element ref) => new Lm.fromRef(ref),
78: (x.Node node, DaxeNode parent) => new Lm.fromNode(node, parent)
79: );
80:
81: addDisplayType('script',
82: (x.Element ref) => new ScriptBlock.fromRef(ref),
83: (x.Node node, DaxeNode parent) => new ScriptBlock.fromNode(node, parent)
84: );
85:
86: addDisplayType('parameter',
87: (x.Element ref) => new LCDParameter.fromRef(ref),
88: (x.Node node, DaxeNode parent) => new LCDParameter.fromNode(node, parent)
89: );
90:
1.2 damieng 91: addDisplayType('numericalresponse',
92: (x.Element ref) => new NumericalResponse.fromRef(ref),
93: (x.Node node, DaxeNode parent) => new NumericalResponse.fromNode(node, parent)
94: );
95:
1.3 damieng 96: addDisplayType('formularesponse',
97: (x.Element ref) => new FormulaResponse.fromRef(ref),
98: (x.Node node, DaxeNode parent) => new FormulaResponse.fromNode(node, parent)
99: );
100:
101: addDisplayType('stringresponse',
102: (x.Element ref) => new StringResponse.fromRef(ref),
103: (x.Node node, DaxeNode parent) => new StringResponse.fromNode(node, parent)
104: );
105:
1.1 damieng 106: addDisplayType('radioresponse',
107: (x.Element ref) => new RadioResponse.fromRef(ref),
108: (x.Node node, DaxeNode parent) => new RadioResponse.fromNode(node, parent)
109: );
110:
111: addDisplayType('optionresponse',
112: (x.Element ref) => new OptionResponse.fromRef(ref),
113: (x.Node node, DaxeNode parent) => new OptionResponse.fromNode(node, parent)
114: );
115:
1.6 damieng 116: addDisplayType('matchresponse',
117: (x.Element ref) => new MatchResponse.fromRef(ref),
118: (x.Node node, DaxeNode parent) => new MatchResponse.fromNode(node, parent)
119: );
120:
1.1 damieng 121: addDisplayType('rankresponse',
122: (x.Element ref) => new RankResponse.fromRef(ref),
123: (x.Node node, DaxeNode parent) => new RankResponse.fromNode(node, parent)
124: );
125:
126: addDisplayType('foilgroup',
127: (x.Element ref) {
128: if (ref.getAttribute('type') == 'radiobuttonresponse--foilgroup')
129: return new RadioFoilgroup.fromRef(ref);
130: else if (ref.getAttribute('type') == 'optionresponse--foilgroup')
131: return new OptionFoilgroup.fromRef(ref);
1.6 damieng 132: else if (ref.getAttribute('type') == 'matchresponse--foilgroup')
133: return new MatchFoilgroup.fromRef(ref);
1.1 damieng 134: else if (ref.getAttribute('type') == 'rankresponse--foilgroup')
135: return new RankFoilgroup.fromRef(ref);
136: return new LCDBlock.fromRef(ref);
137: },
138: (x.Node node, DaxeNode parent) {
139: if (parent is RadioResponse)
140: return new RadioFoilgroup.fromNode(node, parent);
141: else if (parent is OptionResponse)
142: return new OptionFoilgroup.fromNode(node, parent);
1.6 damieng 143: else if (parent is MatchResponse)
144: return new MatchFoilgroup.fromNode(node, parent);
1.1 damieng 145: else if (parent is RankResponse)
146: return new RankFoilgroup.fromNode(node, parent);
147: return new LCDBlock.fromNode(node, parent);
148: }
149: );
150:
151: addDisplayType('foil',
152: (x.Element ref) {
153: if (ref.getAttribute('type') == 'radiobuttonresponse--foil')
154: return new RadioFoil.fromRef(ref);
155: else if (ref.getAttribute('type') == 'optionresponse--foil')
156: return new OptionFoil.fromRef(ref);
1.6 damieng 157: else if (ref.getAttribute('type') == 'matchresponse--foil')
158: return new MatchFoil.fromRef(ref);
1.1 damieng 159: else if (ref.getAttribute('type') == 'rankresponse--foil')
160: return new RankFoil.fromRef(ref);
161: return new LCDBlock.fromRef(ref);
162: },
163: (x.Node node, DaxeNode parent) {
164: if (parent is RadioFoilgroup)
165: return new RadioFoil.fromNode(node, parent);
166: else if (parent is OptionFoilgroup)
167: return new OptionFoil.fromNode(node, parent);
1.6 damieng 168: else if (parent is MatchFoilgroup)
169: return new MatchFoil.fromNode(node, parent);
1.1 damieng 170: else if (parent is RankFoilgroup)
171: return new RankFoil.fromNode(node, parent);
172: return new LCDBlock.fromNode(node, parent);
173: }
174: );
175:
1.6 damieng 176: addDisplayType('itemgroup',
177: (x.Element ref) => new Itemgroup.fromRef(ref),
178: (x.Node node, DaxeNode parent) => new Itemgroup.fromNode(node, parent)
179: );
180:
181: addDisplayType('matchitem',
182: (x.Element ref) => new MatchItem.fromRef(ref),
183: (x.Node node, DaxeNode parent) => new MatchItem.fromNode(node, parent)
184: );
185:
1.5 damieng 186: addDisplayType('textline',
187: (x.Element ref) => new Textline.fromRef(ref),
188: (x.Node node, DaxeNode parent) => new Textline.fromNode(node, parent)
189: );
190:
1.1 damieng 191: addDisplayType('hintgroup',
192: (x.Element ref) => new Hintgroup.fromRef(ref),
193: (x.Node node, DaxeNode parent) => new Hintgroup.fromNode(node, parent)
194: );
195:
1.7 damieng 196: addDisplayType('gnuplot',
197: (x.Element ref) => new Gnuplot.fromRef(ref),
198: (x.Node node, DaxeNode parent) => new Gnuplot.fromNode(node, parent)
199: );
200:
201: addDisplayType('fakeattribute',
202: (x.Element ref) {
203: if (doc.cfg.elementName(ref) != 'title' || ref.getAttribute('type') == 'lonplot--title')
204: return new FakeAttributeElement.fromRef(ref);
205: return new LCDBlock.fromRef(ref);
206: },
207: (x.Node node, DaxeNode parent) {
208: if (parent is Gnuplot || parent is PlotCurve)
209: return new FakeAttributeElement.fromNode(node, parent);
210: return new LCDBlock.fromNode(node, parent);
211: }
212: );
213:
214: addDisplayType('plotaxis',
215: (x.Element ref) => new PlotAxis.fromRef(ref),
216: (x.Node node, DaxeNode parent) => new PlotAxis.fromNode(node, parent)
217: );
218:
219: addDisplayType('plotcurve',
220: (x.Element ref) => new PlotCurve.fromRef(ref),
221: (x.Node node, DaxeNode parent) => new PlotCurve.fromNode(node, parent)
222: );
223:
1.1 damieng 224: Future.wait([Strings.load(), LCDStrings.load(), _readTemplates('templates.xml')]).then((List responses) {
225: _init_daxe().then((v) {
226: // add things to the toolbar
227: ToolbarMenu sectionMenu = _makeSectionMenu();
228: if (sectionMenu != null)
229: page.toolbar.add(sectionMenu);
230: x.Element texRef = doc.cfg.elementReference('m');
231: if (texRef != null) {
232: ToolbarBox insertBox = new ToolbarBox();
233: ToolbarButton texButton = new ToolbarButton(
234: LCDStrings.get('tex_equation'), 'images/tex.png',
235: () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate,
236: data:new ToolbarStyleInfo([texRef], null, null));
237: insertBox.add(texButton);
238: page.toolbar.add(insertBox);
239: }
240: h.Element tbh = h.querySelector('.toolbar');
241: tbh.replaceWith(page.toolbar.html());
242: page.adjustPositionsUnderToolbar();
243: page.updateAfterPathChange();
244: // add things to the menubar
245: if (responses[2] is x.Document) {
246: // at this point the menubar html is already in the document, so we have to fix the HTML
247: h.Element menubarDiv = h.document.getElementsByClassName('menubar')[0];
1.4 damieng 248: if (doc.filePath != null && doc.filePath.indexOf('&url=') != -1) { // otherwise we are not on LON-CAPA
1.1 damieng 249: MenuItem item = new MenuItem(Strings.get('menu.save'), () => save(), shortcut: 'S');
250: Menu fileMenu = page.mbar.menus[0];
251: fileMenu.add(item);
252: menubarDiv.firstChild.replaceWith(page.mbar.createMenuDiv(fileMenu));
253: }
254: Menu m = _makeTemplatesMenu(responses[2]);
255: page.mbar.add(m);
256: menubarDiv.append(page.mbar.createMenuDiv(m));
257: page.updateAfterPathChange();
258: } else
259: print("Error reading templates file, could not build the menu.");
260: });
261: });
262: }
263:
264: Future _init_daxe() {
265: Completer completer = new Completer();
266: doc = new DaxeDocument();
267: page = new WebPage();
268:
269: // check parameters for a config and file to open
270: String file = null;
271: String config = null;
272: String saveURL = null;
273: h.Location location = h.window.location;
274: String search = location.search;
275: if (search.startsWith('?'))
276: search = search.substring(1);
277: List<String> parameters = search.split('&');
278: for (String param in parameters) {
279: List<String> lparam = param.split('=');
280: if (lparam.length != 2)
281: continue;
282: if (lparam[0] == 'config')
283: config = lparam[1];
284: else if (lparam[0] == 'file')
285: file = Uri.decodeComponent(lparam[1]);
286: else if (lparam[0] == 'save')
287: saveURL = lparam[1];
288: }
289: if (saveURL != null)
290: doc.saveURL = saveURL;
291: if (config != null && file != null)
292: page.openDocument(file, config).then((v) => completer.complete());
293: else if (config != null)
294: page.newDocument(config).then((v) => completer.complete());
295: else {
296: h.window.alert(Strings.get('daxe.missing_config'));
297: completer.completeError(Strings.get('daxe.missing_config'));
298: }
299: return(completer.future);
300: }
301:
302: void save() {
303: saveOnLONCAPA().then((_) {
304: h.window.alert(Strings.get('save.success'));
305: }, onError: (DaxeException ex) {
306: h.window.alert(Strings.get('save.error') + ': ' + ex.message);
307: });
308: }
309:
310: /**
311: * Send the document with a POST request to LON-CAPA.
312: */
313: Future saveOnLONCAPA() {
314: int ind = doc.filePath.indexOf('&url=');
315: if (ind == -1)
316: return(new Future.error(new DaxeException('bad URL')));
317: String path = doc.filePath.substring(ind+5);
318: path = Uri.decodeQueryComponent(path);
319: ind = path.lastIndexOf('/');
320: String filename;
321: if (ind == -1)
322: filename = path;
323: else {
324: filename = path.substring(ind+1);
325: path = path.substring(0, ind+1);
326: }
327: Completer completer = new Completer();
328: String bound = 'AaB03x';
329: h.HttpRequest request = new h.HttpRequest();
330: request.onLoad.listen((h.ProgressEvent event) {
331: completer.complete(); // TODO: check for something, status is sometimes wrongly OK
332: });
333: request.onError.listen((h.ProgressEvent event) {
334: completer.completeError(new DaxeException(request.status.toString()));
335: });
336: request.open('POST', '/upload_file');
337: request.setRequestHeader('Content-Type', "multipart/form-data; boundary=$bound");
338:
339: StringBuffer sb = new StringBuffer();
340: sb.write("--$bound\r\n");
341: sb.write('Content-Disposition: form-data; name="uploads_path"\r\n');
342: sb.write('Content-type: text/plain; charset=UTF-8\r\n');
343: sb.write('Content-transfer-encoding: 8bit\r\n\r\n');
344: sb.write(path);
345: sb.write("\r\n--$bound\r\n");
346: sb.write('Content-Disposition: form-data; name="uploads"; filename="$filename"\r\n');
347: sb.write('Content-Type: application/octet-stream\r\n\r\n');
348: doc.dndoc.xmlEncoding = 'UTF-8'; // the document is forced to use UTF-8
349: sb.write(doc.toString());
350: sb.write('\r\n--$bound--\r\n\r\n');
351: request.send(sb.toString());
352: return(completer.future);
353: }
354:
355: ToolbarMenu _makeSectionMenu() {
356: Menu menu = new Menu(LCDStrings.get('Section'));
357: List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
358: if (sectionRefs == null || sectionRefs.length == 0)
359: return(null);
360: x.Element h1Ref = doc.cfg.elementReference('h1');
361: for (String role in ['introduction', 'conclusion', 'prerequisites', 'objectives',
362: 'reminder', 'definition', 'demonstration', 'example', 'advise',
363: 'remark', 'warning', 'more_information', 'method',
364: 'activity', 'bibliography', 'citation']) {
365: MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
366: data:new ToolbarStyleInfo(sectionRefs, null, null));
367: menuItem.action = () {
368: ToolbarStyleInfo info = menuItem.data;
369: x.Element sectionRef = info.validRef;
370: LCDBlock section = NodeFactory.create(sectionRef);
371: section.state = 1;
372: section.setAttribute('class', 'role-' + role);
373: LCDBlock h1 = NodeFactory.create(h1Ref);
374: h1.state = 1;
375: if (doc.insert2(section, page.getSelectionStart())) {
376: doc.insertNode(h1, new Position(section, 0));
377: page.cursor.moveTo(new Position(h1, 0));
378: page.updateAfterPathChange();
379: }
380: };
381: menu.add(menuItem);
382: }
383: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
384: return(tbmenu);
385: }
386:
387: Future<x.Document> _readTemplates(String templatesPath) {
388: x.DOMParser dp = new x.DOMParser();
389: return(dp.parseFromURL(templatesPath));
390: }
391:
392: Menu _makeTemplatesMenu(x.Document templatesDoc) {
393: Menu menu = new Menu(LCDStrings.get('Templates'));
394: x.Element templates = templatesDoc.documentElement;
395: for (x.Node child in templates.childNodes) {
396: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
397: menu.add(_makeMenu(child));
398: }
399: }
400: return(menu);
401: }
402:
403: Menu _makeMenu(x.Element el) {
404: String locale = LCDStrings.systemLocale;
405: String defaultLocale = LCDStrings.defaultLocale;
406: String title;
407: for (x.Node child in el.childNodes) {
408: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
409: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
410: if ((child as x.Element).getAttribute('lang') == locale) {
411: title = child.firstChild.nodeValue;
412: break;
413: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
414: title = child.firstChild.nodeValue;
415: }
416: }
417: }
418: }
419: if (title == null)
420: title = '?';
421: Menu menu = new Menu(title);
422: for (x.Node child in el.childNodes) {
423: if (child.nodeType == x.Node.ELEMENT_NODE) {
424: if (child.nodeName == 'menu') {
425: menu.add(_makeMenu(child));
426: } else if (child.nodeName == 'item') {
427: menu.add(_makeItem(child));
428: }
429: }
430: }
431: return(menu);
432: }
433:
434: MenuItem _makeItem(x.Element item) {
435: String locale = LCDStrings.systemLocale;
436: String defaultLocale = LCDStrings.defaultLocale;
437: String path, type, title, help;
438: for (x.Node child in item.childNodes) {
439: if (child.nodeType == x.Node.ELEMENT_NODE) {
440: if (child.nodeName == 'title') {
441: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
442: if ((child as x.Element).getAttribute('lang') == locale) {
443: title = child.firstChild.nodeValue;
444: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
445: title = child.firstChild.nodeValue;
446: }
447: }
448: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
449: path = child.firstChild.nodeValue;
450: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
451: type = child.firstChild.nodeValue;
452: } else if (child.nodeName == 'help') {
453: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
454: if ((child as x.Element).getAttribute('lang') == locale) {
455: help = child.firstChild.nodeValue;
456: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
457: help = child.firstChild.nodeValue;
458: }
459: }
460: }
461: }
462: }
463: if (type == null) {
464: print("Warning: missing type for template $title\n");
465: type = 'problem';
466: }
467: x.Element refElement = doc.cfg.elementReference(type);
468: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
469: if (help != null)
470: menuItem.toolTipText = help;
471: return menuItem;
472: }
473:
474: void _insertTemplate(String filePath) {
475: try {
476: x.DOMParser dp = new x.DOMParser();
477: dp.parseFromURL(filePath).then((x.Document templateDoc) {
478: x.Element root = templateDoc.documentElement;
479: if (root == null)
480: return;
481: doc.removeWhitespace(root);
482: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
483: UndoableEdit edit;
484: Position pos = page.getSelectionStart();
485: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
486: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
487: else
488: edit = new UndoableEdit.insertNode(pos, dnRoot);
489: doc.doNewEdit(edit);
490: page.updateAfterPathChange();
1.8 ! damieng 491: if (dnRoot.nodeName != 'loncapa')
! 492: page.scrollToNode(dnRoot);
1.1 damieng 493: });
494: } on x.DOMException catch(ex) {
495: h.window.alert(ex.toString());
496: }
497: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>