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

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

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