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