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