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