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