"use strict"; var Widget = require('./widget').Widget, util = require('../util'); var $ = util.$; var _t = util.gettext; var Promise = util.Promise; var NS = "annotator-editor"; // id returns an identifier unique within this session var id = (function () { var counter; counter = -1; return function () { return counter += 1; }; }()); // preventEventDefault prevents an event's default, but handles the condition // that the event is null or doesn't have a preventDefault function. function preventEventDefault(event) { if (typeof event !== 'undefined' && event !== null && typeof event.preventDefault === 'function') { event.preventDefault(); } } // dragTracker is a function which allows a callback to track changes made to // the position of a draggable "handle" element. // // handle - A DOM element to make draggable // callback - Callback function // // Callback arguments: // // delta - An Object with two properties, "x" and "y", denoting the amount the // mouse has moved since the last (tracked) call. // // Callback returns: Boolean indicating whether to track the last movement. If // the movement is not tracked, then the amount the mouse has moved will be // accumulated and passed to the next mousemove event. // var dragTracker = exports.dragTracker = function dragTracker(handle, callback) { var lastPos = null, throttled = false; // Event handler for mousemove function mouseMove(e) { if (throttled || lastPos === null) { return; } var delta = { y: e.pageY - lastPos.top, x: e.pageX - lastPos.left }; var trackLastMove = true; // The callback function can return false to indicate that the tracker // shouldn't keep updating the last position. This can be used to // implement "walls" beyond which (for example) resizing has no effect. if (typeof callback === 'function') { trackLastMove = callback(delta); } if (trackLastMove !== false) { lastPos = { top: e.pageY, left: e.pageX }; } // Throttle repeated mousemove events throttled = true; setTimeout(function () { throttled = false; }, 1000 / 60); } // Event handler for mouseup function mouseUp() { lastPos = null; $(handle.ownerDocument) .off('mouseup', mouseUp) .off('mousemove', mouseMove); } // Event handler for mousedown -- starts drag tracking function mouseDown(e) { if (e.target !== handle) { return; } lastPos = { top: e.pageY, left: e.pageX }; $(handle.ownerDocument) .on('mouseup', mouseUp) .on('mousemove', mouseMove); e.preventDefault(); } // Public: turn off drag tracking for this dragTracker object. function destroy() { $(handle).off('mousedown', mouseDown); } $(handle).on('mousedown', mouseDown); return {destroy: destroy}; }; // resizer is a component that uses a dragTracker under the hood to track the // dragging of a handle element, using that motion to resize another element. // // element - DOM Element to resize // handle - DOM Element to use as a resize handle // options - Object of options. // // Available options: // // invertedX - If this option is defined as a function, and that function // returns a truthy value, the horizontal sense of the drag will be // inverted. Useful if the drag handle is at the left of the // element, and so dragging left means "grow the element" // invertedY - If this option is defined as a function, and that function // returns a truthy value, the vertical sense of the drag will be // inverted. Useful if the drag handle is at the bottom of the // element, and so dragging down means "grow the element" var resizer = exports.resizer = function resizer(element, handle, options) { var $el = $(element); if (typeof options === 'undefined' || options === null) { options = {}; } // Translate the delta supplied by dragTracker into a delta that takes // account of the invertedX and invertedY callbacks if defined. function translate(delta) { var directionX = 1, directionY = -1; if (typeof options.invertedX === 'function' && options.invertedX()) { directionX = -1; } if (typeof options.invertedY === 'function' && options.invertedY()) { directionY = 1; } return { x: delta.x * directionX, y: delta.y * directionY }; } // Callback for dragTracker function resize(delta) { var height = $el.height(), width = $el.width(), translated = translate(delta); if (Math.abs(translated.x) > 0) { $el.width(width + translated.x); } if (Math.abs(translated.y) > 0) { $el.height(height + translated.y); } // Did the element dimensions actually change? If not, then we've // reached the minimum size, and we shouldn't track var didChange = ($el.height() !== height || $el.width() !== width); return didChange; } // We return the dragTracker object in order to expose its methods. return dragTracker(handle, resize); }; // mover is a component that uses a dragTracker under the hood to track the // dragging of a handle element, using that motion to move another element. // // element - DOM Element to move // handle - DOM Element to use as a move handle // var mover = exports.mover = function mover(element, handle) { function move(delta) { $(element).css({ top: parseInt($(element).css('top'), 10) + delta.y, left: parseInt($(element).css('left'), 10) + delta.x }); } // We return the dragTracker object in order to expose its methods. return dragTracker(handle, move); }; // Public: Creates an element for editing annotations. var Editor = exports.Editor = Widget.extend({ // Public: Creates an instance of the Editor object. // // options - An Object literal containing options. // // Examples // // # Creates a new editor, adds a custom field and // # loads an annotation for editing. // editor = new Annotator.Editor // editor.addField({ // label: 'My custom input field', // type: 'textarea' // load: someLoadCallback // save: someSaveCallback // }) // editor.load(annotation) // // Returns a new Editor instance. constructor: function (options) { Widget.call(this, options); this.fields = []; this.annotation = {}; if (this.options.defaultFields) { this.addField({ type: 'textarea', label: _t('Comments') + '\u2026', load: function (field, annotation) { $(field).find('textarea').val(annotation.text || ''); }, submit: function (field, annotation) { annotation.text = $(field).find('textarea').val(); } }); } var self = this; this.element .on("submit." + NS, 'form', function (e) { self._onFormSubmit(e); }) .on("click." + NS, '.annotator-save', function (e) { self._onSaveClick(e); }) .on("click." + NS, '.annotator-cancel', function (e) { self._onCancelClick(e); }) .on("mouseover." + NS, '.annotator-cancel', function (e) { self._onCancelMouseover(e); }) .on("keydown." + NS, 'textarea', function (e) { self._onTextareaKeydown(e); }); }, destroy: function () { this.element.off("." + NS); Widget.prototype.destroy.call(this); }, // Public: Show the editor. // // position - An Object specifying the position in which to show the editor // (optional). // // Examples // // editor.show() // editor.hide() // editor.show({top: '100px', left: '80px'}) // // Returns nothing. show: function (position) { if (typeof position !== 'undefined' && position !== null) { this.element.css({ top: position.top, left: position.left }); } this.element .find('.annotator-save') .addClass(this.classes.focus); Widget.prototype.show.call(this); // give main textarea focus this.element.find(":input:first").focus(); this._setupDraggables(); }, // Public: Load an annotation into the editor and display it. // // annotation - An annotation Object to display for editing. // position - An Object specifying the position in which to show the editor // (optional). // // Returns a Promise that is resolved when the editor is submitted, or // rejected if editing is cancelled. load: function (annotation, position) { this.annotation = annotation; for (var i = 0, len = this.fields.length; i < len; i++) { var field = this.fields[i]; field.load(field.element, this.annotation); } var self = this; return new Promise(function (resolve, reject) { self.dfd = {resolve: resolve, reject: reject}; self.show(position); }); }, // Public: Submits the editor and saves any changes made to the annotation. // // Returns nothing. submit: function () { for (var i = 0, len = this.fields.length; i < len; i++) { var field = this.fields[i]; field.submit(field.element, this.annotation); } if (typeof this.dfd !== 'undefined' && this.dfd !== null) { this.dfd.resolve(); } this.hide(); }, // Public: Cancels the editing process, discarding any edits made to the // annotation. // // Returns itself. cancel: function () { if (typeof this.dfd !== 'undefined' && this.dfd !== null) { this.dfd.reject('editing cancelled'); } this.hide(); }, // Public: Adds an additional form field to the editor. Callbacks can be // provided to update the view and anotations on load and submission. // // options - An options Object. Options are as follows: // id - A unique id for the form element will also be set as // the "for" attribute of a label if there is one. // (default: "annotator-field-{number}") // type - Input type String. One of "input", "textarea", // "checkbox", "select" (default: "input") // label - Label to display either in a label Element or as // placeholder text depending on the type. (default: "") // load - Callback Function called when the editor is loaded // with a new annotation. Receives the field
  • element // and the annotation to be loaded. // submit - Callback Function called when the editor is submitted. // Receives the field
  • element and the annotation to // be updated. // // Examples // // # Add a new input element. // editor.addField({ // label: "Tags", // // # This is called when the editor is loaded use it to update your // # input. // load: (field, annotation) -> // # Do something with the annotation. // value = getTagString(annotation.tags) // $(field).find('input').val(value) // // # This is called when the editor is submitted use it to retrieve data // # from your input and save it to the annotation. // submit: (field, annotation) -> // value = $(field).find('input').val() // annotation.tags = getTagsFromString(value) // }) // // # Add a new checkbox element. // editor.addField({ // type: 'checkbox', // id: 'annotator-field-my-checkbox', // label: 'Allow anyone to see this annotation', // load: (field, annotation) -> // # Check what state of input should be. // if checked // $(field).find('input').attr('checked', 'checked') // else // $(field).find('input').removeAttr('checked') // submit: (field, annotation) -> // checked = $(field).find('input').is(':checked') // # Do something. // }) // // Returns the created
  • Element. addField: function (options) { var field = $.extend({ id: 'annotator-field-' + id(), type: 'input', label: '', load: function () {}, submit: function () {} }, options); var input = null, element = $('
  • '); field.element = element[0]; if (field.type === 'textarea') { input = $('