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