Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.41
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.35 damieng 352: // Initialization
1.1 damieng 353: Future.wait([Strings.load(), LCDStrings.load(), _readTemplates('templates.xml')]).then((List responses) {
1.29 damieng 354: initDaxe(left:left, saveFunction:saveFunction).then((v) {
1.35 damieng 355: // More customizations
356: _addGuttersAndPreview();
1.28 damieng 357: // change insert panel
1.1 damieng 358: // add things to the toolbar
359: ToolbarMenu sectionMenu = _makeSectionMenu();
360: if (sectionMenu != null)
361: page.toolbar.add(sectionMenu);
1.24 damieng 362: ToolbarBox mathBox;
363: if (doc.saveURL != null)
1.30 damieng 364: mathBox = page.toolbar.items[5];
1.24 damieng 365: else
1.30 damieng 366: mathBox = page.toolbar.items[4];
1.1 damieng 367: x.Element texRef = doc.cfg.elementReference('m');
368: if (texRef != null) {
369: ToolbarButton texButton = new ToolbarButton(
1.24 damieng 370: LCDStrings.get('insert_m'), 'images/tex.png',
1.1 damieng 371: () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate,
372: data:new ToolbarStyleInfo([texRef], null, null));
1.23 damieng 373: mathBox.add(texButton);
1.1 damieng 374: }
1.24 damieng 375: x.Element lmRef = doc.cfg.elementReference('lm');
376: if (lmRef != null) {
377: ToolbarButton lmButton = new ToolbarButton(
378: LCDStrings.get('insert_lm'), 'packages/daxe/images/toolbar/equation.png',
379: () => doc.insertNewNode(lmRef, 'element'), Toolbar.insertButtonUpdate,
380: data:new ToolbarStyleInfo([lmRef], null, null));
381: mathBox.add(lmButton);
382: }
1.1 damieng 383: h.Element tbh = h.querySelector('.toolbar');
384: tbh.replaceWith(page.toolbar.html());
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";
458: rightGutter.onMouseDown.listen((h.MouseEvent event) {
459: moving_right_gutter = true;
460: event.preventDefault();
461: previewIFrame.style.pointerEvents = 'none';
462: });
463: h.document.body.append(rightGutter);
464: doc1.style.right = "${previewWidth + 10}px";
465: h.document.body.append(previewDiv);
466: }
467:
468: // mousedown on left gutter
469: leftGutter.onMouseDown.listen((h.MouseEvent event) {
470: moving_left_gutter = true;
471: event.preventDefault();
472: if (previewIFrame != null)
473: previewIFrame.style.pointerEvents = 'none';
474: });
475:
476: // adjust top positions when the toolbar height changes
477: _adjustPositionsUnderToolbar();
478: h.window.onResize.listen((h.Event event) => _adjustPositionsUnderToolbar());
479:
480: // mousemove for both gutters
481: h.document.onMouseMove.listen((h.MouseEvent event) {
482: if (!(moving_left_gutter || moving_right_gutter))
483: return;
484: h.Element left = h.document.getElementById('left_panel');
485: h.Element doc1 = h.document.getElementById('doc1');
486: num x = event.client.x;
487: x = x.round();
1.36 damieng 488: // for IE, temporary change of overflow for doc1,
489: // otherwise IE and Edge never report an empty width when there is a scrollbar
490: // (this slows things down, so it is done only for IE)
491: bool ie = h.window.navigator.appVersion.contains("Trident") || h.window.navigator.appVersion.contains("Edge");
492: if (ie)
493: doc1.style.overflow = 'hidden';
494: num doc1width = doc1.offsetWidth;
495: if (ie)
496: doc1.style.overflow = '';
1.35 damieng 497: if (moving_left_gutter) {
498: h.Element leftGutter = h.document.getElementById('left_gutter');
499: if (x < 5)
500: x = 5;
1.36 damieng 501: int maxX = left.offsetWidth + 5 + doc1width;
1.35 damieng 502: if (x > maxX)
503: x = maxX;
504: if (x == 5)
505: left.style.visibility = 'hidden';
1.36 damieng 506: else
507: left.style.visibility = '';
508: left.style.width = "${x-5}px";
1.35 damieng 509: leftGutter.style.left ="${x-5}px";
1.36 damieng 510: if (x == maxX)
511: doc1.style.visibility = 'hidden';
512: else
513: doc1.style.visibility = '';
1.35 damieng 514: doc1.style.left ="${x+5}px";
515: } else if (moving_right_gutter) {
516: h.Element previewDiv = h.document.getElementById('preview_div');
517: h.Element rightGutter = h.document.getElementById('right_gutter');
518: int minX = left.offsetWidth + 10 + 5;
519: if (x < minX)
520: x = minX;
1.36 damieng 521: int maxX = left.offsetWidth + 10 + doc1width + 5 + previewDiv.offsetWidth;
1.35 damieng 522: if (x > maxX)
523: x = maxX;
524: int previewWidth = maxX - x;
525: if (previewWidth == 0)
526: previewDiv.style.visibility = 'hidden';
1.36 damieng 527: else
528: previewDiv.style.visibility = '';
529: previewDiv.style.width = "${previewWidth}px";
1.35 damieng 530: rightGutter.style.right ="${previewWidth}px";
1.36 damieng 531: if (x == minX)
532: doc1.style.visibility = 'hidden';
533: else
534: doc1.style.visibility = '';
1.35 damieng 535: doc1.style.right ="${previewWidth + 10}px";
536: }
537: event.stopPropagation();
538: event.preventDefault();
539: });
540:
541: // mouseup
542: var mouseup = (h.MouseEvent event) {
543: if (moving_left_gutter || moving_right_gutter) {
544: h.IFrameElement previewIFrame = h.document.getElementById('preview_iframe');
545: if (previewIFrame != null) {
546: // restore events on iframe
547: previewIFrame.style.pointerEvents = '';
548: }
549: moving_left_gutter = false;
550: moving_right_gutter = false;
551: }
552: };
553: h.document.onMouseUp.listen(mouseup);
554: // if we are in an iframe, we need to use the parent to get
555: // mouseup events outside it with some browsers like Chromium
556: var parent = new js.JsObject.fromBrowserObject(js.context['window'])['parent'];
557: if (parent is h.Window) {
558: // happens with Dartium and Firefox
559: parent.onMouseUp.listen((h.MouseEvent event) => mouseup(event));
560: } else if (parent is js.JsObject) {
561: // happens with Chromium
562: parent.callMethod('addEventListener', ['mouseup', mouseup, false]);
563: }
564: }
565:
566: /*
567: * This complements WebPage.adjustPositionsUnderToolbar with positions for the gutters and preview.
568: */
569: void _adjustPositionsUnderToolbar() {
570: h.Element headers = h.document.getElementById('headers');
571: num y = headers.getBoundingClientRect().bottom;
572: String cssTop = (y.round() + 2).toString() + "px";
573: h.document.getElementById('left_gutter').style.top = cssTop;
574: h.DivElement rightGutter = h.document.getElementById('right_gutter');
575: if (rightGutter != null)
576: rightGutter.style.top = cssTop;
577: h.DivElement previewDiv = h.document.getElementById('preview_div');
578: if (previewDiv != null)
579: previewDiv.style.top = cssTop;
580: }
581:
582:
1.1 damieng 583: ToolbarMenu _makeSectionMenu() {
1.18 damieng 584: // sort section roles by title
585: knownSectionRoles.sort((String role1, String role2) => LCDStrings.get(role1).compareTo(LCDStrings.get(role2)));
586:
1.1 damieng 587: Menu menu = new Menu(LCDStrings.get('Section'));
588: List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
589: if (sectionRefs == null || sectionRefs.length == 0)
590: return(null);
591: x.Element h1Ref = doc.cfg.elementReference('h1');
1.18 damieng 592: for (String role in knownSectionRoles) {
1.1 damieng 593: MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
594: data:new ToolbarStyleInfo(sectionRefs, null, null));
595: menuItem.action = () {
596: ToolbarStyleInfo info = menuItem.data;
597: x.Element sectionRef = info.validRef;
1.18 damieng 598: Section section = new Section.fromRef(sectionRef);
1.1 damieng 599: section.state = 1;
600: section.setAttribute('class', 'role-' + role);
1.17 damieng 601: H1 h1 = NodeFactory.create(h1Ref);
1.1 damieng 602: h1.state = 1;
1.18 damieng 603: h1.appendChild(new DNText(LCDStrings.get(role)));
1.9 damieng 604: section.appendChild(h1);
605: x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
1.18 damieng 606: DNHiddenP p = new DNHiddenP.fromRef(hiddenp);
1.9 damieng 607: section.appendChild(p);
1.17 damieng 608: section.setupSimpleUI();
1.1 damieng 609: if (doc.insert2(section, page.getSelectionStart())) {
1.18 damieng 610: page.cursor.moveTo(new Position(p, 0));
1.1 damieng 611: page.updateAfterPathChange();
612: }
613: };
614: menu.add(menuItem);
615: }
616: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
617: return(tbmenu);
618: }
619:
1.35 damieng 620:
1.1 damieng 621: Future<x.Document> _readTemplates(String templatesPath) {
622: x.DOMParser dp = new x.DOMParser();
623: return(dp.parseFromURL(templatesPath));
624: }
625:
626: Menu _makeTemplatesMenu(x.Document templatesDoc) {
1.26 damieng 627: List<x.Element> problemRefs = doc.cfg.elementReferences('problem');
628: if (problemRefs == null || problemRefs.length == 0)
629: return(null);
1.1 damieng 630: Menu menu = new Menu(LCDStrings.get('Templates'));
631: x.Element templates = templatesDoc.documentElement;
632: for (x.Node child in templates.childNodes) {
633: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
634: menu.add(_makeMenu(child));
635: }
636: }
637: return(menu);
638: }
639:
640: Menu _makeMenu(x.Element el) {
641: String locale = LCDStrings.systemLocale;
642: String defaultLocale = LCDStrings.defaultLocale;
643: String title;
644: for (x.Node child in el.childNodes) {
645: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
646: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
647: if ((child as x.Element).getAttribute('lang') == locale) {
648: title = child.firstChild.nodeValue;
649: break;
650: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
651: title = child.firstChild.nodeValue;
652: }
653: }
654: }
655: }
656: if (title == null)
657: title = '?';
658: Menu menu = new Menu(title);
659: for (x.Node child in el.childNodes) {
660: if (child.nodeType == x.Node.ELEMENT_NODE) {
661: if (child.nodeName == 'menu') {
662: menu.add(_makeMenu(child));
663: } else if (child.nodeName == 'item') {
664: menu.add(_makeItem(child));
665: }
666: }
667: }
668: return(menu);
669: }
670:
671: MenuItem _makeItem(x.Element item) {
672: String locale = LCDStrings.systemLocale;
673: String defaultLocale = LCDStrings.defaultLocale;
674: String path, type, title, help;
675: for (x.Node child in item.childNodes) {
676: if (child.nodeType == x.Node.ELEMENT_NODE) {
677: if (child.nodeName == 'title') {
678: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
679: if ((child as x.Element).getAttribute('lang') == locale) {
680: title = child.firstChild.nodeValue;
681: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
682: title = child.firstChild.nodeValue;
683: }
684: }
685: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
686: path = child.firstChild.nodeValue;
687: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
688: type = child.firstChild.nodeValue;
689: } else if (child.nodeName == 'help') {
690: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
691: if ((child as x.Element).getAttribute('lang') == locale) {
692: help = child.firstChild.nodeValue;
693: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
694: help = child.firstChild.nodeValue;
695: }
696: }
697: }
698: }
699: }
700: if (type == null) {
701: print("Warning: missing type for template $title\n");
702: type = 'problem';
703: }
704: x.Element refElement = doc.cfg.elementReference(type);
705: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
706: if (help != null)
707: menuItem.toolTipText = help;
708: return menuItem;
709: }
710:
711: void _insertTemplate(String filePath) {
712: try {
713: x.DOMParser dp = new x.DOMParser();
714: dp.parseFromURL(filePath).then((x.Document templateDoc) {
715: x.Element root = templateDoc.documentElement;
716: if (root == null)
717: return;
718: doc.removeWhitespace(root);
719: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
1.27 damieng 720: if (dnRoot is LCDBlock && dnRoot.attributes.length > 0)
721: dnRoot.state = 0; // make attributes editable for the template root
1.1 damieng 722: UndoableEdit edit;
723: Position pos = page.getSelectionStart();
724: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
725: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12 damieng 726: else {
727: DaxeNode parent = pos.dn;
728: if (parent is DNText)
729: parent = parent.parent;
730: if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
731: // The node must be inserted outside of the paragraph.
732: // If there is something in the paragraph to the right of the cursor, it must be
733: // moved into a new paragraph after the inserted node.
734: // (as in DaxeDocument.insert2)
735: DNHiddenP p = parent;
736: edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
737: Position pend = new Position(p, p.offsetLength);
738: pend.moveInsideTextNodeIfPossible();
739: if (pos < pend) {
740: DaxeNode clone = doc.cloneBetween(pos, pend);
741: edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
742: DNHiddenP newp = NodeFactory.create(p.ref);
743: edit.addSubEdit(new UndoableEdit.insertNode(
744: new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
745: edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
746: }
747: edit.addSubEdit(new UndoableEdit.insertNode(
748: new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
749: } else
750: edit = new UndoableEdit.insertNode(pos, dnRoot);
751: }
1.1 damieng 752: doc.doNewEdit(edit);
753: page.updateAfterPathChange();
1.8 damieng 754: if (dnRoot.nodeName != 'loncapa')
755: page.scrollToNode(dnRoot);
1.1 damieng 756: });
757: } on x.DOMException catch(ex) {
758: h.window.alert(ex.toString());
759: }
760: }
1.33 damieng 761:
762: Menu _makeHelpMenu() {
763: Menu menu = new Menu(LCDStrings.get('help'));
1.38 damieng 764: menu.add(new MenuItem(LCDStrings.get('getting_started'),
765: () => h.window.open('tutorial/getting_started.xhtml', '_blank')));
766: menu.add(new MenuItem(LCDStrings.get('author_manual'),
767: () => h.window.open('/adm/help/author.manual.pdf', '_blank')));
1.41 ! damieng 768: menu.add(new MenuItem(LCDStrings.get('about_lcdaxe'),
! 769: () => (new AboutLCDaxeDlg()).show()));
1.33 damieng 770: return(menu);
771: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>