MDL-42023 assign: Edit PDF plugin - Jerome's contributions
[moodle.git] / mod / assign / feedback / editpdf / yui / src / editor / js / annotationpen.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/>.
16 /**
17  * Class representing a pen.
18  *
19  * @namespace M.assignfeedback_editpdf
20  * @class annotationpen
21  * @extends annotation
22  * @module moodle-assignfeedback_editpdf-editor
23  */
24 ANNOTATIONPEN = function(config) {
25     ANNOTATIONPEN.superclass.constructor.apply(this, [config]);
26 };
28 ANNOTATIONPEN.NAME = "annotationpen";
29 ANNOTATIONPEN.ATTRS = {};
31 Y.extend(ANNOTATIONPEN, M.assignfeedback_editpdf.annotation, {
32     /**
33      * Draw a pen annotation
34      * @protected
35      * @method draw
36      * @return M.assignfeedback_editpdf.drawable
37      */
38     draw : function() {
39         var drawable,
40             shape,
41             first,
42             positions,
43             xy;
45         drawable = new M.assignfeedback_editpdf.drawable(this.editor);
47         shape = this.editor.graphic.addShape({
48            type: Y.Path,
49             fill: false,
50             stroke: {
51                 weight: STROKEWEIGHT,
52                 color: ANNOTATIONCOLOUR[this.colour]
53             }
54         });
56         first = true;
57         // Recreate the pen path array.
58         positions = this.path.split(':');
59         // Redraw all the lines.
60         Y.each(positions, function(position) {
61             xy = position.split(',');
62             if (first) {
63                 shape.moveTo(xy[0], xy[1]);
64                 first = false;
65             } else {
66                 shape.lineTo(xy[0], xy[1]);
67             }
68         }, this);
70         shape.end();
72         drawable.shapes.push(shape);
73         this.drawable = drawable;
75         return ANNOTATIONPEN.superclass.draw.apply(this);
76     },
78     /**
79      * Draw the in progress edit.
80      *
81      * @public
82      * @method draw_current_edit
83      * @param M.assignfeedback_editpdf.edit edit
84      */
85     draw_current_edit : function(edit) {
86         var drawable = new M.assignfeedback_editpdf.drawable(this.editor),
87             shape,
88             first;
90         shape = this.editor.graphic.addShape({
91            type: Y.Path,
92             fill: false,
93             stroke: {
94                 weight: STROKEWEIGHT,
95                 color: ANNOTATIONCOLOUR[edit.annotationcolour]
96             }
97         });
99         first = true;
100         // Recreate the pen path array.
101         // Redraw all the lines.
102         Y.each(edit.path, function(position) {
103             if (first) {
104                 shape.moveTo(position.x, position.y);
105                 first = false;
106             } else {
107                 shape.lineTo(position.x, position.y);
108             }
109         }, this);
111         shape.end();
113         drawable.shapes.push(shape);
115         return drawable;
116     },
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
125      */
126     init_from_edit : function(edit) {
127         var bounds = new M.assignfeedback_editpdf.rect(),
128             pathlist = [],
129             i = 0;
131         // This will get the boundaries of all points in the path.
132         bounds.bound(edit.path);
134         for (i = 0; i < edit.path.length; i++) {
135             pathlist.push(parseInt(edit.path[i].x, 10) + ',' + parseInt(edit.path[i].y, 10));
136         }
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 = pathlist.join(':');
146     }
149 });
151 M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
152 M.assignfeedback_editpdf.annotationpen = ANNOTATIONPEN;