MDL-42023 assign: Edit PDF plugin - Jerome's contributions
[moodle.git] / mod / assign / feedback / editpdf / yui / src / editor / js / annotationstamp.js
CommitLineData
e0cc92c6
JM
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/>.
15
16/**
17 * Class representing a stamp.
18 *
19 * @namespace M.assignfeedback_editpdf
20 * @class annotationstamp
21 * @extends annotation
22 * @module moodle-assignfeedback_editpdf-editor
23 */
24ANNOTATIONSTAMP = function(config) {
25 ANNOTATIONSTAMP.superclass.constructor.apply(this, [config]);
26};
27
28ANNOTATIONSTAMP.NAME = "annotationstamp";
29ANNOTATIONSTAMP.ATTRS = {};
30
31Y.extend(ANNOTATIONSTAMP, M.assignfeedback_editpdf.annotation, {
32 /**
33 * Draw a stamp annotation
34 * @protected
35 * @method draw
36 * @return M.assignfeedback_editpdf.drawable
37 */
38 draw : function() {
39 var drawable = new M.assignfeedback_editpdf.drawable(this.editor),
40 drawingregion = Y.one(SELECTOR.DRAWINGREGION),
41 node,
42 position;
43
44 position = this.editor.get_window_coordinates(new M.assignfeedback_editpdf.point(this.x, this.y));
45 node = Y.Node.create('<div/>');
46 node.setStyles({
47 'display': 'inline-block',
48 'backgroundImage': 'url(' + this.path + ')',
49 'width': (this.endx - this.x),
50 'height': (this.endy - this.y),
51 'backgroundSize': '100%',
52 'zIndex': 50
53 });
54
55 drawingregion.append(node);
56 node.setX(position.x);
57 node.setY(position.y);
58
59 // Pass throught the event handlers on the div.
60 node.on('gesturemovestart', this.editor.edit_start, null, this.editor);
61 node.on('gesturemove', this.editor.edit_move, null, this.editor);
62 node.on('gesturemoveend', this.editor.edit_end, null, this.editor);
63
64 drawable.nodes.push(node);
65
66 this.drawable = drawable;
67 return ANNOTATIONSTAMP.superclass.draw.apply(this);
68 },
69
70 /**
71 * Draw the in progress edit.
72 *
73 * @public
74 * @method draw_current_edit
75 * @param M.assignfeedback_editpdf.edit edit
76 */
77 draw_current_edit : function(edit) {
78 var bounds = new M.assignfeedback_editpdf.rect(),
79 drawable = new M.assignfeedback_editpdf.drawable(this.editor),
80 drawingregion = Y.one(SELECTOR.DRAWINGREGION),
81 node,
82 position;
83
84 bounds.bound([edit.start, edit.end]);
85 position = this.editor.get_window_coordinates(new M.assignfeedback_editpdf.point(bounds.x, bounds.y));
86
87 node = Y.Node.create('<div/>');
88 node.setStyles({
89 'display': 'inline-block',
90 'backgroundImage': 'url(' + edit.stamp + ')',
91 'width': bounds.width,
92 'height': bounds.height,
93 'backgroundSize': '100%',
94 'zIndex': 50
95 });
96
97 drawingregion.append(node);
98 node.setX(position.x);
99 node.setY(position.y);
100
101 drawable.nodes.push(node);
102
103 return drawable;
104 },
105
106 /**
107 * Promote the current edit to a real annotation.
108 *
109 * @public
110 * @method init_from_edit
111 * @param M.assignfeedback_editpdf.edit edit
112 */
113 init_from_edit : function(edit) {
114 var bounds = new M.assignfeedback_editpdf.rect();
115 bounds.bound([edit.start, edit.end]);
116
117 if (bounds.width < 40) {
118 bounds.width = 40;
119 }
120 if (bounds.height < 40) {
121 bounds.height = 40;
122 }
123 this.gradeid = this.editor.get('gradeid');
124 this.pageno = this.editor.currentpage;
125 this.x = bounds.x;
126 this.y = bounds.y;
127 this.endx = bounds.x + bounds.width;
128 this.endy = bounds.y + bounds.height;
129 this.colour = edit.annotationcolour;
130 this.path = edit.stamp;
131 },
132
133 /**
134 * Move an annotation to a new location.
135 * @public
136 * @param int newx
137 * @param int newy
138 * @method move_annotation
139 */
140 move : function(newx, newy) {
141 var diffx = newx - this.x,
142 diffy = newy - this.y;
143
144 this.x += diffx;
145 this.y += diffy;
146 this.endx += diffx;
147 this.endy += diffy;
148
149 if (this.drawable) {
150 this.drawable.erase();
151 }
152 this.editor.drawables.push(this.draw());
153 }
154
155});
156
157M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
158M.assignfeedback_editpdf.annotationstamp = ANNOTATIONSTAMP;