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

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';
1.41      damieng    30: part 'about_lc_daxe_dlg.dart';
                     31: part 'lcd_button.dart';
                     32: part 'lcd_insert_panel.dart';
1.1       damieng    33: part 'nodes/lcd_block.dart';
1.32      damieng    34: part 'nodes/lcd_block_no_newline.dart';
1.1       damieng    35: part 'nodes/lcd_parameter.dart';
                     36: part 'nodes/tex_mathjax.dart';
                     37: part 'nodes/lm.dart';
1.14      damieng    38: part 'nodes/chem.dart';
1.2       damieng    39: part 'nodes/numerical_response.dart';
1.3       damieng    40: part 'nodes/formula_response.dart';
                     41: part 'nodes/string_response.dart';
1.11      damieng    42: part 'nodes/essay_response.dart';
                     43: part 'nodes/textfield.dart';
1.1       damieng    44: part 'nodes/radio_response.dart';
                     45: part 'nodes/radio_foilgroup.dart';
                     46: part 'nodes/radio_foil.dart';
                     47: part 'nodes/option_response.dart';
                     48: part 'nodes/option_foilgroup.dart';
                     49: part 'nodes/option_foil.dart';
1.6       damieng    50: part 'nodes/match_response.dart';
                     51: part 'nodes/match_foilgroup.dart';
                     52: part 'nodes/match_foil.dart';
                     53: part 'nodes/itemgroup.dart';
                     54: part 'nodes/match_item.dart';
1.1       damieng    55: part 'nodes/rank_response.dart';
                     56: part 'nodes/rank_foilgroup.dart';
                     57: part 'nodes/rank_foil.dart';
1.10      damieng    58: part 'nodes/math_response.dart';
                     59: part 'nodes/math_answer.dart';
1.13      damieng    60: part 'nodes/organic_response.dart';
                     61: part 'nodes/jsme_dialog.dart';
                     62: part 'nodes/reaction_response.dart';
1.5       damieng    63: part 'nodes/textline.dart';
1.1       damieng    64: part 'nodes/hintgroup.dart';
                     65: part 'nodes/simple_ui_text.dart';
                     66: part 'nodes/script_block.dart';
1.2       damieng    67: part 'nodes/simple_ui_exception.dart';
1.7       damieng    68: part 'nodes/gnuplot.dart';
1.22      damieng    69: part 'nodes/organic_structure.dart';
1.7       damieng    70: part 'nodes/fake_attribute_element.dart';
                     71: part 'nodes/plot_axis.dart';
                     72: part 'nodes/plot_curve.dart';
1.17      damieng    73: part 'nodes/section.dart';
                     74: part 'nodes/h1.dart';
1.1       damieng    75: 
1.18      damieng    76: List<String> knownSectionRoles = ['activity', 'advice', 'bibliography', 'citation',
                     77:   'conclusion', 'definition', 'demonstration', 'example',
                     78:   'explanation', 'introduction', 'method', 'more_information',
                     79:   'objectives', 'prerequisites', 'remark', 'reminder',
                     80:   'summary', 'syntax', 'warning'];
                     81: 
1.1       damieng    82: 
                     83: void main() {
                     84:   NodeFactory.addCoreDisplayTypes();
                     85:   
1.35      damieng    86:   // LON-CAPA specific display types:
                     87:   
1.1       damieng    88:   addDisplayType('lcdblock',
                     89:         (x.Element ref) => new LCDBlock.fromRef(ref),
                     90:         (x.Node node, DaxeNode parent) => new LCDBlock.fromNode(node, parent)
                     91:     );
                     92:   
                     93:   addDisplayType('texmathjax',
                     94:         (x.Element ref) => new TeXMathJax.fromRef(ref),
                     95:         (x.Node node, DaxeNode parent) => new TeXMathJax.fromNode(node, parent)
                     96:     );
                     97:   
                     98:   addDisplayType('lm',
                     99:         (x.Element ref) => new Lm.fromRef(ref),
                    100:         (x.Node node, DaxeNode parent) => new Lm.fromNode(node, parent)
                    101:     );
                    102:   
1.14      damieng   103:   addDisplayType('chem',
                    104:         (x.Element ref) => new Chem.fromRef(ref),
                    105:         (x.Node node, DaxeNode parent) => new Chem.fromNode(node, parent)
                    106:     );
                    107:   
1.1       damieng   108:   addDisplayType('script',
1.27      damieng   109:         (x.Element ref) => new ScriptBlock.fromRef(ref),
                    110:         (x.Node node, DaxeNode parent) => new ScriptBlock.fromNode(node, parent)
1.1       damieng   111:     );
                    112:   
                    113:   addDisplayType('parameter',
                    114:         (x.Element ref) => new LCDParameter.fromRef(ref),
                    115:         (x.Node node, DaxeNode parent) => new LCDParameter.fromNode(node, parent)
                    116:     );
                    117:   
1.2       damieng   118:   addDisplayType('numericalresponse',
                    119:         (x.Element ref) => new NumericalResponse.fromRef(ref),
                    120:         (x.Node node, DaxeNode parent) => new NumericalResponse.fromNode(node, parent)
                    121:     );
                    122:   
1.3       damieng   123:   addDisplayType('formularesponse',
                    124:         (x.Element ref) => new FormulaResponse.fromRef(ref),
                    125:         (x.Node node, DaxeNode parent) => new FormulaResponse.fromNode(node, parent)
                    126:     );
                    127:   
                    128:   addDisplayType('stringresponse',
                    129:         (x.Element ref) => new StringResponse.fromRef(ref),
                    130:         (x.Node node, DaxeNode parent) => new StringResponse.fromNode(node, parent)
                    131:     );
                    132:   
1.11      damieng   133:   addDisplayType('essayresponse',
                    134:         (x.Element ref) => new EssayResponse.fromRef(ref),
                    135:         (x.Node node, DaxeNode parent) => new EssayResponse.fromNode(node, parent)
                    136:     );
                    137:   
1.1       damieng   138:   addDisplayType('radioresponse',
                    139:         (x.Element ref) => new RadioResponse.fromRef(ref),
                    140:         (x.Node node, DaxeNode parent) => new RadioResponse.fromNode(node, parent)
                    141:     );
                    142:   
                    143:   addDisplayType('optionresponse',
                    144:         (x.Element ref) => new OptionResponse.fromRef(ref),
                    145:         (x.Node node, DaxeNode parent) => new OptionResponse.fromNode(node, parent)
                    146:     );
                    147:   
1.6       damieng   148:   addDisplayType('matchresponse',
                    149:         (x.Element ref) => new MatchResponse.fromRef(ref),
                    150:         (x.Node node, DaxeNode parent) => new MatchResponse.fromNode(node, parent)
                    151:     );
                    152:   
1.1       damieng   153:   addDisplayType('rankresponse',
                    154:         (x.Element ref) => new RankResponse.fromRef(ref),
                    155:         (x.Node node, DaxeNode parent) => new RankResponse.fromNode(node, parent)
                    156:     );
                    157:   
1.10      damieng   158:   addDisplayType('mathresponse',
                    159:         (x.Element ref) => new MathResponse.fromRef(ref),
                    160:         (x.Node node, DaxeNode parent) => new MathResponse.fromNode(node, parent)
                    161:     );
                    162:   
1.13      damieng   163:   addDisplayType('organicresponse',
                    164:         (x.Element ref) => new OrganicResponse.fromRef(ref),
                    165:         (x.Node node, DaxeNode parent) => new OrganicResponse.fromNode(node, parent)
                    166:     );
                    167:   
                    168:   addDisplayType('reactionresponse',
                    169:         (x.Element ref) => new ReactionResponse.fromRef(ref),
                    170:         (x.Node node, DaxeNode parent) => new ReactionResponse.fromNode(node, parent)
                    171:     );
                    172:   
1.1       damieng   173:   addDisplayType('foilgroup',
                    174:         (x.Element ref) {
                    175:           if (ref.getAttribute('type') == 'radiobuttonresponse--foilgroup')
                    176:             return new RadioFoilgroup.fromRef(ref);
                    177:           else if (ref.getAttribute('type') == 'optionresponse--foilgroup')
                    178:             return new OptionFoilgroup.fromRef(ref);
1.6       damieng   179:           else if (ref.getAttribute('type') == 'matchresponse--foilgroup')
                    180:             return new MatchFoilgroup.fromRef(ref);
1.1       damieng   181:           else if (ref.getAttribute('type') == 'rankresponse--foilgroup')
                    182:             return new RankFoilgroup.fromRef(ref);
                    183:           return new LCDBlock.fromRef(ref);
                    184:         },
                    185:         (x.Node node, DaxeNode parent) {
                    186:           if (parent is RadioResponse)
                    187:             return new RadioFoilgroup.fromNode(node, parent);
                    188:           else if (parent is OptionResponse)
                    189:             return new OptionFoilgroup.fromNode(node, parent);
1.6       damieng   190:           else if (parent is MatchResponse)
                    191:             return new MatchFoilgroup.fromNode(node, parent);
1.1       damieng   192:           else if (parent is RankResponse)
                    193:             return new RankFoilgroup.fromNode(node, parent);
                    194:           return new LCDBlock.fromNode(node, parent);
                    195:         }
                    196:     );
                    197:   
                    198:   addDisplayType('foil',
                    199:         (x.Element ref) {
                    200:           if (ref.getAttribute('type') == 'radiobuttonresponse--foil')
                    201:             return new RadioFoil.fromRef(ref);
                    202:           else if (ref.getAttribute('type') == 'optionresponse--foil')
                    203:             return new OptionFoil.fromRef(ref);
1.6       damieng   204:           else if (ref.getAttribute('type') == 'matchresponse--foil')
                    205:             return new MatchFoil.fromRef(ref);
1.1       damieng   206:           else if (ref.getAttribute('type') == 'rankresponse--foil')
                    207:             return new RankFoil.fromRef(ref);
                    208:           return new LCDBlock.fromRef(ref);
                    209:         },
                    210:         (x.Node node, DaxeNode parent) {
                    211:           if (parent is RadioFoilgroup)
                    212:             return new RadioFoil.fromNode(node, parent);
1.19      damieng   213:           else if (parent is OptionFoilgroup ||
                    214:               (parent.parent != null && parent.parent is OptionFoilgroup))
1.1       damieng   215:             return new OptionFoil.fromNode(node, parent);
1.19      damieng   216:           else if (parent is MatchFoilgroup ||
                    217:               (parent.parent != null && parent.parent is MatchFoilgroup))
1.6       damieng   218:             return new MatchFoil.fromNode(node, parent);
1.1       damieng   219:           else if (parent is RankFoilgroup)
                    220:             return new RankFoil.fromNode(node, parent);
                    221:           return new LCDBlock.fromNode(node, parent);
                    222:         }
                    223:     );
                    224:   
1.6       damieng   225:   addDisplayType('itemgroup',
                    226:         (x.Element ref) => new Itemgroup.fromRef(ref),
                    227:         (x.Node node, DaxeNode parent) => new Itemgroup.fromNode(node, parent)
                    228:     );
                    229:   
                    230:   addDisplayType('matchitem',
                    231:         (x.Element ref) => new MatchItem.fromRef(ref),
                    232:         (x.Node node, DaxeNode parent) => new MatchItem.fromNode(node, parent)
                    233:     );
                    234:   
1.5       damieng   235:   addDisplayType('textline',
                    236:         (x.Element ref) => new Textline.fromRef(ref),
                    237:         (x.Node node, DaxeNode parent) => new Textline.fromNode(node, parent)
                    238:     );
                    239:   
1.11      damieng   240:   addDisplayType('textfield',
                    241:         (x.Element ref) => new Textfield.fromRef(ref),
                    242:         (x.Node node, DaxeNode parent) => new Textfield.fromNode(node, parent)
                    243:     );
                    244:   
1.1       damieng   245:   addDisplayType('hintgroup',
                    246:         (x.Element ref) => new Hintgroup.fromRef(ref),
                    247:         (x.Node node, DaxeNode parent) => new Hintgroup.fromNode(node, parent)
                    248:     );
                    249:   
1.10      damieng   250:   addDisplayType('answer',
                    251:         (x.Element ref) {
                    252:           if (ref.getAttribute('type') == 'mathresponse--answer')
                    253:             return new MathAnswer.fromRef(ref);
1.27      damieng   254:           else if (ref.getAttribute('type') == 'caparesponse--answer')
                    255:             return new LCDBlock.fromRef(ref);
                    256:           else
                    257:             return new ScriptBlock.fromRef(ref);
1.10      damieng   258:         },
                    259:         (x.Node node, DaxeNode parent) {
                    260:           if (parent is MathResponse)
                    261:             return new MathAnswer.fromNode(node, parent);
1.27      damieng   262:           else if (parent != null && parent.ref != null &&
                    263:               parent.ref.getAttribute('type') == 'caparesponse--answergroup')
                    264:             return new LCDBlock.fromNode(node, parent);
                    265:           else
                    266:             return new ScriptBlock.fromNode(node, parent);
1.10      damieng   267:         }
                    268:     );
                    269:   
1.7       damieng   270:   addDisplayType('gnuplot',
                    271:         (x.Element ref) => new Gnuplot.fromRef(ref),
                    272:         (x.Node node, DaxeNode parent) => new Gnuplot.fromNode(node, parent)
                    273:     );
                    274:   
1.22      damieng   275:   addDisplayType('organicstructure',
                    276:         (x.Element ref) => new OrganicStructure.fromRef(ref),
                    277:         (x.Node node, DaxeNode parent) => new OrganicStructure.fromNode(node, parent)
                    278:     );
                    279:   
1.7       damieng   280:   addDisplayType('fakeattribute',
                    281:         (x.Element ref) {
                    282:           if (doc.cfg.elementName(ref) != 'title' || ref.getAttribute('type') == 'lonplot--title')
                    283:             return new FakeAttributeElement.fromRef(ref);
                    284:           return new LCDBlock.fromRef(ref);
                    285:         },
                    286:         (x.Node node, DaxeNode parent) {
                    287:           if (parent is Gnuplot || parent is PlotCurve)
                    288:             return new FakeAttributeElement.fromNode(node, parent);
                    289:           return new LCDBlock.fromNode(node, parent);
                    290:         }
                    291:     );
                    292:   
                    293:   addDisplayType('plotaxis',
                    294:         (x.Element ref) => new PlotAxis.fromRef(ref),
                    295:         (x.Node node, DaxeNode parent) => new PlotAxis.fromNode(node, parent)
                    296:     );
                    297:   
                    298:   addDisplayType('plotcurve',
                    299:         (x.Element ref) => new PlotCurve.fromRef(ref),
                    300:         (x.Node node, DaxeNode parent) => new PlotCurve.fromNode(node, parent)
                    301:     );
                    302:   
1.17      damieng   303:   addDisplayType('section',
                    304:         (x.Element ref) => new Section.fromRef(ref),
                    305:         (x.Node node, DaxeNode parent) => new Section.fromNode(node, parent)
                    306:     );
                    307:   
                    308:   addDisplayType('h1',
                    309:         (x.Element ref) => new H1.fromRef(ref),
                    310:         (x.Node node, DaxeNode parent) => new H1.fromNode(node, parent)
                    311:     );
                    312:   
1.32      damieng   313:   addDisplayType('lcdblocknonewline',
                    314:         (x.Element ref) => new LCDBlockNoNewline.fromRef(ref),
                    315:         (x.Node node, DaxeNode parent) => new LCDBlockNoNewline.fromNode(node, parent)
                    316:     );
                    317:   
1.31      damieng   318:   addDisplayType('label',
                    319:         (x.Element ref) {
                    320:           if (ref.getAttribute('type') == 'lonplot--label')
1.37      damieng   321:             return new LCDBlockNoNewline.fromRef(ref); // gnuplot label
1.32      damieng   322:           else if (ref.getAttribute('type') == 'randomlabel--label')
                    323:             return new LCDBlockNoNewline.fromRef(ref); // randomlabel label
                    324:           return new DNString.fromRef(ref); // HTML form label
1.31      damieng   325:         },
                    326:         (x.Node node, DaxeNode parent) {
                    327:           if (parent is Gnuplot || (parent != null && parent.parent is Gnuplot))
1.37      damieng   328:             return new LCDBlockNoNewline.fromNode(node, parent);
1.32      damieng   329:           else if (parent != null && parent.ref != null &&
                    330:               parent.ref.getAttribute('type') == 'randomlabel--labelgroup')
                    331:             return new LCDBlockNoNewline.fromNode(node, parent);
1.31      damieng   332:           return new DNString.fromNode(node, parent);
                    333:         }
                    334:     );
                    335:   
1.35      damieng   336:   // Initialize Daxe, using daxe.initDaxe as a basis.
                    337:   // The insertion panel is customized.
1.28      damieng   338:   InsertPanel insertP = new LCDInsertPanel();
                    339:   LeftPanel left = new LeftPanel(insert:insertP);
1.35      damieng   340:   // The save action is customized.
1.29      damieng   341:   ActionFunction saveFunction= () {
                    342:     doc.save().then((_) {
                    343:       // h.window.alert(Strings.get('save.success'));
                    344:       // update the preview iframe
1.35      damieng   345:       h.IFrameElement previewIFrame = h.document.getElementById('preview_iframe');
                    346:       if (previewIFrame != null)
                    347:         previewIFrame.src = previewIFrame.src;
1.29      damieng   348:     }, onError: (DaxeException ex) {
                    349:       h.window.alert(Strings.get('save.error') + ': ' + ex.message);
                    350:     });
                    351:   };
1.43      damieng   352:   // function to customize the toobar after its default creation
                    353:   ActionFunction customizeToolbar = () {
                    354:     ToolbarMenu sectionMenu = _makeSectionMenu();
                    355:     if (sectionMenu != null)
                    356:       page.toolbar.add(sectionMenu);
                    357:     ToolbarBox mathBox;
                    358:     if (doc.saveURL != null)
                    359:       mathBox = page.toolbar.items[5];
                    360:     else
                    361:       mathBox = page.toolbar.items[4];
                    362:     x.Element texRef = doc.cfg.elementReference('m');
                    363:     if (texRef != null) {
                    364:       ToolbarButton texButton = new ToolbarButton(
                    365:           LCDStrings.get('insert_m'), 'images/tex.png',
                    366:           () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate, 
                    367:           data:new ToolbarStyleInfo([texRef], null, null));
                    368:       mathBox.add(texButton);
                    369:     }
                    370:     x.Element lmRef = doc.cfg.elementReference('lm');
                    371:     if (lmRef != null) {
                    372:       ToolbarButton lmButton = new ToolbarButton(
                    373:           LCDStrings.get('insert_lm'), 'packages/daxe/images/toolbar/equation.png',
                    374:           () => doc.insertNewNode(lmRef, 'element'), Toolbar.insertButtonUpdate, 
                    375:           data:new ToolbarStyleInfo([lmRef], null, null));
                    376:       mathBox.add(lmButton);
                    377:     }
                    378:   };
1.35      damieng   379:   // Initialization
1.1       damieng   380:   Future.wait([Strings.load(), LCDStrings.load(), _readTemplates('templates.xml')]).then((List responses) {
1.43      damieng   381:     initDaxe(left:left, saveFunction:saveFunction,
                    382:         customizeToolbar:customizeToolbar).then((v) {
1.35      damieng   383:       // More customizations
                    384:       _addGuttersAndPreview();
1.1       damieng   385:       page.adjustPositionsUnderToolbar();
                    386:       page.updateAfterPathChange();
                    387:       // add things to the menubar
                    388:       if (responses[2] is x.Document) {
                    389:         // at this point the menubar html is already in the document, so we have to fix the HTML
                    390:         h.Element menubarDiv = h.document.getElementsByClassName('menubar')[0];
                    391:         Menu m = _makeTemplatesMenu(responses[2]);
1.34      damieng   392:         if (m != null) {
1.26      damieng   393:           page.mbar.add(m);
1.34      damieng   394:           menubarDiv.append(page.mbar.createMenuDiv(m));
                    395:         }
1.33      damieng   396:         Menu helpMenu = _makeHelpMenu();
                    397:         page.mbar.add(helpMenu);
                    398:         menubarDiv.append(page.mbar.createMenuDiv(helpMenu));
1.1       damieng   399:         page.updateAfterPathChange();
                    400:       } else
                    401:         print("Error reading templates file, could not build the menu.");
1.15      damieng   402:     }).catchError((e) {
1.34      damieng   403:       String msg = 'Initialization error: ' + (e is String ? e : e.toString());
                    404:       print(msg);
                    405:       h.document.body.appendText(msg);
1.1       damieng   406:     });
1.15      damieng   407:   }).catchError((e) {
1.34      damieng   408:     String msg = 'Initialization error: ' + (e is String ? e : e.toString());
                    409:     print(msg);
                    410:     h.document.body.appendText(msg);
1.1       damieng   411:   });
                    412: }
                    413: 
1.35      damieng   414: 
                    415: /**
                    416:  * Adds a movable left gutter between the left panel and the document,
                    417:  * a preview to the right and another gutter beween the document and the preview.
1.39      damieng   418:  * Also hides the left panel initially.
1.35      damieng   419:  */
                    420: void _addGuttersAndPreview() {
                    421:   // NOTE: The preview is in an iframe within Daxe
                    422:   // (so that it works like another LON-CAPA problem preview),
                    423:   // and Daxe itself is in an iframe (so as to get the parameters from the URL).
                    424:   // This makes handling mouse event a little more complicated: the preview iframe
                    425:   // will consume events by default, and events outside the Daxe iframe are not
                    426:   // always sent to the iframe when happening outside.
                    427:   bool moving_left_gutter = false;
                    428:   bool moving_right_gutter = false;
1.39      damieng   429:   // hide left panel
                    430:   h.Element left = h.document.getElementById('left_panel');
                    431:   left.style.width ='0px';
                    432:   left.style.visibility = 'hidden';
                    433:   // add left gutter
1.35      damieng   434:   h.DivElement leftGutter = new h.DivElement();
                    435:   leftGutter.id = 'left_gutter';
                    436:   leftGutter.classes.add('vertical-gutter');
1.39      damieng   437:   leftGutter.style.left ='0px';
1.35      damieng   438:   h.Element doc1 = h.document.getElementById('doc1');
                    439:   h.document.body.insertBefore(leftGutter, doc1);
1.39      damieng   440:   doc1.style.left = '10px';
1.35      damieng   441:   
                    442:   // preview and right gutter
                    443:   h.DivElement previewDiv;
                    444:   h.IFrameElement previewIFrame;
1.40      damieng   445:   if (doc.filePath != null && doc.filePath.startsWith('/daxeopen')) {
1.35      damieng   446:     previewDiv = new h.DivElement();
                    447:     previewDiv.id = 'preview_div';
                    448:     previewIFrame = new h.IFrameElement();
                    449:     previewIFrame.src = doc.filePath.substring(9) + '?inhibitmenu=yes';
                    450:     previewIFrame.id = 'preview_iframe';
                    451:     int previewWidth = (doc1.offsetWidth / 3).round();
                    452:     previewDiv.style.width = "${previewWidth}px";
                    453:     previewDiv.append(previewIFrame);
                    454:     h.DivElement rightGutter = new h.DivElement();
                    455:     rightGutter.id = 'right_gutter';
                    456:     rightGutter.classes.add('vertical-gutter');
                    457:     rightGutter.style.right = "${previewWidth}px";
1.44    ! damieng   458:     var right_mousedown = (h.Event event) {
1.35      damieng   459:       moving_right_gutter = true;
                    460:       event.preventDefault();
                    461:       previewIFrame.style.pointerEvents = 'none';
1.44    ! damieng   462:     };
        !           463:     rightGutter.onMouseDown.listen(right_mousedown);
        !           464:     rightGutter.onTouchStart.listen(right_mousedown);
1.35      damieng   465:     h.document.body.append(rightGutter);
                    466:     doc1.style.right = "${previewWidth + 10}px";
                    467:     h.document.body.append(previewDiv);
                    468:   }
                    469:   
                    470:   // mousedown on left gutter
1.44    ! damieng   471:   var left_mousedown = (h.Event event) {
1.35      damieng   472:     moving_left_gutter = true;
                    473:     event.preventDefault();
                    474:     if (previewIFrame != null)
                    475:       previewIFrame.style.pointerEvents = 'none';
1.42      damieng   476:   };
1.44    ! damieng   477:   leftGutter.onMouseDown.listen(left_mousedown);
        !           478:   leftGutter.onTouchStart.listen(left_mousedown);
1.35      damieng   479:   
                    480:   // adjust top positions when the toolbar height changes
                    481:   _adjustPositionsUnderToolbar();
                    482:   h.window.onResize.listen((h.Event event) => _adjustPositionsUnderToolbar());
                    483:   
                    484:   // mousemove for both gutters
1.42      damieng   485:   var mousemove = (h.Event event) {
1.35      damieng   486:     if (!(moving_left_gutter || moving_right_gutter))
                    487:       return;
                    488:     h.Element left = h.document.getElementById('left_panel');
                    489:     h.Element doc1 = h.document.getElementById('doc1');
1.42      damieng   490:     num x;
                    491:     if (event is h.MouseEvent)
                    492:       x = event.client.x;
                    493:     else if (event is h.TouchEvent)
                    494:       x = event.changedTouches[0].client.x;
                    495:     else
                    496:       return;
1.35      damieng   497:     x = x.round();
1.36      damieng   498:     // for IE, temporary change of overflow for doc1,
                    499:     // otherwise IE and Edge never report an empty width when there is a scrollbar
                    500:     // (this slows things down, so it is done only for IE)
                    501:     bool ie = h.window.navigator.appVersion.contains("Trident") || h.window.navigator.appVersion.contains("Edge");
                    502:     if (ie)
                    503:       doc1.style.overflow = 'hidden';
                    504:     num doc1width = doc1.offsetWidth;
                    505:     if (ie)
                    506:       doc1.style.overflow = '';
1.35      damieng   507:     if (moving_left_gutter) {
                    508:       h.Element leftGutter = h.document.getElementById('left_gutter');
                    509:       if (x < 5)
                    510:         x = 5;
1.36      damieng   511:       int maxX = left.offsetWidth + 5 + doc1width;
1.35      damieng   512:       if (x > maxX)
                    513:         x = maxX;
                    514:       if (x == 5)
                    515:         left.style.visibility = 'hidden';
1.36      damieng   516:       else
                    517:         left.style.visibility = '';
                    518:       left.style.width = "${x-5}px";
1.35      damieng   519:       leftGutter.style.left ="${x-5}px";
1.36      damieng   520:       if (x == maxX)
                    521:         doc1.style.visibility = 'hidden';
                    522:       else
                    523:         doc1.style.visibility = '';
1.35      damieng   524:       doc1.style.left ="${x+5}px";
                    525:     } else if (moving_right_gutter) {
                    526:       h.Element previewDiv = h.document.getElementById('preview_div');
                    527:       h.Element rightGutter = h.document.getElementById('right_gutter');
                    528:       int minX = left.offsetWidth + 10 + 5;
                    529:       if (x < minX)
                    530:         x = minX;
1.36      damieng   531:       int maxX = left.offsetWidth + 10 + doc1width + 5 + previewDiv.offsetWidth;
1.35      damieng   532:       if (x > maxX)
                    533:         x = maxX;
                    534:       int previewWidth = maxX - x;
                    535:       if (previewWidth == 0)
                    536:         previewDiv.style.visibility = 'hidden';
1.36      damieng   537:       else
                    538:         previewDiv.style.visibility = '';
                    539:       previewDiv.style.width = "${previewWidth}px";
1.35      damieng   540:       rightGutter.style.right ="${previewWidth}px";
1.36      damieng   541:       if (x == minX)
                    542:         doc1.style.visibility = 'hidden';
                    543:       else
                    544:         doc1.style.visibility = '';
1.35      damieng   545:       doc1.style.right ="${previewWidth + 10}px";
                    546:     }
                    547:     event.stopPropagation();
                    548:     event.preventDefault();
1.42      damieng   549:   };
                    550:   h.document.onMouseMove.listen(mousemove);
                    551:   h.document.onTouchMove.listen(mousemove);
1.35      damieng   552:   
                    553:   // mouseup
1.42      damieng   554:   var mouseup = (h.Event event) {
1.35      damieng   555:     if (moving_left_gutter || moving_right_gutter) {
1.42      damieng   556:       event.preventDefault();
1.35      damieng   557:       h.IFrameElement previewIFrame = h.document.getElementById('preview_iframe');
                    558:       if (previewIFrame != null) {
                    559:         // restore events on iframe
                    560:         previewIFrame.style.pointerEvents = '';
                    561:       }
                    562:       moving_left_gutter = false;
                    563:       moving_right_gutter = false;
                    564:     }
                    565:   };
                    566:   h.document.onMouseUp.listen(mouseup);
1.42      damieng   567:   h.document.onTouchEnd.listen(mouseup);
1.35      damieng   568:   // if we are in an iframe, we need to use the parent to get
                    569:   // mouseup events outside it with some browsers like Chromium
                    570:   var parent = new js.JsObject.fromBrowserObject(js.context['window'])['parent'];
                    571:   if (parent is h.Window) {
                    572:     // happens with Dartium and Firefox
                    573:     parent.onMouseUp.listen((h.MouseEvent event) => mouseup(event));
                    574:   } else if (parent is js.JsObject) {
                    575:     // happens with Chromium
                    576:     parent.callMethod('addEventListener', ['mouseup', mouseup, false]);
                    577:   }
                    578: }
                    579: 
                    580: /*
                    581:  * This complements WebPage.adjustPositionsUnderToolbar with positions for the gutters and preview.
                    582:  */
                    583: void _adjustPositionsUnderToolbar() {
                    584:   h.Element headers = h.document.getElementById('headers');
                    585:   num y = headers.getBoundingClientRect().bottom;
                    586:   String cssTop = (y.round() + 2).toString() + "px";
                    587:   h.document.getElementById('left_gutter').style.top = cssTop;
                    588:   h.DivElement rightGutter = h.document.getElementById('right_gutter');
                    589:   if (rightGutter != null)
                    590:     rightGutter.style.top = cssTop;
                    591:   h.DivElement previewDiv = h.document.getElementById('preview_div');
                    592:   if (previewDiv != null)
                    593:     previewDiv.style.top = cssTop;
                    594: }
                    595: 
                    596: 
1.1       damieng   597: ToolbarMenu _makeSectionMenu() {
1.18      damieng   598:   // sort section roles by title
                    599:   knownSectionRoles.sort((String role1, String role2) => LCDStrings.get(role1).compareTo(LCDStrings.get(role2)));
                    600:   
1.1       damieng   601:   Menu menu = new Menu(LCDStrings.get('Section'));
                    602:   List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
                    603:   if (sectionRefs == null || sectionRefs.length == 0)
                    604:     return(null);
                    605:   x.Element h1Ref = doc.cfg.elementReference('h1');
1.18      damieng   606:   for (String role in knownSectionRoles) {
1.1       damieng   607:     MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
                    608:         data:new ToolbarStyleInfo(sectionRefs, null, null));
                    609:     menuItem.action = () {
                    610:       ToolbarStyleInfo info = menuItem.data;
                    611:       x.Element sectionRef = info.validRef;
1.18      damieng   612:       Section section = new Section.fromRef(sectionRef);
1.1       damieng   613:       section.state = 1;
                    614:       section.setAttribute('class', 'role-' + role);
1.17      damieng   615:       H1 h1 = NodeFactory.create(h1Ref);
1.1       damieng   616:       h1.state = 1;
1.18      damieng   617:       h1.appendChild(new DNText(LCDStrings.get(role)));
1.9       damieng   618:       section.appendChild(h1);
                    619:       x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
1.18      damieng   620:       DNHiddenP p = new DNHiddenP.fromRef(hiddenp);
1.9       damieng   621:       section.appendChild(p);
1.17      damieng   622:       section.setupSimpleUI();
1.1       damieng   623:       if (doc.insert2(section, page.getSelectionStart())) {
1.18      damieng   624:         page.cursor.moveTo(new Position(p, 0));
1.1       damieng   625:         page.updateAfterPathChange();
                    626:       }
                    627:     };
                    628:     menu.add(menuItem);
                    629:   }
                    630:   ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
                    631:   return(tbmenu);
                    632: }
                    633: 
1.35      damieng   634: 
1.1       damieng   635: Future<x.Document> _readTemplates(String templatesPath) {
                    636:   x.DOMParser dp = new x.DOMParser();
                    637:   return(dp.parseFromURL(templatesPath));
                    638: }
                    639: 
                    640: Menu _makeTemplatesMenu(x.Document templatesDoc) {
1.26      damieng   641:   List<x.Element> problemRefs = doc.cfg.elementReferences('problem');
                    642:   if (problemRefs == null || problemRefs.length == 0)
                    643:     return(null);
1.1       damieng   644:   Menu menu = new Menu(LCDStrings.get('Templates'));
                    645:   x.Element templates = templatesDoc.documentElement;
                    646:   for (x.Node child in templates.childNodes) {
                    647:     if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
                    648:       menu.add(_makeMenu(child));
                    649:     }
                    650:   }
                    651:   return(menu);
                    652: }
                    653: 
                    654: Menu _makeMenu(x.Element el) {
                    655:   String locale = LCDStrings.systemLocale;
                    656:   String defaultLocale = LCDStrings.defaultLocale;
                    657:   String title;
                    658:   for (x.Node child in el.childNodes) {
                    659:     if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
                    660:       if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    661:         if ((child as x.Element).getAttribute('lang') == locale) {
                    662:           title = child.firstChild.nodeValue;
                    663:           break;
                    664:         } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
                    665:           title = child.firstChild.nodeValue;
                    666:         }
                    667:       }
                    668:     }
                    669:   }
                    670:   if (title == null)
                    671:     title = '?';
                    672:   Menu menu = new Menu(title);
                    673:   for (x.Node child in el.childNodes) {
                    674:     if (child.nodeType == x.Node.ELEMENT_NODE) {
                    675:       if (child.nodeName == 'menu') {
                    676:         menu.add(_makeMenu(child));
                    677:       } else if (child.nodeName == 'item') {
                    678:         menu.add(_makeItem(child));
                    679:       }
                    680:     }
                    681:   }
                    682:   return(menu);
                    683: }
                    684: 
                    685: MenuItem _makeItem(x.Element item) {
                    686:   String locale = LCDStrings.systemLocale;
                    687:   String defaultLocale = LCDStrings.defaultLocale;
                    688:   String path, type, title, help;
                    689:   for (x.Node child in item.childNodes) {
                    690:     if (child.nodeType == x.Node.ELEMENT_NODE) {
                    691:       if (child.nodeName == 'title') {
                    692:         if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    693:           if ((child as x.Element).getAttribute('lang') == locale) {
                    694:             title = child.firstChild.nodeValue;
                    695:           } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
                    696:             title = child.firstChild.nodeValue;
                    697:           }
                    698:         }
                    699:       } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    700:         path = child.firstChild.nodeValue;
                    701:       } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    702:         type = child.firstChild.nodeValue;
                    703:       } else if (child.nodeName == 'help') {
                    704:         if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
                    705:           if ((child as x.Element).getAttribute('lang') == locale) {
                    706:             help = child.firstChild.nodeValue;
                    707:           } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
                    708:             help = child.firstChild.nodeValue;
                    709:           }
                    710:         }
                    711:       }
                    712:     }
                    713:   }
                    714:   if (type == null) {
                    715:     print("Warning: missing type for template $title\n");
                    716:     type = 'problem';
                    717:   }
                    718:   x.Element refElement = doc.cfg.elementReference(type);
                    719:   MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
                    720:   if (help != null)
                    721:     menuItem.toolTipText = help;
                    722:   return menuItem;
                    723: }
                    724: 
                    725: void _insertTemplate(String filePath) {
                    726:   try {
                    727:     x.DOMParser dp = new x.DOMParser();
                    728:     dp.parseFromURL(filePath).then((x.Document templateDoc) {
                    729:       x.Element root = templateDoc.documentElement;
                    730:       if (root == null)
                    731:         return;
                    732:       doc.removeWhitespace(root);
                    733:       DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
1.27      damieng   734:       if (dnRoot is LCDBlock && dnRoot.attributes.length > 0)
                    735:         dnRoot.state = 0; // make attributes editable for the template root
1.1       damieng   736:       UndoableEdit edit;
                    737:       Position pos = page.getSelectionStart();
                    738:       if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
                    739:         edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12      damieng   740:       else {
                    741:         DaxeNode parent = pos.dn;
                    742:         if (parent is DNText)
                    743:           parent = parent.parent;
                    744:         if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
                    745:           // The node must be inserted outside of the paragraph.
                    746:           // If there is something in the paragraph to the right of the cursor, it must be
                    747:           // moved into a new paragraph after the inserted node.
                    748:           // (as in DaxeDocument.insert2)
                    749:           DNHiddenP p = parent;
                    750:           edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
                    751:           Position pend = new Position(p, p.offsetLength);
                    752:           pend.moveInsideTextNodeIfPossible();
                    753:           if (pos < pend) {
                    754:             DaxeNode clone = doc.cloneBetween(pos, pend);
                    755:             edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
                    756:             DNHiddenP newp = NodeFactory.create(p.ref);
                    757:             edit.addSubEdit(new UndoableEdit.insertNode(
                    758:                 new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
                    759:             edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
                    760:           }
                    761:           edit.addSubEdit(new UndoableEdit.insertNode(
                    762:               new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
                    763:         } else
                    764:           edit = new UndoableEdit.insertNode(pos, dnRoot);
                    765:       }
1.1       damieng   766:       doc.doNewEdit(edit);
                    767:       page.updateAfterPathChange();
1.8       damieng   768:       if (dnRoot.nodeName != 'loncapa')
                    769:         page.scrollToNode(dnRoot);
1.1       damieng   770:     });
                    771:   } on x.DOMException catch(ex) {
                    772:     h.window.alert(ex.toString());
                    773:   }
                    774: }
1.33      damieng   775: 
                    776: Menu _makeHelpMenu() {
                    777:   Menu menu = new Menu(LCDStrings.get('help'));
1.38      damieng   778:   menu.add(new MenuItem(LCDStrings.get('getting_started'),
                    779:       () => h.window.open('tutorial/getting_started.xhtml', '_blank')));
                    780:   menu.add(new MenuItem(LCDStrings.get('author_manual'),
                    781:       () => h.window.open('/adm/help/author.manual.pdf', '_blank')));
1.41      damieng   782:   menu.add(new MenuItem(LCDStrings.get('about_lcdaxe'),
                    783:       () => (new AboutLCDaxeDlg()).show()));
1.33      damieng   784:   return(menu);
                    785: }

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