reply-annotator.js 3.81 KB
Newer Older
daniel cebrian committed
1 2
/* 
 Reply Annotator Plugin v1.0 (https://github.com/danielcebrian/reply-annotator)
3
 Copyright (C) 2014 Daniel Cebrian Robles
daniel cebrian committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 License: https://github.com/danielcebrian/reply-annotator/blob/master/License.rst
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// Generated by CoffeeScript 1.6.3
var _ref,
  __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  __hasProp = {}.hasOwnProperty,
  __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; };

Annotator.Plugin.Reply = (function(_super) {
	__extends(Reply, _super);

	function Reply() {
		this.pluginSubmit = __bind(this.pluginSubmit, this);
		this.updateField = __bind(this.updateField, this);
		_ref = Reply.__super__.constructor.apply(this, arguments);
		return _ref;
	}

	Reply.prototype.field = null;

	Reply.prototype.input = null;

	Reply.prototype.pluginInit = function() {
		console.log("Reply-pluginInit");
		//Check that annotator is working
		if (!Annotator.supported()) {
			return;
		}
		
		//-- Editor
		this.field = this.annotator.editor.addField({
			type: 'input', //options (textarea,input,select,checkbox)
      		load: this.updateField,
			submit: this.pluginSubmit,
		});
		var newfield = Annotator.$('<li class="annotator-item reply-item" style="display:none"><span class="parent-annotation">0</span></li>');//reply-item is the parent value
		Annotator.$(this.field).replaceWith(newfield);
		this.field=newfield[0];
		
		//-- Viewer
		var newview = this.annotator.viewer.addField({
			load: this.updateViewer,
		});

		return this.input = $(this.field).find(':input');
	};
	
	// New JSON for the database
	Reply.prototype.pluginSubmit = function(field, annotation) {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		// since each annotation has their own reply item, this "find" is element specific
		var replyItem = $(this.annotator.editor.element).find(".reply-item span.parent-annotation");
		// checks to see if the current annotation is a reply by checking parent numbers
		var parent = replyItem.html() !== '' ? replyItem.html() : '0';
		// if the parent number is not empty or zero, we know that this is a comment
		if (parent !== '0') {
			annotation.media = 'comment';
		}

		// apparently some browsers continue adding <font> tags here for nonreplies
		// this will check and set to 0 (nonreply) if it fails
		if (parseInt(parent, 10) === NaN){
			parent = '0';
		}
		
		// set 0, because it is not a reply
		annotation.parent = parent;
		return annotation.parent;
daniel cebrian committed
85 86 87 88 89 90 91 92 93 94
	};
	

	Reply.prototype.updateViewer = function(field, annotation) {
		var self = this,
			field = $(field),
			ret = field.addClass('reply-viewer-annotator').html(function() {
				var string;
				return self;
			});
95
		field.remove();
daniel cebrian committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		this.annotation = annotation;
		//Create the actions for the buttons
		return ret;
	};
	
	Reply.prototype.updateField = function(field, annotation) {
		//reset parent value
		var replyItem = $(this.annotator.editor.element).find(".reply-item span.parent-annotation");
		return replyItem.html('0');
	};

	return Reply;

})(Annotator.Plugin);