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