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