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