diacritic-annotator.js 9.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 
 Diacritic Annotator Plugin v1.0 (https://github.com/lduarte1991/diacritic-annotator)
 Copyright (C) 2014 Luis F Duarte
 License: https://github.com/lduarte1991/diacritic-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.
 */
 
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
var _ref;
var __bind = function(fn, me){ 
    return function(){ 
        return fn.apply(me, arguments); 
    }; 
};
var __hasProp = {}.hasOwnProperty;
var __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; 
};
39 40

Annotator.Plugin.Diacritics = (function(_super) {
41 42 43
    __extends(Diacritics, _super);
    
    //Options will include diacritic name, picture used, baseline
44 45
    Diacritics.prototype.options = null;
    Diacritics.prototype.diacriticmarks = null;
46 47 48 49 50 51 52 53
    
    /**
     * Declares all the functions and variables that the plugin will need.
     * @constructor
     */
    function Diacritics(element,options) {
        this.pluginSubmit = __bind(this.pluginSubmit, this);
        this.updateDiacritics = __bind(this.updateDiacritics, this);
54 55 56
        this.updateViewer = __bind(this.updateViewer, this);
        this.getDiacritics = __bind(this.getDiacritics, this);
        this.getPos = __bind(this.getPos, this);
57 58 59
        this.putMarkAtLocation = __bind(this.putMarkAtLocation, this);
        this.updateEditorForDiacritics = 
            __bind(this.updateEditorForDiacritics, this);
60 61 62
        
        this.options = options;
        this.diacriticmarks = this.getDiacritics();
63 64 65 66
        _ref = Diacritics.__super__.constructor.apply(this, arguments);
        return _ref;
    }
    
67
    //example variables to be used to receive input in the annotator view
68 69
    Diacritics.prototype.field = null;
    Diacritics.prototype.input = null;
70
    
71 72 73 74 75
    /**
     * Initalizes the Plug-in for diacritic marks. It adds in the field for the mark
     * and sets up listeners from the Annotator.js file to make changes as needed
     */
    Diacritics.prototype.pluginInit = function() {        
76
        //Check that annotator is working
77 78 79 80 81
        if (!Annotator.supported()) {
            return;
        }
        var di = this.diacriticmarks;
        
82 83 84 85 86
        //-- Editor
        var self = this;
        if(di != 'undefined'){
            $.each(di,function(item){
                self.field = self.annotator.editor.addField({
87 88 89 90 91 92 93 94 95 96
                    //options (textarea,input,select,checkbox)
                    type: 'checkbox', 
                    label: Annotator._t(item),
                    submit: self.pluginSubmit,
                });
            });
        
            //-- Viewer
            this.annotator.viewer.addField({
                load: this.updateViewer,
97 98 99 100
            });

            this.annotator.subscribe('annotationsLoaded', this.updateDiacritics);
            this.annotator.subscribe('annotationUploaded', this.updateDiacritics);
101
            this.annotator.subscribe('annotationDeleted', this.updateDiacritics);
102 103 104
            this.annotator.subscribe('annotationUpdated', this.updateDiacritics);
            this.annotator.subscribe('annotationEditorShown', this.updateEditorForDiacritics, this.field);
            
105
            $(window).resize(this.updateDiacritics.bind(this));
106 107
        }
        
108 109
        return this.input = $(this.field).find(':input');
    };
110
    
111 112 113 114 115
    /**
     * Adds or removes tag from checked/unchecked boxes of diacritics available
     * @param field {Object} - element which holds editor
     * @param annotation {Object} - object that contains annotation information from database
     */
116 117 118 119 120 121 122 123
    Diacritics.prototype.pluginSubmit = function(field, annotation) {
        var checkedItems = $(this.field).find(':input');
        var self = this;
        $.each(checkedItems, function(item){
            if(typeof annotation.tags != 'undefined'){
                var index = $.inArray(checkedItems[item].placeholder, annotation.tags);
                if(index != -1){
                    annotation.tags.splice(index, 1);  
124 125 126 127 128
                    var annotatorWrapper = $('.annotator-wrapper').first();
                    var element = annotatorWrapper.find('div.' + annotation.id);
                    
                    if(!element.length){
                        element = annotatorWrapper.find('div.undefined');
129
                    }
130 131
                    
                    element.remove();
132 133
                }
               
134
                if(checkedItems[item].checked === true){
135
                    annotation.tags.unshift(checkedItems[item].placeholder);
136
                    self.putMarkAtLocation(annotation,  checkedItems[item].placeholder);
137 138
                }
            } else {
139 140 141
                if(checkedItems[item].checked === true){
                    annotation.tags = [checkedItems[item].placeholder];
                    self.putMarkAtLocation(annotation, checkedItems[item].placeholder);
142 143 144
                }
            }
        });
145 146
        
    };
147

148 149 150 151 152 153
    /**
     * Draws the mark above a specific annotation
     * @param annotation {Object} - location where mark should go
     * @param mark {string}- type of mark that should go above annotation
     */
    Diacritics.prototype.putMarkAtLocation = function (annotation, mark){
154 155 156 157
        var loc = this.getPos(annotation.highlights[0]);
        var alignment = this.diacriticmarks[mark][1];
        var imgurl = this.diacriticmarks[mark][0];
        
158 159 160 161 162 163 164 165 166 167
        var top;
        switch(alignment){
            case 'top':
                top = (loc.y-5);
                break;
            case 'bottom':
                top = (loc.y + loc.height-5);
                break;
            default:
                top = loc.y;
168
        }
169 170 171 172 173 174 175 176 177 178 179
        $('<div></div>').addClass('mark ' + annotation.id).css({
            'top': top,
            'left': loc.x + (0.5 * loc.width) - 5,
            'background-image': 'url(' + imgurl +')',
        }).appendTo('.annotator-wrapper');
    };
    
    /**
     * Gets the Diacritics from the instantiation in studio
     * @returns An object with the diacritics instantiated
     */ 
180
    Diacritics.prototype.getDiacritics = function(){
181 182
        var diacritics = {};
        var diacriticsList;
183
        if(typeof this.options.diacritics != 'undefined'){
184 185
            diacriticsList = this.options.diacritics.split(",");
            $.each(diacriticsList, function(key, item){
186
                var temp = item.split(";");
187 188 189
                if (temp.length > 2) {
                    diacritics[temp[0]] = [temp[1], temp[2]];
                }
190 191
            });
        }
192 193
        return diacritics;
    };
194
    
195 196 197 198
    /**
     * Gets the position of a specific element given the wrapper
     * @param el {Object} - element you are trying to get the position of
     */
199
    Diacritics.prototype.getPos = function(el) {
200 201 202 203 204 205 206 207 208 209 210
        var element = $(el),
        elementOffset = element.offset(),
        annotatorOffset = $('.annotator-wrapper').offset();

        return {
            x: elementOffset.left - annotatorOffset.left, 
            y: elementOffset.top - annotatorOffset.top, 
            width: element.width(), 
            height: element.height()
        };
    };
211
    
212 213 214
    /**
     * Redraws the marks above annotations by cycling through tags
     */
215 216
    Diacritics.prototype.updateDiacritics = function(){
        $('.mark').remove();
217
        var annotations = this.annotator.plugins.Store.annotations;
218
        var self = this;
219 220 221 222
        $.each(annotations, function(key, annotation){
            $.each(self.diacriticmarks, function(tag){
                if($.inArray(tag, annotation.tags) != -1){
                    self.putMarkAtLocation(annotation, tag); 
223 224 225
                }
            });
        });
226
    };
227
    
228 229 230 231 232 233
    /**
     * Removes unnecessary field that Annotator automatically adds to popup
     * @param {Object} field - the html element that represents the popup
     * @param {Object} annotation - the annotation element that holds metadata
     */
    Diacritics.prototype.updateViewer = function(field, annotation){
234
        $(field).remove();
235 236 237 238 239 240 241 242 243 244 245
    };

    /**
     * Function for adding Diacritic choices to the annotator popup
     * @param {Object} field - the html element that represents the popup
     * @param {Object} annotation - the annotation element that holds metadata
     */
    Diacritics.prototype.updateEditorForDiacritics = 
        function(field, annotation){
        
        // if no tags are present, no need to go through this
246 247 248 249 250
        if (typeof annotation.tags == 'undefined'){
            return;   
        }
        
        var inputItems = $(this.field).find(':input');
251 252 253 254 255 256 257
        var dictOfItems = {};
        var self = this;
        
        // add each diacritic mark to a dictionary and default to off
        $.each(inputItems, function(key,item){
            item.checked = false;
            dictOfItems[item.placeholder] = item;
258
        });
259 260 261 262

        // match tags to diacritics and check off the ones that are true
        $.each(annotation.tags, function(key,tag){
            if(self.diacriticmarks[tag]){
263 264 265
                dictOfItems[tag].checked = true;
            }
        });
266 267
    };
        
268 269
    return Diacritics;

270
})(Annotator.Plugin);