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