weekly release 3.2dev
[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/>.
ad3f8cd1 15/* global SELECTOR */
e0cc92c6 16
1f777e5c
AN
17/**
18 * Provides an in browser PDF editor.
19 *
20 * @module moodle-assignfeedback_editpdf-editor
21 */
22
e0cc92c6
JM
23/**
24 * Class representing a stamp.
25 *
26 * @namespace M.assignfeedback_editpdf
27 * @class annotationstamp
1f777e5c 28 * @extends M.assignfeedback_editpdf.annotation
e0cc92c6 29 */
557f44d9 30var ANNOTATIONSTAMP = function(config) {
e0cc92c6
JM
31 ANNOTATIONSTAMP.superclass.constructor.apply(this, [config]);
32};
33
34ANNOTATIONSTAMP.NAME = "annotationstamp";
35ANNOTATIONSTAMP.ATTRS = {};
36
37Y.extend(ANNOTATIONSTAMP, M.assignfeedback_editpdf.annotation, {
38 /**
39 * Draw a stamp annotation
40 * @protected
41 * @method draw
42 * @return M.assignfeedback_editpdf.drawable
43 */
44 draw : function() {
45 var drawable = new M.assignfeedback_editpdf.drawable(this.editor),
667cec9b 46 drawingregion = this.editor.get_dialogue_element(SELECTOR.DRAWINGREGION),
e0cc92c6
JM
47 node,
48 position;
49
50 position = this.editor.get_window_coordinates(new M.assignfeedback_editpdf.point(this.x, this.y));
51 node = Y.Node.create('<div/>');
52 node.setStyles({
d79d1bd8 53 'position': 'absolute',
e0cc92c6 54 'display': 'inline-block',
5c386472 55 'backgroundImage': 'url(' + this.editor.get_stamp_image_url(this.path) + ')',
e0cc92c6
JM
56 'width': (this.endx - this.x),
57 'height': (this.endy - this.y),
5c386472 58 'backgroundSize': '100% 100%',
e0cc92c6
JM
59 'zIndex': 50
60 });
61
667cec9b 62 drawingregion.append(node);
e0cc92c6
JM
63 node.setX(position.x);
64 node.setY(position.y);
50c12f01 65 drawable.store_position(node, position.x, position.y);
667cec9b
JP
66
67 // Bind events only when editing.
68 if (!this.editor.get('readonly')) {
69 // Pass through the event handlers on the div.
70 node.on('gesturemovestart', this.editor.edit_start, null, this.editor);
71 node.on('gesturemove', this.editor.edit_move, null, this.editor);
72 node.on('gesturemoveend', this.editor.edit_end, null, this.editor);
73 }
74
e0cc92c6
JM
75 drawable.nodes.push(node);
76
77 this.drawable = drawable;
78 return ANNOTATIONSTAMP.superclass.draw.apply(this);
79 },
80
81 /**
82 * Draw the in progress edit.
83 *
84 * @public
85 * @method draw_current_edit
86 * @param M.assignfeedback_editpdf.edit edit
87 */
88 draw_current_edit : function(edit) {
89 var bounds = new M.assignfeedback_editpdf.rect(),
90 drawable = new M.assignfeedback_editpdf.drawable(this.editor),
da91de78 91 drawingregion = this.editor.get_dialogue_element(SELECTOR.DRAWINGREGION),
e0cc92c6
JM
92 node,
93 position;
94
95 bounds.bound([edit.start, edit.end]);
96 position = this.editor.get_window_coordinates(new M.assignfeedback_editpdf.point(bounds.x, bounds.y));
97
98 node = Y.Node.create('<div/>');
99 node.setStyles({
d79d1bd8 100 'position': 'absolute',
e0cc92c6 101 'display': 'inline-block',
5c386472 102 'backgroundImage': 'url(' + this.editor.get_stamp_image_url(edit.stamp) + ')',
e0cc92c6
JM
103 'width': bounds.width,
104 'height': bounds.height,
5c386472 105 'backgroundSize': '100% 100%',
e0cc92c6
JM
106 'zIndex': 50
107 });
108
109 drawingregion.append(node);
110 node.setX(position.x);
111 node.setY(position.y);
50c12f01 112 drawable.store_position(node, position.x, position.y);
e0cc92c6
JM
113
114 drawable.nodes.push(node);
115
116 return drawable;
117 },
118
119 /**
120 * Promote the current edit to a real annotation.
121 *
122 * @public
123 * @method init_from_edit
124 * @param M.assignfeedback_editpdf.edit edit
baf881b8 125 * @return bool if width/height is more than min. required.
e0cc92c6
JM
126 */
127 init_from_edit : function(edit) {
128 var bounds = new M.assignfeedback_editpdf.rect();
129 bounds.bound([edit.start, edit.end]);
130
131 if (bounds.width < 40) {
132 bounds.width = 40;
133 }
134 if (bounds.height < 40) {
135 bounds.height = 40;
136 }
137 this.gradeid = this.editor.get('gradeid');
138 this.pageno = this.editor.currentpage;
139 this.x = bounds.x;
140 this.y = bounds.y;
141 this.endx = bounds.x + bounds.width;
142 this.endy = bounds.y + bounds.height;
143 this.colour = edit.annotationcolour;
144 this.path = edit.stamp;
baf881b8
RT
145
146 // Min width and height is always more than 40px.
147 return true;
e0cc92c6
JM
148 },
149
150 /**
151 * Move an annotation to a new location.
152 * @public
153 * @param int newx
154 * @param int newy
155 * @method move_annotation
156 */
157 move : function(newx, newy) {
158 var diffx = newx - this.x,
159 diffy = newy - this.y;
160
161 this.x += diffx;
162 this.y += diffy;
163 this.endx += diffx;
164 this.endy += diffy;
165
166 if (this.drawable) {
167 this.drawable.erase();
168 }
169 this.editor.drawables.push(this.draw());
170 }
171
172});
173
174M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
175M.assignfeedback_editpdf.annotationstamp = ANNOTATIONSTAMP;