Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.22
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);
307: x.Element texRef = doc.cfg.elementReference('m');
308: if (texRef != null) {
309: ToolbarBox insertBox = new ToolbarBox();
310: ToolbarButton texButton = new ToolbarButton(
311: LCDStrings.get('tex_equation'), 'images/tex.png',
312: () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate,
313: data:new ToolbarStyleInfo([texRef], null, null));
314: insertBox.add(texButton);
315: page.toolbar.add(insertBox);
316: }
317: h.Element tbh = h.querySelector('.toolbar');
318: tbh.replaceWith(page.toolbar.html());
319: page.adjustPositionsUnderToolbar();
320: page.updateAfterPathChange();
321: // add things to the menubar
322: if (responses[2] is x.Document) {
323: // at this point the menubar html is already in the document, so we have to fix the HTML
324: h.Element menubarDiv = h.document.getElementsByClassName('menubar')[0];
1.4 damieng 325: if (doc.filePath != null && doc.filePath.indexOf('&url=') != -1) { // otherwise we are not on LON-CAPA
1.1 damieng 326: MenuItem item = new MenuItem(Strings.get('menu.save'), () => save(), shortcut: 'S');
327: Menu fileMenu = page.mbar.menus[0];
328: fileMenu.add(item);
329: menubarDiv.firstChild.replaceWith(page.mbar.createMenuDiv(fileMenu));
1.21 damieng 330: h.window.onBeforeUnload.listen((h.BeforeUnloadEvent e) {
331: if (doc.changed()) {
332: String message = Strings.get('save.document_not_saved');
333: e.returnValue = message;
334: return message;
335: }
336: });
1.1 damieng 337: }
338: Menu m = _makeTemplatesMenu(responses[2]);
339: page.mbar.add(m);
340: menubarDiv.append(page.mbar.createMenuDiv(m));
341: page.updateAfterPathChange();
342: } else
343: print("Error reading templates file, could not build the menu.");
1.15 damieng 344: }).catchError((e) {
345: h.document.body.appendText('Initialization error: ' + e);
1.1 damieng 346: });
1.15 damieng 347: }).catchError((e) {
1.20 damieng 348: h.document.body.appendText('Initialization error: ' + (e is String ? e : e.toString()));
1.1 damieng 349: });
350: }
351:
352: void save() {
353: saveOnLONCAPA().then((_) {
354: h.window.alert(Strings.get('save.success'));
355: }, onError: (DaxeException ex) {
356: h.window.alert(Strings.get('save.error') + ': ' + ex.message);
357: });
358: }
359:
360: /**
361: * Send the document with a POST request to LON-CAPA.
1.21 damieng 362: * Note: is this needed ? It depends on the server implementation...
1.1 damieng 363: */
364: Future saveOnLONCAPA() {
365: int ind = doc.filePath.indexOf('&url=');
366: if (ind == -1)
367: return(new Future.error(new DaxeException('bad URL')));
368: String path = doc.filePath.substring(ind+5);
369: path = Uri.decodeQueryComponent(path);
370: ind = path.lastIndexOf('/');
371: String filename;
372: if (ind == -1)
373: filename = path;
374: else {
375: filename = path.substring(ind+1);
376: path = path.substring(0, ind+1);
377: }
378: Completer completer = new Completer();
379: String bound = 'AaB03x';
380: h.HttpRequest request = new h.HttpRequest();
381: request.onLoad.listen((h.ProgressEvent event) {
1.21 damieng 382: // TODO: check for something, status is sometimes wrongly OK
383: if (doc.undoPosition >= 0)
384: doc.lastSavedEdit = doc.edits[doc.undoPosition];
385: else
386: doc.lastSavedEdit = null;
387: completer.complete();
1.1 damieng 388: });
389: request.onError.listen((h.ProgressEvent event) {
390: completer.completeError(new DaxeException(request.status.toString()));
391: });
392: request.open('POST', '/upload_file');
393: request.setRequestHeader('Content-Type', "multipart/form-data; boundary=$bound");
394:
395: StringBuffer sb = new StringBuffer();
396: sb.write("--$bound\r\n");
397: sb.write('Content-Disposition: form-data; name="uploads_path"\r\n');
398: sb.write('Content-type: text/plain; charset=UTF-8\r\n');
399: sb.write('Content-transfer-encoding: 8bit\r\n\r\n');
400: sb.write(path);
401: sb.write("\r\n--$bound\r\n");
402: sb.write('Content-Disposition: form-data; name="uploads"; filename="$filename"\r\n');
403: sb.write('Content-Type: application/octet-stream\r\n\r\n');
404: doc.dndoc.xmlEncoding = 'UTF-8'; // the document is forced to use UTF-8
405: sb.write(doc.toString());
406: sb.write('\r\n--$bound--\r\n\r\n');
407: request.send(sb.toString());
408: return(completer.future);
409: }
410:
411: ToolbarMenu _makeSectionMenu() {
1.18 damieng 412: // sort section roles by title
413: knownSectionRoles.sort((String role1, String role2) => LCDStrings.get(role1).compareTo(LCDStrings.get(role2)));
414:
1.1 damieng 415: Menu menu = new Menu(LCDStrings.get('Section'));
416: List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
417: if (sectionRefs == null || sectionRefs.length == 0)
418: return(null);
419: x.Element h1Ref = doc.cfg.elementReference('h1');
1.18 damieng 420: for (String role in knownSectionRoles) {
1.1 damieng 421: MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
422: data:new ToolbarStyleInfo(sectionRefs, null, null));
423: menuItem.action = () {
424: ToolbarStyleInfo info = menuItem.data;
425: x.Element sectionRef = info.validRef;
1.18 damieng 426: Section section = new Section.fromRef(sectionRef);
1.1 damieng 427: section.state = 1;
428: section.setAttribute('class', 'role-' + role);
1.17 damieng 429: H1 h1 = NodeFactory.create(h1Ref);
1.1 damieng 430: h1.state = 1;
1.18 damieng 431: h1.appendChild(new DNText(LCDStrings.get(role)));
1.9 damieng 432: section.appendChild(h1);
433: x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
1.18 damieng 434: DNHiddenP p = new DNHiddenP.fromRef(hiddenp);
1.9 damieng 435: section.appendChild(p);
1.17 damieng 436: section.setupSimpleUI();
1.1 damieng 437: if (doc.insert2(section, page.getSelectionStart())) {
1.18 damieng 438: page.cursor.moveTo(new Position(p, 0));
1.1 damieng 439: page.updateAfterPathChange();
440: }
441: };
442: menu.add(menuItem);
443: }
444: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
445: return(tbmenu);
446: }
447:
448: Future<x.Document> _readTemplates(String templatesPath) {
449: x.DOMParser dp = new x.DOMParser();
450: return(dp.parseFromURL(templatesPath));
451: }
452:
453: Menu _makeTemplatesMenu(x.Document templatesDoc) {
454: Menu menu = new Menu(LCDStrings.get('Templates'));
455: x.Element templates = templatesDoc.documentElement;
456: for (x.Node child in templates.childNodes) {
457: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
458: menu.add(_makeMenu(child));
459: }
460: }
461: return(menu);
462: }
463:
464: Menu _makeMenu(x.Element el) {
465: String locale = LCDStrings.systemLocale;
466: String defaultLocale = LCDStrings.defaultLocale;
467: String title;
468: for (x.Node child in el.childNodes) {
469: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
470: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
471: if ((child as x.Element).getAttribute('lang') == locale) {
472: title = child.firstChild.nodeValue;
473: break;
474: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
475: title = child.firstChild.nodeValue;
476: }
477: }
478: }
479: }
480: if (title == null)
481: title = '?';
482: Menu menu = new Menu(title);
483: for (x.Node child in el.childNodes) {
484: if (child.nodeType == x.Node.ELEMENT_NODE) {
485: if (child.nodeName == 'menu') {
486: menu.add(_makeMenu(child));
487: } else if (child.nodeName == 'item') {
488: menu.add(_makeItem(child));
489: }
490: }
491: }
492: return(menu);
493: }
494:
495: MenuItem _makeItem(x.Element item) {
496: String locale = LCDStrings.systemLocale;
497: String defaultLocale = LCDStrings.defaultLocale;
498: String path, type, title, help;
499: for (x.Node child in item.childNodes) {
500: if (child.nodeType == x.Node.ELEMENT_NODE) {
501: if (child.nodeName == 'title') {
502: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
503: if ((child as x.Element).getAttribute('lang') == locale) {
504: title = child.firstChild.nodeValue;
505: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
506: title = child.firstChild.nodeValue;
507: }
508: }
509: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
510: path = child.firstChild.nodeValue;
511: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
512: type = child.firstChild.nodeValue;
513: } else if (child.nodeName == 'help') {
514: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
515: if ((child as x.Element).getAttribute('lang') == locale) {
516: help = child.firstChild.nodeValue;
517: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
518: help = child.firstChild.nodeValue;
519: }
520: }
521: }
522: }
523: }
524: if (type == null) {
525: print("Warning: missing type for template $title\n");
526: type = 'problem';
527: }
528: x.Element refElement = doc.cfg.elementReference(type);
529: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
530: if (help != null)
531: menuItem.toolTipText = help;
532: return menuItem;
533: }
534:
535: void _insertTemplate(String filePath) {
536: try {
537: x.DOMParser dp = new x.DOMParser();
538: dp.parseFromURL(filePath).then((x.Document templateDoc) {
539: x.Element root = templateDoc.documentElement;
540: if (root == null)
541: return;
542: doc.removeWhitespace(root);
543: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
544: UndoableEdit edit;
545: Position pos = page.getSelectionStart();
546: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
547: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12 damieng 548: else {
549: DaxeNode parent = pos.dn;
550: if (parent is DNText)
551: parent = parent.parent;
552: if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
553: // The node must be inserted outside of the paragraph.
554: // If there is something in the paragraph to the right of the cursor, it must be
555: // moved into a new paragraph after the inserted node.
556: // (as in DaxeDocument.insert2)
557: DNHiddenP p = parent;
558: edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
559: Position pend = new Position(p, p.offsetLength);
560: pend.moveInsideTextNodeIfPossible();
561: if (pos < pend) {
562: DaxeNode clone = doc.cloneBetween(pos, pend);
563: edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
564: DNHiddenP newp = NodeFactory.create(p.ref);
565: edit.addSubEdit(new UndoableEdit.insertNode(
566: new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
567: edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
568: }
569: edit.addSubEdit(new UndoableEdit.insertNode(
570: new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
571: } else
572: edit = new UndoableEdit.insertNode(pos, dnRoot);
573: }
1.1 damieng 574: doc.doNewEdit(edit);
575: page.updateAfterPathChange();
1.8 damieng 576: if (dnRoot.nodeName != 'loncapa')
577: page.scrollToNode(dnRoot);
1.1 damieng 578: });
579: } on x.DOMException catch(ex) {
580: h.window.alert(ex.toString());
581: }
582: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>