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