Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.16

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';
                     65: part 'nodes/fake_attribute_element.dart';
                     66: part 'nodes/plot_axis.dart';
                     67: part 'nodes/plot_curve.dart';
1.1       damieng    68: part 'lcd_button.dart';
                     69: 
                     70: 
                     71: void main() {
                     72:   NodeFactory.addCoreDisplayTypes();
                     73:   
                     74:   addDisplayType('lcdblock',
                     75:         (x.Element ref) => new LCDBlock.fromRef(ref),
                     76:         (x.Node node, DaxeNode parent) => new LCDBlock.fromNode(node, parent)
                     77:     );
                     78:   
                     79:   addDisplayType('texmathjax',
                     80:         (x.Element ref) => new TeXMathJax.fromRef(ref),
                     81:         (x.Node node, DaxeNode parent) => new TeXMathJax.fromNode(node, parent)
                     82:     );
                     83:   
                     84:   addDisplayType('lm',
                     85:         (x.Element ref) => new Lm.fromRef(ref),
                     86:         (x.Node node, DaxeNode parent) => new Lm.fromNode(node, parent)
                     87:     );
                     88:   
1.14      damieng    89:   addDisplayType('chem',
                     90:         (x.Element ref) => new Chem.fromRef(ref),
                     91:         (x.Node node, DaxeNode parent) => new Chem.fromNode(node, parent)
                     92:     );
                     93:   
1.1       damieng    94:   addDisplayType('script',
                     95:         (x.Element ref) => new ScriptBlock.fromRef(ref),
                     96:         (x.Node node, DaxeNode parent) => new ScriptBlock.fromNode(node, parent)
                     97:     );
                     98:   
                     99:   addDisplayType('parameter',
                    100:         (x.Element ref) => new LCDParameter.fromRef(ref),
                    101:         (x.Node node, DaxeNode parent) => new LCDParameter.fromNode(node, parent)
                    102:     );
                    103:   
1.2       damieng   104:   addDisplayType('numericalresponse',
                    105:         (x.Element ref) => new NumericalResponse.fromRef(ref),
                    106:         (x.Node node, DaxeNode parent) => new NumericalResponse.fromNode(node, parent)
                    107:     );
                    108:   
1.3       damieng   109:   addDisplayType('formularesponse',
                    110:         (x.Element ref) => new FormulaResponse.fromRef(ref),
                    111:         (x.Node node, DaxeNode parent) => new FormulaResponse.fromNode(node, parent)
                    112:     );
                    113:   
                    114:   addDisplayType('stringresponse',
                    115:         (x.Element ref) => new StringResponse.fromRef(ref),
                    116:         (x.Node node, DaxeNode parent) => new StringResponse.fromNode(node, parent)
                    117:     );
                    118:   
1.11      damieng   119:   addDisplayType('essayresponse',
                    120:         (x.Element ref) => new EssayResponse.fromRef(ref),
                    121:         (x.Node node, DaxeNode parent) => new EssayResponse.fromNode(node, parent)
                    122:     );
                    123:   
1.1       damieng   124:   addDisplayType('radioresponse',
                    125:         (x.Element ref) => new RadioResponse.fromRef(ref),
                    126:         (x.Node node, DaxeNode parent) => new RadioResponse.fromNode(node, parent)
                    127:     );
                    128:   
                    129:   addDisplayType('optionresponse',
                    130:         (x.Element ref) => new OptionResponse.fromRef(ref),
                    131:         (x.Node node, DaxeNode parent) => new OptionResponse.fromNode(node, parent)
                    132:     );
                    133:   
1.6       damieng   134:   addDisplayType('matchresponse',
                    135:         (x.Element ref) => new MatchResponse.fromRef(ref),
                    136:         (x.Node node, DaxeNode parent) => new MatchResponse.fromNode(node, parent)
                    137:     );
                    138:   
1.1       damieng   139:   addDisplayType('rankresponse',
                    140:         (x.Element ref) => new RankResponse.fromRef(ref),
                    141:         (x.Node node, DaxeNode parent) => new RankResponse.fromNode(node, parent)
                    142:     );
                    143:   
1.10      damieng   144:   addDisplayType('mathresponse',
                    145:         (x.Element ref) => new MathResponse.fromRef(ref),
                    146:         (x.Node node, DaxeNode parent) => new MathResponse.fromNode(node, parent)
                    147:     );
                    148:   
1.13      damieng   149:   addDisplayType('organicresponse',
                    150:         (x.Element ref) => new OrganicResponse.fromRef(ref),
                    151:         (x.Node node, DaxeNode parent) => new OrganicResponse.fromNode(node, parent)
                    152:     );
                    153:   
                    154:   addDisplayType('reactionresponse',
                    155:         (x.Element ref) => new ReactionResponse.fromRef(ref),
                    156:         (x.Node node, DaxeNode parent) => new ReactionResponse.fromNode(node, parent)
                    157:     );
                    158:   
1.1       damieng   159:   addDisplayType('foilgroup',
                    160:         (x.Element ref) {
                    161:           if (ref.getAttribute('type') == 'radiobuttonresponse--foilgroup')
                    162:             return new RadioFoilgroup.fromRef(ref);
                    163:           else if (ref.getAttribute('type') == 'optionresponse--foilgroup')
                    164:             return new OptionFoilgroup.fromRef(ref);
1.6       damieng   165:           else if (ref.getAttribute('type') == 'matchresponse--foilgroup')
                    166:             return new MatchFoilgroup.fromRef(ref);
1.1       damieng   167:           else if (ref.getAttribute('type') == 'rankresponse--foilgroup')
                    168:             return new RankFoilgroup.fromRef(ref);
                    169:           return new LCDBlock.fromRef(ref);
                    170:         },
                    171:         (x.Node node, DaxeNode parent) {
                    172:           if (parent is RadioResponse)
                    173:             return new RadioFoilgroup.fromNode(node, parent);
                    174:           else if (parent is OptionResponse)
                    175:             return new OptionFoilgroup.fromNode(node, parent);
1.6       damieng   176:           else if (parent is MatchResponse)
                    177:             return new MatchFoilgroup.fromNode(node, parent);
1.1       damieng   178:           else if (parent is RankResponse)
                    179:             return new RankFoilgroup.fromNode(node, parent);
                    180:           return new LCDBlock.fromNode(node, parent);
                    181:         }
                    182:     );
                    183:   
                    184:   addDisplayType('foil',
                    185:         (x.Element ref) {
                    186:           if (ref.getAttribute('type') == 'radiobuttonresponse--foil')
                    187:             return new RadioFoil.fromRef(ref);
                    188:           else if (ref.getAttribute('type') == 'optionresponse--foil')
                    189:             return new OptionFoil.fromRef(ref);
1.6       damieng   190:           else if (ref.getAttribute('type') == 'matchresponse--foil')
                    191:             return new MatchFoil.fromRef(ref);
1.1       damieng   192:           else if (ref.getAttribute('type') == 'rankresponse--foil')
                    193:             return new RankFoil.fromRef(ref);
                    194:           return new LCDBlock.fromRef(ref);
                    195:         },
                    196:         (x.Node node, DaxeNode parent) {
                    197:           if (parent is RadioFoilgroup)
                    198:             return new RadioFoil.fromNode(node, parent);
                    199:           else if (parent is OptionFoilgroup)
                    200:             return new OptionFoil.fromNode(node, parent);
1.6       damieng   201:           else if (parent is MatchFoilgroup)
                    202:             return new MatchFoil.fromNode(node, parent);
1.1       damieng   203:           else if (parent is RankFoilgroup)
                    204:             return new RankFoil.fromNode(node, parent);
                    205:           return new LCDBlock.fromNode(node, parent);
                    206:         }
                    207:     );
                    208:   
1.6       damieng   209:   addDisplayType('itemgroup',
                    210:         (x.Element ref) => new Itemgroup.fromRef(ref),
                    211:         (x.Node node, DaxeNode parent) => new Itemgroup.fromNode(node, parent)
                    212:     );
                    213:   
                    214:   addDisplayType('matchitem',
                    215:         (x.Element ref) => new MatchItem.fromRef(ref),
                    216:         (x.Node node, DaxeNode parent) => new MatchItem.fromNode(node, parent)
                    217:     );
                    218:   
1.5       damieng   219:   addDisplayType('textline',
                    220:         (x.Element ref) => new Textline.fromRef(ref),
                    221:         (x.Node node, DaxeNode parent) => new Textline.fromNode(node, parent)
                    222:     );
                    223:   
1.11      damieng   224:   addDisplayType('textfield',
                    225:         (x.Element ref) => new Textfield.fromRef(ref),
                    226:         (x.Node node, DaxeNode parent) => new Textfield.fromNode(node, parent)
                    227:     );
                    228:   
1.1       damieng   229:   addDisplayType('hintgroup',
                    230:         (x.Element ref) => new Hintgroup.fromRef(ref),
                    231:         (x.Node node, DaxeNode parent) => new Hintgroup.fromNode(node, parent)
                    232:     );
                    233:   
1.10      damieng   234:   addDisplayType('answer',
                    235:         (x.Element ref) {
                    236:           if (ref.getAttribute('type') == 'mathresponse--answer')
                    237:             return new MathAnswer.fromRef(ref);
                    238:           return new LCDBlock.fromRef(ref);
                    239:         },
                    240:         (x.Node node, DaxeNode parent) {
                    241:           if (parent is MathResponse)
                    242:             return new MathAnswer.fromNode(node, parent);
                    243:           return new LCDBlock.fromNode(node, parent);
                    244:         }
                    245:     );
                    246:   
1.7       damieng   247:   addDisplayType('gnuplot',
                    248:         (x.Element ref) => new Gnuplot.fromRef(ref),
                    249:         (x.Node node, DaxeNode parent) => new Gnuplot.fromNode(node, parent)
                    250:     );
                    251:   
                    252:   addDisplayType('fakeattribute',
                    253:         (x.Element ref) {
                    254:           if (doc.cfg.elementName(ref) != 'title' || ref.getAttribute('type') == 'lonplot--title')
                    255:             return new FakeAttributeElement.fromRef(ref);
                    256:           return new LCDBlock.fromRef(ref);
                    257:         },
                    258:         (x.Node node, DaxeNode parent) {
                    259:           if (parent is Gnuplot || parent is PlotCurve)
                    260:             return new FakeAttributeElement.fromNode(node, parent);
                    261:           return new LCDBlock.fromNode(node, parent);
                    262:         }
                    263:     );
                    264:   
                    265:   addDisplayType('plotaxis',
                    266:         (x.Element ref) => new PlotAxis.fromRef(ref),
                    267:         (x.Node node, DaxeNode parent) => new PlotAxis.fromNode(node, parent)
                    268:     );
                    269:   
                    270:   addDisplayType('plotcurve',
                    271:         (x.Element ref) => new PlotCurve.fromRef(ref),
                    272:         (x.Node node, DaxeNode parent) => new PlotCurve.fromNode(node, parent)
                    273:     );
                    274:   
1.1       damieng   275:   Future.wait([Strings.load(), LCDStrings.load(), _readTemplates('templates.xml')]).then((List responses) {
1.16    ! damieng   276:     initDaxe().then((v) {
1.1       damieng   277:       // add things to the toolbar
                    278:       ToolbarMenu sectionMenu = _makeSectionMenu();
                    279:       if (sectionMenu != null)
                    280:         page.toolbar.add(sectionMenu);
                    281:       x.Element texRef = doc.cfg.elementReference('m');
                    282:       if (texRef != null) {
                    283:         ToolbarBox insertBox = new ToolbarBox();
                    284:         ToolbarButton texButton = new ToolbarButton(
                    285:             LCDStrings.get('tex_equation'), 'images/tex.png',
                    286:             () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate, 
                    287:             data:new ToolbarStyleInfo([texRef], null, null));
                    288:         insertBox.add(texButton);
                    289:         page.toolbar.add(insertBox);
                    290:       }
                    291:       h.Element tbh = h.querySelector('.toolbar');
                    292:       tbh.replaceWith(page.toolbar.html());
                    293:       page.adjustPositionsUnderToolbar();
                    294:       page.updateAfterPathChange();
                    295:       // add things to the menubar
                    296:       if (responses[2] is x.Document) {
                    297:         // at this point the menubar html is already in the document, so we have to fix the HTML
                    298:         h.Element menubarDiv = h.document.getElementsByClassName('menubar')[0];
1.4       damieng   299:         if (doc.filePath != null && doc.filePath.indexOf('&url=') != -1) { // otherwise we are not on LON-CAPA
1.1       damieng   300:           MenuItem item = new MenuItem(Strings.get('menu.save'), () => save(), shortcut: 'S');
                    301:           Menu fileMenu = page.mbar.menus[0];
                    302:           fileMenu.add(item);
                    303:           menubarDiv.firstChild.replaceWith(page.mbar.createMenuDiv(fileMenu));
                    304:         }
                    305:         Menu m = _makeTemplatesMenu(responses[2]);
                    306:         page.mbar.add(m);
                    307:         menubarDiv.append(page.mbar.createMenuDiv(m));
                    308:         page.updateAfterPathChange();
                    309:       } else
                    310:         print("Error reading templates file, could not build the menu.");
1.15      damieng   311:     }).catchError((e) {
                    312:       h.document.body.appendText('Initialization error: ' + e);
1.1       damieng   313:     });
1.15      damieng   314:   }).catchError((e) {
                    315:     h.document.body.appendText('Initialization error: ' + e);
1.1       damieng   316:   });
                    317: }
                    318: 
                    319: void save() {
                    320:   saveOnLONCAPA().then((_) {
                    321:     h.window.alert(Strings.get('save.success'));
                    322:   }, onError: (DaxeException ex) {
                    323:     h.window.alert(Strings.get('save.error') + ': ' + ex.message);
                    324:   });
                    325: }
                    326: 
                    327: /**
                    328:  * Send the document with a POST request to LON-CAPA.
                    329:  */
                    330: Future saveOnLONCAPA() {
                    331:   int ind = doc.filePath.indexOf('&url=');
                    332:   if (ind == -1)
                    333:     return(new Future.error(new DaxeException('bad URL')));
                    334:   String path = doc.filePath.substring(ind+5);
                    335:   path = Uri.decodeQueryComponent(path);
                    336:   ind = path.lastIndexOf('/');
                    337:   String filename;
                    338:   if (ind == -1)
                    339:     filename = path;
                    340:   else {
                    341:     filename = path.substring(ind+1);
                    342:     path = path.substring(0, ind+1);
                    343:   }
                    344:   Completer completer = new Completer();
                    345:   String bound = 'AaB03x';
                    346:   h.HttpRequest request = new h.HttpRequest();
                    347:   request.onLoad.listen((h.ProgressEvent event) {
                    348:     completer.complete(); // TODO: check for something, status is sometimes wrongly OK
                    349:   });
                    350:   request.onError.listen((h.ProgressEvent event) {
                    351:     completer.completeError(new DaxeException(request.status.toString()));
                    352:   });
                    353:   request.open('POST', '/upload_file');
                    354:   request.setRequestHeader('Content-Type', "multipart/form-data; boundary=$bound");
                    355:   
                    356:   StringBuffer sb = new StringBuffer();
                    357:   sb.write("--$bound\r\n");
                    358:   sb.write('Content-Disposition: form-data; name="uploads_path"\r\n');
                    359:   sb.write('Content-type: text/plain; charset=UTF-8\r\n');
                    360:   sb.write('Content-transfer-encoding: 8bit\r\n\r\n');
                    361:   sb.write(path);
                    362:   sb.write("\r\n--$bound\r\n");
                    363:   sb.write('Content-Disposition: form-data; name="uploads"; filename="$filename"\r\n');
                    364:   sb.write('Content-Type: application/octet-stream\r\n\r\n');
                    365:   doc.dndoc.xmlEncoding = 'UTF-8'; // the document is forced to use UTF-8
                    366:   sb.write(doc.toString());
                    367:   sb.write('\r\n--$bound--\r\n\r\n');
                    368:   request.send(sb.toString());
                    369:   return(completer.future);
                    370: }
                    371: 
                    372: ToolbarMenu _makeSectionMenu() {
                    373:   Menu menu = new Menu(LCDStrings.get('Section'));
                    374:   List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
                    375:   if (sectionRefs == null || sectionRefs.length == 0)
                    376:     return(null);
                    377:   x.Element h1Ref = doc.cfg.elementReference('h1');
                    378:   for (String role in ['introduction', 'conclusion', 'prerequisites', 'objectives',
                    379:                        'reminder', 'definition', 'demonstration', 'example', 'advise',
                    380:                        'remark', 'warning', 'more_information', 'method',
                    381:                        'activity', 'bibliography', 'citation']) {
                    382:     MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
                    383:         data:new ToolbarStyleInfo(sectionRefs, null, null));
                    384:     menuItem.action = () {
                    385:       ToolbarStyleInfo info = menuItem.data;
                    386:       x.Element sectionRef = info.validRef;
                    387:       LCDBlock section = NodeFactory.create(sectionRef);
                    388:       section.state = 1;
                    389:       section.setAttribute('class', 'role-' + role);
                    390:       LCDBlock h1 = NodeFactory.create(h1Ref);
                    391:       h1.state = 1;
1.9       damieng   392:       section.appendChild(h1);
                    393:       x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
                    394:       DNHiddenP p = NodeFactory.create(hiddenp);
                    395:       section.appendChild(p);
1.1       damieng   396:       if (doc.insert2(section, page.getSelectionStart())) {
                    397:         page.cursor.moveTo(new Position(h1, 0));
                    398:         page.updateAfterPathChange();
                    399:       }
                    400:     };
                    401:     menu.add(menuItem);
                    402:   }
                    403:   ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
                    404:   return(tbmenu);
                    405: }
                    406: 
                    407: Future<x.Document> _readTemplates(String templatesPath) {
                    408:   x.DOMParser dp = new x.DOMParser();
                    409:   return(dp.parseFromURL(templatesPath));
                    410: }
                    411: 
                    412: Menu _makeTemplatesMenu(x.Document templatesDoc) {
                    413:   Menu menu = new Menu(LCDStrings.get('Templates'));
                    414:   x.Element templates = templatesDoc.documentElement;
                    415:   for (x.Node child in templates.childNodes) {
                    416:     if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
                    417:       menu.add(_makeMenu(child));
                    418:     }
                    419:   }
                    420:   return(menu);
                    421: }
                    422: 
                    423: Menu _makeMenu(x.Element el) {
                    424:   String locale = LCDStrings.systemLocale;
                    425:   String defaultLocale = LCDStrings.defaultLocale;
                    426:   String title;
                    427:   for (x.Node child in el.childNodes) {
                    428:     if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
                    429:       if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    430:         if ((child as x.Element).getAttribute('lang') == locale) {
                    431:           title = child.firstChild.nodeValue;
                    432:           break;
                    433:         } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
                    434:           title = child.firstChild.nodeValue;
                    435:         }
                    436:       }
                    437:     }
                    438:   }
                    439:   if (title == null)
                    440:     title = '?';
                    441:   Menu menu = new Menu(title);
                    442:   for (x.Node child in el.childNodes) {
                    443:     if (child.nodeType == x.Node.ELEMENT_NODE) {
                    444:       if (child.nodeName == 'menu') {
                    445:         menu.add(_makeMenu(child));
                    446:       } else if (child.nodeName == 'item') {
                    447:         menu.add(_makeItem(child));
                    448:       }
                    449:     }
                    450:   }
                    451:   return(menu);
                    452: }
                    453: 
                    454: MenuItem _makeItem(x.Element item) {
                    455:   String locale = LCDStrings.systemLocale;
                    456:   String defaultLocale = LCDStrings.defaultLocale;
                    457:   String path, type, title, help;
                    458:   for (x.Node child in item.childNodes) {
                    459:     if (child.nodeType == x.Node.ELEMENT_NODE) {
                    460:       if (child.nodeName == 'title') {
                    461:         if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    462:           if ((child as x.Element).getAttribute('lang') == locale) {
                    463:             title = child.firstChild.nodeValue;
                    464:           } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
                    465:             title = child.firstChild.nodeValue;
                    466:           }
                    467:         }
                    468:       } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    469:         path = child.firstChild.nodeValue;
                    470:       } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    471:         type = child.firstChild.nodeValue;
                    472:       } else if (child.nodeName == 'help') {
                    473:         if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    474:           if ((child as x.Element).getAttribute('lang') == locale) {
                    475:             help = child.firstChild.nodeValue;
                    476:           } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
                    477:             help = child.firstChild.nodeValue;
                    478:           }
                    479:         }
                    480:       }
                    481:     }
                    482:   }
                    483:   if (type == null) {
                    484:     print("Warning: missing type for template $title\n");
                    485:     type = 'problem';
                    486:   }
                    487:   x.Element refElement = doc.cfg.elementReference(type);
                    488:   MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
                    489:   if (help != null)
                    490:     menuItem.toolTipText = help;
                    491:   return menuItem;
                    492: }
                    493: 
                    494: void _insertTemplate(String filePath) {
                    495:   try {
                    496:     x.DOMParser dp = new x.DOMParser();
                    497:     dp.parseFromURL(filePath).then((x.Document templateDoc) {
                    498:       x.Element root = templateDoc.documentElement;
                    499:       if (root == null)
                    500:         return;
                    501:       doc.removeWhitespace(root);
                    502:       DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
                    503:       UndoableEdit edit;
                    504:       Position pos = page.getSelectionStart();
                    505:       if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
                    506:         edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12      damieng   507:       else {
                    508:         DaxeNode parent = pos.dn;
                    509:         if (parent is DNText)
                    510:           parent = parent.parent;
                    511:         if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
                    512:           // The node must be inserted outside of the paragraph.
                    513:           // If there is something in the paragraph to the right of the cursor, it must be
                    514:           // moved into a new paragraph after the inserted node.
                    515:           // (as in DaxeDocument.insert2)
                    516:           DNHiddenP p = parent;
                    517:           edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
                    518:           Position pend = new Position(p, p.offsetLength);
                    519:           pend.moveInsideTextNodeIfPossible();
                    520:           if (pos < pend) {
                    521:             DaxeNode clone = doc.cloneBetween(pos, pend);
                    522:             edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
                    523:             DNHiddenP newp = NodeFactory.create(p.ref);
                    524:             edit.addSubEdit(new UndoableEdit.insertNode(
                    525:                 new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
                    526:             edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
                    527:           }
                    528:           edit.addSubEdit(new UndoableEdit.insertNode(
                    529:               new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
                    530:         } else
                    531:           edit = new UndoableEdit.insertNode(pos, dnRoot);
                    532:       }
1.1       damieng   533:       doc.doNewEdit(edit);
                    534:       page.updateAfterPathChange();
1.8       damieng   535:       if (dnRoot.nodeName != 'loncapa')
                    536:         page.scrollToNode(dnRoot);
1.1       damieng   537:     });
                    538:   } on x.DOMException catch(ex) {
                    539:     h.window.alert(ex.toString());
                    540:   }
                    541: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>