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