MDL-60008 assignfeedback_editpdf: Show warnings
[moodle.git] / mod / assign / feedback / editpdf / yui / src / editor / js / editor.js
CommitLineData
5c386472
DW
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
ad3f8cd1
DP
15/* eslint-disable no-unused-vars */
16/* global SELECTOR, TOOLSELECTOR, AJAXBASE, COMMENTCOLOUR, ANNOTATIONCOLOUR, AJAXBASEPROGRESS, CLICKTIMEOUT */
5c386472
DW
17
18/**
19 * Provides an in browser PDF editor.
20 *
21 * @module moodle-assignfeedback_editpdf-editor
22 */
23
24/**
25 * EDITOR
26 * This is an in browser PDF editor.
27 *
1f777e5c
AN
28 * @namespace M.assignfeedback_editpdf
29 * @class editor
5c386472
DW
30 * @constructor
31 * @extends Y.Base
32 */
557f44d9 33var EDITOR = function() {
5c386472
DW
34 EDITOR.superclass.constructor.apply(this, arguments);
35};
36EDITOR.prototype = {
37
5c386472
DW
38 /**
39 * The dialogue used for all action menu displays.
1f777e5c 40 *
5c386472
DW
41 * @property type
42 * @type M.core.dialogue
43 * @protected
44 */
5bb4f444 45 dialogue: null,
5c386472 46
bb690849
DW
47 /**
48 * The panel used for all action menu displays.
49 *
50 * @property type
51 * @type Y.Node
52 * @protected
53 */
5bb4f444 54 panel: null,
bb690849 55
5c386472
DW
56 /**
57 * The number of pages in the pdf.
1f777e5c
AN
58 *
59 * @property pagecount
60 * @type Number
5c386472
DW
61 * @protected
62 */
5bb4f444 63 pagecount: 0,
5c386472
DW
64
65 /**
66 * The active page in the editor.
1f777e5c
AN
67 *
68 * @property currentpage
69 * @type Number
5c386472
DW
70 * @protected
71 */
5bb4f444 72 currentpage: 0,
5c386472
DW
73
74 /**
75 * A list of page objects. Each page has a list of comments and annotations.
1f777e5c
AN
76 *
77 * @property pages
5c386472
DW
78 * @type array
79 * @protected
80 */
5bb4f444 81 pages: [],
5c386472 82
f7a9f1dd
AN
83 /**
84 * The reported status of the document.
85 *
86 * @property documentstatus
87 * @type int
88 * @protected
89 */
90 documentstatus: 0,
91
5c386472
DW
92 /**
93 * The yui node for the loading icon.
1f777e5c
AN
94 *
95 * @property loadingicon
96 * @type Node
5c386472
DW
97 * @protected
98 */
5bb4f444 99 loadingicon: null,
5c386472
DW
100
101 /**
102 * Image object of the current page image.
1f777e5c
AN
103 *
104 * @property pageimage
5c386472
DW
105 * @type Image
106 * @protected
107 */
5bb4f444 108 pageimage: null,
5c386472
DW
109
110 /**
111 * YUI Graphic class for drawing shapes.
1f777e5c
AN
112 *
113 * @property graphic
114 * @type Graphic
5c386472
DW
115 * @protected
116 */
5bb4f444 117 graphic: null,
5c386472
DW
118
119 /**
120 * Info about the current edit operation.
1f777e5c 121 *
5c386472
DW
122 * @property currentedit
123 * @type M.assignfeedback_editpdf.edit
124 * @protected
125 */
5bb4f444 126 currentedit: new M.assignfeedback_editpdf.edit(),
5c386472
DW
127
128 /**
129 * Current drawable.
1f777e5c 130 *
5c386472 131 * @property currentdrawable
1f777e5c 132 * @type M.assignfeedback_editpdf.drawable|false
5c386472
DW
133 * @protected
134 */
5bb4f444 135 currentdrawable: false,
5c386472
DW
136
137 /**
138 * Current drawables.
1f777e5c 139 *
5c386472
DW
140 * @property drawables
141 * @type array(M.assignfeedback_editpdf.drawable)
142 * @protected
143 */
5bb4f444 144 drawables: [],
5c386472
DW
145
146 /**
147 * Current comment when the comment menu is open.
148 * @property currentcomment
149 * @type M.assignfeedback_editpdf.comment
150 * @protected
151 */
5bb4f444 152 currentcomment: null,
5c386472
DW
153
154 /**
155 * Current annotation when the select tool is used.
156 * @property currentannotation
157 * @type M.assignfeedback_editpdf.annotation
158 * @protected
159 */
5bb4f444 160 currentannotation: null,
5c386472 161
324debc3
JD
162 /**
163 * Track the previous annotation so we can remove selection highlights.
164 * @property lastannotation
165 * @type M.assignfeedback_editpdf.annotation
166 * @protected
167 */
168 lastannotation: null,
169
5c386472
DW
170 /**
171 * Last selected annotation tool
172 * @property lastannotationtool
173 * @type String
174 * @protected
175 */
7962b484 176 lastannotationtool: "pen",
5c386472
DW
177
178 /**
179 * The users comments quick list
180 * @property quicklist
181 * @type M.assignfeedback_editpdf.quickcommentlist
182 * @protected
183 */
5bb4f444 184 quicklist: null,
5c386472
DW
185
186 /**
187 * The search comments window.
188 * @property searchcommentswindow
189 * @type M.core.dialogue
190 * @protected
191 */
5bb4f444 192 searchcommentswindow: null,
5c386472
DW
193
194
195 /**
196 * The selected stamp picture.
197 * @property currentstamp
198 * @type String
199 * @protected
200 */
5bb4f444 201 currentstamp: null,
5c386472
DW
202
203 /**
204 * The stamps.
205 * @property stamps
206 * @type Array
207 * @protected
208 */
5bb4f444 209 stamps: [],
5c386472 210
60b87080
DS
211 /**
212 * Prevent new comments from appearing
213 * immediately after clicking off a current
214 * comment
215 * @property editingcomment
216 * @type Boolean
217 * @public
218 */
5bb4f444 219 editingcomment: false,
60b87080 220
23990a0a
TB
221 /**
222 * Should inactive comments be collapsed?
223 *
224 * @property collapsecomments
225 * @type Boolean
226 * @public
227 */
228 collapsecomments: true,
229
5c386472
DW
230 /**
231 * Called during the initialisation process of the object.
232 * @method initializer
233 */
5bb4f444 234 initializer: function() {
5c386472
DW
235 var link;
236
237 link = Y.one('#' + this.get('linkid'));
238
239 if (link) {
240 link.on('click', this.link_handler, this);
241 link.on('key', this.link_handler, 'down:13', this);
242
bb690849
DW
243 // We call the amd module to see if we can take control of the review panel.
244 require(['mod_assign/grading_review_panel'], function(ReviewPanelManager) {
245 var panelManager = new ReviewPanelManager();
246
247 var panel = panelManager.getReviewPanel('assignfeedback_editpdf');
248 if (panel) {
249 panel = Y.one(panel);
250 panel.empty();
251 link.ancestor('.fitem').hide();
252 this.open_in_panel(panel);
253 }
254 this.currentedit.start = false;
255 this.currentedit.end = false;
256 if (!this.get('readonly')) {
257 this.quicklist = new M.assignfeedback_editpdf.quickcommentlist(this);
258 }
259 }.bind(this));
260
5c386472
DW
261 }
262 },
263
264 /**
265 * Called to show/hide buttons and set the current colours/stamps.
266 * @method refresh_button_state
267 */
5bb4f444 268 refresh_button_state: function() {
166ac972 269 var button, currenttoolnode, imgurl, drawingregion, stampimgurl, drawingcanvas;
5c386472
DW
270
271 // Initalise the colour buttons.
da91de78 272 button = this.get_dialogue_element(SELECTOR.COMMENTCOLOURBUTTON);
5c386472
DW
273
274 imgurl = M.util.image_url('background_colour_' + this.currentedit.commentcolour, 'assignfeedback_editpdf');
275 button.one('img').setAttribute('src', imgurl);
276
277 if (this.currentedit.commentcolour === 'clear') {
278 button.one('img').setStyle('borderStyle', 'dashed');
279 } else {
280 button.one('img').setStyle('borderStyle', 'solid');
281 }
282
da91de78 283 button = this.get_dialogue_element(SELECTOR.ANNOTATIONCOLOURBUTTON);
5c386472
DW
284 imgurl = M.util.image_url('colour_' + this.currentedit.annotationcolour, 'assignfeedback_editpdf');
285 button.one('img').setAttribute('src', imgurl);
286
da91de78 287 currenttoolnode = this.get_dialogue_element(TOOLSELECTOR[this.currentedit.tool]);
5c386472
DW
288 currenttoolnode.addClass('assignfeedback_editpdf_selectedbutton');
289 currenttoolnode.setAttribute('aria-pressed', 'true');
ab4d78ea
DW
290 drawingregion = this.get_dialogue_element(SELECTOR.DRAWINGREGION);
291 drawingregion.setAttribute('data-currenttool', this.currentedit.tool);
5c386472 292
da91de78 293 button = this.get_dialogue_element(SELECTOR.STAMPSBUTTON);
166ac972
TB
294 stampimgurl = this.get_stamp_image_url(this.currentedit.stamp);
295 button.one('img').setAttrs({'src': stampimgurl,
5c386472
DW
296 'height': '16',
297 'width': '16'});
166ac972
TB
298
299 drawingcanvas = this.get_dialogue_element(SELECTOR.DRAWINGCANVAS);
300 switch (this.currentedit.tool) {
301 case 'drag':
302 drawingcanvas.setStyle('cursor', 'move');
303 break;
304 case 'highlight':
305 drawingcanvas.setStyle('cursor', 'text');
306 break;
307 case 'select':
308 drawingcanvas.setStyle('cursor', 'default');
309 break;
310 case 'stamp':
311 drawingcanvas.setStyle('cursor', 'url(' + stampimgurl + '), crosshair');
312 break;
313 default:
314 drawingcanvas.setStyle('cursor', 'crosshair');
315 }
5c386472
DW
316 },
317
318 /**
319 * Called to get the bounds of the drawing region.
320 * @method get_canvas_bounds
321 */
5bb4f444 322 get_canvas_bounds: function() {
da91de78 323 var canvas = this.get_dialogue_element(SELECTOR.DRAWINGCANVAS),
5c386472
DW
324 offsetcanvas = canvas.getXY(),
325 offsetleft = offsetcanvas[0],
326 offsettop = offsetcanvas[1],
327 width = parseInt(canvas.getStyle('width'), 10),
328 height = parseInt(canvas.getStyle('height'), 10);
329
330 return new M.assignfeedback_editpdf.rect(offsetleft, offsettop, width, height);
331 },
332
333 /**
334 * Called to translate from window coordinates to canvas coordinates.
335 * @method get_canvas_coordinates
336 * @param M.assignfeedback_editpdf.point point in window coordinats.
337 */
5bb4f444 338 get_canvas_coordinates: function(point) {
5c386472
DW
339 var bounds = this.get_canvas_bounds(),
340 newpoint = new M.assignfeedback_editpdf.point(point.x - bounds.x, point.y - bounds.y);
341
342 bounds.x = bounds.y = 0;
343
344 newpoint.clip(bounds);
345 return newpoint;
346 },
347
348 /**
349 * Called to translate from canvas coordinates to window coordinates.
350 * @method get_window_coordinates
351 * @param M.assignfeedback_editpdf.point point in window coordinats.
352 */
5bb4f444 353 get_window_coordinates: function(point) {
5c386472
DW
354 var bounds = this.get_canvas_bounds(),
355 newpoint = new M.assignfeedback_editpdf.point(point.x + bounds.x, point.y + bounds.y);
356
357 return newpoint;
358 },
359
bb690849
DW
360 /**
361 * Open the edit-pdf editor in the panel in the page instead of a popup.
362 * @method open_in_panel
363 */
5bb4f444 364 open_in_panel: function(panel) {
bb690849
DW
365 var drawingcanvas, drawingregion;
366
367 this.panel = panel;
368 panel.append(this.get('body'));
369 panel.addClass(CSS.DIALOGUE);
370
371 this.loadingicon = this.get_dialogue_element(SELECTOR.LOADINGICON);
372
373 drawingcanvas = this.get_dialogue_element(SELECTOR.DRAWINGCANVAS);
5bb4f444 374 this.graphic = new Y.Graphic({render: drawingcanvas});
bb690849
DW
375
376 drawingregion = this.get_dialogue_element(SELECTOR.DRAWINGREGION);
377 drawingregion.on('scroll', this.move_canvas, this);
378
379 if (!this.get('readonly')) {
380 drawingcanvas.on('gesturemovestart', this.edit_start, null, this);
381 drawingcanvas.on('gesturemove', this.edit_move, null, this);
382 drawingcanvas.on('gesturemoveend', this.edit_end, null, this);
383
384 this.refresh_button_state();
385 }
386
f7a9f1dd 387 this.start_generation();
bb690849
DW
388 },
389
5c386472
DW
390 /**
391 * Called to open the pdf editing dialogue.
392 * @method link_handler
393 */
5bb4f444 394 link_handler: function(e) {
bc8b6dc6
DP
395 var drawingcanvas, drawingregion;
396 var resize = true;
5c386472
DW
397 e.preventDefault();
398
399 if (!this.dialogue) {
400 this.dialogue = new M.core.dialogue({
401 headerContent: this.get('header'),
402 bodyContent: this.get('body'),
403 footerContent: this.get('footer'),
aebc768f 404 modal: true,
5c386472 405 width: '840px',
6d87dceb
DW
406 visible: false,
407 draggable: true
5c386472
DW
408 });
409
5c386472
DW
410 // Add custom class for styling.
411 this.dialogue.get('boundingBox').addClass(CSS.DIALOGUE);
412
da91de78 413 this.loadingicon = this.get_dialogue_element(SELECTOR.LOADINGICON);
5c386472 414
da91de78 415 drawingcanvas = this.get_dialogue_element(SELECTOR.DRAWINGCANVAS);
5bb4f444 416 this.graphic = new Y.Graphic({render: drawingcanvas});
5c386472 417
da91de78 418 drawingregion = this.get_dialogue_element(SELECTOR.DRAWINGREGION);
50c12f01
DS
419 drawingregion.on('scroll', this.move_canvas, this);
420
5c386472
DW
421 if (!this.get('readonly')) {
422 drawingcanvas.on('gesturemovestart', this.edit_start, null, this);
423 drawingcanvas.on('gesturemove', this.edit_move, null, this);
424 drawingcanvas.on('gesturemoveend', this.edit_end, null, this);
425
426 this.refresh_button_state();
427 }
d40ce26f 428
f7a9f1dd 429 this.start_generation();
50c12f01
DS
430 drawingcanvas.on('windowresize', this.resize, this);
431
432 resize = false;
5c386472 433 }
6d87dceb
DW
434 this.dialogue.centerDialogue();
435 this.dialogue.show();
50c12f01
DS
436
437 // Redraw when the dialogue is moved, to ensure the absolute elements are all positioned correctly.
438 this.dialogue.dd.on('drag:end', this.redraw, this);
439 if (resize) {
440 this.resize(); // When re-opening the dialog call redraw, to make sure the size + layout is correct.
441 }
5c386472
DW
442 },
443
444 /**
445 * Called to load the information and annotations for all pages.
f7a9f1dd
AN
446 *
447 * @method start_generation
5c386472 448 */
f7a9f1dd
AN
449 start_generation: function() {
450 this.poll_document_conversion_status();
451 },
5c386472 452
f7a9f1dd
AN
453 /**
454 * Poll the current document conversion status and start the next step
455 * in the process.
456 *
457 * @method poll_document_conversion_status
458 */
459 poll_document_conversion_status: function() {
460 if (this.get('destroyed')) {
461 return;
462 }
463
464 Y.io(AJAXBASE, {
5c386472
DW
465 method: 'get',
466 context: this,
467 sync: false,
5bb4f444
DP
468 data: {
469 sesskey: M.cfg.sesskey,
f7a9f1dd 470 action: 'pollconversions',
5bb4f444
DP
471 userid: this.get('userid'),
472 attemptnumber: this.get('attemptnumber'),
473 assignmentid: this.get('assignmentid'),
474 readonly: this.get('readonly') ? 1 : 0
5c386472
DW
475 },
476 on: {
477 success: function(tid, response) {
f7a9f1dd
AN
478 var data = this.handle_response_data(response),
479 poll = false;
480 if (data) {
481 this.documentstatus = data.status;
482 if (data.status === 0) {
483 // The combined document is still waiting for input to be ready.
484 poll = true;
485
486 } else if (data.status === 1) {
487 // The combine document is ready for conversion into a single PDF.
488 poll = true;
489
490 } else if (data.status === 2 || data.status === -1) {
491 // The combined PDF is ready.
492 // We now know the page count and can convert it to a set of images.
493 this.pagecount = data.pagecount;
494
495 if (data.pageready == data.pagecount) {
496 this.prepare_pages_for_display(data);
497 } else {
498 // Some pages are not ready yet.
499 // Note: We use a different polling process here which does not block.
500 this.update_page_load_progress();
501
502 // Fetch the images for the combined document.
503 this.start_document_to_image_conversion();
504 }
505 }
506
507 if (poll) {
508 // Check again in 1 second.
509 Y.later(1000, this, this.poll_document_conversion_status);
510 }
511 }
5c386472
DW
512 },
513 failure: function(tid, response) {
aa3e4bde 514 return new M.core.exception(response.responseText);
5c386472
DW
515 }
516 }
f7a9f1dd
AN
517 });
518 },
aa3e4bde 519
f7a9f1dd
AN
520 /**
521 * Spwan the PDF to Image conversion on the server.
522 *
523 * @method get_images_for_documents
524 */
525 start_document_to_image_conversion: function() {
526 if (this.get('destroyed')) {
527 return;
528 }
529 Y.io(AJAXBASE, {
530 method: 'get',
531 context: this,
532 sync: false,
533 data: {
534 sesskey: M.cfg.sesskey,
535 action: 'pollconversions',
536 userid: this.get('userid'),
537 attemptnumber: this.get('attemptnumber'),
538 assignmentid: this.get('assignmentid'),
539 readonly: this.get('readonly') ? 1 : 0
540 },
541 on: {
542 success: function(tid, response) {
543 var data = this.handle_response_data(response);
544 if (data) {
545 this.documentstatus = data.status;
546 if (data.status === 2) {
547 // The pages are ready. Add all of the annotations to them.
548 this.prepare_pages_for_display(data);
aa3e4bde 549 }
aa3e4bde 550 }
f7a9f1dd
AN
551 },
552 failure: function(tid, response) {
553 return new M.core.exception(response.responseText);
aa3e4bde 554 }
f7a9f1dd
AN
555 }
556 });
5c386472
DW
557 },
558
86c57709
DW
559 /**
560 * Display an error in a small part of the page (don't block everything).
561 *
562 * @param string The error text.
563 * @protected
564 * @method warning
565 */
566 warning: function(message) {
567 var drawingregion = this.get_dialogue_element(SELECTOR.DRAWINGREGION);
568
569 var warningelement = Y.Node.create('<div class="alert alert-warning" role="alert"></div>');
570 warningelement.setHTML(message);
571 drawingregion.prepend(warningelement);
572 },
573
5c386472
DW
574 /**
575 * The info about all pages in the pdf has been returned.
f7a9f1dd 576 *
5c386472
DW
577 * @param string The ajax response as text.
578 * @protected
f7a9f1dd 579 * @method prepare_pages_for_display
5c386472 580 */
f7a9f1dd 581 prepare_pages_for_display: function(data) {
86c57709
DW
582 var i, j, comment, error, annotation, readonly;
583
f7a9f1dd 584 if (!data.pagecount) {
bb690849
DW
585 if (this.dialogue) {
586 this.dialogue.hide();
587 }
5c386472 588 // Display alert dialogue.
f7a9f1dd 589 error = new M.core.alert({message: M.util.get_string('cannotopenpdf', 'assignfeedback_editpdf')});
5c386472
DW
590 error.show();
591 return;
592 }
593
5c386472
DW
594 this.pages = data.pages;
595
596 for (i = 0; i < this.pages.length; i++) {
597 for (j = 0; j < this.pages[i].comments.length; j++) {
598 comment = this.pages[i].comments[j];
599 this.pages[i].comments[j] = new M.assignfeedback_editpdf.comment(this,
600 comment.gradeid,
601 comment.pageno,
602 comment.x,
603 comment.y,
604 comment.width,
605 comment.colour,
606 comment.rawtext);
607 }
608 for (j = 0; j < this.pages[i].annotations.length; j++) {
86c57709
DW
609 annotation = this.pages[i].annotations[j];
610 this.pages[i].annotations[j] = this.create_annotation(annotation.type, annotation);
5c386472
DW
611 }
612 }
613
86c57709
DW
614 readonly = this.get('readonly');
615 if (!readonly && data.partial) {
616 // Warn about non converted files, but only for teachers.
617 this.warning(M.util.get_string('partialwarning', 'assignfeedback_editpdf'));
618 }
619
5c386472 620 // Update the ui.
8ae76e13
AN
621 if (this.quicklist) {
622 this.quicklist.load();
623 }
5c386472
DW
624 this.setup_navigation();
625 this.setup_toolbar();
626 this.change_page();
627 },
628
f7a9f1dd
AN
629 /**
630 * Fetch the page images.
631 *
632 * @method update_page_load_progress
633 */
634 update_page_load_progress: function() {
635 if (this.get('destroyed')) {
636 return;
637 }
638 var checkconversionstatus,
639 ajax_error_total = 0,
640 progressbar = this.get_dialogue_element(SELECTOR.PROGRESSBARCONTAINER + ' .bar');
641
642 if (!progressbar) {
643 return;
644 }
645
646 // If pages are not loaded, check PDF conversion status for the progress bar.
647 checkconversionstatus = {
648 method: 'get',
649 context: this,
650 sync: false,
651 data: {
652 sesskey: M.cfg.sesskey,
653 action: 'conversionstatus',
654 userid: this.get('userid'),
655 attemptnumber: this.get('attemptnumber'),
656 assignmentid: this.get('assignmentid')
657 },
658 on: {
659 success: function(tid, response) {
660 if (this.get('destroyed')) {
661 return;
662 }
663 ajax_error_total = 0;
664
665 var progress = 0;
666 var progressbar = this.get_dialogue_element(SELECTOR.PROGRESSBARCONTAINER + ' .bar');
667 if (progressbar) {
668 // Calculate progress.
669 progress = (response.response / this.pagecount) * 100;
670 progressbar.setStyle('width', progress + '%');
671 progressbar.ancestor(SELECTOR.PROGRESSBARCONTAINER).setAttribute('aria-valuenow', progress);
672
673 if (progress < 100) {
674 // Keep polling until all pages are generated.
675 M.util.js_pending('checkconversionstatus');
676 Y.later(1000, this, function() {
677 M.util.js_complete('checkconversionstatus');
678 Y.io(AJAXBASEPROGRESS, checkconversionstatus);
679 });
680 }
681 }
682 },
683 failure: function(tid, response) {
684 if (this.get('destroyed')) {
685 return;
686 }
687 ajax_error_total = ajax_error_total + 1;
688 // We only continue on error if the all pages were not generated,
689 // and if the ajax call did not produce 5 errors in the row.
690 if (this.pagecount === 0 && ajax_error_total < 5) {
691 M.util.js_pending('checkconversionstatus');
692 Y.later(1000, this, function() {
693 M.util.js_complete('checkconversionstatus');
694 Y.io(AJAXBASEPROGRESS, checkconversionstatus);
695 });
696 }
697 return new M.core.exception(response.responseText);
698 }
699 }
700 };
701 // We start the AJAX "generated page total number" call a second later to give a chance to
702 // the AJAX "combined pdf generation" call to clean the previous submission images.
703 M.util.js_pending('checkconversionstatus');
704 Y.later(1000, this, function() {
705 ajax_error_total = 0;
706 M.util.js_complete('checkconversionstatus');
707 Y.io(AJAXBASEPROGRESS, checkconversionstatus);
708 });
709 },
710
711 /**
712 * Handle response data.
713 *
714 * @method handle_response_data
715 * @param {object} response
716 * @return {object}
717 */
718 handle_response_data: function(response) {
719 if (this.get('destroyed')) {
720 return;
721 }
722 var data;
723 try {
724 data = Y.JSON.parse(response.responseText);
725 if (data.error) {
726 if (this.dialogue) {
727 this.dialogue.hide();
728 }
729
730 new M.core.alert({
731 message: M.util.get_string('cannotopenpdf', 'assignfeedback_editpdf'),
732 visible: true
733 });
734 } else {
735 return data;
736 }
737 } catch (e) {
738 if (this.dialogue) {
739 this.dialogue.hide();
740 }
741
742 new M.core.alert({
743 title: M.util.get_string('cannotopenpdf', 'assignfeedback_editpdf'),
744 visible: true
745 });
746 }
747
748 return;
749 },
750
5c386472
DW
751 /**
752 * Get the full pluginfile url for an image file - just given the filename.
753 *
754 * @public
755 * @method get_stamp_image_url
756 * @param string filename
757 */
5bb4f444 758 get_stamp_image_url: function(filename) {
5c386472
DW
759 var urls = this.get('stampfiles'),
760 fullurl = '';
761
762 Y.Array.each(urls, function(url) {
763 if (url.indexOf(filename) > 0) {
764 fullurl = url;
765 }
766 }, this);
767
768 return fullurl;
769 },
770
771 /**
772 * Attach listeners and enable the color picker buttons.
773 * @protected
774 * @method setup_toolbar
775 */
5bb4f444 776 setup_toolbar: function() {
5c386472
DW
777 var toolnode,
778 commentcolourbutton,
779 annotationcolourbutton,
780 searchcommentsbutton,
23990a0a 781 expcolcommentsbutton,
5c386472
DW
782 currentstampbutton,
783 stampfiles,
784 picker,
785 filename;
786
da91de78 787 searchcommentsbutton = this.get_dialogue_element(SELECTOR.SEARCHCOMMENTSBUTTON);
5c386472
DW
788 searchcommentsbutton.on('click', this.open_search_comments, this);
789 searchcommentsbutton.on('key', this.open_search_comments, 'down:13', this);
790
23990a0a
TB
791 expcolcommentsbutton = this.get_dialogue_element(SELECTOR.EXPCOLCOMMENTSBUTTON);
792 expcolcommentsbutton.on('click', this.expandCollapseComments, this);
793 expcolcommentsbutton.on('key', this.expandCollapseComments, 'down:13', this);
794
5c386472
DW
795 if (this.get('readonly')) {
796 return;
797 }
a0b00911 798 this.disable_touch_scroll();
923641f7 799
5c386472
DW
800 // Setup the tool buttons.
801 Y.each(TOOLSELECTOR, function(selector, tool) {
da91de78 802 toolnode = this.get_dialogue_element(selector);
5c386472
DW
803 toolnode.on('click', this.handle_tool_button, this, tool);
804 toolnode.on('key', this.handle_tool_button, 'down:13', this, tool);
805 toolnode.setAttribute('aria-pressed', 'false');
806 }, this);
807
808 // Set the default tool.
809
da91de78 810 commentcolourbutton = this.get_dialogue_element(SELECTOR.COMMENTCOLOURBUTTON);
5c386472
DW
811 picker = new M.assignfeedback_editpdf.colourpicker({
812 buttonNode: commentcolourbutton,
813 colours: COMMENTCOLOUR,
814 iconprefix: 'background_colour_',
5bb4f444 815 callback: function(e) {
5c386472
DW
816 var colour = e.target.getAttribute('data-colour');
817 if (!colour) {
818 colour = e.target.ancestor().getAttribute('data-colour');
819 }
820 this.currentedit.commentcolour = colour;
821 this.handle_tool_button(e, "comment");
822 },
823 context: this
824 });
825
da91de78 826 annotationcolourbutton = this.get_dialogue_element(SELECTOR.ANNOTATIONCOLOURBUTTON);
5c386472
DW
827 picker = new M.assignfeedback_editpdf.colourpicker({
828 buttonNode: annotationcolourbutton,
829 iconprefix: 'colour_',
830 colours: ANNOTATIONCOLOUR,
5bb4f444 831 callback: function(e) {
5c386472
DW
832 var colour = e.target.getAttribute('data-colour');
833 if (!colour) {
834 colour = e.target.ancestor().getAttribute('data-colour');
835 }
836 this.currentedit.annotationcolour = colour;
837 if (this.lastannotationtool) {
838 this.handle_tool_button(e, this.lastannotationtool);
839 } else {
840 this.handle_tool_button(e, "pen");
841 }
842 },
843 context: this
844 });
845
846 stampfiles = this.get('stampfiles');
847 if (stampfiles.length <= 0) {
da91de78 848 this.get_dialogue_element(TOOLSELECTOR.stamp).ancestor().hide();
5c386472
DW
849 } else {
850 filename = stampfiles[0].substr(stampfiles[0].lastIndexOf('/') + 1);
851 this.currentedit.stamp = filename;
da91de78 852 currentstampbutton = this.get_dialogue_element(SELECTOR.STAMPSBUTTON);
5c386472
DW
853
854 picker = new M.assignfeedback_editpdf.stamppicker({
855 buttonNode: currentstampbutton,
856 stamps: stampfiles,
857 callback: function(e) {
858 var stamp = e.target.getAttribute('data-stamp'),
859 filename;
860
861 if (!stamp) {
862 stamp = e.target.ancestor().getAttribute('data-stamp');
863 }
864 filename = stamp.substr(stamp.lastIndexOf('/'));
865 this.currentedit.stamp = filename;
673c4ffe 866 this.handle_tool_button(e, "stamp");
5c386472
DW
867 },
868 context: this
869 });
870 this.refresh_button_state();
871 }
872 },
873
874 /**
875 * Change the current tool.
876 * @protected
877 * @method handle_tool_button
878 */
5bb4f444 879 handle_tool_button: function(e, tool) {
5c386472
DW
880 var currenttoolnode;
881
882 e.preventDefault();
883
884 // Change style of the pressed button.
da91de78 885 currenttoolnode = this.get_dialogue_element(TOOLSELECTOR[this.currentedit.tool]);
5c386472
DW
886 currenttoolnode.removeClass('assignfeedback_editpdf_selectedbutton');
887 currenttoolnode.setAttribute('aria-pressed', 'false');
888 this.currentedit.tool = tool;
ab4d78ea 889
55907a73 890 if (tool !== "comment" && tool !== "select" && tool !== "drag" && tool !== "stamp") {
5c386472
DW
891 this.lastannotationtool = tool;
892 }
923641f7 893
5c386472
DW
894 this.refresh_button_state();
895 },
896
897 /**
898 * JSON encode the current page data - stripping out drawable references which cannot be encoded.
899 * @protected
900 * @method stringify_current_page
901 * @return string
902 */
5bb4f444 903 stringify_current_page: function() {
5c386472
DW
904 var comments = [],
905 annotations = [],
906 page,
907 i = 0;
908
909 for (i = 0; i < this.pages[this.currentpage].comments.length; i++) {
910 comments[i] = this.pages[this.currentpage].comments[i].clean();
911 }
912 for (i = 0; i < this.pages[this.currentpage].annotations.length; i++) {
913 annotations[i] = this.pages[this.currentpage].annotations[i].clean();
914 }
915
5bb4f444 916 page = {comments: comments, annotations: annotations};
5c386472
DW
917
918 return Y.JSON.stringify(page);
919 },
920
921 /**
922 * Generate a drawable from the current in progress edit.
923 * @protected
924 * @method get_current_drawable
925 */
5bb4f444 926 get_current_drawable: function() {
5c386472
DW
927 var comment,
928 annotation,
929 drawable = false;
930
931 if (!this.currentedit.start || !this.currentedit.end) {
932 return false;
933 }
934
935 if (this.currentedit.tool === 'comment') {
936 comment = new M.assignfeedback_editpdf.comment(this);
937 drawable = comment.draw_current_edit(this.currentedit);
938 } else {
939 annotation = this.create_annotation(this.currentedit.tool, {});
940 if (annotation) {
941 drawable = annotation.draw_current_edit(this.currentedit);
942 }
943 }
944
945 return drawable;
946 },
947
da91de78
BL
948 /**
949 * Find an element within the dialogue.
950 * @protected
951 * @method get_dialogue_element
952 */
5bb4f444 953 get_dialogue_element: function(selector) {
bb690849
DW
954 if (this.panel) {
955 return this.panel.one(selector);
956 } else {
957 return this.dialogue.get('boundingBox').one(selector);
958 }
da91de78 959 },
5c386472
DW
960
961 /**
962 * Redraw the active edit.
963 * @protected
964 * @method redraw_active_edit
965 */
5bb4f444 966 redraw_current_edit: function() {
5c386472
DW
967 if (this.currentdrawable) {
968 this.currentdrawable.erase();
969 }
970 this.currentdrawable = this.get_current_drawable();
971 },
972
973 /**
974 * Event handler for mousedown or touchstart.
975 * @protected
976 * @param Event
977 * @method edit_start
978 */
5bb4f444 979 edit_start: function(e) {
da91de78 980 var canvas = this.get_dialogue_element(SELECTOR.DRAWINGCANVAS),
5c386472
DW
981 offset = canvas.getXY(),
982 scrolltop = canvas.get('docScrollY'),
983 scrollleft = canvas.get('docScrollX'),
5bb4f444
DP
984 point = {x: e.clientX - offset[0] + scrollleft,
985 y: e.clientY - offset[1] + scrolltop},
324debc3 986 selected = false;
5c386472
DW
987
988 // Ignore right mouse click.
989 if (e.button === 3) {
990 return;
991 }
992
993 if (this.currentedit.starttime) {
994 return;
995 }
996
60b87080
DS
997 if (this.editingcomment) {
998 return;
999 }
1000
5c386472
DW
1001 this.currentedit.starttime = new Date().getTime();
1002 this.currentedit.start = point;
5bb4f444 1003 this.currentedit.end = {x: point.x, y: point.y};
5c386472
DW
1004
1005 if (this.currentedit.tool === 'select') {
557f44d9
AN
1006 var x = this.currentedit.end.x,
1007 y = this.currentedit.end.y,
1008 annotations = this.pages[this.currentpage].annotations;
5c386472
DW
1009 // Find the first annotation whose bounds encompass the click.
1010 Y.each(annotations, function(annotation) {
1011 if (((x - annotation.x) * (x - annotation.endx)) <= 0 &&
1012 ((y - annotation.y) * (y - annotation.endy)) <= 0) {
1013 selected = annotation;
1014 }
1015 });
1016
1017 if (selected) {
324debc3 1018 this.lastannotation = this.currentannotation;
5c386472 1019 this.currentannotation = selected;
324debc3 1020 if (this.lastannotation && this.lastannotation !== selected) {
5c386472 1021 // Redraw the last selected annotation to remove the highlight.
324debc3
JD
1022 if (this.lastannotation.drawable) {
1023 this.lastannotation.drawable.erase();
1024 this.drawables.push(this.lastannotation.draw());
5c386472
DW
1025 }
1026 }
1027 // Redraw the newly selected annotation to show the highlight.
1028 if (this.currentannotation.drawable) {
1029 this.currentannotation.drawable.erase();
1030 }
1031 this.drawables.push(this.currentannotation.draw());
324debc3
JD
1032 } else {
1033 this.lastannotation = this.currentannotation;
1034 this.currentannotation = null;
1035
1036 // Redraw the last selected annotation to remove the highlight.
1037 if (this.lastannotation && this.lastannotation.drawable) {
1038 this.lastannotation.drawable.erase();
1039 this.drawables.push(this.lastannotation.draw());
1040 }
5c386472
DW
1041 }
1042 }
1043 if (this.currentannotation) {
1044 // Used to calculate drag offset.
5bb4f444
DP
1045 this.currentedit.annotationstart = {x: this.currentannotation.x,
1046 y: this.currentannotation.y};
5c386472
DW
1047 }
1048 },
1049
1050 /**
1051 * Event handler for mousemove.
1052 * @protected
1053 * @param Event
1054 * @method edit_move
1055 */
5bb4f444 1056 edit_move: function(e) {
ce091776 1057 e.preventDefault();
5c386472 1058 var bounds = this.get_canvas_bounds(),
da91de78 1059 canvas = this.get_dialogue_element(SELECTOR.DRAWINGCANVAS),
55907a73 1060 drawingregion = this.get_dialogue_element(SELECTOR.DRAWINGREGION),
5c386472
DW
1061 clientpoint = new M.assignfeedback_editpdf.point(e.clientX + canvas.get('docScrollX'),
1062 e.clientY + canvas.get('docScrollY')),
55907a73
DW
1063 point = this.get_canvas_coordinates(clientpoint),
1064 diffX,
1065 diffY;
5c386472
DW
1066
1067 // Ignore events out of the canvas area.
1068 if (point.x < 0 || point.x > bounds.width || point.y < 0 || point.y > bounds.height) {
1069 return;
1070 }
1071
1072 if (this.currentedit.tool === 'pen') {
1073 this.currentedit.path.push(point);
1074 }
1075
1076 if (this.currentedit.tool === 'select') {
1077 if (this.currentannotation && this.currentedit) {
5bb4f444 1078 this.currentannotation.move(this.currentedit.annotationstart.x + point.x - this.currentedit.start.x,
5c386472
DW
1079 this.currentedit.annotationstart.y + point.y - this.currentedit.start.y);
1080 }
55907a73
DW
1081 } else if (this.currentedit.tool === 'drag') {
1082 diffX = point.x - this.currentedit.start.x;
1083 diffY = point.y - this.currentedit.start.y;
1084
1085 drawingregion.getDOMNode().scrollLeft -= diffX;
1086 drawingregion.getDOMNode().scrollTop -= diffY;
1087
5c386472
DW
1088 } else {
1089 if (this.currentedit.start) {
1090 this.currentedit.end = point;
1091 this.redraw_current_edit();
1092 }
1093 }
1094 },
1095
1096 /**
1097 * Event handler for mouseup or touchend.
1098 * @protected
1099 * @param Event
1100 * @method edit_end
1101 */
5bb4f444 1102 edit_end: function() {
5c386472
DW
1103 var duration,
1104 comment,
1105 annotation;
1106
1107 duration = new Date().getTime() - this.currentedit.start;
1108
1109 if (duration < CLICKTIMEOUT || this.currentedit.start === false) {
1110 return;
1111 }
1112
1113 if (this.currentedit.tool === 'comment') {
1114 if (this.currentdrawable) {
1115 this.currentdrawable.erase();
1116 }
1117 this.currentdrawable = false;
1118 comment = new M.assignfeedback_editpdf.comment(this);
baf881b8
RT
1119 if (comment.init_from_edit(this.currentedit)) {
1120 this.pages[this.currentpage].comments.push(comment);
1121 this.drawables.push(comment.draw(true));
1122 this.editingcomment = true;
1123 }
5c386472
DW
1124 } else {
1125 annotation = this.create_annotation(this.currentedit.tool, {});
1126 if (annotation) {
1127 if (this.currentdrawable) {
1128 this.currentdrawable.erase();
1129 }
1130 this.currentdrawable = false;
baf881b8
RT
1131 if (annotation.init_from_edit(this.currentedit)) {
1132 this.pages[this.currentpage].annotations.push(annotation);
1133 this.drawables.push(annotation.draw());
1134 }
5c386472
DW
1135 }
1136 }
1137
5c386472
DW
1138 // Save the changes.
1139 this.save_current_page();
1140
1141 // Reset the current edit.
1142 this.currentedit.starttime = 0;
1143 this.currentedit.start = false;
1144 this.currentedit.end = false;
1145 this.currentedit.path = [];
1146 },
1147
452beca9
GF
1148 /**
1149 * Resize the dialogue window when the browser is resized.
1150 * @public
1151 * @method resize
1152 */
5bb4f444 1153 resize: function() {
50c12f01
DS
1154 var drawingregion, drawregionheight;
1155
bb690849
DW
1156 if (this.dialogue) {
1157 if (!this.dialogue.get('visible')) {
1158 return;
1159 }
1160 this.dialogue.centerDialogue();
50c12f01 1161 }
50c12f01
DS
1162
1163 // Make sure the dialogue box is not bigger than the max height of the viewport.
1164 drawregionheight = Y.one('body').get('winHeight') - 120; // Space for toolbar + titlebar.
1165 if (drawregionheight < 100) {
1166 drawregionheight = 100;
1167 }
da91de78 1168 drawingregion = this.get_dialogue_element(SELECTOR.DRAWINGREGION);
bb690849 1169 if (this.dialogue) {
5bb4f444 1170 drawingregion.setStyle('maxHeight', drawregionheight + 'px');
bb690849 1171 }
50c12f01 1172 this.redraw();
452beca9
GF
1173 return true;
1174 },
1175
5c386472
DW
1176 /**
1177 * Factory method for creating annotations of the correct subclass.
1178 * @public
1179 * @method create_annotation
1180 */
5bb4f444 1181 create_annotation: function(type, data) {
5c386472
DW
1182 data.type = type;
1183 data.editor = this;
1184 if (type === "line") {
1185 return new M.assignfeedback_editpdf.annotationline(data);
1186 } else if (type === "rectangle") {
1187 return new M.assignfeedback_editpdf.annotationrectangle(data);
1188 } else if (type === "oval") {
1189 return new M.assignfeedback_editpdf.annotationoval(data);
1190 } else if (type === "pen") {
1191 return new M.assignfeedback_editpdf.annotationpen(data);
1192 } else if (type === "highlight") {
1193 return new M.assignfeedback_editpdf.annotationhighlight(data);
1194 } else if (type === "stamp") {
1195 return new M.assignfeedback_editpdf.annotationstamp(data);
1196 }
1197 return false;
1198 },
1199
1200 /**
1201 * Save all the annotations and comments for the current page.
1202 * @protected
1203 * @method save_current_page
1204 */
5bb4f444 1205 save_current_page: function() {
f7a9f1dd
AN
1206 if (this.get('destroyed')) {
1207 return;
1208 }
5c386472
DW
1209 var ajaxurl = AJAXBASE,
1210 config;
1211
1212 config = {
1213 method: 'post',
1214 context: this,
1215 sync: false,
5bb4f444
DP
1216 data: {
1217 'sesskey': M.cfg.sesskey,
1218 'action': 'savepage',
1219 'index': this.currentpage,
1220 'userid': this.get('userid'),
1221 'attemptnumber': this.get('attemptnumber'),
1222 'assignmentid': this.get('assignmentid'),
1223 'page': this.stringify_current_page()
5c386472
DW
1224 },
1225 on: {
1226 success: function(tid, response) {
1227 var jsondata;
1228 try {
1229 jsondata = Y.JSON.parse(response.responseText);
1230 if (jsondata.error) {
1231 return new M.core.ajaxException(jsondata);
1232 }
bb690849
DW
1233 Y.one(SELECTOR.UNSAVEDCHANGESINPUT).set('value', 'true');
1234 Y.one(SELECTOR.UNSAVEDCHANGESDIV).setStyle('opacity', 1);
1235 Y.one(SELECTOR.UNSAVEDCHANGESDIV).setStyle('display', 'inline-block');
1236 Y.one(SELECTOR.UNSAVEDCHANGESDIV).transition({
1237 duration: 1,
1238 delay: 2,
1239 opacity: 0
1240 }, function() {
1241 Y.one(SELECTOR.UNSAVEDCHANGESDIV).setStyle('display', 'none');
1242 });
5c386472
DW
1243 } catch (e) {
1244 return new M.core.exception(e);
1245 }
1246 },
1247 failure: function(tid, response) {
aa3e4bde 1248 return new M.core.exception(response.responseText);
5c386472
DW
1249 }
1250 }
1251 };
1252
1253 Y.io(ajaxurl, config);
5c386472
DW
1254 },
1255
1256 /**
1257 * Event handler to open the comment search interface.
1258 *
1259 * @param Event e
1260 * @protected
1261 * @method open_search_comments
1262 */
5bb4f444 1263 open_search_comments: function(e) {
5c386472
DW
1264 if (!this.searchcommentswindow) {
1265 this.searchcommentswindow = new M.assignfeedback_editpdf.commentsearch({
5bb4f444 1266 editor: this
5c386472
DW
1267 });
1268 }
1269
1270 this.searchcommentswindow.show();
1271 e.preventDefault();
1272 },
1273
23990a0a
TB
1274 /**
1275 * Toggle function to expand/collapse all comments on page.
1276 *
1277 * @protected
1278 * @method expandCollapseComments
1279 */
1280 expandCollapseComments: function() {
60e9f908
TB
1281 var comments = Y.all('.commentdrawable');
1282
23990a0a
TB
1283 if (this.collapsecomments) {
1284 this.collapsecomments = false;
60e9f908 1285 comments.removeClass('commentcollapsed');
23990a0a
TB
1286 } else {
1287 this.collapsecomments = true;
60e9f908 1288 comments.addClass('commentcollapsed');
23990a0a 1289 }
23990a0a
TB
1290 },
1291
5c386472
DW
1292 /**
1293 * Redraw all the comments and annotations.
1294 * @protected
1295 * @method redraw
1296 */
5bb4f444 1297 redraw: function() {
5c386472
DW
1298 var i,
1299 page;
1300
1301 page = this.pages[this.currentpage];
50c12f01
DS
1302 if (page === undefined) {
1303 return; // Can happen if a redraw is triggered by an event, before the page has been selected.
1304 }
5c386472
DW
1305 while (this.drawables.length > 0) {
1306 this.drawables.pop().erase();
1307 }
1308
1309 for (i = 0; i < page.annotations.length; i++) {
1310 this.drawables.push(page.annotations[i].draw());
1311 }
1312 for (i = 0; i < page.comments.length; i++) {
1313 this.drawables.push(page.comments[i].draw(false));
1314 }
1315 },
1316
1317 /**
1318 * Load the image for this pdf page and remove the loading icon (if there).
1319 * @protected
1320 * @method change_page
1321 */
5bb4f444 1322 change_page: function() {
da91de78 1323 var drawingcanvas = this.get_dialogue_element(SELECTOR.DRAWINGCANVAS),
5c386472
DW
1324 page,
1325 previousbutton,
1326 nextbutton;
1327
da91de78
BL
1328 previousbutton = this.get_dialogue_element(SELECTOR.PREVIOUSBUTTON);
1329 nextbutton = this.get_dialogue_element(SELECTOR.NEXTBUTTON);
5c386472
DW
1330
1331 if (this.currentpage > 0) {
1332 previousbutton.removeAttribute('disabled');
1333 } else {
1334 previousbutton.setAttribute('disabled', 'true');
1335 }
1336 if (this.currentpage < (this.pagecount - 1)) {
1337 nextbutton.removeAttribute('disabled');
1338 } else {
1339 nextbutton.setAttribute('disabled', 'true');
1340 }
1341
1342 page = this.pages[this.currentpage];
1343 this.loadingicon.hide();
1344 drawingcanvas.setStyle('backgroundImage', 'url("' + page.url + '")');
50c12f01
DS
1345 drawingcanvas.setStyle('width', page.width + 'px');
1346 drawingcanvas.setStyle('height', page.height + 'px');
5c386472 1347
ea6d8674 1348 // Update page select.
bb2281d5 1349 this.get_dialogue_element(SELECTOR.PAGESELECT).set('selectedIndex', this.currentpage);
ea6d8674 1350
50c12f01 1351 this.resize(); // Internally will call 'redraw', after checking the dialogue size.
5c386472
DW
1352 },
1353
1354 /**
1355 * Now we know how many pages there are,
1356 * we can enable the navigation controls.
1357 * @protected
1358 * @method setup_navigation
1359 */
5bb4f444 1360 setup_navigation: function() {
5c386472
DW
1361 var pageselect,
1362 i,
0d5a260e 1363 strinfo,
5c386472
DW
1364 option,
1365 previousbutton,
1366 nextbutton;
1367
da91de78 1368 pageselect = this.get_dialogue_element(SELECTOR.PAGESELECT);
5c386472 1369
557f44d9 1370 var options = pageselect.all('option');
5c386472
DW
1371 if (options.size() <= 1) {
1372 for (i = 0; i < this.pages.length; i++) {
1373 option = Y.Node.create('<option/>');
1374 option.setAttribute('value', i);
5bb4f444 1375 strinfo = {page: i + 1, total: this.pages.length};
0d5a260e 1376 option.setHTML(M.util.get_string('pagexofy', 'assignfeedback_editpdf', strinfo));
5c386472
DW
1377 pageselect.append(option);
1378 }
1379 }
1380 pageselect.removeAttribute('disabled');
1381 pageselect.on('change', function() {
1382 this.currentpage = pageselect.get('value');
1383 this.change_page();
1384 }, this);
1385
da91de78
BL
1386 previousbutton = this.get_dialogue_element(SELECTOR.PREVIOUSBUTTON);
1387 nextbutton = this.get_dialogue_element(SELECTOR.NEXTBUTTON);
5c386472
DW
1388
1389 previousbutton.on('click', this.previous_page, this);
1390 previousbutton.on('key', this.previous_page, 'down:13', this);
1391 nextbutton.on('click', this.next_page, this);
1392 nextbutton.on('key', this.next_page, 'down:13', this);
1393 },
1394
1395 /**
1396 * Navigate to the previous page.
1397 * @protected
1398 * @method previous_page
1399 */
5bb4f444 1400 previous_page: function(e) {
114913e3 1401 e.preventDefault();
05aa787b 1402 this.currentpage--;
5c386472
DW
1403 if (this.currentpage < 0) {
1404 this.currentpage = 0;
1405 }
1406 this.change_page();
1407 },
1408
1409 /**
1410 * Navigate to the next page.
1411 * @protected
1412 * @method next_page
1413 */
5bb4f444 1414 next_page: function(e) {
114913e3 1415 e.preventDefault();
05aa787b 1416 this.currentpage++;
5c386472
DW
1417 if (this.currentpage >= this.pages.length) {
1418 this.currentpage = this.pages.length - 1;
1419 }
1420 this.change_page();
50c12f01
DS
1421 },
1422
1423 /**
1424 * Update any absolutely positioned nodes, within each drawable, when the drawing canvas is scrolled
1425 * @protected
1426 * @method move_canvas
1427 */
1428 move_canvas: function() {
1429 var drawingregion, x, y, i;
5c386472 1430
da91de78 1431 drawingregion = this.get_dialogue_element(SELECTOR.DRAWINGREGION);
50c12f01
DS
1432 x = parseInt(drawingregion.get('scrollLeft'), 10);
1433 y = parseInt(drawingregion.get('scrollTop'), 10);
5c386472 1434
50c12f01
DS
1435 for (i = 0; i < this.drawables.length; i++) {
1436 this.drawables[i].scroll_update(x, y);
1437 }
923641f7
NN
1438 },
1439
1440 /**
a0b00911
DW
1441 * Test the browser support for options objects on event listeners.
1442 * @return Boolean
923641f7 1443 */
a0b00911
DW
1444 event_listener_options_supported: function() {
1445 var passivesupported = false,
1446 options,
1447 testeventname = "testpassiveeventoptions";
1448
1449 // Options support testing example from:
1450 // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
1451
923641f7 1452 try {
a0b00911 1453 options = Object.defineProperty({}, "passive", {
923641f7 1454 get: function() {
a0b00911
DW
1455 passivesupported = true;
1456 }
923641f7
NN
1457 });
1458
a0b00911
DW
1459 // We use an event name that is not likely to conflict with any real event.
1460 document.addEventListener(testeventname, options, options);
1461 // We remove the event listener as we have tested the options already.
1462 document.removeEventListener(testeventname, options, options);
1463 } catch(err) {
1464 // It's already false.
1465 passivesupported = false;
923641f7 1466 }
a0b00911 1467 return passivesupported;
923641f7
NN
1468 },
1469
1470 /**
1471 * Disable Touch Move scrolling
1472 */
a0b00911
DW
1473 disable_touch_scroll: function() {
1474 if (this.event_listener_options_supported()) {
923641f7
NN
1475 document.addEventListener('touchmove', this.stop_touch_scroll, {passive: false});
1476 }
1477 },
1478
923641f7
NN
1479 /**
1480 * Stop Touch Scrolling
1481 * @param {Object} e
1482 */
a0b00911 1483 stop_touch_scroll: function(e) {
923641f7
NN
1484 e.stopPropagation();
1485 e.preventDefault();
50c12f01 1486 }
5c386472
DW
1487
1488};
1489
1490Y.extend(EDITOR, Y.Base, EDITOR.prototype, {
5bb4f444
DP
1491 NAME: 'moodle-assignfeedback_editpdf-editor',
1492 ATTRS: {
1493 userid: {
1494 validator: Y.Lang.isInteger,
1495 value: 0
5c386472 1496 },
5bb4f444
DP
1497 assignmentid: {
1498 validator: Y.Lang.isInteger,
1499 value: 0
5c386472 1500 },
5bb4f444
DP
1501 attemptnumber: {
1502 validator: Y.Lang.isInteger,
1503 value: 0
5c386472 1504 },
5bb4f444
DP
1505 header: {
1506 validator: Y.Lang.isString,
1507 value: ''
5c386472 1508 },
5bb4f444
DP
1509 body: {
1510 validator: Y.Lang.isString,
1511 value: ''
5c386472 1512 },
5bb4f444
DP
1513 footer: {
1514 validator: Y.Lang.isString,
1515 value: ''
5c386472 1516 },
5bb4f444
DP
1517 linkid: {
1518 validator: Y.Lang.isString,
1519 value: ''
5c386472 1520 },
5bb4f444
DP
1521 deletelinkid: {
1522 validator: Y.Lang.isString,
1523 value: ''
5c386472 1524 },
5bb4f444
DP
1525 readonly: {
1526 validator: Y.Lang.isBoolean,
1527 value: true
5c386472 1528 },
5bb4f444
DP
1529 stampfiles: {
1530 validator: Y.Lang.isArray,
1531 value: ''
5c386472
DW
1532 }
1533 }
1534});
1535
5c386472 1536M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
5c386472
DW
1537M.assignfeedback_editpdf.editor = M.assignfeedback_editpdf.editor || {};
1538
1539/**
1540 * Init function - will create a new instance every time.
1f777e5c 1541 * @method editor.init
5c386472
DW
1542 * @static
1543 * @param {Object} params
1544 */
1545M.assignfeedback_editpdf.editor.init = M.assignfeedback_editpdf.editor.init || function(params) {
f7a9f1dd
AN
1546 if (typeof M.assignfeedback_editpdf.instance !== 'undefined') {
1547 M.assignfeedback_editpdf.instance.destroy();
1548 }
1549
5bb4f444 1550 M.assignfeedback_editpdf.instance = new EDITOR(params);
bb690849 1551 return M.assignfeedback_editpdf.instance;
a0b00911 1552};