27a1cbd28ffd393f3adb42e8d7c3172355dc0966
[moodle.git] / mod / assign / feedback / editpdf / yui / src / editor / js / annotationstamp.js
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 /* global SELECTOR */
17 /**
18  * Provides an in browser PDF editor.
19  *
20  * @module moodle-assignfeedback_editpdf-editor
21  */
23 /**
24  * Class representing a stamp.
25  *
26  * @namespace M.assignfeedback_editpdf
27  * @class annotationstamp
28  * @extends M.assignfeedback_editpdf.annotation
29  */
30 var ANNOTATIONSTAMP = function(config) {
31     ANNOTATIONSTAMP.superclass.constructor.apply(this, [config]);
32 };
34 ANNOTATIONSTAMP.NAME = "annotationstamp";
35 ANNOTATIONSTAMP.ATTRS = {};
37 Y.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),
46             drawingregion = this.editor.get_dialogue_element(SELECTOR.DRAWINGREGION),
47             node,
48             position;
50         position = this.editor.get_window_coordinates(new M.assignfeedback_editpdf.point(this.x, this.y));
51         node = Y.Node.create('<div/>');
52         // If these are absolutely positioned, they escape their scroll container.
53         node.setStyles({
54             'position': 'relative',
55             'display': 'inline-block',
56             'backgroundImage': 'url(' + this.editor.get_stamp_image_url(this.path) + ')',
57             'width': (this.endx - this.x),
58             'height': (this.endy - this.y),
59             'backgroundSize': '100% 100%',
60             'zIndex': 50
61         });
63         drawingregion.append(node);
64         node.setX(position.x);
65         node.setY(position.y);
66         drawable.store_position(node, position.x, position.y);
68         // Bind events only when editing.
69         if (!this.editor.get('readonly')) {
70             // Pass through the event handlers on the div.
71             node.on('gesturemovestart', this.editor.edit_start, null, this.editor);
72             node.on('gesturemove', this.editor.edit_move, null, this.editor);
73             node.on('gesturemoveend', this.editor.edit_end, null, this.editor);
74         }
76         drawable.nodes.push(node);
78         this.drawable = drawable;
79         return ANNOTATIONSTAMP.superclass.draw.apply(this);
80     },
82     /**
83      * Draw the in progress edit.
84      *
85      * @public
86      * @method draw_current_edit
87      * @param M.assignfeedback_editpdf.edit edit
88      */
89     draw_current_edit: function(edit) {
90         var bounds = new M.assignfeedback_editpdf.rect(),
91             drawable = new M.assignfeedback_editpdf.drawable(this.editor),
92             drawingregion = this.editor.get_dialogue_element(SELECTOR.DRAWINGREGION),
93             node,
94             position;
96         bounds.bound([edit.start, edit.end]);
97         position = this.editor.get_window_coordinates(new M.assignfeedback_editpdf.point(bounds.x, bounds.y));
99         node = Y.Node.create('<div/>');
100         node.setStyles({
101             'position': 'absolute',
102             'display': 'inline-block',
103             'backgroundImage': 'url(' + this.editor.get_stamp_image_url(edit.stamp) + ')',
104             'width': bounds.width,
105             'height': bounds.height,
106             'backgroundSize': '100% 100%',
107             'zIndex': 50
108         });
110         drawingregion.append(node);
111         node.setX(position.x);
112         node.setY(position.y);
113         drawable.store_position(node, position.x, position.y);
115         drawable.nodes.push(node);
117         return drawable;
118     },
120     /**
121      * Promote the current edit to a real annotation.
122      *
123      * @public
124      * @method init_from_edit
125      * @param M.assignfeedback_editpdf.edit edit
126      * @return bool if width/height is more than min. required.
127      */
128     init_from_edit: function(edit) {
129         var bounds = new M.assignfeedback_editpdf.rect();
130         bounds.bound([edit.start, edit.end]);
132         if (bounds.width < 40) {
133             bounds.width = 40;
134         }
135         if (bounds.height < 40) {
136             bounds.height = 40;
137         }
138         this.gradeid = this.editor.get('gradeid');
139         this.pageno = this.editor.currentpage;
140         this.x = bounds.x;
141         this.y = bounds.y;
142         this.endx = bounds.x + bounds.width;
143         this.endy = bounds.y + bounds.height;
144         this.colour = edit.annotationcolour;
145         this.path = edit.stamp;
147         // Min width and height is always more than 40px.
148         return true;
149     },
151     /**
152      * Move an annotation to a new location.
153      * @public
154      * @param int newx
155      * @param int newy
156      * @method move_annotation
157      */
158     move: function(newx, newy) {
159         var diffx = newx - this.x,
160             diffy = newy - this.y;
162         this.x += diffx;
163         this.y += diffy;
164         this.endx += diffx;
165         this.endy += diffy;
167         if (this.drawable) {
168             this.drawable.erase();
169         }
170         this.editor.drawables.push(this.draw());
171     }
173 });
175 M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
176 M.assignfeedback_editpdf.annotationstamp = ANNOTATIONSTAMP;