Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.24
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.14 damieng 26: import 'package:daxe/src/nodes/nodes.dart' show DNCData, DNText, SimpleTypeControl, ParentUpdatingDNText, DNHiddenP, DNString;
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.14 damieng 34: part 'nodes/chem.dart';
1.2 damieng 35: part 'nodes/numerical_response.dart';
1.3 damieng 36: part 'nodes/formula_response.dart';
37: part 'nodes/string_response.dart';
1.11 damieng 38: part 'nodes/essay_response.dart';
39: part 'nodes/textfield.dart';
1.1 damieng 40: part 'nodes/radio_response.dart';
41: part 'nodes/radio_foilgroup.dart';
42: part 'nodes/radio_foil.dart';
43: part 'nodes/option_response.dart';
44: part 'nodes/option_foilgroup.dart';
45: part 'nodes/option_foil.dart';
1.6 damieng 46: part 'nodes/match_response.dart';
47: part 'nodes/match_foilgroup.dart';
48: part 'nodes/match_foil.dart';
49: part 'nodes/itemgroup.dart';
50: part 'nodes/match_item.dart';
1.1 damieng 51: part 'nodes/rank_response.dart';
52: part 'nodes/rank_foilgroup.dart';
53: part 'nodes/rank_foil.dart';
1.10 damieng 54: part 'nodes/math_response.dart';
55: part 'nodes/math_answer.dart';
1.13 damieng 56: part 'nodes/organic_response.dart';
57: part 'nodes/jsme_dialog.dart';
58: part 'nodes/reaction_response.dart';
1.5 damieng 59: part 'nodes/textline.dart';
1.1 damieng 60: part 'nodes/hintgroup.dart';
61: part 'nodes/simple_ui_text.dart';
62: part 'nodes/script_block.dart';
1.2 damieng 63: part 'nodes/simple_ui_exception.dart';
1.7 damieng 64: part 'nodes/gnuplot.dart';
1.22 damieng 65: part 'nodes/organic_structure.dart';
1.7 damieng 66: part 'nodes/fake_attribute_element.dart';
67: part 'nodes/plot_axis.dart';
68: part 'nodes/plot_curve.dart';
1.17 damieng 69: part 'nodes/section.dart';
70: part 'nodes/h1.dart';
1.1 damieng 71: part 'lcd_button.dart';
72:
1.18 damieng 73: List<String> knownSectionRoles = ['activity', 'advice', 'bibliography', 'citation',
74: 'conclusion', 'definition', 'demonstration', 'example',
75: 'explanation', 'introduction', 'method', 'more_information',
76: 'objectives', 'prerequisites', 'remark', 'reminder',
77: 'summary', 'syntax', 'warning'];
78:
1.1 damieng 79:
80: void main() {
81: NodeFactory.addCoreDisplayTypes();
82:
83: addDisplayType('lcdblock',
84: (x.Element ref) => new LCDBlock.fromRef(ref),
85: (x.Node node, DaxeNode parent) => new LCDBlock.fromNode(node, parent)
86: );
87:
88: addDisplayType('texmathjax',
89: (x.Element ref) => new TeXMathJax.fromRef(ref),
90: (x.Node node, DaxeNode parent) => new TeXMathJax.fromNode(node, parent)
91: );
92:
93: addDisplayType('lm',
94: (x.Element ref) => new Lm.fromRef(ref),
95: (x.Node node, DaxeNode parent) => new Lm.fromNode(node, parent)
96: );
97:
1.14 damieng 98: addDisplayType('chem',
99: (x.Element ref) => new Chem.fromRef(ref),
100: (x.Node node, DaxeNode parent) => new Chem.fromNode(node, parent)
101: );
102:
1.1 damieng 103: addDisplayType('script',
104: (x.Element ref) => new ScriptBlock.fromRef(ref),
105: (x.Node node, DaxeNode parent) => new ScriptBlock.fromNode(node, parent)
106: );
107:
108: addDisplayType('parameter',
109: (x.Element ref) => new LCDParameter.fromRef(ref),
110: (x.Node node, DaxeNode parent) => new LCDParameter.fromNode(node, parent)
111: );
112:
1.2 damieng 113: addDisplayType('numericalresponse',
114: (x.Element ref) => new NumericalResponse.fromRef(ref),
115: (x.Node node, DaxeNode parent) => new NumericalResponse.fromNode(node, parent)
116: );
117:
1.3 damieng 118: addDisplayType('formularesponse',
119: (x.Element ref) => new FormulaResponse.fromRef(ref),
120: (x.Node node, DaxeNode parent) => new FormulaResponse.fromNode(node, parent)
121: );
122:
123: addDisplayType('stringresponse',
124: (x.Element ref) => new StringResponse.fromRef(ref),
125: (x.Node node, DaxeNode parent) => new StringResponse.fromNode(node, parent)
126: );
127:
1.11 damieng 128: addDisplayType('essayresponse',
129: (x.Element ref) => new EssayResponse.fromRef(ref),
130: (x.Node node, DaxeNode parent) => new EssayResponse.fromNode(node, parent)
131: );
132:
1.1 damieng 133: addDisplayType('radioresponse',
134: (x.Element ref) => new RadioResponse.fromRef(ref),
135: (x.Node node, DaxeNode parent) => new RadioResponse.fromNode(node, parent)
136: );
137:
138: addDisplayType('optionresponse',
139: (x.Element ref) => new OptionResponse.fromRef(ref),
140: (x.Node node, DaxeNode parent) => new OptionResponse.fromNode(node, parent)
141: );
142:
1.6 damieng 143: addDisplayType('matchresponse',
144: (x.Element ref) => new MatchResponse.fromRef(ref),
145: (x.Node node, DaxeNode parent) => new MatchResponse.fromNode(node, parent)
146: );
147:
1.1 damieng 148: addDisplayType('rankresponse',
149: (x.Element ref) => new RankResponse.fromRef(ref),
150: (x.Node node, DaxeNode parent) => new RankResponse.fromNode(node, parent)
151: );
152:
1.10 damieng 153: addDisplayType('mathresponse',
154: (x.Element ref) => new MathResponse.fromRef(ref),
155: (x.Node node, DaxeNode parent) => new MathResponse.fromNode(node, parent)
156: );
157:
1.13 damieng 158: addDisplayType('organicresponse',
159: (x.Element ref) => new OrganicResponse.fromRef(ref),
160: (x.Node node, DaxeNode parent) => new OrganicResponse.fromNode(node, parent)
161: );
162:
163: addDisplayType('reactionresponse',
164: (x.Element ref) => new ReactionResponse.fromRef(ref),
165: (x.Node node, DaxeNode parent) => new ReactionResponse.fromNode(node, parent)
166: );
167:
1.1 damieng 168: addDisplayType('foilgroup',
169: (x.Element ref) {
170: if (ref.getAttribute('type') == 'radiobuttonresponse--foilgroup')
171: return new RadioFoilgroup.fromRef(ref);
172: else if (ref.getAttribute('type') == 'optionresponse--foilgroup')
173: return new OptionFoilgroup.fromRef(ref);
1.6 damieng 174: else if (ref.getAttribute('type') == 'matchresponse--foilgroup')
175: return new MatchFoilgroup.fromRef(ref);
1.1 damieng 176: else if (ref.getAttribute('type') == 'rankresponse--foilgroup')
177: return new RankFoilgroup.fromRef(ref);
178: return new LCDBlock.fromRef(ref);
179: },
180: (x.Node node, DaxeNode parent) {
181: if (parent is RadioResponse)
182: return new RadioFoilgroup.fromNode(node, parent);
183: else if (parent is OptionResponse)
184: return new OptionFoilgroup.fromNode(node, parent);
1.6 damieng 185: else if (parent is MatchResponse)
186: return new MatchFoilgroup.fromNode(node, parent);
1.1 damieng 187: else if (parent is RankResponse)
188: return new RankFoilgroup.fromNode(node, parent);
189: return new LCDBlock.fromNode(node, parent);
190: }
191: );
192:
193: addDisplayType('foil',
194: (x.Element ref) {
195: if (ref.getAttribute('type') == 'radiobuttonresponse--foil')
196: return new RadioFoil.fromRef(ref);
197: else if (ref.getAttribute('type') == 'optionresponse--foil')
198: return new OptionFoil.fromRef(ref);
1.6 damieng 199: else if (ref.getAttribute('type') == 'matchresponse--foil')
200: return new MatchFoil.fromRef(ref);
1.1 damieng 201: else if (ref.getAttribute('type') == 'rankresponse--foil')
202: return new RankFoil.fromRef(ref);
203: return new LCDBlock.fromRef(ref);
204: },
205: (x.Node node, DaxeNode parent) {
206: if (parent is RadioFoilgroup)
207: return new RadioFoil.fromNode(node, parent);
1.19 damieng 208: else if (parent is OptionFoilgroup ||
209: (parent.parent != null && parent.parent is OptionFoilgroup))
1.1 damieng 210: return new OptionFoil.fromNode(node, parent);
1.19 damieng 211: else if (parent is MatchFoilgroup ||
212: (parent.parent != null && parent.parent is MatchFoilgroup))
1.6 damieng 213: return new MatchFoil.fromNode(node, parent);
1.1 damieng 214: else if (parent is RankFoilgroup)
215: return new RankFoil.fromNode(node, parent);
216: return new LCDBlock.fromNode(node, parent);
217: }
218: );
219:
1.6 damieng 220: addDisplayType('itemgroup',
221: (x.Element ref) => new Itemgroup.fromRef(ref),
222: (x.Node node, DaxeNode parent) => new Itemgroup.fromNode(node, parent)
223: );
224:
225: addDisplayType('matchitem',
226: (x.Element ref) => new MatchItem.fromRef(ref),
227: (x.Node node, DaxeNode parent) => new MatchItem.fromNode(node, parent)
228: );
229:
1.5 damieng 230: addDisplayType('textline',
231: (x.Element ref) => new Textline.fromRef(ref),
232: (x.Node node, DaxeNode parent) => new Textline.fromNode(node, parent)
233: );
234:
1.11 damieng 235: addDisplayType('textfield',
236: (x.Element ref) => new Textfield.fromRef(ref),
237: (x.Node node, DaxeNode parent) => new Textfield.fromNode(node, parent)
238: );
239:
1.1 damieng 240: addDisplayType('hintgroup',
241: (x.Element ref) => new Hintgroup.fromRef(ref),
242: (x.Node node, DaxeNode parent) => new Hintgroup.fromNode(node, parent)
243: );
244:
1.10 damieng 245: addDisplayType('answer',
246: (x.Element ref) {
247: if (ref.getAttribute('type') == 'mathresponse--answer')
248: return new MathAnswer.fromRef(ref);
249: return new LCDBlock.fromRef(ref);
250: },
251: (x.Node node, DaxeNode parent) {
252: if (parent is MathResponse)
253: return new MathAnswer.fromNode(node, parent);
254: return new LCDBlock.fromNode(node, parent);
255: }
256: );
257:
1.7 damieng 258: addDisplayType('gnuplot',
259: (x.Element ref) => new Gnuplot.fromRef(ref),
260: (x.Node node, DaxeNode parent) => new Gnuplot.fromNode(node, parent)
261: );
262:
1.22 damieng 263: addDisplayType('organicstructure',
264: (x.Element ref) => new OrganicStructure.fromRef(ref),
265: (x.Node node, DaxeNode parent) => new OrganicStructure.fromNode(node, parent)
266: );
267:
1.7 damieng 268: addDisplayType('fakeattribute',
269: (x.Element ref) {
270: if (doc.cfg.elementName(ref) != 'title' || ref.getAttribute('type') == 'lonplot--title')
271: return new FakeAttributeElement.fromRef(ref);
272: return new LCDBlock.fromRef(ref);
273: },
274: (x.Node node, DaxeNode parent) {
275: if (parent is Gnuplot || parent is PlotCurve)
276: return new FakeAttributeElement.fromNode(node, parent);
277: return new LCDBlock.fromNode(node, parent);
278: }
279: );
280:
281: addDisplayType('plotaxis',
282: (x.Element ref) => new PlotAxis.fromRef(ref),
283: (x.Node node, DaxeNode parent) => new PlotAxis.fromNode(node, parent)
284: );
285:
286: addDisplayType('plotcurve',
287: (x.Element ref) => new PlotCurve.fromRef(ref),
288: (x.Node node, DaxeNode parent) => new PlotCurve.fromNode(node, parent)
289: );
290:
1.17 damieng 291: addDisplayType('section',
292: (x.Element ref) => new Section.fromRef(ref),
293: (x.Node node, DaxeNode parent) => new Section.fromNode(node, parent)
294: );
295:
296: addDisplayType('h1',
297: (x.Element ref) => new H1.fromRef(ref),
298: (x.Node node, DaxeNode parent) => new H1.fromNode(node, parent)
299: );
300:
1.1 damieng 301: Future.wait([Strings.load(), LCDStrings.load(), _readTemplates('templates.xml')]).then((List responses) {
1.16 damieng 302: initDaxe().then((v) {
1.1 damieng 303: // add things to the toolbar
304: ToolbarMenu sectionMenu = _makeSectionMenu();
305: if (sectionMenu != null)
306: page.toolbar.add(sectionMenu);
1.24 ! damieng 307: ToolbarBox mathBox;
! 308: if (doc.saveURL != null)
! 309: mathBox = page.toolbar.items[3];
! 310: else
! 311: mathBox = page.toolbar.items[2];
1.1 damieng 312: x.Element texRef = doc.cfg.elementReference('m');
313: if (texRef != null) {
314: ToolbarButton texButton = new ToolbarButton(
1.24 ! damieng 315: LCDStrings.get('insert_m'), 'images/tex.png',
1.1 damieng 316: () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate,
317: data:new ToolbarStyleInfo([texRef], null, null));
1.23 damieng 318: mathBox.add(texButton);
1.1 damieng 319: }
1.24 ! damieng 320: x.Element lmRef = doc.cfg.elementReference('lm');
! 321: if (lmRef != null) {
! 322: ToolbarButton lmButton = new ToolbarButton(
! 323: LCDStrings.get('insert_lm'), 'packages/daxe/images/toolbar/equation.png',
! 324: () => doc.insertNewNode(lmRef, 'element'), Toolbar.insertButtonUpdate,
! 325: data:new ToolbarStyleInfo([lmRef], null, null));
! 326: mathBox.add(lmButton);
! 327: }
1.1 damieng 328: h.Element tbh = h.querySelector('.toolbar');
329: tbh.replaceWith(page.toolbar.html());
330: page.adjustPositionsUnderToolbar();
331: page.updateAfterPathChange();
332: // add things to the menubar
333: if (responses[2] is x.Document) {
334: // at this point the menubar html is already in the document, so we have to fix the HTML
335: h.Element menubarDiv = h.document.getElementsByClassName('menubar')[0];
1.4 damieng 336: if (doc.filePath != null && doc.filePath.indexOf('&url=') != -1) { // otherwise we are not on LON-CAPA
1.1 damieng 337: MenuItem item = new MenuItem(Strings.get('menu.save'), () => save(), shortcut: 'S');
338: Menu fileMenu = page.mbar.menus[0];
339: fileMenu.add(item);
340: menubarDiv.firstChild.replaceWith(page.mbar.createMenuDiv(fileMenu));
1.21 damieng 341: h.window.onBeforeUnload.listen((h.BeforeUnloadEvent e) {
342: if (doc.changed()) {
343: String message = Strings.get('save.document_not_saved');
344: e.returnValue = message;
345: return message;
346: }
347: });
1.1 damieng 348: }
349: Menu m = _makeTemplatesMenu(responses[2]);
350: page.mbar.add(m);
351: menubarDiv.append(page.mbar.createMenuDiv(m));
352: page.updateAfterPathChange();
353: } else
354: print("Error reading templates file, could not build the menu.");
1.15 damieng 355: }).catchError((e) {
356: h.document.body.appendText('Initialization error: ' + e);
1.1 damieng 357: });
1.15 damieng 358: }).catchError((e) {
1.20 damieng 359: h.document.body.appendText('Initialization error: ' + (e is String ? e : e.toString()));
1.1 damieng 360: });
361: }
362:
363: void save() {
364: saveOnLONCAPA().then((_) {
365: h.window.alert(Strings.get('save.success'));
366: }, onError: (DaxeException ex) {
367: h.window.alert(Strings.get('save.error') + ': ' + ex.message);
368: });
369: }
370:
371: /**
372: * Send the document with a POST request to LON-CAPA.
1.21 damieng 373: * Note: is this needed ? It depends on the server implementation...
1.1 damieng 374: */
375: Future saveOnLONCAPA() {
376: int ind = doc.filePath.indexOf('&url=');
377: if (ind == -1)
378: return(new Future.error(new DaxeException('bad URL')));
379: String path = doc.filePath.substring(ind+5);
380: path = Uri.decodeQueryComponent(path);
381: ind = path.lastIndexOf('/');
382: String filename;
383: if (ind == -1)
384: filename = path;
385: else {
386: filename = path.substring(ind+1);
387: path = path.substring(0, ind+1);
388: }
389: Completer completer = new Completer();
390: String bound = 'AaB03x';
391: h.HttpRequest request = new h.HttpRequest();
392: request.onLoad.listen((h.ProgressEvent event) {
1.21 damieng 393: // TODO: check for something, status is sometimes wrongly OK
394: if (doc.undoPosition >= 0)
395: doc.lastSavedEdit = doc.edits[doc.undoPosition];
396: else
397: doc.lastSavedEdit = null;
398: completer.complete();
1.1 damieng 399: });
400: request.onError.listen((h.ProgressEvent event) {
401: completer.completeError(new DaxeException(request.status.toString()));
402: });
403: request.open('POST', '/upload_file');
404: request.setRequestHeader('Content-Type', "multipart/form-data; boundary=$bound");
405:
406: StringBuffer sb = new StringBuffer();
407: sb.write("--$bound\r\n");
408: sb.write('Content-Disposition: form-data; name="uploads_path"\r\n');
409: sb.write('Content-type: text/plain; charset=UTF-8\r\n');
410: sb.write('Content-transfer-encoding: 8bit\r\n\r\n');
411: sb.write(path);
412: sb.write("\r\n--$bound\r\n");
413: sb.write('Content-Disposition: form-data; name="uploads"; filename="$filename"\r\n');
414: sb.write('Content-Type: application/octet-stream\r\n\r\n');
415: doc.dndoc.xmlEncoding = 'UTF-8'; // the document is forced to use UTF-8
416: sb.write(doc.toString());
417: sb.write('\r\n--$bound--\r\n\r\n');
418: request.send(sb.toString());
419: return(completer.future);
420: }
421:
422: ToolbarMenu _makeSectionMenu() {
1.18 damieng 423: // sort section roles by title
424: knownSectionRoles.sort((String role1, String role2) => LCDStrings.get(role1).compareTo(LCDStrings.get(role2)));
425:
1.1 damieng 426: Menu menu = new Menu(LCDStrings.get('Section'));
427: List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
428: if (sectionRefs == null || sectionRefs.length == 0)
429: return(null);
430: x.Element h1Ref = doc.cfg.elementReference('h1');
1.18 damieng 431: for (String role in knownSectionRoles) {
1.1 damieng 432: MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
433: data:new ToolbarStyleInfo(sectionRefs, null, null));
434: menuItem.action = () {
435: ToolbarStyleInfo info = menuItem.data;
436: x.Element sectionRef = info.validRef;
1.18 damieng 437: Section section = new Section.fromRef(sectionRef);
1.1 damieng 438: section.state = 1;
439: section.setAttribute('class', 'role-' + role);
1.17 damieng 440: H1 h1 = NodeFactory.create(h1Ref);
1.1 damieng 441: h1.state = 1;
1.18 damieng 442: h1.appendChild(new DNText(LCDStrings.get(role)));
1.9 damieng 443: section.appendChild(h1);
444: x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
1.18 damieng 445: DNHiddenP p = new DNHiddenP.fromRef(hiddenp);
1.9 damieng 446: section.appendChild(p);
1.17 damieng 447: section.setupSimpleUI();
1.1 damieng 448: if (doc.insert2(section, page.getSelectionStart())) {
1.18 damieng 449: page.cursor.moveTo(new Position(p, 0));
1.1 damieng 450: page.updateAfterPathChange();
451: }
452: };
453: menu.add(menuItem);
454: }
455: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
456: return(tbmenu);
457: }
458:
459: Future<x.Document> _readTemplates(String templatesPath) {
460: x.DOMParser dp = new x.DOMParser();
461: return(dp.parseFromURL(templatesPath));
462: }
463:
464: Menu _makeTemplatesMenu(x.Document templatesDoc) {
465: Menu menu = new Menu(LCDStrings.get('Templates'));
466: x.Element templates = templatesDoc.documentElement;
467: for (x.Node child in templates.childNodes) {
468: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
469: menu.add(_makeMenu(child));
470: }
471: }
472: return(menu);
473: }
474:
475: Menu _makeMenu(x.Element el) {
476: String locale = LCDStrings.systemLocale;
477: String defaultLocale = LCDStrings.defaultLocale;
478: String title;
479: for (x.Node child in el.childNodes) {
480: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
481: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
482: if ((child as x.Element).getAttribute('lang') == locale) {
483: title = child.firstChild.nodeValue;
484: break;
485: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
486: title = child.firstChild.nodeValue;
487: }
488: }
489: }
490: }
491: if (title == null)
492: title = '?';
493: Menu menu = new Menu(title);
494: for (x.Node child in el.childNodes) {
495: if (child.nodeType == x.Node.ELEMENT_NODE) {
496: if (child.nodeName == 'menu') {
497: menu.add(_makeMenu(child));
498: } else if (child.nodeName == 'item') {
499: menu.add(_makeItem(child));
500: }
501: }
502: }
503: return(menu);
504: }
505:
506: MenuItem _makeItem(x.Element item) {
507: String locale = LCDStrings.systemLocale;
508: String defaultLocale = LCDStrings.defaultLocale;
509: String path, type, title, help;
510: for (x.Node child in item.childNodes) {
511: if (child.nodeType == x.Node.ELEMENT_NODE) {
512: if (child.nodeName == 'title') {
513: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
514: if ((child as x.Element).getAttribute('lang') == locale) {
515: title = child.firstChild.nodeValue;
516: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
517: title = child.firstChild.nodeValue;
518: }
519: }
520: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
521: path = child.firstChild.nodeValue;
522: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
523: type = child.firstChild.nodeValue;
524: } else if (child.nodeName == 'help') {
525: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
526: if ((child as x.Element).getAttribute('lang') == locale) {
527: help = child.firstChild.nodeValue;
528: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
529: help = child.firstChild.nodeValue;
530: }
531: }
532: }
533: }
534: }
535: if (type == null) {
536: print("Warning: missing type for template $title\n");
537: type = 'problem';
538: }
539: x.Element refElement = doc.cfg.elementReference(type);
540: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
541: if (help != null)
542: menuItem.toolTipText = help;
543: return menuItem;
544: }
545:
546: void _insertTemplate(String filePath) {
547: try {
548: x.DOMParser dp = new x.DOMParser();
549: dp.parseFromURL(filePath).then((x.Document templateDoc) {
550: x.Element root = templateDoc.documentElement;
551: if (root == null)
552: return;
553: doc.removeWhitespace(root);
554: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
555: UndoableEdit edit;
556: Position pos = page.getSelectionStart();
557: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
558: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12 damieng 559: else {
560: DaxeNode parent = pos.dn;
561: if (parent is DNText)
562: parent = parent.parent;
563: if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
564: // The node must be inserted outside of the paragraph.
565: // If there is something in the paragraph to the right of the cursor, it must be
566: // moved into a new paragraph after the inserted node.
567: // (as in DaxeDocument.insert2)
568: DNHiddenP p = parent;
569: edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
570: Position pend = new Position(p, p.offsetLength);
571: pend.moveInsideTextNodeIfPossible();
572: if (pos < pend) {
573: DaxeNode clone = doc.cloneBetween(pos, pend);
574: edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
575: DNHiddenP newp = NodeFactory.create(p.ref);
576: edit.addSubEdit(new UndoableEdit.insertNode(
577: new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
578: edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
579: }
580: edit.addSubEdit(new UndoableEdit.insertNode(
581: new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
582: } else
583: edit = new UndoableEdit.insertNode(pos, dnRoot);
584: }
1.1 damieng 585: doc.doNewEdit(edit);
586: page.updateAfterPathChange();
1.8 damieng 587: if (dnRoot.nodeName != 'loncapa')
588: page.scrollToNode(dnRoot);
1.1 damieng 589: });
590: } on x.DOMException catch(ex) {
591: h.window.alert(ex.toString());
592: }
593: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>