[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/assign/feedback/editpdf/yui/src/editor/js/ -> annotation.js (source)

   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   * Provides an in browser PDF editor.
  18   *
  19   * @module moodle-assignfeedback_editpdf-editor
  20   */
  21  
  22  /**
  23   * Class representing a highlight.
  24   *
  25   * @namespace M.assignfeedback_editpdf
  26   * @class annotation
  27   * @constructor
  28   */
  29  ANNOTATION = function(config) {
  30      ANNOTATION.superclass.constructor.apply(this, [config]);
  31  };
  32  
  33  ANNOTATION.NAME = "annotation";
  34  ANNOTATION.ATTRS = {};
  35  
  36  Y.extend(ANNOTATION, Y.Base, {
  37      /**
  38       * Reference to M.assignfeedback_editpdf.editor.
  39       * @property editor
  40       * @type M.assignfeedback_editpdf.editor
  41       * @public
  42       */
  43      editor : null,
  44  
  45      /**
  46       * Grade id
  47       * @property gradeid
  48       * @type Int
  49       * @public
  50       */
  51      gradeid : 0,
  52  
  53      /**
  54       * Comment page number
  55       * @property pageno
  56       * @type Int
  57       * @public
  58       */
  59      pageno : 0,
  60  
  61      /**
  62       * X position
  63       * @property x
  64       * @type Int
  65       * @public
  66       */
  67      x : 0,
  68  
  69      /**
  70       * Y position
  71       * @property y
  72       * @type Int
  73       * @public
  74       */
  75      y : 0,
  76  
  77      /**
  78       * Ending x position
  79       * @property endx
  80       * @type Int
  81       * @public
  82       */
  83      endx : 0,
  84  
  85      /**
  86       * Ending y position
  87       * @property endy
  88       * @type Int
  89       * @public
  90       */
  91      endy : 0,
  92  
  93      /**
  94       * Path
  95       * @property path
  96       * @type String - list of points like x1,y1:x2,y2
  97       * @public
  98       */
  99      path : '',
 100  
 101      /**
 102       * Tool.
 103       * @property type
 104       * @type String
 105       * @public
 106       */
 107      type : 'rect',
 108  
 109      /**
 110       * Annotation colour.
 111       * @property colour
 112       * @type String
 113       * @public
 114       */
 115      colour : 'red',
 116  
 117      /**
 118       * Reference to M.assignfeedback_editpdf.drawable
 119       * @property drawable
 120       * @type M.assignfeedback_editpdf.drawable
 121       * @public
 122       */
 123      drawable : false,
 124  
 125      /**
 126       * Initialise the annotation.
 127       *
 128       * @method initializer
 129       * @return void
 130       */
 131      initializer : function(config) {
 132          this.editor = config.editor || null;
 133          this.gradeid = parseInt(config.gradeid, 10) || 0;
 134          this.pageno = parseInt(config.pageno, 10) || 0;
 135          this.x = parseInt(config.x, 10) || 0;
 136          this.y = parseInt(config.y, 10) || 0;
 137          this.endx = parseInt(config.endx, 10) || 0;
 138          this.endy = parseInt(config.endy, 10) || 0;
 139          this.path = config.path || '';
 140          this.type = config.type || 'rect';
 141          this.colour = config.colour || 'red';
 142          this.drawable = false;
 143      },
 144  
 145      /**
 146       * Clean a comment record, returning an oject with only fields that are valid.
 147       * @public
 148       * @method clean
 149       * @return {}
 150       */
 151      clean : function() {
 152          return {
 153              gradeid : this.gradeid,
 154              x : parseInt(this.x, 10),
 155              y : parseInt(this.y, 10),
 156              endx : parseInt(this.endx, 10),
 157              endy : parseInt(this.endy, 10),
 158              type : this.type,
 159              path : this.path,
 160              pageno : this.pageno,
 161              colour : this.colour
 162          };
 163      },
 164  
 165      /**
 166       * Draw a selection around this annotation if it is selected.
 167       * @public
 168       * @method draw_highlight
 169       * @return M.assignfeedback_editpdf.drawable
 170       */
 171      draw_highlight : function() {
 172          var bounds,
 173              drawingregion = Y.one(SELECTOR.DRAWINGREGION),
 174              offsetcanvas = Y.one(SELECTOR.DRAWINGCANVAS).getXY(),
 175              shape;
 176  
 177          if (this.editor.currentannotation === this) {
 178              // Draw a highlight around the annotation.
 179              bounds = new M.assignfeedback_editpdf.rect();
 180              bounds.bound([new M.assignfeedback_editpdf.point(this.x, this.y),
 181                            new M.assignfeedback_editpdf.point(this.endx, this.endy)]);
 182  
 183              shape = this.editor.graphic.addShape({
 184                  type: Y.Rect,
 185                  width: bounds.width,
 186                  height: bounds.height,
 187                  stroke: {
 188                     weight: STROKEWEIGHT,
 189                     color: SELECTEDBORDERCOLOUR
 190                  },
 191                  fill: {
 192                     color: SELECTEDFILLCOLOUR
 193                  },
 194                  x: bounds.x,
 195                  y: bounds.y
 196              });
 197              this.drawable.shapes.push(shape);
 198  
 199              // Add a delete X to the annotation.
 200              var deleteicon = Y.Node.create('<img src="' + M.util.image_url('trash', 'assignfeedback_editpdf') + '"/>'),
 201                  deletelink = Y.Node.create('<a href="#" role="button"></a>');
 202  
 203              deleteicon.setAttrs({
 204                  'alt': M.util.get_string('deleteannotation', 'assignfeedback_editpdf')
 205              });
 206              deleteicon.setStyles({
 207                  'backgroundColor' : 'white'
 208              });
 209              deletelink.addClass('deleteannotationbutton');
 210              deletelink.append(deleteicon);
 211  
 212              drawingregion.append(deletelink);
 213              deletelink.setData('annotation', this);
 214              deletelink.setStyle('zIndex', '200');
 215  
 216              deletelink.on('click', this.remove, this);
 217              deletelink.on('key', this.remove, 'space,enter', this);
 218  
 219              deletelink.setX(offsetcanvas[0] + bounds.x + bounds.width - 18);
 220              deletelink.setY(offsetcanvas[1] + bounds.y + 6);
 221              this.drawable.nodes.push(deletelink);
 222          }
 223          return this.drawable;
 224      },
 225  
 226      /**
 227       * Draw an annotation
 228       * @public
 229       * @method draw
 230       * @return M.assignfeedback_editpdf.drawable|false
 231       */
 232      draw : function() {
 233          // Should be overridden by the subclass.
 234          this.draw_highlight();
 235          return this.drawable;
 236      },
 237  
 238      /**
 239       * Delete an annotation
 240       * @protected
 241       * @method remove
 242       * @param event
 243       */
 244      remove : function(e) {
 245          var annotations;
 246  
 247          e.preventDefault();
 248  
 249          annotations = this.editor.pages[this.editor.currentpage].annotations;
 250          for (i = 0; i < annotations.length; i++) {
 251              if (annotations[i] === this) {
 252                  annotations.splice(i, 1);
 253                  if (this.drawable) {
 254                      this.drawable.erase();
 255                  }
 256                  this.editor.currentannotation = false;
 257                  this.editor.save_current_page();
 258                  return;
 259              }
 260          }
 261      },
 262  
 263      /**
 264       * Move an annotation to a new location.
 265       * @public
 266       * @param int newx
 267       * @param int newy
 268       * @method move_annotation
 269       */
 270      move : function(newx, newy) {
 271          var diffx = newx - this.x,
 272              diffy = newy - this.y,
 273              newpath, oldpath, xy,
 274              x, y;
 275  
 276          this.x += diffx;
 277          this.y += diffy;
 278          this.endx += diffx;
 279          this.endy += diffy;
 280  
 281          if (this.path) {
 282              newpath = [];
 283              oldpath = this.path.split(':');
 284              Y.each(oldpath, function(position) {
 285                  xy = position.split(',');
 286                  x = parseInt(xy[0], 10);
 287                  y = parseInt(xy[1], 10);
 288                  newpath.push((x + diffx) + ',' + (y + diffy));
 289              });
 290  
 291              this.path = newpath.join(':');
 292  
 293          }
 294          if (this.drawable) {
 295              this.drawable.erase();
 296          }
 297          this.editor.drawables.push(this.draw());
 298      },
 299  
 300      /**
 301       * Draw the in progress edit.
 302       *
 303       * @public
 304       * @method draw_current_edit
 305       * @param M.assignfeedback_editpdf.edit edit
 306       */
 307      draw_current_edit : function(edit) {
 308          var noop = edit && false;
 309          // Override me please.
 310          return noop;
 311      },
 312  
 313      /**
 314       * Promote the current edit to a real annotation.
 315       *
 316       * @public
 317       * @method init_from_edit
 318       * @param M.assignfeedback_editpdf.edit edit
 319       * @return bool if width/height is more than min. required.
 320       */
 321      init_from_edit : function(edit) {
 322          var bounds = new M.assignfeedback_editpdf.rect();
 323          bounds.bound([edit.start, edit.end]);
 324  
 325          this.gradeid = this.editor.get('gradeid');
 326          this.pageno = this.editor.currentpage;
 327          this.x = bounds.x;
 328          this.y = bounds.y;
 329          this.endx = bounds.x + bounds.width;
 330          this.endy = bounds.y + bounds.height;
 331          this.colour = edit.annotationcolour;
 332          this.path = '';
 333          return (bounds.has_min_width() && bounds.has_min_height());
 334      }
 335  
 336  });
 337  
 338  M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
 339  M.assignfeedback_editpdf.annotation = ANNOTATION;


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1