Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.33
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));
1.33 ! damieng 388: Menu helpMenu = _makeHelpMenu();
! 389: page.mbar.add(helpMenu);
! 390: menubarDiv.append(page.mbar.createMenuDiv(helpMenu));
1.1 damieng 391: page.updateAfterPathChange();
392: } else
393: print("Error reading templates file, could not build the menu.");
1.15 damieng 394: }).catchError((e) {
1.26 damieng 395: h.document.body.appendText('Initialization error: ' + (e is String ? e : e.toString()));
1.1 damieng 396: });
1.15 damieng 397: }).catchError((e) {
1.20 damieng 398: h.document.body.appendText('Initialization error: ' + (e is String ? e : e.toString()));
1.1 damieng 399: });
400: }
401:
402: ToolbarMenu _makeSectionMenu() {
1.18 damieng 403: // sort section roles by title
404: knownSectionRoles.sort((String role1, String role2) => LCDStrings.get(role1).compareTo(LCDStrings.get(role2)));
405:
1.1 damieng 406: Menu menu = new Menu(LCDStrings.get('Section'));
407: List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
408: if (sectionRefs == null || sectionRefs.length == 0)
409: return(null);
410: x.Element h1Ref = doc.cfg.elementReference('h1');
1.18 damieng 411: for (String role in knownSectionRoles) {
1.1 damieng 412: MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
413: data:new ToolbarStyleInfo(sectionRefs, null, null));
414: menuItem.action = () {
415: ToolbarStyleInfo info = menuItem.data;
416: x.Element sectionRef = info.validRef;
1.18 damieng 417: Section section = new Section.fromRef(sectionRef);
1.1 damieng 418: section.state = 1;
419: section.setAttribute('class', 'role-' + role);
1.17 damieng 420: H1 h1 = NodeFactory.create(h1Ref);
1.1 damieng 421: h1.state = 1;
1.18 damieng 422: h1.appendChild(new DNText(LCDStrings.get(role)));
1.9 damieng 423: section.appendChild(h1);
424: x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
1.18 damieng 425: DNHiddenP p = new DNHiddenP.fromRef(hiddenp);
1.9 damieng 426: section.appendChild(p);
1.17 damieng 427: section.setupSimpleUI();
1.1 damieng 428: if (doc.insert2(section, page.getSelectionStart())) {
1.18 damieng 429: page.cursor.moveTo(new Position(p, 0));
1.1 damieng 430: page.updateAfterPathChange();
431: }
432: };
433: menu.add(menuItem);
434: }
435: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
436: return(tbmenu);
437: }
438:
439: Future<x.Document> _readTemplates(String templatesPath) {
440: x.DOMParser dp = new x.DOMParser();
441: return(dp.parseFromURL(templatesPath));
442: }
443:
444: Menu _makeTemplatesMenu(x.Document templatesDoc) {
1.26 damieng 445: List<x.Element> problemRefs = doc.cfg.elementReferences('problem');
446: if (problemRefs == null || problemRefs.length == 0)
447: return(null);
1.1 damieng 448: Menu menu = new Menu(LCDStrings.get('Templates'));
449: x.Element templates = templatesDoc.documentElement;
450: for (x.Node child in templates.childNodes) {
451: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
452: menu.add(_makeMenu(child));
453: }
454: }
455: return(menu);
456: }
457:
458: Menu _makeMenu(x.Element el) {
459: String locale = LCDStrings.systemLocale;
460: String defaultLocale = LCDStrings.defaultLocale;
461: String title;
462: for (x.Node child in el.childNodes) {
463: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
464: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
465: if ((child as x.Element).getAttribute('lang') == locale) {
466: title = child.firstChild.nodeValue;
467: break;
468: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
469: title = child.firstChild.nodeValue;
470: }
471: }
472: }
473: }
474: if (title == null)
475: title = '?';
476: Menu menu = new Menu(title);
477: for (x.Node child in el.childNodes) {
478: if (child.nodeType == x.Node.ELEMENT_NODE) {
479: if (child.nodeName == 'menu') {
480: menu.add(_makeMenu(child));
481: } else if (child.nodeName == 'item') {
482: menu.add(_makeItem(child));
483: }
484: }
485: }
486: return(menu);
487: }
488:
489: MenuItem _makeItem(x.Element item) {
490: String locale = LCDStrings.systemLocale;
491: String defaultLocale = LCDStrings.defaultLocale;
492: String path, type, title, help;
493: for (x.Node child in item.childNodes) {
494: if (child.nodeType == x.Node.ELEMENT_NODE) {
495: if (child.nodeName == 'title') {
496: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
497: if ((child as x.Element).getAttribute('lang') == locale) {
498: title = child.firstChild.nodeValue;
499: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
500: title = child.firstChild.nodeValue;
501: }
502: }
503: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
504: path = child.firstChild.nodeValue;
505: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
506: type = child.firstChild.nodeValue;
507: } else if (child.nodeName == 'help') {
508: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
509: if ((child as x.Element).getAttribute('lang') == locale) {
510: help = child.firstChild.nodeValue;
511: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
512: help = child.firstChild.nodeValue;
513: }
514: }
515: }
516: }
517: }
518: if (type == null) {
519: print("Warning: missing type for template $title\n");
520: type = 'problem';
521: }
522: x.Element refElement = doc.cfg.elementReference(type);
523: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
524: if (help != null)
525: menuItem.toolTipText = help;
526: return menuItem;
527: }
528:
529: void _insertTemplate(String filePath) {
530: try {
531: x.DOMParser dp = new x.DOMParser();
532: dp.parseFromURL(filePath).then((x.Document templateDoc) {
533: x.Element root = templateDoc.documentElement;
534: if (root == null)
535: return;
536: doc.removeWhitespace(root);
537: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
1.27 damieng 538: if (dnRoot is LCDBlock && dnRoot.attributes.length > 0)
539: dnRoot.state = 0; // make attributes editable for the template root
1.1 damieng 540: UndoableEdit edit;
541: Position pos = page.getSelectionStart();
542: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
543: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12 damieng 544: else {
545: DaxeNode parent = pos.dn;
546: if (parent is DNText)
547: parent = parent.parent;
548: if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
549: // The node must be inserted outside of the paragraph.
550: // If there is something in the paragraph to the right of the cursor, it must be
551: // moved into a new paragraph after the inserted node.
552: // (as in DaxeDocument.insert2)
553: DNHiddenP p = parent;
554: edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
555: Position pend = new Position(p, p.offsetLength);
556: pend.moveInsideTextNodeIfPossible();
557: if (pos < pend) {
558: DaxeNode clone = doc.cloneBetween(pos, pend);
559: edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
560: DNHiddenP newp = NodeFactory.create(p.ref);
561: edit.addSubEdit(new UndoableEdit.insertNode(
562: new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
563: edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
564: }
565: edit.addSubEdit(new UndoableEdit.insertNode(
566: new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
567: } else
568: edit = new UndoableEdit.insertNode(pos, dnRoot);
569: }
1.1 damieng 570: doc.doNewEdit(edit);
571: page.updateAfterPathChange();
1.8 damieng 572: if (dnRoot.nodeName != 'loncapa')
573: page.scrollToNode(dnRoot);
1.1 damieng 574: });
575: } on x.DOMException catch(ex) {
576: h.window.alert(ex.toString());
577: }
578: }
1.33 ! damieng 579:
! 580: Menu _makeHelpMenu() {
! 581: Menu menu = new Menu(LCDStrings.get('help'));
! 582: MenuItem menuItem = new MenuItem(LCDStrings.get('getting_started'),
! 583: () => h.window.open('tutorial/getting_started.xhtml', '_blank'));
! 584: menu.add(menuItem);
! 585: return(menu);
! 586: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>