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