Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.9
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.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;
1.9 ! damieng 375: section.appendChild(h1);
! 376: x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
! 377: DNHiddenP p = NodeFactory.create(hiddenp);
! 378: section.appendChild(p);
1.1 damieng 379: if (doc.insert2(section, page.getSelectionStart())) {
380: page.cursor.moveTo(new Position(h1, 0));
381: page.updateAfterPathChange();
382: }
383: };
384: menu.add(menuItem);
385: }
386: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
387: return(tbmenu);
388: }
389:
390: Future<x.Document> _readTemplates(String templatesPath) {
391: x.DOMParser dp = new x.DOMParser();
392: return(dp.parseFromURL(templatesPath));
393: }
394:
395: Menu _makeTemplatesMenu(x.Document templatesDoc) {
396: Menu menu = new Menu(LCDStrings.get('Templates'));
397: x.Element templates = templatesDoc.documentElement;
398: for (x.Node child in templates.childNodes) {
399: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
400: menu.add(_makeMenu(child));
401: }
402: }
403: return(menu);
404: }
405:
406: Menu _makeMenu(x.Element el) {
407: String locale = LCDStrings.systemLocale;
408: String defaultLocale = LCDStrings.defaultLocale;
409: String title;
410: for (x.Node child in el.childNodes) {
411: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
412: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
413: if ((child as x.Element).getAttribute('lang') == locale) {
414: title = child.firstChild.nodeValue;
415: break;
416: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
417: title = child.firstChild.nodeValue;
418: }
419: }
420: }
421: }
422: if (title == null)
423: title = '?';
424: Menu menu = new Menu(title);
425: for (x.Node child in el.childNodes) {
426: if (child.nodeType == x.Node.ELEMENT_NODE) {
427: if (child.nodeName == 'menu') {
428: menu.add(_makeMenu(child));
429: } else if (child.nodeName == 'item') {
430: menu.add(_makeItem(child));
431: }
432: }
433: }
434: return(menu);
435: }
436:
437: MenuItem _makeItem(x.Element item) {
438: String locale = LCDStrings.systemLocale;
439: String defaultLocale = LCDStrings.defaultLocale;
440: String path, type, title, help;
441: for (x.Node child in item.childNodes) {
442: if (child.nodeType == x.Node.ELEMENT_NODE) {
443: if (child.nodeName == 'title') {
444: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
445: if ((child as x.Element).getAttribute('lang') == locale) {
446: title = child.firstChild.nodeValue;
447: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
448: title = child.firstChild.nodeValue;
449: }
450: }
451: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
452: path = child.firstChild.nodeValue;
453: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
454: type = child.firstChild.nodeValue;
455: } else if (child.nodeName == 'help') {
456: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
457: if ((child as x.Element).getAttribute('lang') == locale) {
458: help = child.firstChild.nodeValue;
459: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
460: help = child.firstChild.nodeValue;
461: }
462: }
463: }
464: }
465: }
466: if (type == null) {
467: print("Warning: missing type for template $title\n");
468: type = 'problem';
469: }
470: x.Element refElement = doc.cfg.elementReference(type);
471: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
472: if (help != null)
473: menuItem.toolTipText = help;
474: return menuItem;
475: }
476:
477: void _insertTemplate(String filePath) {
478: try {
479: x.DOMParser dp = new x.DOMParser();
480: dp.parseFromURL(filePath).then((x.Document templateDoc) {
481: x.Element root = templateDoc.documentElement;
482: if (root == null)
483: return;
484: doc.removeWhitespace(root);
485: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
486: UndoableEdit edit;
487: Position pos = page.getSelectionStart();
488: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
489: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
490: else
491: edit = new UndoableEdit.insertNode(pos, dnRoot);
492: doc.doNewEdit(edit);
493: page.updateAfterPathChange();
1.8 damieng 494: if (dnRoot.nodeName != 'loncapa')
495: page.scrollToNode(dnRoot);
1.1 damieng 496: });
497: } on x.DOMException catch(ex) {
498: h.window.alert(ex.toString());
499: }
500: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>