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),
cdbdb2f9 46 drawingcanvas = this.editor.get_dialogue_element(SELECTOR.DRAWINGCANVAS),
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
cdbdb2f9 62 drawingcanvas.append(node);
e0cc92c6
JM
63 node.setX(position.x);
64 node.setY(position.y);
50c12f01 65 drawable.store_position(node, position.x, position.y);
e0cc92c6
JM
66 drawable.nodes.push(node);
67
68 this.drawable = drawable;
69 return ANNOTATIONSTAMP.superclass.draw.apply(this);
70 },
71
72 /**
73 * Draw the in progress edit.
74 *
75 * @public
76 * @method draw_current_edit
77 * @param M.assignfeedback_editpdf.edit edit
78 */
79 draw_current_edit : function(edit) {
80 var bounds = new M.assignfeedback_editpdf.rect(),
81 drawable = new M.assignfeedback_editpdf.drawable(this.editor),
da91de78 82 drawingregion = this.editor.get_dialogue_element(SELECTOR.DRAWINGREGION),
e0cc92c6
JM
83 node,
84 position;
85
86 bounds.bound([edit.start, edit.end]);
87 position = this.editor.get_window_coordinates(new M.assignfeedback_editpdf.point(bounds.x, bounds.y));
88
89 node = Y.Node.create('<div/>');
90 node.setStyles({
d79d1bd8 91 'position': 'absolute',
e0cc92c6 92 'display': 'inline-block',
5c386472 93 'backgroundImage': 'url(' + this.editor.get_stamp_image_url(edit.stamp) + ')',
e0cc92c6
JM
94 'width': bounds.width,
95 'height': bounds.height,
5c386472 96 'backgroundSize': '100% 100%',
e0cc92c6
JM
97 'zIndex': 50
98 });
99
100 drawingregion.append(node);
101 node.setX(position.x);
102 node.setY(position.y);
50c12f01 103 drawable.store_position(node, position.x, position.y);
e0cc92c6
JM
104
105 drawable.nodes.push(node);
106
107 return drawable;
108 },
109
110 /**
111 * Promote the current edit to a real annotation.
112 *
113 * @public
114 * @method init_from_edit
115 * @param M.assignfeedback_editpdf.edit edit
baf881b8 116 * @return bool if width/height is more than min. required.
e0cc92c6
JM
117 */
118 init_from_edit : function(edit) {
119 var bounds = new M.assignfeedback_editpdf.rect();
120 bounds.bound([edit.start, edit.end]);
121
122 if (bounds.width < 40) {
123 bounds.width = 40;
124 }
125 if (bounds.height < 40) {
126 bounds.height = 40;
127 }
128 this.gradeid = this.editor.get('gradeid');
129 this.pageno = this.editor.currentpage;
130 this.x = bounds.x;
131 this.y = bounds.y;
132 this.endx = bounds.x + bounds.width;
133 this.endy = bounds.y + bounds.height;
134 this.colour = edit.annotationcolour;
135 this.path = edit.stamp;
baf881b8
RT
136
137 // Min width and height is always more than 40px.
138 return true;
e0cc92c6
JM
139 },
140
141 /**
142 * Move an annotation to a new location.
143 * @public
144 * @param int newx
145 * @param int newy
146 * @method move_annotation
147 */
148 move : function(newx, newy) {
149 var diffx = newx - this.x,
150 diffy = newy - this.y;
151
152 this.x += diffx;
153 this.y += diffy;
154 this.endx += diffx;
155 this.endy += diffy;
156
157 if (this.drawable) {
158 this.drawable.erase();
159 }
160 this.editor.drawables.push(this.draw());
161 }
162
163});
164
165M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
166M.assignfeedback_editpdf.annotationstamp = ANNOTATIONSTAMP;