source: OKFNAnnotator (for Zope)/annotator_files/lib/editor.js @ 4:6979313586cf

Last change on this file since 4:6979313586cf was 4:6979313586cf, checked in by casties, 12 years ago

new version of annotator.

File size: 7.3 KB
Line 
1// Generated by CoffeeScript 1.3.3
2var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
3  __hasProp = {}.hasOwnProperty,
4  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
5
6Annotator.Editor = (function(_super) {
7
8  __extends(Editor, _super);
9
10  Editor.prototype.events = {
11    "form submit": "submit",
12    ".annotator-save click": "submit",
13    ".annotator-cancel click": "hide",
14    ".annotator-cancel mouseover": "onCancelButtonMouseover",
15    "textarea keydown": "processKeypress"
16  };
17
18  Editor.prototype.classes = {
19    hide: 'annotator-hide',
20    focus: 'annotator-focus'
21  };
22
23  Editor.prototype.html = "<div class=\"annotator-outer annotator-editor\">\n  <form class=\"annotator-widget\">\n    <ul class=\"annotator-listing\"></ul>\n    <div class=\"annotator-controls\">\n      <a href=\"#cancel\" class=\"annotator-cancel\">" + _t('Cancel') + "</a>\n<a href=\"#save\" class=\"annotator-save annotator-focus\">" + _t('Save') + "</a>\n    </div>\n  </form>\n</div>";
24
25  Editor.prototype.options = {};
26
27  function Editor(options) {
28    this.onCancelButtonMouseover = __bind(this.onCancelButtonMouseover, this);
29
30    this.processKeypress = __bind(this.processKeypress, this);
31
32    this.submit = __bind(this.submit, this);
33
34    this.load = __bind(this.load, this);
35
36    this.hide = __bind(this.hide, this);
37
38    this.show = __bind(this.show, this);
39    Editor.__super__.constructor.call(this, $(this.html)[0], options);
40    this.fields = [];
41    this.annotation = {};
42  }
43
44  Editor.prototype.show = function(event) {
45    util.preventEventDefault(event);
46    this.element.removeClass(this.classes.hide);
47    this.element.find('.annotator-save').addClass(this.classes.focus);
48    this.checkOrientation();
49    this.element.find(":input:first").focus();
50    this.setupDraggables();
51    return this.publish('show');
52  };
53
54  Editor.prototype.hide = function(event) {
55    util.preventEventDefault(event);
56    this.element.addClass(this.classes.hide);
57    return this.publish('hide');
58  };
59
60  Editor.prototype.load = function(annotation) {
61    var field, _i, _len, _ref;
62    this.annotation = annotation;
63    this.publish('load', [this.annotation]);
64    _ref = this.fields;
65    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
66      field = _ref[_i];
67      field.load(field.element, this.annotation);
68    }
69    return this.show();
70  };
71
72  Editor.prototype.submit = function(event) {
73    var field, _i, _len, _ref;
74    util.preventEventDefault(event);
75    _ref = this.fields;
76    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
77      field = _ref[_i];
78      field.submit(field.element, this.annotation);
79    }
80    this.publish('save', [this.annotation]);
81    return this.hide();
82  };
83
84  Editor.prototype.addField = function(options) {
85    var element, field, input;
86    field = $.extend({
87      id: 'annotator-field-' + util.uuid(),
88      type: 'input',
89      label: '',
90      load: function() {},
91      submit: function() {}
92    }, options);
93    input = null;
94    element = $('<li class="annotator-item" />');
95    field.element = element[0];
96    switch (field.type) {
97      case 'textarea':
98        input = $('<textarea />');
99        break;
100      case 'input':
101      case 'checkbox':
102        input = $('<input />');
103    }
104    element.append(input);
105    input.attr({
106      id: field.id,
107      placeholder: field.label
108    });
109    if (field.type === 'checkbox') {
110      input[0].type = 'checkbox';
111      element.addClass('annotator-checkbox');
112      element.append($('<label />', {
113        "for": field.id,
114        html: field.label
115      }));
116    }
117    this.element.find('ul:first').append(element);
118    this.fields.push(field);
119    return field.element;
120  };
121
122  Editor.prototype.checkOrientation = function() {
123    var controls, list;
124    Editor.__super__.checkOrientation.apply(this, arguments);
125    list = this.element.find('ul');
126    controls = this.element.find('.annotator-controls');
127    if (this.element.hasClass(this.classes.invert.y)) {
128      controls.insertBefore(list);
129    } else if (controls.is(':first-child')) {
130      controls.insertAfter(list);
131    }
132    return this;
133  };
134
135  Editor.prototype.processKeypress = function(event) {
136    if (event.keyCode === 27) {
137      return this.hide();
138    } else if (event.keyCode === 13 && !event.shiftKey) {
139      return this.submit();
140    }
141  };
142
143  Editor.prototype.onCancelButtonMouseover = function() {
144    return this.element.find('.' + this.classes.focus).removeClass(this.classes.focus);
145  };
146
147  Editor.prototype.setupDraggables = function() {
148    var classes, controls, cornerItem, editor, mousedown, onMousedown, onMousemove, onMouseup, resize, textarea, throttle,
149      _this = this;
150    this.element.find('.annotator-resize').remove();
151    if (this.element.hasClass(this.classes.invert.y)) {
152      cornerItem = this.element.find('.annotator-item:last');
153    } else {
154      cornerItem = this.element.find('.annotator-item:first');
155    }
156    if (cornerItem) {
157      $('<span class="annotator-resize"></span>').appendTo(cornerItem);
158    }
159    mousedown = null;
160    classes = this.classes;
161    editor = this.element;
162    textarea = null;
163    resize = editor.find('.annotator-resize');
164    controls = editor.find('.annotator-controls');
165    throttle = false;
166    onMousedown = function(event) {
167      if (event.target === this) {
168        mousedown = {
169          element: this,
170          top: event.pageY,
171          left: event.pageX
172        };
173        textarea = editor.find('textarea:first');
174        $(window).bind({
175          'mouseup.annotator-editor-resize': onMouseup,
176          'mousemove.annotator-editor-resize': onMousemove
177        });
178        return event.preventDefault();
179      }
180    };
181    onMouseup = function() {
182      mousedown = null;
183      return $(window).unbind('.annotator-editor-resize');
184    };
185    onMousemove = function(event) {
186      var diff, directionX, directionY, height, width;
187      if (mousedown && throttle === false) {
188        diff = {
189          top: event.pageY - mousedown.top,
190          left: event.pageX - mousedown.left
191        };
192        if (mousedown.element === resize[0]) {
193          height = textarea.outerHeight();
194          width = textarea.outerWidth();
195          directionX = editor.hasClass(classes.invert.x) ? -1 : 1;
196          directionY = editor.hasClass(classes.invert.y) ? 1 : -1;
197          textarea.height(height + (diff.top * directionY));
198          textarea.width(width + (diff.left * directionX));
199          if (textarea.outerHeight() !== height) {
200            mousedown.top = event.pageY;
201          }
202          if (textarea.outerWidth() !== width) {
203            mousedown.left = event.pageX;
204          }
205        } else if (mousedown.element === controls[0]) {
206          editor.css({
207            top: parseInt(editor.css('top'), 10) + diff.top,
208            left: parseInt(editor.css('left'), 10) + diff.left
209          });
210          mousedown.top = event.pageY;
211          mousedown.left = event.pageX;
212        }
213        throttle = true;
214        return setTimeout(function() {
215          return throttle = false;
216        }, 1000 / 60);
217      }
218    };
219    resize.bind('mousedown', onMousedown);
220    return controls.bind('mousedown', onMousedown);
221  };
222
223  return Editor;
224
225})(Annotator.Widget);
Note: See TracBrowser for help on using the repository browser.